Grayscale of RGB images (method + code)

Source: Internet
Author: User

To grayscale the RGB image, the popular point is that the RGB three components of the image weighted average to get the final gray value. The most common weighting methods are as follows:

1) gray=b;gray=g;gray=r

2) Gray=max (B+G+R)

3) gray= (B+G+R)/3

4) gray= 0.072169b+ 0.715160g+ 0.212671R

5) gray= 0.11b+ 0.59g+ 0.3R

The first is the component method , which uses a component of the RGB three component as the gray value of the point

The second method is the maximum value method , the maximum value of the three-component luminance in the color image as the grayscale value

The third method averages The three-component luminance of the color image to obtain a grayscale image;

The fourth type is the gray weights used in the OpenCV Open Library.

The fifth is a weight from the human physiology Point of view (the human eye is the most sensitive to green, the lowest sensitivity to blue)

1 Mat Rgb2grey (Mat src)2 {3     floatR, G, B;4 5     intNR = src.rows;//number of original image lines6     intNC = Src.cols;//number of original image columns7 8Mat DST (Src.size (), CV_8UC1);//create a eight-bit single-channel mat with the same size as the image9 Ten      for(inty =0; Y < nr; y++)//traverse the original image One     { Auchar* data = dst.ptr<uchar> (y);//unsigned character pointer, used to point to every pixel of a grayscale image -  -          for(intx =0; X < NC; X + +) the         { -             //access three channels per pixel of the RGB image: 0, 1, 2 for B, G, R, respectively . -B = src.at<vec3b> (y, x) [0]; -G = src.at<vec3b> (y, x) [1]; +R = src.at<vec3b> (y, x) [2]; -  +DATA[X] = (int) (R *0.3+ G *0.59+ B *0.11);//weighted average method, BGR three columns into one column A         } at     } -     returnDST; -}

Grayscale of RGB images (method + code)

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.