什么是"着色"灰度图像的直接方法.通过着色,我的意思是将灰度强度值移植到新图像中的三个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;
Run Code Online (Sandbox Code Playgroud)
但令人惊讶的是,我在OpenCV中找不到类似的东西.
嗯,同样的事情需要在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);
Run Code Online (Sandbox Code Playgroud)
或作为一种功能(压缩):
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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5714 次 |
最近记录: |