Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert RGB to grayscale in C

I used many formulas to convert from RGB to grayscale, but didn't get good results compared to GIMP.

I already tested the following formulas:

gray = (int)(0.299 * r + 0.587 * g + 0.144 * b);

gray = (int)(0.299 * r + 0.587 * g + 0.114 * b);

gray = (int)(0.2126 * r + 0.7152 * g + 0.0722 * b);

gray = (int) (0.35*r + 0.50*g + 0.15*b);

gray = ((r * 61) + (g * 174) + (b * 21)) / 256;

gray = (int)((4897 * r + 9617 * g + 1868 * b) >> 14);

gray = r; if(g > gray) gray = g; if(b > gray) gray = b;
like image 509
user2000255 Avatar asked Mar 28 '13 15:03

user2000255


People also ask

How do I convert RGB to grayscale?

You just have to take the average of three colors. Since its an RGB image, so it means that you have add r with g with b and then divide it by 3 to get your desired grayscale image. Its done in this way.

Why do we convert RGB to grayscale?

The main reason why grayscale representations are often used for extracting descriptors instead of operating on color images directly is that grayscale simplifies the algorithm and reduces computational requirements.

Can convert images from RGB mode directly to grayscale?

I = rgb2gray( RGB ) converts the truecolor image RGB to the grayscale image I . The rgb2gray function converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance. If you have Parallel Computing Toolbox™ installed, rgb2gray can perform this conversion on a GPU.


3 Answers

In order to correctly convert RGB to grayscale, you should transform the gamma corrected R,G and B values into the linear color space, apply the conversion in the linear space, and then apply a gamma correction to the result.

The sRGB colorspace has a transform that only approximates a gamma curve. See the Wikipedia article for the particulars. Wikipedia also has a specific section on the subject of sRGB to grayscale.

Basically, to convert a sRGB component to linear format:

double sRGB_to_linear(double x) {
    if (x < 0.04045) return x/12.92;
    return pow((x+0.055)/1.055, 2.4);
}

Then, to convert sRGB to linear grayscale:

double R_linear = sRGB_to_linear(R/255.0);
double G_linear = sRGB_to_linear(G/255.0);
double B_linear = sRGB_to_linear(B/255.0);
double gray_linear = 0.2126 * R_linear + 0.7152 * G_linear + 0.0722 * B_linear;

Then, apply the sRGB gamma correction again:

double linear_to_sRGB(double y) {
    if (y <= 0.0031308) return 12.92 * y;
    return 1.055 * pow(y, 1/2.4) - 0.055;
}

I.e.

double gray_color = round(linear_to_sRGB(gray_linear) * 255);
like image 105
digitalvision Avatar answered Oct 19 '22 23:10

digitalvision


There is a GIMP tutorial that describes several different ways that The GIMP can convert a color image to grayscale. One method is a weighted desaturation like you've tried already, and the article gives some weights that can be used to approximate The GIMP's conversion.

The GIMP's gegl library, which is the basis for all of its image operations, has a rather elaborate mechanism for converting color to greyscale, which takes into account the color of neighboring pixels and uses an iterative approach that seems to emulate the process of developing black and white photographs.

like image 35
Martin Atkins Avatar answered Oct 19 '22 22:10

Martin Atkins


The gimp help for the desaturate tool states three available formulas corresponding to the options named "Lighness", "Luminosity" and "Average". Those would be:

Lightness: 0.5 * (max(R,G,B) + min(R,G,B))

Luminosity: (0.21 * R) + (0.72 * G) + (0.07 * B)

Average: (R + G + B)/3

like image 41
vlz Avatar answered Oct 19 '22 21:10

vlz