Ask Your Question

keeping the alpha channel in transparent image mat

asked 2016-10-02 20:05:31 -0600

inac gravatar image

How do you keep an image's transparent background or alpha channel?

i.e., Canny input: .png with existing transparent alpha channel background. I'd like to figure out how to get the latter output instead of the first.

image description

image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-10-02 23:25:23 -0600

berak gravatar image

updated 2016-10-03 00:49:56 -0600

first note, that alpha is irrelevant to computer vision, so opencv won't ever use or try to display it.

step1: (shave off the original alpha channel, and keep it for later)

Mat image = ... // 4 channels bgra

Mat channels[4];
split(image, channels);

Mat alpha = channels[3];

step2: (when you're done with your canny or whatever, make a new Mat with alpha)

Mat canny = ... // 1 channel grayscale !

Mat newchan[4] = { canny, canny, canny, alpha };
Mat final;
merge(newchan, 4, final);

step3: (save it to disk):

imwrite("glasses.png", final);
edit flag offensive delete link more



Thanks - sorry for asking the question wrongly, when I was in-fact looking for whether the shaving had to be done or not - apparently it had to be!

inac gravatar imageinac ( 2016-10-03 01:29:35 -0600 )edit

Question Tools



Asked: 2016-10-02 20:05:31 -0600

Seen: 3,610 times

Last updated: Oct 03 '16