我需要从灰度图像创建一个“红色”图像。我正在使用这段代码:
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
有一些解释:
void build_red(const cv::Mat& in, cv::Mat& out) {
in 是输入矩阵,out 是输出。
out = Mat::zeros(in.rows, in.cols, CV_8UC1);
为 out 分配一些空间(可能没用,但我尝试的一部分)
Mat zeros = Mat::zeros(in.rows, in.cols, CV_8UC1);
Mat tmp;
in.convertTo(tmp, CV_8UC1);
创建一个相同大小的空矩阵,并将输入图像转换为单通道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);
创建一个具有三个通道的向量,然后将它们合并到“输出”中。
但是,当我运行代码时,我收到以下消息:
Using 3 channels
和以下例外:
OpenCV Error: Bad number of channels (Source image must have 1, 3 or 4 channels)
in cvConvertImage, file /[...]/libs/OpenCV-2.4.0/modules/highgui/src/utils.cpp,
line 611
terminate called after throwing an instance of 'cv::Exception'
what(): /[...]/libs/OpenCV-2.4.0/modules/highgui/src/utils.cpp:611:
error: (-15) Source image must have 1, 3 or 4 channels in function cvConvertImage
请你帮助我好吗?从我没有经验的角度来看,图像的类型是相同的,通道数是正确的。