minhduc
minhduc

Reputation: 163

Cannot run Opencv with C++ syntax

#include "StdAfx.h"
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main ()
{
    IplImage* img = cvLoadImage("D:\cat_helmet.jpg", CV_LOAD_IMAGE_UNCHANGED);
    cvNamedWindow("display", CV_WINDOW_AUTOSIZE);
    cvShowImage("display", img );
    cvWaitKey(0);        

    return 0;
}

However, I cannot run the C++ syntax program like

#include "StdAfx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std; 

int main(  )
{ 
    namedWindow( "Display window", CV_WINDOW_AUTOSIZE );
    Mat image;
    image = imread("D:\cat_helmet", CV_LOAD_IMAGE_COLOR);   

    if(! image.data )                             
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }
    imshow( "Display window", image );                   

    waitKey(0);                                          
    return 0;
}

I got the error messages (in the function calls: namedWindow, imread, imshow)

First-chance exception at 0x5361fcc3 in FirstOpencv2.3.exe: 0xC0000005: Access violation reading location 0x2079616c.

Unhandled exception at 0x5361fcc3 in FirstOpencv2.3.exe: 0xC0000005: Access violation reading location 0x2079616c.

How can I fix this?

Upvotes: 3

Views: 3181

Answers (2)

user2139958
user2139958

Reputation:

The above mentioned answers doesn't make sense. I am also facing the same problem. The main reason for this exception is that you are trying to display image (read by imread) which is empty. The main problem in the program is the line

    image = imread("D:\cat_helmet", CV_LOAD_IMAGE_COLOR); 

I think imread function is not behaving the way it is expected. One more thing, while going through the references i came across a following link:

http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#Mat imread(const string& filename, int flags)

Here, imread is used via call by reference method. I am not a C++ expert, but i feel it could be the problem.

    int main() 
    {
      std::string imgPath("splash.bmp"); //Add your file name
      Mat img = imread(imgPath);
      namedWindow( "Example1", WINDOW_AUTOSIZE);
      imshow("Example1", img);
      waitKey(0);
      return 0;
    }

This code worked for me. Also, I put the file next to executable to decrease complexity.

Upvotes: 0

Sonaten
Sonaten

Reputation: 502

You say that you have followed a multitude of guides and tutorials. I've had great success with this one http://www.anlak.com/using-opencv-2-3-1-with-visual-studio-2010-tutorial/

The thing is that this guy walks you through the 'park' and helps you unravel two major issues whilst setting up OpenCV 2.3.1; one of which is placement of .dll files in your project folder. The other is a missing .dll 'tbb_debug.dll' (the absense of this .dll is considered a bug in OpenCV 2.3.1).

He also provides some decent code-snippets for your to try out (in c++ syntax).

Good luck.

Upvotes: 5

Related Questions