Grayscale color image

I have a grayscale image and some color represented in an RGB triplet. And I need to colorize the image in shades of gray using this triplet.

coloring_image.jpg

The left image represents what we have and what I need. Now in the program I have some function where the input values ​​are the R, G and B values ​​of the original image and the color of the RGB value, which should be used as the color value. And I can't decide how to increase or decrease the source RGB using color RGB in order to have the right color palette.

C ++ language, but it is not so important. Thanks in advance.

+1
source share
2 answers

Other answers suggest multiplying the grayscale value with the target RGB color. This is normal, but there is a problem in that it will change the overall brightness of your image. For example, if you choose a darker shade of green, the whole image will become darker.

I think the RGB color model is not suitable for this. An alternative would be to select a color separately, without the corresponding brightness, and then colorize the image, preserving the original brightness of each pixel.

The algorithm will be something like this:

  • select the target color in terms of two values, hue and saturation. The color tone determines the color tone (green, red, etc.), and the saturation determines how pure the color is (the cleaner, the more the color becomes gray).
  • Now for each pixel in the grayscale image, calculate a new pixel in the HLS color model, where H and S are your target hue and saturation, and L is the gray value of your pixel.
  • Convert this HLS color to RGB and save to a new image.

For the HLS to RGB conversion algorithm, see this page or this page .

+9
source

It’s clear that you take the grayscale value of each pixel in the original image and use it as a percentage of the green value. therefore, if a pixel has a grayscale value of 87, then the equivalent pixel in the colorized image would be:

colorized_red = (87 / 255) * red_component(green_shade); colorized_green = (87 / 255) * green_component(green_shade); colorized_blue = (87 / 255) * blue_component(green_shade); 
0
source

Source: https://habr.com/ru/post/898453/


All Articles