开发者

Colorize grayscale image

开发者 https://www.devze.com 2023-04-07 00:42 出处:网络
I have a grayscale image and a some color, represented in RGB triplet. And i need to colorize grayscale image using this triplet.

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

Colorize grayscale image

The left image represents what we have and the right what i need to have. Now in p开发者_开发百科rogram 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.


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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消