11
14
2013
0

Some Interesting things with OpenCV 1.0[1] cvShowImage and cvWaitKey

If We use cvShowImage function, The image wouldn't be shown on the window until cvWaiKey is called.

I got this problem when I displayed a video sequence. It's interesting that the window showed nothing without cvWaitKey, while when I stepped into the program, I could see the image correctly with the tool Image Watch ,The following code could directly show that:

#include "stdafx.h"

int main(int argc, char** argv)
{
	cvNamedWindow("testWindow",0);
	IplImage *img=cvLoadImage("E:/lena.jpg");
	cvShowImage("testWindow",img);
	Sleep(10);
	cvWaitKey(0);
        cvReleaseImage(&img);
	cvDestroyWindow("testWindow");
	return 0;
}

img won't be shown until the Sleep time ends.

Why could this happen? I refered to the offical documentation and It said:

The function waitKey waits for a key event infinitely (when \texttt{delay}\leq 0 ) or for delay milliseconds, when it is positive. Since the OS has a minimum time between switching threads, the function will not wait exactly delay ms, it will wait at least delay ms, depending on what else is running on your computer at that time. It returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed.

Note that:

Note

 

This function is the only method in HighGUI that can fetch and handle events, so it needs to be called periodically for normal event processing unless HighGUI is used within an environment that takes care of event processing.

Note

 

The function only works if there is at least one HighGUI window created and the window is active. If there are several HighGUI windows, any of them can be active.

Then I read the source code of cvWaitKey:

CV_IMPL int
cvWaitKey( int delay )
{
    int time0 = GetTickCount();

    for(;;)
    {
        CvWindow* window;
        MSG message;
        int is_processed = 0;

        if( (delay > 0 && abs((int)(GetTickCount() - time0)) >= delay) || hg_windows == 0 )
            return -1;

        if( delay <= 0 )
            GetMessage(&message, 0, 0, 0);
        else if( PeekMessage(&message, 0, 0, 0, PM_REMOVE) == FALSE )
        {
            Sleep(1);
            continue;
        }

        for( window = hg_windows; window != 0 && is_processed == 0; window = window->next )
        {
            if( window->hwnd == message.hwnd || window->frame == message.hwnd )
            {
                is_processed = 1;
                switch(message.message)
                {
                case WM_DESTROY:
                case WM_CHAR:
                    DispatchMessage(&message);
                    return (int)message.wParam;

                case WM_KEYDOWN:
                    TranslateMessage(&message);
                default:
                    DispatchMessage(&message);
                    is_processed = 1;
                    break;
                }
            }
        }

        if( !is_processed )
        {
            TranslateMessage(&message);
            DispatchMessage(&message);
        }
    }
}

The source code clearly shows that cvWaitKey get the message from the system and then dispatch the message. After that ,is_processed flag is set to 1. So, if we use cvShowImage, we just throw a message that A "image show" function is bound with hwnd of the window. What's more, the source code shows that if there is no active window (hg_window=0), it would return -1.

At last, I wanna say that cvWaitKey is very useful if we break the running program in the middle, a useful code is:

if(cvWaitKey(1)>=0)

A key down event could be detected such that we could process the breaking and release the memory, otherwise some memory error may take place.

 

Category: OpenCV | Tags: OpenCV1.0 | Read Count: 2991

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

| Theme: Aeros 2.0 by TheBuckmaker.com