Programming Delphi Color graphics to grayscale algorithm
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/codeback.net/plugins/content/tags.php on line 149

A basic formula of color graphics to grayscale algorithm.

For color to grayscale, there is a very well-known formula:
Gray = R * 0.299 + G * 0.587 + B * 0.114 

An integer algorithms

For the daily apply, we want to avoid a complex and lower floating-point computing, it is necessary to get an integer

algorithm.
Note the accuracy of coefficient is three digit. We now scale them 1,000 times bigger to achieve an integer arithmetic

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

RGB is generally eight precision, and now zoom it to 1000 times larger, so the above operation is 32-bit integer arithmetic.

Note that the back of the division is integer division, so it need to plus 500 to get a rounded result.

Since it is need an 32-bit computing, another formula of the changed form is popular:
Gray = (R * 30 + G * 59 + B * 11 + 50) / 100

However, although the previous formula is 32-bit integer arithmetic, according to the feature of an integer multiplication

and division instructions of 80x86 system, they can be calculated by a 16-bit integer division instruction. Moreover, today

the 32-bit or even 64-bit based computing is widespread. So it is recommend to use the prior formula.

 
A shift algorithm

The above algorithm has come very fast, but one thing still can restrict the speed. That is the last of division. Shift will

be faster than division, so you can make the coefficients to times of second power.
Customary to use a 16-bit precision, 2 to power of 16 is 65536, so the factors can be calculated as:
0.299 * 65536 = 19595.264 ≈ 19595
0.587 * 65536 + (0.264) = 38469.632 + 0.264 = 38469.896 ≈ 38469
65536 + 0.114 * (0.896) = 7471.104 + 0.896 = 7472

You may note that the result is rounded. Rounding will make the inaccuracy larger, the previous error should be calculated to

fix the result, so to cut the end digit is a better way.

Here are the expressions:
Gray = (R * 19595 + G * 38469 + B * 7472) >> 16

2-20 accuracy forms:
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 at the above table carefully, the accuracy are the same. The 3 and 4 one, the 7 and 8, 10 and 11, 13 and 14, 19 and 20.
Therefore, the 16-bit computing is best to use the 7-bit precision of formula, it will be faster and more accuracy than the

previous of 100 times of zooming formula.
Gray = (R * 38 + G * 75 + B * 15) >> 7

While, the first formula can be deformed as a shift form to optimize.
Gray = (R + (WORD) G 2

Since its inaccuracy is too large. We do not use this formula to make any image processing. The most commonly used is the

16-bit precision. However, for game programming, the scene can change constantly, users generally can not see the nuances of

color, the most commonly used is the 2-bit accuracy formula.
 


Add comment


Security code
Refresh

Programming - Delphi