Vinoj John Hosan
Vinoj John Hosan

Reputation: 6863

PNG image is not loading properly in OpenCV

I am using the below code to view the following image.

This

(If you cannot see the image, right click the above space and save image..) The image is 32 bit. OpenCV shows white image. It is a white arrow image, with alpha value around it.

Mat img = imread("FordwardIcon.png", CV_LOAD_IMAGE_UNCHANGED | CV_LOAD_IMAGE_ANYDEPTH);
cout << img.depth() << "\t" << img.channels() << endl;
imshow("img", img);
waitKey(0);

Kindly let me know how to correctly load the image

Upvotes: 4

Views: 5819

Answers (2)

kebs
kebs

Reputation: 6707

At present, Opencv (3.0) does not seem to be able to load png images with transparency (aka "alpha channel"). To load these kind of images with cv::imread(), you need to remove the transparency channel first, and replace it with some color.

This can be done easily with Linux using the Imagemagick tools. For example:

convert input.png -background '#00ff00' -flatten result.png

You can check if an image has it (or not) with the file tool. On the image you provide:

>$ file input.png 

gives:

input.png: PNG image data, 52 x 32, 8-bit gray+alpha, non-interlaced

while

>$ file result.png 

gives

result.png: PNG image data, 52 x 32, 8-bit colormap, non-interlaced

See http://www.imagemagick.org/script/convert.php

Upvotes: 3

berak
berak

Reputation: 39796

no fear, your image loaded correctly.

the problem is more that you expected imshow() to honour the alpha channel ( it does not ) .

Upvotes: 3

Related Questions