Ask Your Question

Conversion of 3 channel to 1 channel

asked 2017-08-29 04:13:49 -0600

vps gravatar image

updated 2017-09-06 12:36:30 -0600

Hi all, I want to convert BGR video frame (CV_8UC3) to CV_16UC1. In this case, blue channel contains 0 values. I want to do concatenation of green and red frames which should be in short format. I have tried hconcat but it is not working as type of matrix should be same. It will be very helpful, if anyone suggest.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-08-29 04:27:36 -0600

berak gravatar image

updated 2017-08-29 09:18:55 -0600

maybe like this:

// 1. convert to CV_16U
Mat u16; 
bgr.convertTo(u16, CV_16U); // no scaling factor, keep values "as is" !

// 2. split it (and throw away channel 0):
Mat chan[3];
split(16u, chan);

// 3. join green & red channel. 
// one of them will be the "high" 8bit nibble in a 16 bit value, the other the "low" nibble.
//   we cannot simply "shift" Mat's so multiply by 256 instead:

Mat res = chan[1] + chan[2]*256; // M = G | (R<<8)
edit flag offensive delete link more



It works..thanks Berak

vps gravatar imagevps ( 2017-08-29 06:12:08 -0600 )edit

Hi Break, I have another question. First, I am converting depth data (CV_16UC1) to BGR (CV_8UC3) video Stream.. After that I am doing reverse procedure to get the original depth frames from the BGR video. But I am unable to get the correct results. I am using below code snippet for writing the video.

auto writer = VideoWriter("out.avi", VideoWriter::fourcc('I', '4', '2', '0'), 12, frame.size());

vps gravatar imagevps ( 2017-08-30 02:13:33 -0600 )edit

Question Tools

1 follower


Asked: 2017-08-29 04:13:49 -0600

Seen: 10,370 times

Last updated: Aug 29 '17