This article summarizes the grayscale method and implementation process of color images, and finally provides the code for implementing emgu cv.
I. grayscale principle and mathematical implementation (reproduced from: "image grayscale method summary and VC implementation". This article uses opencv for implementation. It is followed by code and can be learned well, better than what I wrote \ (^ o ^ )/~)
During image processing, the first step is to grayscale the collected color image. This is because the volume of black and white photos is small and it is easier to implement real-time algorithms than the color photos, on the other hand, black and white photos are formed by Unprocessed light. Therefore, from the perspective of image processing, the information covered by such images without special filtering is more valuable.
Currently, the most commonly used color image formats include RGB, HSV, YUV, and HLS.
1. RGB spatial images
Color Map defined in RGB space. The color of each pixel is determined by the R, G, and B components. The memory size of each component determines the image depth, that is, the number of bytes occupied by each pixel. In a common 24-Depth Color RGB diagram, each of the three components occupies one byte, so that each component can be 0 ~ 255, so that a pixel can have a color change range of more than 16 million (255*255*255. For such a color image, the gray-scale image corresponding to it is only 8-bit image depth (it can be considered as RGB with three equal components ), this also shows that the amount of computing required for grayscale image processing is indeed small. However, although some color levels are lost, we can see from the overall and local color and brightness level distribution features of the entire image, the grayscale image description is consistent with the color image description.
For the grayscale RGB image, the general point is to calculate the final grayscale value by weighted average of the three RGB components of the image. The most common weighting method is 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
Among the three methods, the first method is the component method, that is, using a component of RGB three components as the gray value of the point; the second method is the maximum value method, the maximum brightness of a three-part color image is used as the gray value of the grayscale image. The third method is to calculate the average brightness of three parts in the color image to obtain a grayscale image. The latter two are weighted average, and the fourth method is the gray weight used by the opencv Open Library, the fifth type is a kind of weight proposed from the perspective of human physiology (human eyes are most sensitive to green and blue ).
2. grayscale of other color spaces
For the grayscale color map of HSV and HLS color spaces, refer to "HSL and HSV Color Space" on the webpage. The methods described in this webpage can convert different color expressions, convert it to RGB space, and then use the above formula for Grayscale.
For a color image in the YUV space, the physical meaning of its Y component is the brightness of the pixel. This value reflects the brightness level, therefore, the correspondence between brightness Y and the three color components of R, G, and B can be established based on the relationship between RGB and YUV color spaces: Y = 0.3r + 0.59G + 0.11b, use this brightness value to express the gray value of the image..
3. Programming tips)
In code implementation, the following code is used as an example:
Gray = (0.11 * Blue + 0.59 * green + 0.30 * red );
In actual computer processing, this method is very fast, but there is still room for optimization. The above Code uses floating point operations. In image processing, speed is life, and real-time performance is often an important indicator. This requires that we must consider the code efficiency when implementing algorithms. So there is a principle: in image processing, it is best not to use floating point operations!
Therefore, the above Code can be equivalent to the following optimization:
Gray = (30 * red + 59 * green + 11 * Blue)/100;
This change can effectively avoid floating-point operations and improve code efficiency.
This line of code can be further improved as follows:
Gray = hibyte (77 * red + 151 * green + 28 * Blue );
77,151, 28 divided by 256 respectively, that is, the above three coefficients.
You can also achieve the following:
Gray = (77 * red + 151 * green + 28 * Blue) SHR 8;
This method achieves shift operations, avoiding division and improving efficiency.
For specific code efficiency issues, refer to the blog "view basic image processing by Grayscale Images".
Ii. emgu CV implementation (reproduced)
See the following two articles.
Grayscale emgucv Images
C # opencv study NOTE 4: image binarization
Steps:
1. Create a Windows form application, change the text attribute of form1 to "grayscale image tograyimage", and change the name attribute to "tograyimage"
2. Add a button to the form. Change the name attribute to "sltimagebtn", and change the text attribute to "select image ..."
3. Add an openfiledialog to the form to select an image file and set the filter attribute to "Image File | *. jpg; *. PNG; *. BMP; *. gif ;"
4. Add two pictureboxes to the form and set sizemodel to zoom.
5. The layout is shown as follows:
6. Double-click the button. The system automatically generates a click empty event.
7. Add the reference of emgucv. Here, add the three most commonly used [emgu. cv. dll, emgu. cv. UI. dll, emgu. util. dll]
8. Add a namespace
Using emgu. CV;
Using emgu. cv. cvenum;
Using emgu. cv. structure;
Using system. runtime. interopservices;
9. write code
/// <Summary> /// select an image event, convert a color image to a grayscale image /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void sltimagebtn_click (Object sender, eventargs e) {If (openfiledialog1.showdialog () = dialogresult. OK) {// intptr srcimg = cvinvoke is displayed in the source image. cvloadimage (openfiledialog1.filename, emgu. CV. cvenum. load_image_type.cv_load_image_anycolor); If (srcimg = NULL) {MessageBox. show ("no Pi C! "); Return;}/* iplimage * To bitmap */miplimage srcmi = (miplimage) Marshal. ptrtostructure (srcimg, typeof (miplimage); image <BGR, byte> srcimage = new image <BGR, byte> (srcmi. width, srcmi. height, srcmi. widthstep, srcmi. imagedata); picturebox1.image = srcimage. tobitmap (); // grayscale display intptr grayimg = cvinvoke. cvcreateimage (cvinvoke. cvgetsize (srcimg), emgu. CV. cvenum. ipl_depth.ipl_depth_8u, 1); cvinvoke. cvcvtcolor (srcimg, grayimg, emgu. CV. cvenum. color_conversion.cv_rgb2gray); miplimage graymi = (miplimage) Marshal. ptrtostructure (grayimg, typeof (miplimage); // different from the color image display function. If you continue using the above function, a memory error image <gray, byte> grayimage = new image <gray, byte> (graymi. width, graymi. height, graymi. widthstep, graymi. imagedata); picturebox2.image = grayimage. tobitmap (); cvinvoke. cvwaitkey (0); cvinvoke. cvreleaseimage (ref srcimg); cvinvoke. cvreleaseimage (ref grayimg );}}
Due to the rush of time, other gray-scale algorithms in image grayscale method summary and VC implementation do not know how to implement them in emgucv.