gpt4 book ai didi

c++ - 在 OpenCV 中将灰度图像转换为单色

转载 作者:太空宇宙 更新时间:2023-11-03 22:24:24 27 4
gpt4 key购买 nike

我想知道是否有办法将灰度图像转换为彩色图像?就像我有一张灰度图像,我想将其转换为蓝色阴影?这在 OpenCV 中可能吗?

非常感谢!

最佳答案

根据 opencv 社区answer ,你应该自己创建一个 3 channel 图像。

   Mat empty_image = Mat::zeros(src.rows, src.cols, CV_8UC1);//initial empty layer
Mat result_blue(src.rows, src.cols, CV_8UC3); //initial blue result

Mat in1[] = { ***GRAYINPUT***, empty_image, empty_image }; //construct 3 layer Matrix
int from_to1[] = { 0,0, 1,1, 2,2 };
mixChannels( in1, 3, &result_blue, 1, from_to1, 3 ); //combine image

之后,你就可以得到你的蓝色 channel 图像了。通常,opencv 中彩色图像的蓝色 channel 是第一层(因为他们将 3 个 channel 作为 BGR)。

顺便说一句,如果你想使用复制每个像素的方法,你可以初始化一个空图像

   Mat result_blue(src.rows, src.cols, CV_8UC3); //blue result
for (int i =0; i<src.rows; i++)
for (int j=0; j<src.cols; j++){
Vec3b temp = result_blue.at<Vec3b>(Point(i,j));//get each pixel
temp[0] = gray.at<uchar>(i,j); //give value to blue channel
result_blue.at<Vec3b>(Point(x,y)) = temp; //copy back to img
}

但是,由于有两个循环,因此需要更长的时间!

关于c++ - 在 OpenCV 中将灰度图像转换为单色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43918878/

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