RGB colors to the gray Algorithm

Original: http: //www.cnblogs.com/carekee/articles/3629964.html

First, the basis 
  for the color to gray scale, there is a very well-known psychology formula:

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

Second, the integer arithmetic

  The practice, want to avoid the low-speed floating-point, integer arithmetic is required.
  Notes that all three precision coefficient is not, we can scale them 1000 times the integer arithmetic algorithms to achieve:

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

RGB is an 8-bit precision is generally now scaled 1000, so the above calculation are 32-bit integer arithmetic. Note that behind the division is integer division, so the need to add 500 to achieve rounding.
  This is because the algorithm needs 32-bit computing, so another variant of this formula is very popular:

                          Gray = (R*30 + G*59 + B*11 + 50) / 100

  However, although the formula is a 32-bit integer arithmetic, but according to the characteristics of 80x86 Integer multiply and divide instructions system, can be a 16-bit integer arithmetic multiplication and division instructions. And now the popularity of the early 32 (AMD64 are out), it is recommended to use a formula.

Third, the integer arithmetic shift

  The above integer arithmetic has been very fast, but one thing is still restricted speed, is the last of that division. Shifting much faster than division, the coefficients can be scaled to an integer power of two.
  Using the customary 16-bit precision, 2 to the power 16 is 65536, so this factor is calculated:

                          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 saw it, rounding the way I used is not rounded. Rounding errors have a greater, should the computing error calculation result with the previous in, the end rounding process is to:

  Written expression is:

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

  2-20 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

  Carefully observed form above, these are in fact the same accuracy: 3 and 4, 7 and 8, 10 and 11, 13 and 14, 19 and 20
  it is best calculated using 16-bit arithmetic precision 7, than previous scaling factor 100 times higher that the accuracy and speed:

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

  Actually, the most interesting is that two precision, can shift optimization:

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

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

In most computers use an RGB color space, corresponding to red, green and blue colors; to the composition ratio of allocation by each color component of the three. Generally may be used, 2, 4, 8, 16, 24, 32 to store the three colors, but now with a maximum component represented by eight bits, the maximum value is 255, for 32-bit color, 8 bits are high It is used to indicate degrees of transparency. More generally refers to the color map 16 of FIG. There is a unique position grayscale composed of three color components are equal; 8-bit grayscale but generally less.

In color television systems typically use something called YUV color space, where Y represents the luminance signal; that is, the YUV space to solve the black and white TV and color TV compatibility issues.

For the human eye is most sensitive to the luminance signal, if the image is converted to grayscale color image, the luminance signal is only necessary to save can be converted.

From RGB to YUV conversion formula Y space is:

Y = 0.299R + 0.587G + 0.114B

IN WINDOWS, it represents more than 16 and the following figures FIG slightly different; the following 16-bit palette to FIG represented using a particular selected color palette each cell is four bytes, wherein a transparency ; and specific pixel value is stored in the index, are 1, 2, 4, 8. Over 16 represented in FIG directly using the pixel color.

=================================================
then how to convert it to grayscale color map?

There grayscale palette, you first need to determine the specific color palette values. We mentioned earlier, the three components are equal grayscale.

When converted to 8-bit time, there are 256 palette colors, each from 0 to 255 exactly, three components are equal.

When converted to 4-bit time, the color palette 16, and other equally spaced color values ​​255, three components are equal.

When the time is converted to 2, 4 color palette, 255 colors equally intervals equal three components.

When converted into a time, two palette colors, is 0 and 255, represent black and white.

Converted to grayscale color when calculated according to the equation corresponding to the value that is actually the level of brightness; luminance from 0 to 255; bit different due to different brightness levels, the specific value Y as follows:

       Y = Y / (1 << (8- converted bits));


The last thing to note, the Y value obtained is stored in a different way; respectively corresponding to the number of bits corresponding to the stored Y value.

// ------------------------------------------------ ----------
// RGB565 turn eight grayscale
// ----------------------------- -----------------------------
   TUint8 gm_red, gm_green, gm_blue;
   TInt16 * des_ptr;
   TInt16 * Pt;
   Pt = (TInt16 *) p8; // RGB565 stream

   for(TInt j=0;j<h;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;   //灰度图指针

 

A face questions

A write function, a 32-bit RGB color values ​​into the pixel grayscale, RGB gradation turn formula: Grey = .03 * red + 0.59 * green + 0.11 * blue; RGB pixel format (the most significant bit on the left, the right lowest bit): 00000000RRRRRRRRGGGGGGGGBBBBBBBB.

unsigned int ToGrey(unsigned int rgb)

Please fill {}

 

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;
}

  Another formula is the Adobe Photoshop in  
     the Adobe the RGB (1998) [Gamma = 2.20]  
     Gray = (R & lt ^ G ^ + .2973 * 2.2 2.2 * 0.6274 * 0.0753 + B ^ 2.2) ^ (. 1 / 2.2)

The method runs slightly slower, but works well.

      There is an average value method 

      GRAY = (RED+BLUE+GREEN)/3

    (GRAY,GRAY,GRAY ) 替代 (RED,GREEN,BLUE)

 

Guess you like

Origin blog.csdn.net/qq_30263737/article/details/90711903