Ask Your Question
4

Problem Reading PNG with Transparency layer

asked 2012-10-18 12:43:26 -0500

radford.parker gravatar image

I'm using 2.3.1 and am having problems loading a PNG file. An example picture is given below. Apparently the background of this image is transparent. You can see this because I can grab the alpha layer using imagemagick. The output from imagemagick is shown below. The problem is that when I load the image using imread, the matrix becomes the output shown below. I have tried loading the image with -1, 0, and 1 as flags and nothing has helped. When I load it with -1, it has 4 channels, but the 4th channel just contains all zeros. Has anyone seen something similar to this before? image description image description image description

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
5

answered 2012-10-18 13:11:24 -0500

Kirill Kornyakov gravatar image

On my Ubuntu with OpenCV from master I can load 4-channel matrix with correct alpha channel, and I can write it back to the hard drive. But I can see the last attached image if I do imshow for the loaded image. My code for reference is below.

So, it is possible that imshow's behavior is undefined for the cases with alpha. But I'm not sure...

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <iostream>

using namespace std;
using namespace cv;

int main()
{
    Mat image = imread("/tmp/13505817932658911.png", -1);
    cout << image.channels() << endl;
    imshow("image", image); //shows incorrect image in window with orange lines
    waitKey();

    vector<Mat> ch;
    split(image, ch);
    imshow("mask", ch[3]); //shows correct mask
    waitKey();

    imwrite("/tmp/out.png", image); // saves correct image

    return 0;
}
edit flag offensive delete link more
1

answered 2012-12-03 14:27:52 -0500

mjepson gravatar image

The implementation of highgui's imshow fails to show any transparent images correctly. I place the (semi) transparent image over another (possibly a checkerboard or something) to show how it actually looks. See this post for a method to do so easily. You can display the resulting image (which is a 3 channel BGR image) correctly with imshow.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2012-10-18 12:43:26 -0500

Seen: 11,368 times

Last updated: Dec 03 '12