Anda di halaman 1dari 3

Merging channels in OpenCV

up vote 4 I need to create a 'red' image from a grayscale image. I am using this code:
down vote
favorite
void build_red(const cv::Mat& in, cv::Mat& out) {
out = Mat::zeros(in.rows, in.cols, CV_8UC1);

Mat zeros = Mat::zeros(in.rows, in.cols, CV_8UC1);


Mat tmp;
in.convertTo(tmp, CV_8UC1);
vector<Mat> ch;
ch.push_back(zeros);
ch.push_back(zeros);
ch.push_back(tmp);
cout << "Using " << ch.size() << " channels" << endl;
merge(ch, out);
} // build_red

With some explanations:


void build_red(const cv::Mat& in, cv::Mat& out) {

in is the input matrix, out the output.


out = Mat::zeros(in.rows, in.cols, CV_8UC1);

allocate some space for out (may be useless, but part of my attempts)
Mat zeros = Mat::zeros(in.rows, in.cols, CV_8UC1);
Mat tmp;
in.convertTo(tmp, CV_8UC1);

Create an empty matrix with the same size and convert the input image to singlechannel uchar.
vector<Mat> ch;
ch.push_back(zeros);
ch.push_back(zeros);
ch.push_back(tmp);
cout << "Using " << ch.size() << " channels" << endl;
merge(ch, out);

Create a vector with three channels, then merge them into 'out'.
However, when I run the code I get the following message:
Using 3 channels

and the following exception:


OpenCV Error: Bad number of channels (Source image must have 1, 3
or 4 channels)
in cvConvertImage, file /[...]/libs/OpenCV2.4.0/modules/highgui/src/utils.cpp,
line 611
terminate called after throwing an instance of 'cv::Exception'

what(): /[...]/libs/OpenCV2.4.0/modules/highgui/src/utils.cpp:611:
error: (-15) Source image must have 1, 3 or 4 channels in function
cvConvertImage

Could you please help me? From my inexperienced point of view, the type of the
images is the same and the number of channels is correct.
c++ opencv
asked Jan 29 '13 at 11:41
shareimprove this question
Antonio Sesto
4331616
The out image should be three-channel. Try: out =
2 Mat::zeros(in.rows, in.cols, CV_8UC3); Ann Orlova Jan 29 '13 at
11:53
The function is running perfectly. I only get the specified error if the
input is a 3 channel image. Make sure that input is 1 channel and
output is 3 channel image. sgarizvi Jan 29 '13 at 11:58
Are you sure the exception is thrown by the function you provided?
luhb Jan 29 '13 at 11:58
add a comment

1 Answer
active oldest votes
up vote 5 Why are you converting the image if you have a grayscale image present?
down
vote
Just create two empty matrix of the same size for Blue and Green.
accepted

And you have defined your output matrix as 1 channel matrix. Your output matrix
must contain at least 3 channels. (Blue, Green and Red). Where Blue and Green
will be completely empty and you put your grayscale image as Red channel of the

output image.
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
using namespace std;
using namespace cv;
int main()
{
Mat img, g, fin_img;
img = imread("Lenna.png",CV_LOAD_IMAGE_GRAYSCALE);
vector<Mat> channels;
g = Mat::zeros(Size(img.rows, img.cols), CV_8UC1);
channels.push_back(g);
channels.push_back(g);
channels.push_back(img);

merge(channels, fin_img);
imshow("img", fin_img);
waitKey(0);
return 0;

Anda mungkin juga menyukai