Ask Your Question

Convert 3 channel Mat into 4 channel doesn't work?

asked 2019-11-29 05:16:54 -0500

Glychee gravatar image

I'm building a VideoMixer class that can take a 3Channel and 4Channel(has alpha) and overlays the 4Channel onto the 3Channel matrix, then converts these back into a 3Channel matrix for further processing.

The idea is that I don't need the alpha channel in the rest of the program so I'm not going to pass that around.

To convert my 3channel passedFrame (input) into my 4channel temporary frame (for mixing) I'm using Mat tempFrame = Mat::zeros(overlayFrame.size(), 30); passedFrame.convertTo(tempFrame,30,1,1);

Where 30 is the enum value of "CV_64FC4", 1 is Alpha (toggling doesn't change anything) and the other 1 is Beta

I'm printing the sizes and types of each frame and my output however is as follows: - Size of overlay is [704 x 576] and type is 30 - Size of passedframe is [704 x 576] and type is 16 - Size of temporaryframe is [704 x 576] and type is 22

When explicitly setting the conversion type to 30, why is my output type 22? (that 8 should be the alpha channel and seems to be ignored)

edit retag flag offensive close merge delete


I'm building a VideoMixer class

opencv is probably the wrong library (it's all about computer-vision, not about video editing)

also, why do you think, you need alpha channels ? nothing here will respect those

berak gravatar imageberak ( 2019-11-29 05:33:18 -0500 )edit

also, please do not use magic numbers, but well defined enum values, like CV_8UC3

berak gravatar imageberak ( 2019-11-29 05:38:09 -0500 )edit

I was merely using magic numbers to show that my deductions were correct, in the program itself i use the correct enumeration, because I can't print " CV_8UC3" afaik, the name of an ENUM is not resolvable or is it?

Glychee gravatar imageGlychee ( 2019-11-29 06:18:05 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-11-29 05:35:35 -0500

berak gravatar image

convertTo() does not change the channel count, only the depth (e.g. 8U -> 32F)

instead use:


to go from 3 to 4 channels.

edit flag offensive delete link more


Thank you! This worked great. Does this function change the channel count according to what depth the input was or always output CV_8UC4?

Glychee gravatar imageGlychee ( 2019-11-29 06:19:18 -0500 )edit

no it never looks at the depth

berak gravatar imageberak ( 2019-11-29 06:40:58 -0500 )edit

again, YAGNI.

video does not come with alpha channels, and linear blending does not need it

berak gravatar imageberak ( 2019-11-29 09:35:17 -0500 )edit

Question Tools

1 follower


Asked: 2019-11-29 05:15:42 -0500

Seen: 1,076 times

Last updated: Nov 29 '19