Convert from RGB color to gray color algorithm

Source: Internet
Author: User
Tags integer division

First, the basis
For color to grayscale, there is a well-known psychological formula:

Gray = r*0.299 + g*0.587 + b*0.114

Second, integer algorithm

In practical application, we want to avoid low speed floating-point operation, so we need integer algorithm.
Note that the coefficients are all 3-bit precision, we can scale them 1000 times times to implement the integer arithmetic:

Gray = (r*299 + g*587 + b*114 + 500)/1000

RGB is generally 8-bit precision, now scaling 1000 times times, so the above operation is a 32-bit integer operation. Note that the next Division is integer division, so you need to add 500来 to achieve rounding.
Because the algorithm requires 32-bit operations, another variant of the formula is popular:

Gray = (r*30 + g*59 + b*11 + 50)/100

However, although the last formula is a 32-bit integer operation, but according to the 80x86 system of integer multiplication and division of the characteristics of the instructions, it can be used in the 16-bit integer multiplication and division instructions. And now the 32 are popular (AMD64 are out), so it is recommended to use the previous formula.

Three, Integer shift algorithm

The above integer algorithm is fast, but one thing still restricts speed, which is the last division. The shift is much faster than division, so you can scale the coefficients to an integer power of 2.
It is customary to use 16-bit precision, 2 of the 16 power is 65536, so calculate the coefficient:

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 have seen it, and the rounding method I used was not rounded. Rounding will have a larger error, should be calculated with the error of the previous calculation, rounding method is the tail:

Write an expression is:

Gray = (r*19595 + g*38469 + b*7472) >> 16

2 to 20-digit precision coefficients:

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

Looking closely at the table above, these precision are actually the same: 3 and 4, 7 and 8, 10 and 11, 13 and 14, 19 and 20
So the best formula for 16-bit computing is to use 7-bit precision, which is 100 times times faster than the previous one, and is fast:

Gray = (r*38 + g*75 + b*15) >> 7

In fact, the most interesting thing is the 2-bit precision, which can be completely shifted to optimize:

Gray = (R + (WORD) g<<1 + B) >> 2

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

In the computer to use the most RGB color space, corresponding to red, green, blue three colors, by leveling the proportion of three components to form a variety of colors. Generally can use 1, 2, 4, 8, 16, 24, 32 bits to store these three colors, but now a component is the largest with 8 digits, the maximum is 255, for 32-bit color, high 8 is used to indicate the degree of visibility. A color chart generally refers to more than 16 figures. One of the special parts of a grayscale image is that the three components of the color are equal, while the general grayscale is below 8 digits.

In color TV systems, a color space called YUV is usually used, where Y represents the brightness signal; This YUV space solves the compatibility problem of color TV and Black-and-white TV.

For the human eye, the brightness signal is the most sensitive, if the color image conversion to grayscale images, only need to convert to save the brightness signal.

The Y conversion formula from RGB to YUV space is:

Y = 0.299r+0.587g+0.114b

In WINDOWS, a diagram representing more than 16 digits is somewhat different from the following diagram; The 16-bit figure uses a palette to indicate the selection of a specific color, each cell of the palette is 4 bytes, one of which is transparent, while the specific pixel value stores the index, 1, 2, 4, 8 bits respectively. More than 16 figures directly use pixels to represent colors.

=================================================
So how do you convert a color graph to a grayscale image?

There is a color palette in the grayscale chart, first you need to determine the color palette of the specific values. As we mentioned earlier, the three components of a grayscale chart are equal.

When converted to 8-bit, the palette has 256 colors, each exactly from 0 to 255, and three components equal.

When converted to 4-bit, the color palette of 16 colors, equal spacing evenly divided into 255 color values, three components are the same.

When converted to 2-bit, the color palette of 4 colors, evenly spaced 255 colors, three components equal.

When converted to 1-bit, two colors in the palette, 0 and 255, represent black and white.

When the color is converted to grayscale, the corresponding value is calculated according to the formula, which is actually the level of brightness, brightness from 0 to 255, and because different bits have different brightness levels, the specific values of Y are as follows:

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


Finally, it is important to note that the Y-value is stored in a different way, with the corresponding number of digits to store the corresponding Y-value.

//----------------------------------------------------------
RGB565 Turn 8-bit grayscale
//----------------------------------------------------------
TUint8 Gm_red,gm_green,gm_blue;
TInt16 *des_ptr;
TInt16 *pt;
PT = (TInt16 *) P8; RGB565 Flow

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; Gray Chart pointer

A question of a face

Write a function to convert a 32-bit RGB pixel color value to grayscale, RGB to grayscale formula: Grey=.03*red+0.59*green+0.11*blue;rgb pixel format (left-highest, Right lowest bit): 00000000RRRRRRRRGGGGGGGGBBBBBBBB. unsigned int togrey (unsigned int rgb) {Please fill in} answer

Unsigned int togrey (unsigned int rgb)
{
 unsigned int blue = (RGB & 0x000000ff) >>0;
 un Signed 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 +  Blue) >>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.