1

I have a grayscale image and a some color, represented in RGB triplet. And i need to colorize grayscale image using this triplet.

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

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

fryme
  • 281
  • 4
  • 15

2 Answers2

9

The other answers suggest multiplying the grayscale value by the target RGB color. This is okay, but has the problem that it will alter the overall brightness of your picture. For example, if you pick a dark shade of green the whole image will go darker.

I think the RGB color model is not best suited for this. An alternative would be to pick the color alone, without an associated brightness, then colorize the image while preserving the original brightness of each pixel.

The algorithm would be something like this:

  • pick the target color in terms of two values, a Hue and a Saturation. The hue determines the tone of the color (green, red, etc.), and the saturation determines how pure the color is (the less pure the more the color turns to gray).
  • Now for each pixel in your grayscale image, compute the 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 the new picture.

For the algorithm to convert HLS to RGB see this page or this page.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
0

Conceptually, you'd take the greyscale value of each pixel in the original image and use that as a percentage of the green value. so if a pixel has greyscale value 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);
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Assuming that the "greyscale" source image is actually an RGB triplet which *looks* grey, how does one compute the `87`? – Robᵩ Sep 22 '11 at 21:08
  • greyscale is just colors where the three triplets are the same (or extremely similar). in theory, picking out any one of the r/g/b values would get you the 87, as the other two should be identical, or close (eg. 86/88). – Marc B Sep 22 '11 at 21:10
  • The effect shown in the sample image doesn't quite match this simple formula. Notice that white maps to white, not green. – Mark Ransom Sep 22 '11 at 21:10
  • Yes, Mark was wright. Image should have effect like it is colored image on a white paper. – fryme Sep 22 '11 at 21:15
  • 3
    87/255 == 0. everything will be zero, here – Emilio Garavaglia Sep 22 '11 at 21:21
  • Emilio, yes, but if we wrote: (float) 87/255 * red we should get correct value. – fryme Sep 22 '11 at 21:30
  • Marc, i was tried your formula, and it looks good, but it is very darker then it should to be. Maybe i need to have some threshold value? – fryme Sep 22 '11 at 21:36
  • Yeah, the actual implementation won't be so simple, but this is the basics of how you'd start working on it. – Marc B Sep 22 '11 at 22:14