问题描述
这是我的代码。但是每次显示数据时都无法读取。我尝试了多种格式将图像写入路径,但这没有用。还有其他人遇到同样的问题吗?
#include <iostream>
#include <fstream>
#include <string>
#include <opencv2/opencv.hpp>
#include <vector>
using namespace cv;
int main()
{
Mat source = imread("C:\\Untitled.jpg");
namedWindow("My Image");
waitKey(0);
if (!source.data)
{
std::cout << "Data cannot be read" << std::endl;
return -1;
}
imshow("My Image",source);
destroyAllWindows();
return 0;
}
解决方法
imshow()
之后必须加上waitKey()
,否则您将永远看不到命名窗口。
imshow("My Image",source);
waitKey(0); // should be here.
destroyAllWindows();
OpenCV 2.4中imshow()
的注释说:
此函数后应带有waitKey函数,该函数将显示指定毫秒的图像。否则,它将不会显示图像。 ...