Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert 1 channel image to 3 channel

Tags:

opencv

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!

like image 984
keshavdv Avatar asked Apr 02 '12 03:04

keshavdv


People also ask

Can a grayscale image have 3 channels?

A grayscale image has just one channel.

What is an image channel?

Image channels are a result of the superheterodyne receiver design, which uses a balanced mixer with two inputs followed by a filter for the purpose of separating out the desired signal from the spectrum at its input terminals. From: Modern Cable Television Technology (Second Edition), 2004.

What is cv2 merge?

The cv2. split() function splits the source multichannel image into several single-channel images. The cv2. merge() function merges several single-channel images into a multichannel image.


2 Answers

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.

like image 50
volting Avatar answered Sep 22 '22 05:09

volting


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);
like image 38
RawMean Avatar answered Sep 21 '22 05:09

RawMean