Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled exception on OpenCV+VS2010

I can build and execute the code below successfully :

 IplImage* img = cvLoadImage("C:\\hello.jpg");
 cvNamedWindow("myfirstwindow");
 cvShowImage("myfirstwindow", img);

But I got the unhandled exception when executed the code below:

    cv::Mat image= cv::imread("boldt.jpg");
    cv::namedWindow("Image");
    cv::imshow("Image",image);

although,I can build the code successfully.

I'm using opencv2.2 with VS2010 x86 version on windows 7 x86 version. please help !

update: I tried it on winxp ,and it works fine...and it works fine with win7 on Release mode only.

like image 795
Zombia Avatar asked Nov 17 '11 09:11

Zombia


4 Answers

It might be the problem where people don't realize that when VStudio runs your application it tries to find it's resources in the same directory as the compiled executable and not in the folder where the source files are.

Your first code works because you are loading the image passing the FULL PATH to the file!

That's why is so important to check the success of functions when you are coding:

try 
{
    cv::Mat image = cv::imread("boldt.jpg");

    if (!image.data) 
        printf("!!! No data !!!");
} 
catch(std::exception e) 
{
    printf("Exception: [%s]\n", e.what());
}

This sort of programming practice will save you a lot of time.

EDIT:

Well, if the crash is still happening it means that it could be either cv::namedWindow() or cv::imshow() fault, and my money is on cv::namedWindow().

Other users reported a similar behavior on Windows:

OpenCV 2.2 Windows XP MinGW build crashes on namedWindow, imshow

Open CV crashes under WIN7 when opening NamedWindow

namedWindow() causes crash in opencv 2.3.1? (Eclipse+MinGW on XP, C++)

It seems that to solve the problem you need to disable SSE optimizations.

like image 66
karlphillip Avatar answered Oct 24 '22 03:10

karlphillip


I'm using OpenCV 2.3.1 and when I run it in Release mode (linked to a release highgui lib), everything is fine. When I switch to Debug mode (still linked to a release highgui lib), it crashes. Linking to a debug highgui lib helped.

Maybe you have the same problem...

like image 39
Ben Avatar answered Oct 24 '22 02:10

Ben


Did you checked the output of the imread() function?

if(image.empty())
{
        cout << "where's my image?" << endl;
        return 0;
}
like image 29
Sam Avatar answered Oct 24 '22 03:10

Sam


I have the exact same problem as have been described.

It turns out that the problem very much lies with the settings of the linker!

I found the answer in another thread: OpenCV 2.3 and Visual Studio 2010

Here it is:

"Properties of your project (right click on it)

  • C/C++
    • General
      • include directory add the < your directory >\OpenCV2.3\include\opencv2, < your directory >\OpenCV2.3\include\opencv and < your directory >\OpenCV2.3\include
  • Linker

    • General
      • List item
    • Input
      • Add all the libs like opencv_core230d.lib opencv_highgui230d.lib and so on..."

Once I've done the above, I can run imshow and imread + all other cpp functions seamlessly! The author who asked the question probably has already solved it. but just in case there are other people who are led here looking for the same solution!

cheers!

like image 1
user1722909 Avatar answered Oct 24 '22 01:10

user1722909