Several different methods of color image turning grayscale

Source: Internet
Author: User

Recently in the analysis of several open source image processing programs, found several different color image to grayscale method, the method is very simple, but also very easy to understand, but the effect is obviously different. Before introducing the method of calculation, the difference between brightness (lightness) and illuminance (luminosity) should be mentioned first. Brightness (lightness), refers to the light that people feel when they see it. Illuminance (luminosity), refers to the light source on the object, the unit is irradiated area on the luminous flux. It can be said that brightness is the body reflected light to the strength of the eye, is a subjective sense of the person, and illuminance is the light source on the object of strength, is an objective physical quantity. The same light source, the same distance on white and black paper, brightness is different, illuminance is the same. The following is a simple code example:

1. Method based on the mean value:

Uchar *psrcimg = src.data;for (int i = 0; i < src.rows; i++) {for (int j = 0; J < Src.cols; J + +, psrcimg + = 3) {Double Average = (Psrcimg[0] + psrcimg[1] + psrcimg[2])/3;psrcimg[0] = (uchar) min (255, average);p srcimg[1] = (uchar) min (255, a verage);p srcimg[2] = (UCHAR) MIN (255, average);}}

2. Brightness-based method:

Uchar *psrcimg = src.data;for (int i = 0; i < src.rows; i++) {for (int j = 0; J < Src.cols; J + +, psrcimg + = 3) {Uchar Max_val = Max (psrcimg[0], psrcimg[1]); max_val = max (Max_val, psrcimg[2]); Uchar min_val = min (psrcimg[0], psrcimg[1]); min _val = min (Min_val, psrcimg[2]);d ouble lightness = (max_val + min_val)/2;psrcimg[0] = (UCHAR) MIN (255, lightness);p srcimg [1] = (uchar) min (255, lightness);p srcimg[2] = (uchar) min (255, lightness);}}

3. Illuminance-Based approach:

HDTV with Bt.709uchar *psrcimg = src.data;for (int i = 0; i < src.rows; i++) {for (int j = 0; J < Src.cols; j + +, p Srcimg + = 3) {Double luminosity = psrcimg[0] * 0.0722 + psrcimg[1] * 0.7152 + psrcimg[2] * 0.2126;psrcimg[0] = (UCHAR) MIN (2 luminosity);p srcimg[1] = (uchar) min (255, luminosity);p srcimg[2] = (UCHAR) MIN (255, luminosity);}}
SDTV with Bt.601uchar *psrcimg = src3.data;for (int i = 0; i < src.rows; i++) {for (int j = 0; J < Src.cols; J + +, Psrcimg + = 3) {Double luminosity = psrcimg[0] * 0.114 + psrcimg[1] * 0.587 + psrcimg[2] * 0.299;psrcimg[0] = (UCHAR) MIN (255 , luminosity);p srcimg[1] = (uchar) min (255, luminosity);p srcimg[2] = (UCHAR) MIN (255, luminosity);}}
Here are some of the conversion methods described above:





Several different methods of color image turning grayscale

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.