Color-to-grayscale algorithms for complete Learning)

Source: Internet
Author: User
Tags integer division
Complete color-to-gray algorithm Learning
Source: gameres Author: zyl910 Date: 2006/05/26
--------------------------------------------------------------------------------
 
File: studyrgb2gray.txt
Name: complete learning of color to grayscale Algorithms
Author: zyl910
Version: V1.0
Updata: 2006-5-22

  
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

This formula is not required for image processing because of the large error (the most commonly used is the 16-bit precision ). However, for game programming, scenarios often change, and users generally cannot observe the nuances of colors. Therefore, the most commonly used is the 2-bit precision.

// --------- The following is my non-original text -----------------------------

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

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.