3
  • Im using Opencv 2.3.1 on Visual studio 2010 (vc10)
  • I have configured opencv based on many tutorials and can compile & run C-syntax program like:

#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?

minhduc
  • 163
  • 2
  • 11
  • `"D:\cat_helmet"` is definitely wrong. `"D:\\cat_helmet.jpg"`. – user786653 Oct 23 '11 at 09:46
  • The access violation error hit right at the namedWindow() command. And the two ways declaring image file are both OK. I compiled and ran the former piece of code above successfully. – minhduc Oct 23 '11 at 13:10
  • Have you placed the opencv_core231.dll and opencv_highgui231.dll's in the run directory, or placed these DLLs in the path, so they can be linked to dynamically? – mevatron Oct 23 '11 at 14:58
  • Consider checking *our* tutorial on how to install/configure OpenCV 2.3 on VS2010: http://stackoverflow.com/questions/7011238/opencv-2-3-c-visual-studio-2010/7014918#7014918 – karlphillip Oct 24 '11 at 23:57
  • @karlphillip: I followed your tutorial several days ago. It is amazing, thanks :) Now, I have moved to Ubuntu 11.10, still using Opencv 2.3 and CAN run the codes correctly. May be the C++ interface of Opencv is still not stable for Windows. Anyone who want to use opencv on ubuntu can see a great tutorial here: http://ozbots.org/opencv-installation/ – minhduc Oct 26 '11 at 08:04
  • I also had several problems with the C++ interface on Windows. I gave up and now if I need anything on Windows I use only the C interface of OpenCV. – karlphillip Oct 26 '11 at 11:58

2 Answers2

5

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.

Sonaten
  • 502
  • 2
  • 14
  • I tried following the tutorial and now everything works great. Thanks a lot. – minhduc Dec 05 '11 at 18:38
  • @ducnm, Glad I could help. On my study, a multitude of 8 groups 5 people each ventured tries on both OpenCV and OpenFrameworks, and more than 75% had major issues. A guide like the one I posted was just what we had needed back at the beginning of learing about image processing. – Sonaten Dec 14 '11 at 19:22
0

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.