简体   繁体   English

将三个灰度[R,G,B]图像合并为opencv中的单色图像

[英]Merging three grayscale [R, G, B] images into a single color image in opencv

I have 3 grayscale images that I created using the split function. 我有3个使用分割功能创建的灰度图像。 Now I want to regenerate the color image back. 现在我想重新生成彩色图像。 I tried the following code but it did not work. 我尝试了以下代码,但它没有用。 The resultant image I got was still grayscale. 我得到的图像仍然是灰度。

        cv::Mat R = cv::imread("/home/r/secret_R.png",0);
        cv::Mat G = cv::imread("/home/r/secret_G.png",0);
        cv::Mat B = cv::imread("/home/r/secret_B.png",0);
        std::vector<cv::Mat> array_to_merge;
        array_to_merge.push_back(R);
        array_to_merge.push_back(G);
        array_to_merge.push_back(B);
        cv::Mat color;
        cv::merge(array_to_merge, color);

So then I tried the cvtColor function: 那么我尝试了cvtColor函数:

        cv::Mat color_converted;
        cv::cvtColor(color, color_converted, CV_GRAY2BGR);
        cv::imwrite("/home/r/color_secret.png",color_converted);

But apparently, it only works for source images that have only a single channel. 但显然,它仅适用于只有一个通道的源图像。 I passed it one of my grayscale images: 我通过了我的一张灰度图像:

cv::cvtColor(R, color_converted, CV_GRAY2BGR);

But I still could not get a color image back. 但我仍然无法获得彩色图像。 The resultant image was still grayscale 得到的图像仍然是灰度的

What am I doing wrong ? 我究竟做错了什么 ? How do I get my color image back ? 如何恢复我的彩色图像?

I can't see anything wrong with your code. 我看不出你的代码有什么问题。 I suspect it may be the source data, or how it is being treated by the importer. 我怀疑它可能是源数据,或者它是如何被进口商处理的。 After you load the images, can you print out the image metadata to see what attributes OpenCV is reading? 加载图像后,是否可以打印出图像元数据以查看OpenCV正在读取的属性? They all must have the same dimensions and a single channel. 它们都必须具有相同的尺寸和单个通道。

std::cout << "Red:   " << R.size().width << " x " << R.size().height << " x " << R.channels() << std::endl;
std::cout << "Green: " << G.size().width << " x " << G.size().height << " x " << G.channels() << std::endl;
std::cout << "Blue:  " << B.size().width << " x " << B.size().height << " x " << B.channels() << std::endl;

I just ran your program, just with the headers and main added. 我只是运行你的程序,只是添加了标题和主要内容。 I made one small change, to merge in B,G,R order (the default). 我做了一个小改动,合并为B,G,R顺序(默认)。 I compiled it on Mac OS X 10.8 with: 我在Mac OS X 10.8上编译它:

g++ -o merge merge.cpp -lopencv_core -lopencv_highgui

against OpenCV version 2.4.3. 针对OpenCV版本2.4.3。

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>

int main(int argc, char* argv[])
{
    cv::Mat R = cv::imread("chan_dest_red.png", 0);
    cv::Mat G = cv::imread("chan_dest_green.png", 0);
    cv::Mat B = cv::imread("chan_dest_blue.png", 0);

    std::vector<cv::Mat> array_to_merge;

    array_to_merge.push_back(B);
    array_to_merge.push_back(G);
    array_to_merge.push_back(R);

    cv::Mat color;

    cv::merge(array_to_merge, color);

    imwrite("merged.png", color);

    return 0;
}

I had some single channel images from another project already split into files, and I just merged those into a single colour image and it worked just fine. 我有一些来自另一个项目的单通道图像已经拆分成文件,我只是将它们合并为一个彩色图像,它工作得很好。 The source file info is as follows: 源文件信息如下:

Red:   500 x 333 x 1
Green: 500 x 333 x 1
Blue:  500 x 333 x 1

One way would be to use cvtColor to create a color image from one channel, then replace the other two colors pixel-by-pixel in a loop using the at function. 一种方法是使用cvtColor从一个通道创建彩色图像,然后使用at函数在循环中逐个像素地替换另外两种颜色。 There is probably a way to do this more easily with the matrix operators but I'm not familiar enough with openCV to know for sure. 对于矩阵运算符,可能有一种方法可以更轻松地完成此操作,但我对openCV不太熟悉,无法确切知道。

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

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