Ask Your Question

Revision history [back]

A simple code solution in C++. If you want only a 3 channel, then remove the fourth channel everywhere.

// Paste the single layer image into a multi layer image
Mat fgMask_color(fgMask.rows, fgMask.cols, CV_8UC4);
Mat in[] = { fgMask.clone(), fgMask.clone(), fgMask.clone(), fgMask.clone() };
int from_to[] = { 0,0, 1,1, 2,2, 3,3 };
mixChannels( in, 4, &fgMask_color, 1, from_to, 4 );
// Paste it back to the original image
 fgMask_color.copyTo(cvImage(roi));

A simple code solution in C++. If you want only a 3 channel, then remove the fourth channel everywhere.

// Paste the single layer image into a multi layer image
Mat fgMask_color(fgMask.rows, fgMask.cols, CV_8UC4);
Mat in[] = { fgMask.clone(), fgMask.clone(), fgMask.clone(), fgMask.clone() };
int from_to[] = { 0,0, 1,1, 2,2, 3,3 };
mixChannels( in, 4, &fgMask_color, 1, from_to, 4 );
// Paste it back to the original image
 fgMask_color.copyTo(cvImage(roi));