我是 OpenCV 的初学者。我的编程环境是VC++ Express 2010 + OpenCV 2.4.2 + Win 7 64位。
我在我的 VC++ 和路径中使用纯 32 位配置。
我输入以下代码:
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\core\core.hpp"
using namespace cv;
int main(int argc, char** argv) {
char* imgPath = "logo.png";
Mat img = imread(imgPath);
namedWindow( "Example1", WINDOW_AUTOSIZE);
imshow("Example1", img);
waitKey(0);
return 0;
}
然后我编译并运行。它确实出现了一个窗口(但没有图片)但随后给了我这个(运行时错误?)
Unhandled exception at 0x770515de in Helloworld2.exe: Microsoft C++ exception: cv::Exception at memory location 0x001ef038..
然后我将 imread 更改为 cvLoadImage,它可以正常工作。
谁能告诉我哪里出了问题?
最佳答案
我试过你给的代码。它与我安装的 OpenCV 完美配合。
但是我在该行收到警告:
char* imgPath = "logo.png";
main.cpp:6:21: warning: deprecated conversion from string constant to 'char*' [-
写入字符串]
我认为这不会导致代码崩溃,但在您的情况下这可能是问题所在,因为我没有使用 VC++ 进行编译。
您可以尝试检查这是否是问题所在,直接将 imgPath
替换为字符串,这样代码现在就像
Mat img = imread("logo.png");
关于c++ - OpenCV 2.4.2 imread 函数导致运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11989127/