gpt4 book ai didi

c++ - OpenCV:一种简单明了的灰度图像着色方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:19:16 26 4
gpt4 key购买 nike

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

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

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

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

但令人惊讶的是,我在 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);

或作为函数(压缩):

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;
}

关于c++ - OpenCV:一种简单明了的灰度图像着色方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15843206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com