6649429
Method One:
For color turn grayscale, there is a well-known psychological formula:
Gray = r*0.299 + g*0.587 + b*0.114
Method Two:
In practical applications, it is desirable to avoid low-speed floating-point operations, so integer arithmetic is required.
Note that the coefficients are all 3-bit accurate, and we can scale them up to 1000 times times to implement an 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 it needs to be rounded with the 500来 implementation.
Because the algorithm requires 32-bit arithmetic, another variant of the formula is popular:
Gray = (r*30 + g*59 + b*11 + 50)/100
Method Three:
The integer algorithm above 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.
Accustomed to using 16-bit precision, 2 of the 16 power is 65536, so this calculation factor:
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'm using is not rounded. Rounding will have a greater error, the previous calculation should be calculated in conjunction with the error, rounding is the method of the tail:
The written expression is:
Gray = (r*19595 + g*38469 + b*7472) >> 16
Coefficients of 2 to 20-bit accuracy:
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
Look closely at the table above, the accuracy is actually the same: 3 and 4, 7 and 8, 10 and 11, 13 and 14, 19 and 20
So the best formula for a 16-bit operation is to use 7-bit precision, which is 100 times times more accurate than the previous one, and is fast:
Gray = (r*38 + g*75 + b*15) >> 7
In fact, the most interesting thing is that the 2-bit precision, completely can be shifted optimization:
Gray = (R + (WORD) g<<1 + B) >> 2
The other is the formula in Adobe Photoshop
Adobe RGB (1998) [gamma=2.20]
Gray = (r^2.2 * 0.2973 + g^2.2 * 0.6274 + b^2.2 * 0.0753) ^ (1/2.2)
This method runs slightly slower, but works well.
And there's the average method.
GRAY = (red+blue+green)/3
(Gray,gray,gray) Replacement (Red,green,blue)
Several algorithms of RGB to gray scale graph