Ask Your Question

Displaying image results in real time

asked 2016-04-04 05:18:53 -0500

mark92m gravatar image

I want to process a set of images and display them in real time in order to time the frames per second with which the code executes.

    for(int i = 0 ; i  < numOfImages ; i ++ ){


        imshow("Frame", image); 

Is the waitKey(1) the most efficient way to display the succession of frames as fast as possible ? Or is there a better way ?


edit retag flag offensive close merge delete


  • make it work
  • profile
  • then make it fast

(in exactly that order)

berak gravatar imageberak ( 2016-04-04 05:25:16 -0500 )edit

Actually, this is something I've wondered about too. Is this the recommended way of displaying things as fast as possible? I know if I've got several imshow windows open the ones not with focus sometimes stutter and don't show every frame.

Assuming every other part of the program has been profiled and optimized, then what? Is this better than calling updateWindow? Actually, I can test that myself.

Tetragramm gravatar imageTetragramm ( 2016-04-04 19:16:24 -0500 )edit

Ok, updateWindow only applies to OpenGL windows, so not what I thought it was.

As far as I can tell, waitKey(1) is the fastest way to refresh windows.

Tetragramm gravatar imageTetragramm ( 2016-04-04 19:25:26 -0500 )edit

It is the fastest way, simply because imshow windows should not be used for your final application :D OpenCV is a backend library, not a user interface library. Those windows are there for the sole purpose of debugging!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-04-05 07:55:18 -0500 )edit

@StevenPuttemans: What would be the "correct" way to rapidly throw images on the screen in the final application (managed Visual C++)?

My application works and was profiled and imshow/waitkey(1) is a bottleneck...

Guyygarty gravatar imageGuyygarty ( 2016-04-05 09:24:17 -0500 )edit

@Guyygarty, actually that is one though one to answer since I only make proof of concept software and thus when passing the code to the UI step, I am basically not involved anymore in my job. I am thus not sure how I could provide you with the best answer.

StevenPuttemans gravatar imageStevenPuttemans ( 2016-04-06 04:19:25 -0500 )edit

in mfc,use ontimer

jsxyhelu gravatar imagejsxyhelu ( 2017-02-20 09:51:45 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-04-06 04:50:04 -0500

pklab gravatar image

I'm agree with @StevenPuttemans. The correct and efficient way depends on GUI you are using: Win32,MFC, QT, wxWidgets, GTK, OpenGL, ... and/or CUDA, OpenCL and so on.

OpenCV provides a memory bank that you have to copy to (or share with) the graphic device and this is done by some user interface library. HighGUI is simplest one.

imshow and waitKey(delay) uses basic functions from platform you are using.On Windows, the Win32 library is used by default.

imshow copy the cv::Mat into a DIB image and Invalidate the Rect related to window on the screen.

Despite of its name, the waitKey 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.

This means that waitKey takes care of (executes) all GUI events such as show, refresh a window and so on. In addictions it waits for N millisecs a key press over an OpenCV window. I think it should be considered as UpdateGUI+WaitKey

Sure you will waste N millisecs waiting an usefulness key press but you have to wait also to serve all GUI events.

If you want to use HighGUI from OpenCV, you could write your own UpdateGUI hacking the waitKey code around delay<=0 and choose to serve only message for a specific window (window->hwnd == message.hwnd). Not so easy and maybe not so useful but I think feasible.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-04-04 05:18:53 -0500

Seen: 14,216 times

Last updated: Apr 06 '16