Not working in opencv

I am 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 was filled with pointers of zeros, that is, the image did not load.

If I use cvLoadImage then everything works correctly.

The file exists, and I do not mix release and debug libraries. Why imread not working?

+27
c ++ opencv
Sep 14 2018-11-11T00:
source share
15 answers

Reproduced using opencv 2.4.8.

If you are working in Debug, make sure that you also use debug libraries, this fixed our problem .: OpenCV imread (filename) error in debug mode when using release libraries .

+17
Jan 27 '14 at 9:02
source share

I can confirm that there are some problems with imread in OpenCV 2.2. However, problems arose only on a 32-bit Windows system. On linux and mac it worked. I cannot say why this did not work, but we had a small workaround for this.

We fixed this problem with the following macros: maybe you can try this and use "ourImread" from now on.

 #ifdef WIN32 #define ourImread(filename, isColor) cvLoadImage(filename.c_str(), isColor) #else #define ourImread(filename, isColor) imread(filename, isColor) #endif 
+9
Sep 15 2018-11-11T00:
source share

i ran into the same problem with 2.4.6. The reason is that when I selected the library, I chose both the debug version and the release version. When I selected only the debug version for the library, everything worked fine

+8
Nov 10 '13 at 10:14
source share

I 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 cv::waitKey(5000);
I don’t know why, but with system pause it cannot load the image, and it continues to pause after loading the image!

+6
Oct 12
source share

If you think this is an OpenCV bug, please post your image and instructions for playing the OpenCV bugtracker .

+1
Sep 14 '11 at 17:11
source share

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

+1
Sep 12 '12 at 11:12
source share

I know it's late, but someone might find this helpful. I ran into the same problem when using imread using OpenCV-3.0. I tried all the solutions, but in fact I did not add the opencv2/imgcodecs.hpp . Although imshow worked without it, but after that I was able to read the image.

+1
Dec 08 '15 at 13:48
source share

I have a similar problem for Linux when reading only 32-bit tiff images.

 Mat mat= imread(filename, CV_LOAD_IMAGE_ANYDEPTH); 

The problem was that for some reason OpenCV was not built with Tiff support.

+1
Jun 16 '16 at 16:40
source share
  • see related question here

  • Make sure your path is right

  • According to the Opencv API, I would try this call:

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

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, eg 4-channel RGBA image will be loaded as RGB if ). 

Good luck s

0
01 Oct 2018-11-11T00
source share

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); 
0
Sep 30 '14 at 12:02
source share

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

0
Mar 26 '15 at 17:37
source share

Another possibility:

If you are on OS X and statically bind OpenCV, be sure to use libjpeg, which is associated with OpenCV, not the system one.

I had a similar problem with OpenCV 3.0, except that cvLoadImage didn't work either. Thus, this may not answer your question, but perhaps it will help someone else.

0
Sep 24 '15 at 8:18
source share

I also had the same problem that imread did not work and cvLoadImage really worked.

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.

-one
Nov 29 '12 at 14:22
source share

I had the same problem

 cvLoadImage 

older c style

 imread 

- C ++ style, but when using debug libraries it works fine. but slow, when using the release libraries, it will not work, because the opencv C ++ interface has many errors. So you need to use debug libraries and then imread works fine

-one
Nov 23 '14 at 10:30
source share

I know that you want to use "imread" and "CV_LOAD_IMAGE_GRAYSCALE" and convert automatically. But this is another way to upload one image and convert to grayscale:

 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); } 
-2
05 Oct 2018-11-11T00:
source share



All Articles