0

I encountered a problem when I want to read an image using the OpenCV function imread(). The image is Ok and I can show it in the image display software. But when I use the imdecode() to get the image data, the data returns NULL. I will upload the image and the code and hope some one could help me

Mat img = imread(image_name);
if(!img.data) return -1;

The image's link is here: http://img3.douban.com/view/photo/raw/public/p2198361185.jpg

PS: The image_name is all right. I guess OpenCV cannot decode this image. So is there any way to decode this image using OpenCV?, like add new decode library. By the way, I can read this image using other image library such as freeImage.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Minhui Wu
  • 15
  • 1
  • 4
  • 1
    The path to the image might be wrong i.e. `image_name`. – bikz05 Oct 28 '14 at 05:12
  • the image_name is all right, you can test it on your pc I think it's the image's problem and the opencv can not decode this image. So is there any thing I can do to make opencv enable to decode this image. – Minhui Wu Oct 28 '14 at 06:04
  • Can't access image.. 403 error. Well, can you tell exact content in image_name. – Pervez Alam Oct 28 '14 at 06:12
  • Can you read different images (image types) from the same path? – Micka Oct 28 '14 at 07:49

3 Answers3

1

Your image is in .gif and it is not supported by OpenCV as of now.

Note OpenCV offers support for the image formats Windows bitmap (bmp), portable image formats (pbm, pgm, ppm) and Sun raster (sr, ras). With help of plugins (you need to specify to use them if you build yourself the library, nevertheless in the packages we ship present by default) you may also load image formats like JPEG (jpeg, jpg, jpe), JPEG 2000 (jp2 - codenamed in the CMake as Jasper), TIFF files (tiff, tif) and portable network graphics (png). Furthermore, OpenEXR is also a possibility.

Source - Click here

You can use something like this, to perform the conversion.

I was able to load your image using imread using this. Also, you can check out FreeImage.

bikz05
  • 1,575
  • 12
  • 17
0

You can also try to use the library gif2numpy. It converts a gif image to a numpy image which then can be loaded by OpenCV:

import cv2, gif2numpy
np_images, extensions, image_specs = gif2numpy.convert("yourgifimage.gif")
cv2.imshow("np_image", np_images[0])
cv2.waitKey()

The library can be found here: https://github.com/bunkahle/gif2numpy It is not dependent on PIL or pillow for this like imageio.

bunkus
  • 975
  • 11
  • 19
-5

There are two methods to read an image in OpenCV, one is using Mat the other one using IplImage. I see you have used the former one. You can try with the second argument of imread also:

image = imread("image.jpg", CV_LOAD_IMAGE_COLOR);   // Read the file

else use IplImage

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <opencv2/core/core.hpp>

IplImage* src = 0;

if( (src = cvLoadImage("filename.jpg",1)) == 0 )
{
    printf("Cannot load file image %s\n", filename);
}

If they don't work please check if you have installed libjpeg, libtiff and other dependencies for reading an image in OpenCV.

Hope it would help.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
  • 1
    please, do not advise folks here to use the deprecated c-api. it is no more an option since 2010. – berak Oct 28 '14 at 07:09