Algorithm for converting from RGB to grayscale

Source: Internet
Author: User
Tags integer division ranges

I. Basics
There is a well-known psychological formula for the conversion of color to Gray:

Gray = r * 0.299 + G * 0.587 + B * 0.114

Ii. Integer Algorithm

In actual use, it is necessary to use an integer algorithm to avoid low-speed floating-point operations.
Note that the coefficients are not accurate by three digits. We can scale them by 1000 times to realize the integer algorithm:

Gray = (R * 299 + G * 587 + B * 114 + 500)/1000

RGB is generally 8-bit precision, and is now scaled 1000 times, so the above operation is a 32-bit integer operation. Note that the Division following is an integer division, so we need to add 500 to implement rounding.
Because this algorithm requires 32-bit operations, another variant of this formula is very popular:

Gray = (r x 30 + G x 59 + B x 11 + 50)/100

However, although the previous formula is a 32-bit integer operation, 16-bit integer multiplication and division commands can be used based on the characteristics of the integer multiplication and division commands in the 80 x86 system. Now 32 bits are popular (amd64 is available), so we recommend that you use the previous formula.

Iii. Integer Shift Algorithm

The above integer algorithm is very fast, but it still restricts the speed, that is, the last division. Shift is much faster than division, so the coefficient can be scaled to an integer power of 2.
Traditionally, the 16-bit precision is used. The 16 power of 2 is 65536, so the coefficient is calculated as follows:

0.299*65536 = 19595.264 ≈ 19595
0.587*65536 + (0.264) = 38469.632 + 0.264 = 38469.896 ≈ 38469
0.114*65536 + (0.896) = 7471.104 + 0.896 = 7472

Many people may see that the rounding method I use is not rounding. There will be a large error in rounding, and the error of the previous calculation results should be calculated together. The rounding method is the tail removal method:

The written expression is:

Gray = (R * 19595 + G * 38469 + B * 7472)> 16

Coefficient of 2 to 20 bit precision:

Gray = (R * 1 + G * 2 + B * 1)> 2
Gray = (R * 2 + G * 5 + B * 1)> 3
Gray = (R * 4 + G * 10 + B * 2)> 4
Gray = (R * 9 + G * 19 + B * 4)> 5
Gray = (R * 19 + G * 37 + B * 8)> 6
Gray = (R * 38 + G * 75 + B * 15)> 7
Gray = (R * 76 + G * 150 + B * 30)> 8
Gray = (R * 153 + G * 300 + B * 59)> 9
Gray = (R * 306 + G * 601 + B * 117)> 10
Gray = (R * 612 + G * 1202 + B * 234)> 11
Gray = (R * 1224 + G * 2405 + B * 467)> 12
Gray = (R * 2449 + G * 4809 + B * 934)> 13
Gray = (R * 4898 + G * 9618 + B * 1868)> 14
Gray = (R * 9797 + G * 19235 + B * 3736)> 15
Gray = (R * 19595 + G * 38469 + B * 7472)> 16
Gray = (R * 39190 + G * 76939 + B * 14943)> 17
Gray = (R * 78381 + G * 153878 + B * 29885)> 18
Gray = (R * 156762 + G * 307757 + B * 59769)> 19
Gray = (R * 313524 + G * 615514 + B * 119538)> 20

Observe the table above, and the precision is actually the same: 3 and 4, 7 and 8, 10 and 11, 13 and 14, 19 and 20
Therefore, the best formula for 16-bit computation is to use a 7-bit precision, which is higher than the precision of the previous zooming coefficient by 100 times and faster:

Gray = (R * 38 + G * 75 + B * 15)> 7

In fact, the most interesting thing is the 2-bit precision, which can be fully optimized by shift:

Gray = (R + (Word) G <1 + B)> 2

======================================

In the computer, the most RGB color space is used, which corresponds to three colors: Red, green, and blue. A variety of colors are formed by the ratio of three components. Generally, the three colors can be stored in 1, 2, 4, 8, 16, 24, and 32 bits. However, the maximum value of a component is 8 bits, and the maximum value is 255, for 32-bit color, the 8-bit height is used to indicate the brightness. A color chart is generally a 16-bit or more graph. A special feature of grayscale is that the three components of a color are equal, while a gray scale is generally less than 8 digits.

In the color TV system, a color space called YUV is usually used, where Y indicates the Brightness Signal. That is, the YUV space solves the compatibility problem between the color TV set and the black and white TV set.

For human eyes, the brightness signal is the most sensitive. If you convert a color image to a gray image, you only need to convert and save the Brightness Signal.

The y conversion formula from RGB to YUV space is as follows:

Y = 0.299r + 0.587G + 0.114b

In Windows, a 16-bit or higher graph is a little different from the following graph. A 16-bit or lower graph uses a color palette to select a specific color, each cell in the palette contains four bytes, one of which is transparency. The specific pixel values store indexes, which are 1, 2, 4, and 8 bits. A 16-bit or more image uses pixels to represent the color.

========================================================== ==========
How can we convert a color image to a grayscale image?

There is a palette in a grayscale image. First, you must determine the specific color values of the palette. As we mentioned earlier, the three components of the grayscale image are equal.

When converted to 8 bits, the color palette contains 256 colors, each of which ranges from 0 to 255, and the three components are equal.

When it is converted to 4 bits, the color palette contains 16 colors, and 255 color values are evenly divided at an equal interval. All three components are equal.

When the color is converted to 2 bits, the four colors in the color palette are evenly divided into 255 colors at an equal interval, and the three components are equal.

When the color is converted to one digit, the two colors in the color palette are 0 and 255, indicating black and white.

When the color is converted to a gray scale, the corresponding value is calculated according to the formula. The value is actually the brightness level. The brightness ranges from 0 to 255. Because different bits have different brightness levels, the specific values of Y are as follows:

Y = y/(1 <(8-number of digits converted ));


The last note is that the method for storing y values is different. The corresponding digits are used to store the corresponding y values.

//----------------------------------------------------------
// Rgb565 to 8-bit grayscale
//----------------------------------------------------------
Tuint8 gm_red, gm_green, gm_blue;
Tint16 * des_ptr;
Tint16 * PT;
PT = (tint16 *) P8; // rgb565 stream

For (tint J = 0; j {
For (tint I = W; I> 0; I --)
{
Gm_red = (* (tint16 *) pt) & 0xf800)> 8;
Gm_green = (* (tint16 *) pt) & 0x07e0)> 3;
Gm_blue = (* (tint16 *) pt) & 0x001f) <3;
P [0] = (tuint8) (gm_red * 77 + gm_green * 150 + gm_blue * 29 + 128)/256 );
P ++;
PT ++;
}
}
P = QT; // grayscale pointer

 

One interview question

Write a function to convert the color value of a 32-bit RGB pixel to grayscale. The formula for converting RGB to grayscale is: grey =. 03 * red + 0.59 * green + 0.11 * blue; RGB pixel format (highest bits on the left and lowest bits on the right): rrrrrrrrggggggggbbbbbbbb.

Unsigned int togrey (unsigned int RGB) {please fill in} answer

Unsigned int togrey (unsigned int RGB)
{
Unsigned int Blue = (RGB & 0x000000ff)> 0;
Unsigned int Green = (RGB & 0x0000ff00)> 8;
Unsigned int Red = (RGB & 0x00ff0000)> 16;
Printf ("\ nred = % d, Green = % d, Blue = % d \ n", red, green, blue );
Return (red * 38 + green * 75 + blue * 15)> 7;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.