1

Hello I am trying to read an image by using imread function of opencv as in the link (http://opencv.itseez.com/doc/tutorials/introduction/display_image/display_image.html#display-image). I have VS2010 with 64 bit windows 7. Each time I try I get error message "no image data", however the image I want to read is in the same folder with codes. Can someone please help me how to read an image with imread function? My code is as below:

#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
using namespace cv;

int _tmain(int argc, _TCHAR* argv[])
{
    Mat image;  
    image = imread("al.jpg");
    if(argc != 2 || !image.data )
    {
        printf("no image data \n");
        return -1;
    }
    namedWindow("Display Image", CV_WINDOW_AUTOSIZE);
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}
Sergey K.
  • 24,894
  • 13
  • 106
  • 174
mozendi
  • 85
  • 1
  • 11

7 Answers7

5

It you can load image using

IplImage *img=cvLoadImage("Image_Name);

Then you can convert this into cv::Mat using,

Mat mat(img);
hirantha129
  • 559
  • 1
  • 8
  • 19
2

I've had the same problem on WinXP with VS2005 and OpenCV 2.3. It seems that the C++ interface of OpenCV is not working well for Windows. I also had problems with imread(), which returned NULL data.

I solved the problem using the C interface of OpenCV instead. For more info, check Reading and Writing Images and Video.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • I am having the same problem with C++ OpenCV 2.4 interface and Visual Studio 2010. I am porting code from my MAC (where I had no issues with the interface) to Windows. Is there any work around apart from converting all my code to using the C interface? – Sid Jan 24 '13 at 23:02
  • 3
    I came across this http://stackoverflow.com/questions/10901905/installing-opencv-2-4-in-visual-c-2010-express?lq=1 that mentions to use the updated libraries *d.lib when adding dependencies for the linker. I tried that it seems to work. Not sure if that is the case in general or not. – Sid Jan 24 '13 at 23:23
1

I have met the same problems as yours. The C++ interface for OpenCV2.3 didn't work and some functions have to convert to C version.
But at last, I found what the ** problem is.
That is because the project properties when you compiled your OpenCV source code with CMake, is not the same as your current project properties.(May be your OpenCV's dll and libs are download from the internet, and these may be compiled with other wierd properties.)
So I check my CMake generated OpenCV source code project, I found the project property , C/C++-->Code Generation, the Runtime Library is Multi-threaded Debug DLL(/MDd).
Then, I change the corresponding property in my current project, and my project work well. At the same time, the other C++ interface for OpenCV bugs is solved well.

YuChan
  • 197
  • 1
  • 1
  • 8
0

OpenCV offers support for the image formats Windows bitmap (bmp), portable image formats (pbm, pgm, ppm) and Sun raster (sr, ras).

0

I experience that same problem, This may come from VS project compilation in VS 10.

In visual studio 2010: Go to project --> properties --> Character set --> Use multi byte instead of unicode.

TripleS
  • 1,216
  • 1
  • 23
  • 39
0

Take out the argc != 2 test. You aren't using the arguments passed to your program so it's superfluous. And if argc is, in fact, not eaual to 2, your program is terminating before even getting to the !image.data test.

Edit: I just looked at the code sample you linked to. It loads an image whose name is passed to the program on the command line. That's why the argc != 2 test is there. You definitely want to take that out because you are most likely not passing a file name on the command line so your argc is 1, thus the test will always fail.

SSteve
  • 10,550
  • 5
  • 46
  • 72
-1

Set the Common Language Runtime Support (/clr) in Visual C++. It works.