Optimization of color-to-gray algorithm based on image processing---

Source: Internet
Author: User
Tags integer division

File:StudyRGB2Gray.txt
Name: Color-to-grayscale algorithm thorough learning
author:zyl910
version:v1.0
Updata:2006-5-22


First, the basic

For color turn grayscale, there is a well-known psychological formula:
Gray = r*0.299 + g*0.587 + b*0.114

Second, integer algorithm

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

However, although the previous formula is a 32-bit integer operation, it is possible to use a 16-bit integer multiplication instruction according to the characteristics of the integer multiplication directive of the 80x86 system. And now 32 people have been popularized (AMD64 all come out), so we recommend using the previous formula.

Three, Integer shift algorithm

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

2 to 20-bit 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*1 5) >> 7
Gray = (r*76 + g*150 + b*30) >> 8
Gray = (r*153 + g*300 + b*59) >> 9
Gray = (r*306 + g* 601 + b*117) >>
Gray = (r*612 + g*1202 + b*234) >> one
Gray = (r*1224 + g*2405 + b*467) >> 12
gray = (r*2449 + g*4809 + b*934) >>
Gray = (r*4898 + g*9618 + b*1868) >>
Gray = (r*9797 + g*19235 + b*3736) >>
Gray = (r*19595 + g*38469 + b*7472) >>
Gray = (r*39190 + g*76939 + b*14943) >>
Gray = (r*78381 + g*153878 + b*29885) >>
Gray = (r*156762 + g*307757 + b*59769) >>
Gray = ( r*313524 + g*615514 + b*119538) >>

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

Since the error is very large, do not use this formula for image processing (most commonly used is 16-bit precision). But for game programming, the scene often changes, the user is generally not able to observe the subtle differences in color, so the most commonly used is 2-bit precision.

http://blog.csdn.net/zyl910/article/details/749752

http://bbs.csdn.net/topics/280041280

Optimization of color-to-gray algorithm based on image processing---

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.