34

I'm trying to use the imread function from OpenCV2.2.

My code is very simple.

cv::Mat host= imread("1.bmp", CV_LOAD_IMAGE_GRAYSCALE);

After that, the host matrix became filled by zeros pointers, i.e. an image has not loaded.

If I use cvLoadImage then it all works properly.

The file exists, and I am not mixing the release and debug libraries. Why imread doesn't work?

ronalchn
  • 12,225
  • 10
  • 51
  • 61
luckydevil
  • 357
  • 1
  • 3
  • 4
  • 7
    @asandwhich yes there is "Why imread doesn't work?". Isn't that a question? Probably not the best way to ask for help but still a question. People instead of just being sarcastic really try to help people !! I know many of us don't describe and ask correctly but if you're not going to help please don't bother being sarcastic. – BRabbit27 Apr 02 '13 at 21:57
  • Sorry for responding to the original question. "Why imread doesn't work?" wasn't initially in the question. I pointed that out to the asker. But, hey, I guess I need to stop that. – a sandwhich Apr 02 '13 at 23:12

15 Answers15

26

Reproduced with opencv 2.4.8.

If you are running in Debug, check that you are also using debug libraries, it fixed our problem. : OpenCV imread(filename) fails in debug mode when using release libraries.

Community
  • 1
  • 1
12

i was facing the same problem with 2.4.6 . The reason was that while selecting the library , i had selected both the debug version and the release version. When i selected only the debug version for the library everything worked fine

user2975919
  • 121
  • 1
  • 2
9

I can confirm, that there are some problems with imread in OpenCV 2.2. However the problems only occurred on a Windows 32bit system. On a linux and on a mac it worked. I can't tell why it didn't work, but we had a small workaround for that.

We fixed this problem with the following macros, maybe you could try this out and use "ourImread" from then on.

#ifdef WIN32
#define ourImread(filename, isColor) cvLoadImage(filename.c_str(), isColor)
#else
#define ourImread(filename, isColor) imread(filename, isColor)
#endif
kadrian
  • 4,761
  • 8
  • 39
  • 61
  • 1
    Actually this bug still exists on OpenCV 2.4.2 win32. – vinjn Sep 01 '12 at 16:20
  • I also faced this issue with OpenCV 2.4.7, win32. Running with Visual Studio 2012 triggered the bug. Running in release mode on console worked sometimes. – franckysnow Nov 19 '13 at 15:23
  • 2
    Mixing release and debug modes is the cause of the problem. See http://stackoverflow.com/questions/9125817/opencv-imreadfilename-fails-in-debug-mode-when-using-release-libraries – Aerospace Sep 17 '14 at 07:40
6

I've had the same problem

cv::Mat image= cv::imread("immagine12.jpg");   // Read the file

if(! image.data )                              // Check for invalid input
{
    cout <<  "Could not open or find the image" << std::endl ;
    cv::waitKey(5000);
    return -1;
}

cv::namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image );  

//non so perchè ma senza il waitKey e lasciando solo il system pause non carica l'immagine...
cv::waitKey(5000);


system("pause");

but I fixed it when I inserted the cv::waitKey(5000);
I don't know why but with system pause it can't load the image and it goes on pause after it loads the image!

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Lorenzo
  • 673
  • 1
  • 11
  • 25
  • 1
    Mixing release and debug modes is the cause of the problem. See http://stackoverflow.com/questions/9125817/opencv-imreadfilename-fails-in-debug-mode-when-using-release-libraries – Aerospace Sep 17 '14 at 07:41
3

I have similar problem on Linux, when reading 32 bit tiff images only.

Mat mat= imread(filename, CV_LOAD_IMAGE_ANYDEPTH);

Problem was because OpenCV was not build with Tiff support for some reason.

mrgloom
  • 20,061
  • 36
  • 171
  • 301
3

Opencv4.3 Windows10 x64 Visual Studio 2019 I finally solved that problem!!!

Ok, now, when you meet the problem, you can try:

  1. Check your settings, make sure you don't mix include .lib file in "Project->Properties->Linker->Input" for Release/Debug, that opencv_xxx430d.lib(note the d) is for Debug, and opencv_xxx430.lib(no d) is for Release, and the platform is x64.
  2. And in C/C++->General, you need to set the Additional include directory that X:/(opencv_build/install)/include and X:/(opencv_build/install)/include/opencv2 respectively for Release/Debug.
  3. Maybe you should #include <opencv2/imgcodecs.hpp>.
  4. Finally, the Unicode Problem, which is also my problem that, the unicode symbol in the image file path, fuck! I don't know why because my code is very normal string path = "D:/kk.jpg(note that, it is / not \) but you can try to debug your code, maybe you can find there is something like path = "?D:/kk.jpg", what the fuck! So don't directly copy/paste your image path, even you've already use the absolute path, you can still meet the unicode problem, so, what you need to do is delete the line of code(string path = "D:/kk.jpg), and then just retype each character one by one! If you're lucky, you will see that image with imshow. Good luck.
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Hu Xixi
  • 1,799
  • 2
  • 21
  • 29
1

I know it is late but someone might found this helpful. I was facing the same problem while using imread using OpenCV-3.0. I tried all solutions but actually I was not adding the library opencv2/imgcodecs.hpp. Although imshow was working without it, but after I add this I was able to read the image.

Maham
  • 432
  • 1
  • 8
  • 22
1

If you think that it is an OpenCV bug then, please, post your image and instructions for reproducing to the OpenCV bugtracker.

Andrey Kamaev
  • 29,582
  • 6
  • 94
  • 88
0

I have the same problem. I solved it. The key is whether the filename has jpg. If the filename is p1, you should use something like this imread("p1.jpg"). But we often set the filename as p1.jpg, here we should use something like this imread("p1.jpg.jpg").

biegleux
  • 13,179
  • 11
  • 45
  • 52
lifeiteng
  • 17
  • 1
0

This also happened to me, my simple solution was to use the C API and then convert to Mat:

IplImage* img = cvLoadImage("c://frame_201.bmp");
Mat mat = Mat(img);
madhat1
  • 676
  • 9
  • 15
0

The reverse is also true: if you are building Release and you have Debug libraries, then imread() quietly fails (errno is 0 after imread(), but the image object is not populated).

0

Another possibility:

If you're on OS X and statically link OpenCV be sure to use libjpeg which is bundled with OpenCV, not the system's one.

I had similar problem with OpenCV 3.0, except that cvLoadImage was not working as well. So, this might not really answer your question, but maybe it will help someone else.

Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
0
  1. see related question here

  2. Please make sure your path is correct ,

  3. According to the Opencv's API , I'd try this call:

arrayMat[i]=imread("1.jpg" , 1 );

The parameters for imread :

Mat imread(const string& filename, int flags=1)
Loads an image from a file.

Parameters: 
 filename – Name of file to be loaded.
 flags – Specifies color type of the loaded image:
   >0 the loaded image is forced to be a 3-channel color image
   =0 the loaded image is forced to be grayscale
   <0 the loaded image will be loaded as-is (note that in the current implementation the alpha channel, if any, is stripped from the output image, e.g. 4-channel RGBA image will be loaded as RGB if  ). 

Good luck S

Community
  • 1
  • 1
TripleS
  • 1,216
  • 1
  • 23
  • 39
-1

I also had the very same problem that imread didn't work and cvLoadImage did work.

I decided to create a new Visual Studio from scratch and now it works.

There is no general problem with imread in OpenCV 2.4.3 under win32.

testalino
  • 5,474
  • 6
  • 36
  • 48
-2

I know you want use "imread" & "CV_LOAD_IMAGE_GRAYSCALE" and convert automatically. But this other way for load one picture and convert to gray scale:

define CV_NO_BACKWARD_COMPATIBILITY

#include <cv.h>
#include <highgui.h>
#include <math.h>

int main(){

/* load the image */
IplImage* img  = cvLoadImage("yourPicture.bmp");  //jpg - bmp

/* retrieve properties */
int width     = img->width;
int height    = img->height;
int nchannels = img->nChannels;
int step      = img->widthStep;

IplImage* img2 = cvCreateImage(cvSize(img->height, img->width),IPL_DEPTH_8U,1);

/* setup the pointer to access image data */
uchar *data = ( uchar* )img->imageData;    
uchar *data2= ( uchar* )img2->imageData;

/* convert to grayscale manually */
int i, j, r, g, b, byte;
for( i = 0 ; i < height ; i++ ) {
    for( j = 0 ; j < width ; j++ ) {
        r = data[i*step + j*nchannels + 0];
        g = data[i*step + j*nchannels + 1];
        b = data[i*step + j*nchannels + 2];

        byte = ( r + g + b ) / 3;

    int v0=0, v1=0, v2=0;

    data2[i*(img2->widthStep)+j*(img2->nChannels)+0] = byte;
data2[i*(img2->widthStep)+j*(img2->nChannels)+1] = byte;
data2[i*(img2->widthStep)+j*(img2->nChannels)+2] = byte;
}
}

cvNamedWindow("ImagenColor", 1);
cvShowImage("ImagenColor", img);

cvNamedWindow("Gray", 1);
cvShowImage("Gray", img2);
}
Orlando Herrera
  • 3,481
  • 1
  • 34
  • 44