5
votes

I am trying to convert a 1 channel image (16 bit) to a 3 channel image in OpenCV 2.3.1. I am having trouble using the merge function and get the following error:

    Mat temp, tmp2;
    Mat hud;
    tmp2 = cv_ptr->image;
    tmp2.convertTo(temp, CV_16UC1);
    temp = temp.t();
    cv::flip(temp, temp, 1);
    resize(temp, temp, Size(320, 240));
    merge(temp, 3, hud);

error: no matching function for call to ‘merge(cv::Mat&, int, cv::Mat&)’

Can anyone help me with this? Thanks in advance!

3

3 Answers

14
votes

If temp is the 1 channel matrix that you want to convert to 3 channels, then the following will work:

cv::Mat out;
cv::Mat in[] = {temp, temp, temp};
cv::merge(in, 3, out);

check the Documenation for more info.

3
votes

Here is a solution that does not require replicating the single channel image before creating a 3-channel image from it. The memory footprint of this solution is 3 times less than the solution that uses merge (by volting above). See openCV documentation for cv::mixChannels if you want to understand why this works

// copy channel 0 from the first image to all channels of the second image
int from_to[] = { 0,0, 0,1, 0,2}; 
Mat threeChannelImage(singleChannelImage.size(), CV_8UC3);
mixChannels(&singleChannelImage, 1, & threeChannelImage, 1, from_to, 3);
1
votes

It looks like you aren't quite using merge correctly. You need to specify all of the cannels that are to be 'merged'. I think you want a three channel frame, with all the channels identical, in Python I would write this:

cv.Merge(temp, temp, temp, None, hud)

From the opencv documentation:

cvMerge: Composes a multi-channel array from several single-channel arrays or inserts a single channel into the array.