6

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

例如,当图片被着色为“蓝色”时8UC1,强度为 的灰度像素I = 50应该成为强度的8UC3彩色像素。BGR = (50, 0, 0)

例如,在 Matlab 中,我所要求的可以简单地用两行代码创建:

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

但令人惊讶的是,我在 OpenCV 中找不到任何类似的东西。

4

2 回答 2

5

好吧,同样的事情需要在 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;
}
于 2013-04-05T21:42:17.850 回答
5

特殊的功能可以做到这一点-从contrib模块中的v2.4.5 开始,OpenCV 中的 applyColorMap 。有不同的颜色图可用:

彩色地图

于 2013-04-07T20:17:39.123 回答