c++ - OpenCV 2.4.2 imread 函数导致运行时错误

标签 c++ opencv

我是 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/

相关文章:

C++ 指针数学

opencv - 轮廓分割

带有 CUDA : registerPageLocked fails 的 OpenCV 2.4.4

python - 在 ubuntu 16.04 上安装 opencv 后出现错误 no module opencv

c++ - friend 没有得到私有(private)成员(member)

c++ - libc++ 中 C++11 标准支持的状态?

c++ - 为什么 MSVC 2019 在以三元形式返回静态大小的数组时失败?

python - Opencv:好的点匹配,但单应性错误

java - 如何将C++运算符转换为Java

c++ - 编译器错误 : Expected Nested Name Specifier before typedef