C++ 在 OpenCV 中合并通道
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14582082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Merging channels in OpenCV
提问by Antonio Sesto
I need to create a 'red' image from a grayscale image. I am using this code:
我需要从灰度图像创建一个“红色”图像。我正在使用此代码:
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.
in 是输入矩阵,out 是输出。
out = Mat::zeros(in.rows, in.cols, CV_8UC1);
allocate some space for out (may be useless, but part of my attempts)
为 out 分配一些空间(可能没用,但我尝试的一部分)
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 single-channel uchar.
创建一个相同大小的空矩阵,并将输入图像转换为单通道 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/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
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.
请你帮助我好吗?从我没有经验的角度来看,图像的类型是相同的,通道数是正确的。
回答by Froyo
Why are you converting the image if you have a grayscale image present?
如果存在灰度图像,为什么要转换图像?
Just create two empty matrix of the same size for Blue and Green.
只需为蓝色和绿色创建两个相同大小的空矩阵。
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.
并且您已将输出矩阵定义为 1 通道矩阵。您的输出矩阵必须至少包含 3 个通道。(蓝色、绿色和红色)。其中蓝色和绿色将完全为空,您将灰度图像作为输出图像的红色通道。
#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;
}