我想在openCV中使用Mat加载图像
我的代码是:
Mat I = imread("C:/images/apple.jpg", 0);
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", I );
我在消息框中收到以下错误:
Unhandled exception at 0x70270149 in matching.exe: 0xC0000005: Access violation
reading location 0xcccccccc.
请注意我包括:
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <iostream>
#include <math.h>
请帮忙。谢谢。
答案 0 :(得分:4)
之前我已talked关于此so many times,我认为再次执行此操作毫无意义,但代码防御性:如果方法/函数调用失败,请执行确定你知道它什么时候发生:
Mat I = imread("C:\\images\\apple.jpg", 0);
if (I.empty())
{
std::cout << "!!! Failed imread(): image not found" << std::endl;
// don't let the execution continue, else imshow() will crash.
}
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", I );
waitKey(0);
请注意,Windows的路径使用反斜杠\
,而不是* nix系统上使用的标准/
。传递文件名时需要转义反斜杠:C:\\images\\apple.jpg
Calling waitKey()
is mandatory if you use imshow()
.
修改强>:
如果它是cv::imread()
抛出异常我知道工作的唯一解决方案是在机器上下载OpenCV源并构建它,因为重新安装OpenCV无法修复问题。
答案 1 :(得分:1)
在imread之后你检查过我存在吗?也许文件读取失败
阅读文件后,执行if ( I.empty() )
检查是否失败
答案 2 :(得分:1)
我不知道为什么你没有包含问题,因为通常它.hpp文件所以你想要做
#include <opencv2/highgui/highgui.hpp>
#include <opencv2\core\eigen.hpp>
但您的代码似乎不错,但在您的imshow之后添加cv::waitKey(0);
。
答案 3 :(得分:0)
您是否使用visual studio 2010来运行OpenCV代码?如果是这样,请尝试在发布模式下进行编译。
答案 4 :(得分:0)
@karlphillip指出,这听起来很琐碎,但是这句话“传递文件名C:\ images \ apple.jpg时需要转义反斜杠”非常重要。