You need to add
cvWaitKey(30);
to the end of while
-loop.
cvWaitKey(x) / cv::waitKey(x)
does two things:
- It waits for x milliseconds for a key press. If a key was pressed during that time, it returns the key's ASCII code. Otherwise, it returns
-1
. - It handles any windowing events, such as creating windows with
cvNamedWindow()
, or showing images withcvShowImage()
.
A common mistake for opencv newcomers is to call cvShowImage()
in a loop through video frames, without following up each draw with cvWaitKey(30)
. In this case, nothing appears on screen, because highgui is never given time to process the draw requests from cvShowImage()
.
See What does OpenCV's cvWaitKey( ) function do? for more info.