C++ – OpenCV 2.4.2 imread function causing runtime error

copencv

I'm a starter in OpenCV. My programming environment is VC++ Express 2010 + OpenCV 2.4.2 + Win 7 64 bit.

I use purely 32bit configuration in my VC++ and Path.

I type in the following code:

#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;
}

Then I compile and run. It does come up with a window (but without picture) but then gave me this (a runtime error?)

Unhandled exception at 0x770515de in Helloworld2.exe: Microsoft C++ exception: cv::Exception at memory location 0x001ef038..

Then I change the imread into cvLoadImage and it works without any errors.

Can someone tell me what's wrong?

Best Answer

I have tried the code you have given. It works perfectly fine with my installation of OpenCV.

However I am getting a warning at the line:

char* imgPath = "logo.png";

main.cpp:6:21: warning: deprecated conversion from string constant to 'char*' [-

Wwrite-strings]

Which i think is nothing serious to make the code crash, however it might be the problem in your case, as I am not using VC++ to compile.

What you can try to check if this is the issue is to replace imgPath with directly the string, so the code will now be like

Mat img = imread("logo.png"); 
Related Topic