简体   繁体   中英

PNG image is not loading properly in OpenCV

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

这个

(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

no fear, your image loaded correctly.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM