[英]OpenCV imshow() in a loop to show image
I'd like to create a C++ program with OpenCV which would allow me to continuously run a loop to ask a user what to do with an image. 我想用OpenCV创建一个C ++程序,这将允许我连续运行一个循环来询问用户如何处理图像。 For example the user can enter a specific number to execute a command. 例如,用户可以输入特定的数字来执行命令。 The code I have as an example is: 我作为示例的代码是:
int main()
{
int choose = -1;
for (;;)
{
cin >> choose;
if (choose == 0)
{
Mat img = imread(fileName1);//, CV_LOAD_IMAGE_GRAYSCALE);
if (!img.data)
{
cout << "Unable to load file." << endl;
}
else
{
namedWindow(fileName1, 1);
imshow(fileName1, img);
}
}
else if (choose == 1)
{
Mat img = imread(fileName2, CV_LOAD_IMAGE_GRAYSCALE);
if (!img.data)
{
cout << "Unable to load file." << endl;
}
else
{
namedWindow(fileName2, 1);
imshow(fileName2, img);
}
}
}
waitKey(0);
return 0;
}
The window where the image is supposed to load is just filled with grey. 应该加载图像的窗口仅填充有灰色。 I don't have any problems loading the image when it is not in a loop. 当图像不在循环中时,加载图像没有任何问题。 Can anyone help me figure out why this causes a problem please? 谁能帮我弄清楚为什么这会引起问题?
EDIT: Of course, forgot the waitKey(0) although even if I did have that, when I go back to the start of the loop to enter a number the window that was opened crashes for some reason. 编辑:当然,即使我确实有那个,也忘记了waitKey(0),当我回到循环开始输入数字时,由于某种原因,打开的窗口崩溃了。
Use waitKey
to get the pressed key instead of cin
. 使用waitKey
代替cin
获取按下的键。 Alternatively as said in the comments do a waitKey(1)
after imshow
. 或者如说,在评论做一个waitKey(1)
后imshow
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.