C++ – Unhandled exception Microsoft C++ exception: cv::Exception at memory location

copencvvisual studio 2010windows

I just started with OpenCV. I downloaded OpenCV 2.4.9, and installed MSVS 2010. My Windows is X64. I followed the following steps:

a. Under Configuration Properties, click Debugging -> Environment and copy paste: PATH=C:\opencv\build\x86\vc10\bin

b. VC++ Directories -> Include directories and add the entries: C:\opencv\build\include

c. VC++ Directories -> Library directories and add the entries: C:\opencv\build\x86\vc10\lib

d. Linker -> Input -> Additional Dependencies and add the following:

opencv_calib3d249.lib;opencv_contrib249.lib;opencv_core249.lib;opencv_features2d249.lib;opencv_flann249.lib;opencv_gpu249.lib;opencv_nonfree249.lib;opencv_highgui249.lib;opencv_imgproc249.lib;opencv_legacy249.lib;opencv_ml249.lib;opencv_objdetect249.lib;opencv_ts249.lib;opencv_video249.lib;

I ran the following code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main() {
        // read an image
        cv::Mat image= cv::imread("img.jpg");
        // create image window named "My Image"
        cv::namedWindow("My Image");
         cv::waitKey(1000);
        // show the image on window
        cv::imshow("My Image", image);
        // wait key for 5000 ms
        cv::waitKey(50);
        return 1;
}

To get the error:

Unhandled exception at 0x76d2b727 in BTP1.exe: Microsoft C++ exception: cv::Exception at memory location 0x003af414

I figured this might be because of the X64 and x86 mismatch. On changing the entries in a. to PATH=C:\opencv\build\ x64 \vc10\bin and in c. to C:\opencv\build\ x64 \vc10\lib, I get the following error:

The application was unable to start correctly (0xc000007b). Click OK to close the application.

Any tips on how I can get over this issue?

Best Answer

This is probably happening because the image you are trying to display is empty, perhaps because the image isn't in the right folder. To confirm this, change your code to

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <iostream>  // std::cout

int main() {
    // read an image
    cv::Mat image= cv::imread("img.jpg");

    // add the following lines
    if(image.empty())
       std::cout << "failed to open img.jpg" << std::endl;
    else
       std::cout << "img.jpg loaded OK" << std::endl;

    ...   // the rest of your code