2013-04-05 104 views
5

什么是“着色”灰度图像的直接方法。通过着色,我的意思是将灰度强度值移植到新图像中的三个R,G,B通道中的一个。OpenCV:将灰度图像着色的直接方法

例如,当图像被着色为“蓝色”与I = 50强度8UC1灰度像素应该成为强度​​的8UC3彩色像素。

在Matlab中的例子,就是我要问的可以用两行代码简单地创作:

color_im = zeros([size(gray_im) 3], class(gray_im)); 
color_im(:, :, 3) = gray_im; 

但出乎意料的是,我不能找到OpenCV的类似的事情。

回答

4

那么,同样的事情需要用C多做一些工作++和OpenCV:

// Load a single-channel grayscale image 
cv::Mat gray = cv::imread("filename.ext", CV_LOAD_IMAGE_GRAYSCALE); 

// Create an empty matrix of the same size (for the two empty channels) 
cv::Mat empty = cv::Mat::zeros(gray.size(), CV_8UC1); 

// Create a vector containing the channels of the new colored image 
std::vector<cv::Mat> channels; 

channels.push_back(gray); // 1st channel 
channels.push_back(empty); // 2nd channel 
channels.push_back(empty); // 3rd channel 

// Construct a new 3-channel image of the same size and depth 
cv::Mat color; 
cv::merge(channels, color); 

或(压缩)功能:

cv::Mat colorize(cv::Mat gray, unsigned int channel = 0) 
{ 
    CV_Assert(gray.channels() == 1 && channel <= 2); 

    cv::Mat empty = cv::Mat::zeros(gray.size(), gray.depth()); 
    std::vector<cv::Mat> channels(3, empty); 
    channels.at(channel) = gray; 

    cv::Mat color; 
    cv::merge(channels, color); 
    return color; 
} 
+0

有趣的是,之后我问这个问题,我发现了关于'CV ::合并()'函数和CV的'了'VECTOR' ::垫'并且做了和你一样的事情。谢谢。 – Bee 2013-04-06 15:59:57

3

special function to do this - 在OpenCV中applyColorMap从v2.4.5中的contrib模块。有不同颜色可供地图:

Color maps

+2

我不明白这应该如何帮助实现所需的输出?显然,我们无法定义自定义颜色映射。 – Niko 2013-04-08 06:47:16

+0

对不起。我错了。 – brotherofken 2013-04-08 06:50:22