简体   繁体   English

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

[英]Convert grayscale image to single color in OpenCV

I'm wondering if there is a way to convert a grayscale image to one color image? 我想知道是否有一种方法可以将灰度图像转换为彩色图像? Like if I have an image in grayscale and I want to convert it to shades of blue instead? 就像我有灰度图像,而是想将其转换为蓝色阴影一样? Is that possible in OpenCV? 在OpenCV中有可能吗?

Thank you very much! 非常感谢你!

A gray scale image is usually just one dimensional. 灰度图像通常只是一维。 Usually what I do if I want to pass in a gray scale image into a function that accepts RGB (3-dimensional), I replicate the the matrix 3 times to create a MxNx3 matrix. 通常,如果我想将灰度图像传递到接受RGB(3维)的函数中,该怎么做,我将矩阵复制3次以创建MxNx3矩阵。 If you wish to only use the Blue channel, just concat MxN of zeros in the 1st dimension and the 2nd dimension while putting the original gray scale values in the 3rd dimension. 如果只希望使用“蓝色”通道,则只需在第一维和第二维上合并零的MxN,而将原始灰度值放入第三维。

According to the opencv community answer , you should of creating a 3-channel image by yourself. 根据opencv社区的答案 ,您应该自己创建一个3通道图像。

   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

After that, you can get your blue channel image. 之后,您可以获取蓝色通道图像。 Normally, the blue channel of an colour image in opencv is the first layer (cuz they put 3 channels as BGR). 通常,opencv中彩色图像的蓝色通道是第一层(因为他们将3个通道作为BGR)。

By the way, if you wanna use the copy each pixel method, you can initial an empty image 顺便说一句,如果您想使用复制每个像素的方法,则可以初始化一个空白图像

   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
        }

However, it will take longer as there are two loops! 但是,由于存在两个循环,因此需要更长的时间!

To accomplish this you would essentially just need to iterate over all the pixels in the grayscale image and map the values over to a color range. 为此,您基本上只需要遍历灰度图像中的所有像素并将值映射到颜色范围即可。 Here is pseudo-code: 这是伪代码:

grayImage:imageObject;
tintedImage:imageObject;

//Define your color tint here
myColorR:Int = 115;
myColorG:Int = 186;
myColorB:Int = 241;

for(int i=0; i<imagePixelArray.length; i++){
   float pixelBrightness = grayImage.getPixelValueAt(i)/255;
   int pixelColorR = myColorR*pixelBrightness;
   int pixelColorG = myColorG*pixelBrightness;
   int pixelColorB = myColorB*pixelBrightness;
   tintedImage.setPixelColorAt(i, pixelColorR, pixelColorG, pixelColorB);
}

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM