Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 859
Vinoj John Hosan Avatar asked Oct 19 '25 15:10

Vinoj John Hosan


2 Answers

no fear, your image loaded correctly.

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

like image 179
berak Avatar answered Oct 21 '25 04:10

berak


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

like image 33
kebs Avatar answered Oct 21 '25 04:10

kebs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!