quasimodo quasimodo - 1 year ago 58
C++ Question

image window not responding opencv during program running

I am running a code something like that;

Mat img1 = imread("C:\\input0.png");

imshow("original", img1 );

int lowThreshold = 20;
int ratio = 2;
int kernel_size = 5;
Mat edge_map;
Mat gray_image;
cvtColor(img1, gray_image, CV_BGR2GRAY);
Canny(gray_image, edge_map, lowThreshold, lowThreshold*ratio, kernel_size);

namedWindow("Edge Image");
imshow("Edge Image", edge_map);

Mat result_image = produce_the_result_image(img1, edge_map);

namedWindow("Final Image");
imshow("Final Image", result_image );

int key = 1;
while (key != 'q') {
key = waitKey(5);

Till shows the last image (Final Image window), other windows show 'not responding', however, after the last function finishes (produce_the_result_image), which lasts 2-3 minutos and shows the all image windows, the error disappears. Is that normal?


Answer Source

Images in OpenCV will not display or respond until you have called waitKey();. So if after your first two calls to imshow you call waitKey(1);, the image will display (it will wait one millisecond for a key press and then it will become nonresponsive). If you call waitKey(); with no arguments or use a while loop similar to the one at the end of the code, the images will display and respond. Once you press a key, the images will become nonresponsive again.

So yes, this is normal behavior for OpenCV. Note that it applies to all windows at once: either they are all responsive, if waitKey is currently being called, or none of them are responsive.