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)