简体   繁体   English

如何在OpenCV中将灰度图像复制到RGB图像红色通道?

[英]How to copy grayscale image to RGB images red channel in OpenCV?

I have two input images , which are greyscale and I am creating one more image , which is RGB and should contain in the red channel one of the grey image and in the green the other. 我有两个输入图像 ,它们是灰度 ,我创建了另一个图像 ,它是RGB ,应该在红色通道中包含一个灰色图像,在绿色中包含另一个。

Mat img, img2;
img =imread("above.jpg", CV_LOAD_IMAGE_GRAYSCALE);
img2 =imread("left.jpg", CV_LOAD_IMAGE_GRAYSCALE);

Mat * aboveLeft = new Mat(img.rows, img.cols, CV_LOAD_IMAGE_COLOR);

int from_to [] = {0,1};
cv::mixChannels(&img, 1, aboveLeft, 3, from_to, 1);

I've tried to use the mixChannels command, to write img input into aboveLeft 's first channel, but I couldn't figure out the right syntax and get assertion errors. 我尝试使用mixChannels命令,将img输入写入aboveLeft的第一个通道,但我无法找出正确的语法并得到断言错误。

Is this the right way to fill in a channel? 这是填写频道的正确方法吗?

I assume img and img2 has same size and type . 我假设imgimg2具有相同的大小类型

cv::Mat img = cv::imread("above.jpg", CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat img2 = cv::imread("left.jpg", CV_LOAD_IMAGE_GRAYSCALE);

std::vector<cv::Mat> images(3);
Mat black = Mat::zeros(img.rows, img.cols, img.type());
images.at(0) = black; //for blue channel
images.at(1) = img;   //for green channel
images.at(2) = img2;  //for red channel

cv::Mat colorImage;
cv::merge(images, colorImage);

Assuming that you have 3 grayscale matrices, you can use cv::merge() : 假设你有3个灰度矩阵,你可以使用cv::merge()

cv::Mat blue, green, red;
std::vector<cv::Mat> images(3);

// OpenCV works natively with BGR ordering
images.at(0) = blue;
images.at(1) = green;
images.at(2) = red;

cv::Mat color;
cv::merge(images, color);

As pointed out in the comments, you need to find a way to figure out what you want for the blue channel! 正如评论中指出的那样,你需要找到一种方法来弄清楚你想要的蓝色通道!

/ Using OpenCV3 / 使用OpenCV3
Take a Binary Image using zeros convert it to gray Then to color using / 使用零使用二进制图像将其转换为灰色然后使用 /

cvtColor(Image, channels, CV_GRAY2BGR);
Mat bgr[3];

/ Split the channels into BGR / / 将频道拆分为BGR /

split(channels, bgr);

bgr[0] = 0, bgr[1] = img2, bgr[2] = img;
merge(bgr, 3, channels);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM