Reputation: 321
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.
Upvotes: 1
Views: 4590
Reputation: 67502
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:
For the algorithm to convert HLS to RGB see this page or this page.
Upvotes: 9
Reputation: 360872
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);
Upvotes: 0