Chris Mondiek
Chris Mondiek

Reputation: 153

combine two RGB images to a 6 channel image - openCV

I am working with a openCV and I need to combine two three channel RGB images into one 6 channel image. I'm not sure exactly how I would go about combining them. Any ideas?

Upvotes: 3

Views: 3688

Answers (2)

gavinb
gavinb

Reputation: 20048

First of all you will have to create a destination matrix.
cv::Mat allow to create images with up to CV_CN_MAX (512) channels.
For example cv::Mat(cv::Size(640,480),CV_8UC(6)) is a 640x480 image with six 8-bit channels. More generally, given s input, you can obtain an adapting destination matrix:

cv::Mat(s.size(), CV_MAKETYPE(s.depth(), 6))

See Data types and cv::Mat constructor.

You might want to use Mat::convertTo to ensure both your input images have the same format, then use mixChannels:

mixChannels(const Mat *     src,
    size_t      nsrcs,
    Mat *   dst,
    size_t      ndsts,
    const int *     fromTo,
    size_t      npairs 
)   

The 2 source images need to be put into a container (remember that assigning a matrix just creates a new header, without copying the image buffer unless explicitly requested).

The last thing to do is to create a from_to vector with the channel mapping. Given s0 and s1 input images, and aiming to have the following mapping:

s0 red   -> 1st output channel
s0 green -> 2nd
s0 blue  -> 3rd
s1 red   -> 4th
s1 green -> 5th
s1 blue  -> 6th

The final code will be:

std::vector<cv::Mat> s;
s.resize(2); //Or `push_back` your 2 input images
s[0] = ...   //1st input image
s[1] = ...   //2nd input image
auto d = cv::Mat(s[0].size(), CV_MAKETYPE(s[0].depth(), 6));

int from_to[] = { 0,0, 1,1, 2,2, 3,3, 4,4, 5,5 };
cv::mixChannels(s.data(), s.size(), &d, 1, from_to, 6);

Upvotes: 2

Martin Beckett
Martin Beckett

Reputation: 96167

split splits an image in an array of single channel images and merge puts them back together in any order. There is also mixchannels() which is a more efficenct but more complex combination of the two

In theory opencv works with any number of channels but I suspect many of the functions only work with 1 or 3 (ie grey or 'normal' color)

Upvotes: -1

Related Questions