Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Display webcam input in window

I'd done build a window to display input image using multithreading. But now I'm stuck, how can I display image in this window (as in figure attached here) since I'm using OpenCV.

image description

Display webcam input in window

I'd done build a window to display input image using multithreading. But now I'm stuck, how can I display image in this window (as in figure attached here) since I'm using OpenCV.

image description

EDIT 3/11/15 LRESULT CALLBACK SmplProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam);

int OnGrab(void);
DWORD StopThread(LPDWORD lpdwParam);
DWORD ImaqThread(LPDWORD lpdwParam);
static HANDLE HStopThread, HStopEvent, HThread;
static HINSTANCE hInst;
static HWND SmplHwnd;
static HWND HStop, HGrab, HQuit;
static BOOL StopGrab; 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
    CHAR        SmplClassName[] = "Sample";
    WNDCLASS            SmplClass;
    MSG         msg;

    hInst = hInstance;
    if (!hPrevInstance)
    {
        SmplClass.style             = CS_HREDRAW | CS_VREDRAW;
        SmplClass.lpfnWndProc       = (WNDPROC)SmplProc;
        SmplClass.cbClsExtra        = 0;
        SmplClass.cbWndExtra        = 0;
        SmplClass.hInstance         = hInstance;
        SmplClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        SmplClass.hCursor           = LoadCursor(NULL, IDC_ARROW);
        SmplClass.hbrBackground     = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
        SmplClass.lpszMenuName      = 0;
        SmplClass.lpszClassName     =SmplClassName;
        if (!RegisterClass(&ImaqSmplClass))
            return (0);
    }
    SmplHwnd = CreateWindow(SmplClassName, "LLGrab", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1180, 740, NULL, NULL, hInstance, NULL);

    if (!(HGrab = CreateWindow("Button", "Grab", BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_BORDER, 1050, 72, 80, 40, SmplHwnd, (HMENU)PB_GRAB, hInstance, NULL))) 
        return (FALSE);
    if (!(HStop = CreateWindow("Button", "Stop", BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_BORDER, 1050, 112, 80, 40, SmplHwnd, (HMENU)PB_STOP, hInstance, NULL))) 
        return (FALSE);

    EnableWindow(HStop, FALSE);

    if (!(HQuit = CreateWindow("Button", "Quit", BS_DEFPUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_BORDER, 1050, 152, 80, 40, SmplHwnd, (HMENU)PB_QUIT, hInstance, NULL)))
        return (FALSE);

    ShowWindow(SmplHwnd, SW_SHOW);
    UpdateWindow(SmplHwnd);

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    WaitForSingleObject(HStopThread, INFINITE);
    return (int)(msg.wParam);
}

LRESULT CALLBACK SmplProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam)
{
    WORD wmID;


    switch (iMessage)
    {
        case WM_COMMAND:
            wmID = LOWORD(wParam);
            switch (wmID)
            {
            case PB_QUIT:
                PostQuitMessage(0);
                break;

            case PB_GRAB:

                OnGrab();
                break;

            case PB_STOP:

                SetEvent(HStopEvent);
                break;

            }
            break;
        case WM_DESTROY:

            SetEvent(HStopEvent);
            PostQuitMessage(0);

        default:

            return DefWindowProc(hWnd, iMessage, wParam, lParam);
            break;

    }
    return 0;
}

int OnGrab(void)
{
    DWORD dwThreadId;

    HStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (!HStopEvent)
        return 0;
    HStopThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)StopThread, (LPWORD)&HStopEvent, 0, &dwThreadId);
    if (!HStopThread)
        return 0;
    StopGrab = FALSE;
    HThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Thread, (LPDWORD*)&StopGrab, 0, &dwThreadId);
    if (HThread == NULL)
        return 0;
    EnableWindow(HStop, TRUE);
    EnableWindow(HGrab, FALSE);
    EnableWindow(HQuit, FALSE);
    return 0;
}

DWORD Thread(LPDWORD lpdwParam)
{
    BOOL* volatile stop = (BOOL*)lpdwParam;
    VideoCapture captures;
    captures.open("2pm.avi");

    while (!*stop)
    {
        //display video here
    }
    return 0;
}

DWORD StopThread(LPDWORD lpdwParam)
{
    DWORD dwResult;

    HANDLE event = *((HANDLE*)lpdwParam);

    dwResult = WaitForSingleObject(event, INFINITE);
    if (dwResult != WAIT_FAILED)
    {
        CloseHandle(event);
        event = NULL;
    }
    EnableWindow(HStop, FALSE);
    EnableWindow(HGrab, TRUE);
    EnableWindow(HQuit, TRUE);
    return 0;
}

Display webcam input in window

I'd done build a window to display input image using multithreading. But now I'm stuck, how can I display image in this window (as in figure attached here) since I'm using OpenCV.

image description

EDIT 3/11/15 3/11/15

LRESULT CALLBACK SmplProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam);

lParam);

int OnGrab(void);
DWORD StopThread(LPDWORD lpdwParam);
DWORD ImaqThread(LPDWORD lpdwParam);
static HANDLE HStopThread, HStopEvent, HThread;
static HINSTANCE hInst;
static HWND SmplHwnd;
static HWND HStop, HGrab, HQuit;
static BOOL StopGrab; 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
    CHAR        SmplClassName[] = "Sample";
    WNDCLASS            SmplClass;
    MSG         msg;

    hInst = hInstance;
    if (!hPrevInstance)
    {
        SmplClass.style             = CS_HREDRAW | CS_VREDRAW;
        SmplClass.lpfnWndProc       = (WNDPROC)SmplProc;
        SmplClass.cbClsExtra        = 0;
        SmplClass.cbWndExtra        = 0;
        SmplClass.hInstance         = hInstance;
        SmplClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        SmplClass.hCursor           = LoadCursor(NULL, IDC_ARROW);
        SmplClass.hbrBackground     = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
        SmplClass.lpszMenuName      = 0;
        SmplClass.lpszClassName     =SmplClassName;
        if (!RegisterClass(&ImaqSmplClass))
            return (0);
    }
    SmplHwnd = CreateWindow(SmplClassName, "LLGrab", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1180, 740, NULL, NULL, hInstance, NULL);

    if (!(HGrab = CreateWindow("Button", "Grab", BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_BORDER, 1050, 72, 80, 40, SmplHwnd, (HMENU)PB_GRAB, hInstance, NULL))) 
        return (FALSE);
    if (!(HStop = CreateWindow("Button", "Stop", BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_BORDER, 1050, 112, 80, 40, SmplHwnd, (HMENU)PB_STOP, hInstance, NULL))) 
        return (FALSE);

    EnableWindow(HStop, FALSE);

    if (!(HQuit = CreateWindow("Button", "Quit", BS_DEFPUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_BORDER, 1050, 152, 80, 40, SmplHwnd, (HMENU)PB_QUIT, hInstance, NULL)))
        return (FALSE);

    ShowWindow(SmplHwnd, SW_SHOW);
    UpdateWindow(SmplHwnd);

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    WaitForSingleObject(HStopThread, INFINITE);
    return (int)(msg.wParam);
}

LRESULT CALLBACK SmplProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam)
{
    WORD wmID;


    switch (iMessage)
    {
        case WM_COMMAND:
            wmID = LOWORD(wParam);
            switch (wmID)
            {
            case PB_QUIT:
                PostQuitMessage(0);
                break;

            case PB_GRAB:

                OnGrab();
                break;

            case PB_STOP:

                SetEvent(HStopEvent);
                break;

            }
            break;
        case WM_DESTROY:

            SetEvent(HStopEvent);
            PostQuitMessage(0);

        default:

            return DefWindowProc(hWnd, iMessage, wParam, lParam);
            break;

    }
    return 0;
}

int OnGrab(void)
{
    DWORD dwThreadId;

    HStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (!HStopEvent)
        return 0;
    HStopThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)StopThread, (LPWORD)&HStopEvent, 0, &dwThreadId);
    if (!HStopThread)
        return 0;
    StopGrab = FALSE;
    HThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Thread, (LPDWORD*)&StopGrab, 0, &dwThreadId);
    if (HThread == NULL)
        return 0;
    EnableWindow(HStop, TRUE);
    EnableWindow(HGrab, FALSE);
    EnableWindow(HQuit, FALSE);
    return 0;
}

DWORD Thread(LPDWORD lpdwParam)
{
    BOOL* volatile stop = (BOOL*)lpdwParam;
    VideoCapture captures;
    captures.open("2pm.avi");

    while (!*stop)
    {
        //display video here
    }
    return 0;
}

DWORD StopThread(LPDWORD lpdwParam)
{
    DWORD dwResult;

    HANDLE event = *((HANDLE*)lpdwParam);

    dwResult = WaitForSingleObject(event, INFINITE);
    if (dwResult != WAIT_FAILED)
    {
        CloseHandle(event);
        event = NULL;
    }
    EnableWindow(HStop, FALSE);
    EnableWindow(HGrab, TRUE);
    EnableWindow(HQuit, TRUE);
    return 0;
}
click to hide/show revision 4
retagged

Display webcam input in window

I'd done build a window to display input image using multithreading. But now I'm stuck, how can I display image in this window (as in figure attached here) since I'm using OpenCV.

image description

EDIT 3/11/15

LRESULT CALLBACK SmplProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam);

int OnGrab(void);
DWORD StopThread(LPDWORD lpdwParam);
DWORD ImaqThread(LPDWORD lpdwParam);
static HANDLE HStopThread, HStopEvent, HThread;
static HINSTANCE hInst;
static HWND SmplHwnd;
static HWND HStop, HGrab, HQuit;
static BOOL StopGrab; 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
    CHAR        SmplClassName[] = "Sample";
    WNDCLASS            SmplClass;
    MSG         msg;

    hInst = hInstance;
    if (!hPrevInstance)
    {
        SmplClass.style             = CS_HREDRAW | CS_VREDRAW;
        SmplClass.lpfnWndProc       = (WNDPROC)SmplProc;
        SmplClass.cbClsExtra        = 0;
        SmplClass.cbWndExtra        = 0;
        SmplClass.hInstance         = hInstance;
        SmplClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        SmplClass.hCursor           = LoadCursor(NULL, IDC_ARROW);
        SmplClass.hbrBackground     = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
        SmplClass.lpszMenuName      = 0;
        SmplClass.lpszClassName     =SmplClassName;
        if (!RegisterClass(&ImaqSmplClass))
            return (0);
    }
    SmplHwnd = CreateWindow(SmplClassName, "LLGrab", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1180, 740, NULL, NULL, hInstance, NULL);

    if (!(HGrab = CreateWindow("Button", "Grab", BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_BORDER, 1050, 72, 80, 40, SmplHwnd, (HMENU)PB_GRAB, hInstance, NULL))) 
        return (FALSE);
    if (!(HStop = CreateWindow("Button", "Stop", BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_BORDER, 1050, 112, 80, 40, SmplHwnd, (HMENU)PB_STOP, hInstance, NULL))) 
        return (FALSE);

    EnableWindow(HStop, FALSE);

    if (!(HQuit = CreateWindow("Button", "Quit", BS_DEFPUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_BORDER, 1050, 152, 80, 40, SmplHwnd, (HMENU)PB_QUIT, hInstance, NULL)))
        return (FALSE);

    ShowWindow(SmplHwnd, SW_SHOW);
    UpdateWindow(SmplHwnd);

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    WaitForSingleObject(HStopThread, INFINITE);
    return (int)(msg.wParam);
}

LRESULT CALLBACK SmplProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam)
{
    WORD wmID;


    switch (iMessage)
    {
        case WM_COMMAND:
            wmID = LOWORD(wParam);
            switch (wmID)
            {
            case PB_QUIT:
                PostQuitMessage(0);
                break;

            case PB_GRAB:

                OnGrab();
                break;

            case PB_STOP:

                SetEvent(HStopEvent);
                break;

            }
            break;
        case WM_DESTROY:

            SetEvent(HStopEvent);
            PostQuitMessage(0);

        default:

            return DefWindowProc(hWnd, iMessage, wParam, lParam);
            break;

    }
    return 0;
}

int OnGrab(void)
{
    DWORD dwThreadId;

    HStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (!HStopEvent)
        return 0;
    HStopThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)StopThread, (LPWORD)&HStopEvent, 0, &dwThreadId);
    if (!HStopThread)
        return 0;
    StopGrab = FALSE;
    HThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Thread, (LPDWORD*)&StopGrab, 0, &dwThreadId);
    if (HThread == NULL)
        return 0;
    EnableWindow(HStop, TRUE);
    EnableWindow(HGrab, FALSE);
    EnableWindow(HQuit, FALSE);
    return 0;
}

DWORD Thread(LPDWORD lpdwParam)
{
    BOOL* volatile stop = (BOOL*)lpdwParam;
    VideoCapture captures;
    captures.open("2pm.avi");

    while (!*stop)
    {
        //display video here
    }
    return 0;
}

DWORD StopThread(LPDWORD lpdwParam)
{
    DWORD dwResult;

    HANDLE event = *((HANDLE*)lpdwParam);

    dwResult = WaitForSingleObject(event, INFINITE);
    if (dwResult != WAIT_FAILED)
    {
        CloseHandle(event);
        event = NULL;
    }
    EnableWindow(HStop, FALSE);
    EnableWindow(HGrab, TRUE);
    EnableWindow(HQuit, TRUE);
    return 0;
}