imread not working in Opencv
Asked Answered
D

15

35

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?

Dunlavy answered 14/9, 2011 at 14:3 Comment(2)
@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.Godless
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.Contrapositive
A
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.

Alveolate answered 27/1, 2014 at 9:2 Comment(0)
M
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

Meltwater answered 10/11, 2013 at 10:14 Comment(0)
I
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
Inapprehensible answered 15/9, 2011 at 8:7 Comment(3)
Actually this bug still exists on OpenCV 2.4.2 win32.Prepossession
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.Pellucid
Mixing release and debug modes is the cause of the problem. See #9126317Pismire
T
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!

Tagmemics answered 12/10, 2012 at 7:39 Comment(1)
Mixing release and debug modes is the cause of the problem. See #9126317Pismire
T
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.

Tachistoscope answered 16/6, 2016 at 16:40 Comment(0)
Z
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.
Zagazig answered 1/10, 2020 at 9:58 Comment(1)
It worked for me. Thank you so much for the solution and proper explanation.Giuseppe
B
1

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

Bharal answered 14/9, 2011 at 17:11 Comment(0)
S
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.

Seagirt answered 8/12, 2015 at 13:48 Comment(0)
F
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

Fardel answered 1/10, 2011 at 8:2 Comment(0)
S
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").

Superclass answered 12/9, 2012 at 11:12 Comment(0)
O
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);
Olivier answered 30/9, 2014 at 12:2 Comment(0)
W
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).

Wilbertwilborn answered 26/3, 2015 at 17:37 Comment(0)
S
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.

Stephniestepladder answered 24/9, 2015 at 8:18 Comment(0)
R
-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.

Receiptor answered 29/11, 2012 at 14:22 Comment(0)
W
-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);
}
Wilbanks answered 5/10, 2011 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.