例如,当图片被彩色化为“蓝色”时,强度为I = 50的8UC1灰度像素应变为强度BGR =(50,0)的8UC3颜色像素.
例如,在Matlab中,可以使用两行代码简单地创建我要求的内容:
color_im = zeros([size(gray_im) 3],class(gray_im));color_im(:,:,3) = gray_im;
但令人惊讶的是,我在OpenCV中找不到类似的东西.
解决方法 好吧,同样的事情需要在C和OpenCV中多做一些工作:// Load a single-channel grayscale imagecv::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 imagestd::vector<cv::Mat> channels;channels.push_back(gray); // 1st channelchannels.push_back(empty); // 2nd channelchannels.push_back(empty); // 3rd channel// Construct a new 3-channel image of the same size and depthcv::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:一种对灰度图像进行着色的直接方法全部内容,希望文章能够帮你解决c – OpenCV:一种对灰度图像进行着色的直接方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)