Summary of "turn" image grayscale method and its VC implementation

Source: Internet
Author: User
Tags scale image

Reprinted from: http://blog.csdn.net/likezhaobin/article/details/6915754

Recently, the author began to design the positioning system of moving target recognition, this article and several subsequent articles are from an image processing beginner's point of view to summarize the target detection and positioning process of the various common algorithms used in the In particular, solving the problems caused by carelessness or the lack of solid basic skills of C programming in the process of algorithm implementation. This paper summarizes the method and implementation process of color image grayscale, and finally gives the C Code of implementation.

In the video stream target recognition and tracking, usually the first step is to grayscale the collected color image, this is because of the small black and white photo data, compared to color photo easier to implement real-time algorithm, on the other hand, black and white photos are caused by the processing of the light of the photo, so from the image processing point of view, The information covered by this image without special filtering is more valuable.

At present, in the image processing process, the most commonly used color picture format has RGB,HSV, YUV and HLS three kinds. The following is a grayscale implementation of the color images in these three formats respectively.

1. RGB Space Image

A color map defined in RGB space whose color is determined by a combination of R, G, and b three components per pixel. The number of bits that each component occupies in memory determines the depth of the image, which is the number of bytes per pixel. In a common 24-depth color RGB graph, the three components each account for 1 bytes, so that each component can have a value of 0~255, so that a pixel can have a range of more than 16 million (255*255*255) color variations. For such a color map, the corresponding grayscale image is only 8 bits of the depth of the picture (it is considered to be an RGB three components equal), which also shows that the gray-scale image processing requires less computational. However, it is important to note that although some color grades have been lost, the description of the grayscale image is consistent with the description of the color map in terms of the overall and local color of the whole image and the distribution of luminance levels.
For the RGB image grayscale, 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

Of these three methods, the first is the component method, that is, a component of the RGB three component as the gray value of the point; the second method is the maximum value method, which uses the maximum of three component luminance in the color image as the gray value of the gray scale. In the third method, the three-component luminance of color image is averaged to obtain a gray scale, and the second is the weighted average method, the fourth one is the gray weights used in the open library of OpenCV, and the fifth is a kind of weight from the human physiology angle (the human eye is sensitive to green and the blue is the lowest).

2. Grayscale of other color spaces

About the color map of HSV and HLS color space, you can refer to the Web page "HSL and HSV color space", the method described in this page can convert several different color expressions, convert them to RGB space, and then use the above formula for grayscale.

Regarding the color image of YUV space, the physical meaning of its Y component is the luminance of the pixel, which reflects the luminance level, so the corresponding brightness Y and R, G and b three color components can be established according to the variation relationship between RGB and YUV color space: y=0.3r+0.59g+0.11b, The grayscale value of the image is expressed with this luminance value .

3. Code implementation

The purpose of this paper is to summarize the whole realization principle and idea, so the following basic function based on OPENCV to realize these kinds of changes, as for bitmap and other forms of image, after acquiring the original image data, the processing method is the same, only need to pay attention to the position of the pointer operation. The specific code is as follows:

  1. Iplimage *colorimage; //define the corresponding image pointer
  2. Iplimage *grayimage1; //The result of different weights represented in 5
  3. Iplimage *grayimage2;
  4. Iplimage *grayimage3;
  5. Iplimage *grayimage4;
  6. Iplimage *grayimage5;
  7. Iplimage *grayimage6;
  8. Iplimage *grayimage7;
  9. Colorimage = Cvloadimage ( "49138.jpg",-1); //Read pictures
  10. if (colorimage = = NULL)
  11. return;
  12. GrayImage1 = Cvcreateimage (Cvgetsize (colorimage), 8, 1);
  13. GrayImage2 = Cvcreateimage (Cvgetsize (colorimage), 8, 1);
  14. GrayImage3 = Cvcreateimage (Cvgetsize (colorimage), 8, 1);
  15. GrayImage4 = Cvcreateimage (Cvgetsize (colorimage), 8, 1);
  16. GrayImage5 = Cvcreateimage (Cvgetsize (colorimage), 8, 1);
  17. GrayImage6 = Cvcreateimage (Cvgetsize (colorimage), 8, 1);
  18. GrayImage7 = Cvcreateimage (Cvgetsize (colorimage), 8, 1);
  19. cvmat* PGRAYMAT1 = NULL; //define data pointers associated with the image
  20. cvmat* pGrayMat2 = NULL;
  21. cvmat* PGRAYMAT3 = NULL;
  22. cvmat* pGrayMat4 = NULL;
  23. cvmat* pGrayMat5 = NULL;
  24. cvmat* pGrayMat6 = NULL;
  25. cvmat* pGrayMat7 = NULL;
  26. PGRAYMAT1 = Cvcreatemat (Colorimage->height, Colorimage->width, CV_32FC1);
  27. PGRAYMAT2 = Cvcreatemat (Colorimage->height, Colorimage->width, CV_32FC1);
  28. PGRAYMAT3 = Cvcreatemat (Colorimage->height, Colorimage->width, CV_32FC1);
  29. PGRAYMAT4 = Cvcreatemat (Colorimage->height, Colorimage->width, CV_32FC1);
  30. PGRAYMAT5 = Cvcreatemat (Colorimage->height, Colorimage->width, CV_32FC1);
  31. PGrayMat6 = Cvcreatemat (Colorimage->height, Colorimage->width, CV_32FC1);
  32. PGRAYMAT7 = Cvcreatemat (Colorimage->height, Colorimage->width, CV_32FC1);
  33. BYTE data1; //Intermediate process variables
  34. BYTE data2;
  35. BYTE data3;
  36. BYTE data4;
  37. BYTE Data5;
  38. BYTE data6;
  39. BYTE Data7;
  40. for (int j=0; j<colorimage->height; j + +)
  41. {
  42. For (int i=0; i<colorimage->width; i++)
  43. {
  44. Data1 = (BYTE) Colorimage->imagedata[j*colorimage->widthstep + i*3]; //b Component
  45. Data2 = (BYTE) Colorimage->imagedata[j*colorimage->widthstep + i*3 + 1]; //g Component
  46. Data3 = (BYTE) Colorimage->imagedata[j*colorimage->widthstep + i*3 + 2]; //r Component
  47. DATA4 = Max (data1, Max (data2, data3)); //Maximum value
  48. Data5 = (BYTE) ((data1 + data2 + data3)/3);
  49. DATA6 = (BYTE) (0.072169*data1 + 0.715160*data2 + 0.212671*data3);
  50. Data7 = (BYTE) (0.11*data1 + 0.59*data2 + 0.30*data3);
  51. Cvmset (PGRAYMAT1, J, I, data1);
  52. Cvmset (PGrayMat2, J, I, data2);
  53. Cvmset (PGRAYMAT3, J, I, data3);
  54. Cvmset (PGRAYMAT4, J, I, DATA4);
  55. Cvmset (PGRAYMAT5, J, I, DATA5);
  56. Cvmset (PGrayMat6, J, I, DATA6);
  57. Cvmset (PGRAYMAT7, J, I, DATA6);
  58. }
  59. }
  60. Cvconvert (PGRAYMAT1, GrayImage1);
  61. Cvconvert (PGRAYMAT2, GrayImage2);
  62. Cvconvert (PGRAYMAT3, GrayImage3);
  63. Cvconvert (PGRAYMAT4, GrayImage4);
  64. Cvconvert (PGRAYMAT5, GrayImage5);
  65. Cvconvert (PGrayMat6, GrayImage6);
  66. Cvconvert (PGRAYMAT7, GrayImage7);
  67. Cvnamedwindow ( "Colorimage", cv_window_autosize);
  68. Cvnamedwindow ( "GrayImage1", cv_window_autosize);
  69. Cvnamedwindow ( "GrayImage2", cv_window_autosize);
  70. Cvnamedwindow ( "GrayImage3", cv_window_autosize);
  71. Cvnamedwindow ( "GrayImage4", cv_window_autosize);
  72. Cvnamedwindow ( "GrayImage5", cv_window_autosize);
  73. Cvnamedwindow ( "GrayImage6", cv_window_autosize);
  74. Cvnamedwindow ( "GrayImage7", cv_window_autosize);
  75. Cvshowimage ("Colorimage", colorimage);
  76. Cvshowimage ("GrayImage1", GrayImage1);
  77. Cvshowimage ("GrayImage2", GrayImage2);
  78. Cvshowimage ("GrayImage3", GrayImage3);
  79. Cvshowimage ("GrayImage4", GrayImage4);
  80. Cvshowimage ("GrayImage5", GrayImage5);
  81. Cvshowimage ("GrayImage6", GrayImage6);
  82. Cvshowimage ("GrayImage7", GrayImage7);
  83. Cvwaitkey (0);
  84. Cvdestroywindow ("Colorimage");
  85. Cvdestroywindow ("GrayImage1");
  86. Cvdestroywindow ("GrayImage2");
  87. Cvdestroywindow ("GrayImage3");
  88. Cvdestroywindow ("GrayImage4");
  89. Cvdestroywindow ("GrayImage5");
  90. Cvdestroywindow ("GrayImage6");
  91. Cvdestroywindow ("GrayImage7");
  92. Cvreleaseimage (&colorimage);
  93. Cvreleaseimage (&GRAYIMAGE1);
  94. Cvreleaseimage (&grayimage2);
  95. Cvreleaseimage (&grayimage3);
  96. Cvreleaseimage (&GRAYIMAGE4);
  97. Cvreleaseimage (&GRAYIMAGE5);
  98. Cvreleaseimage (&grayimage6);
  99. Cvreleaseimage (&grayimage7);
  100. Cvreleasemat (&PGRAYMAT1);
  101. Cvreleasemat (&PGRAYMAT2);
  102. Cvreleasemat (&PGRAYMAT3);
  103. Cvreleasemat (&PGRAYMAT4);
  104. Cvreleasemat (&PGRAYMAT5);
  105. Cvreleasemat (&PGRAYMAT6);
  106. Cvreleasemat (&PGRAYMAT7);


The comparison of different methods to achieve grayscale is as follows;


Figure 1 Raw images to be processed-out of the vulgar cat


Figure 2 Component Grayscale-B component


Figure 3 Component Grayscale-G component


Figure 4 Component Grayscale-R component


Figure 5 Grayscale of the maximum value


Figure 6 mean Grayscale


Fig. 7 Gray weight coefficient of OpenCV


Fig. 8 Gray value of network common weights

4. Thinking

In the preceding code implementation, the last method, for example, uses the following code:

Gray= (0.11* Blue + 0.59* Green + 0.30* Red);

The actual computer processing, this method is very fast, but in fact there is room to optimize. The above code is based on floating-point arithmetic. In image processing, the speed is life, real-time is often a very important indicator, which requires us to implement the algorithm must take into account the efficiency of the Code. So there is a principle: in the image processing, can not use floating point operation, it is best not used!

Therefore, the above code can be optimized for the equivalent:

Gray = (* Red + *green + one * Blue)/100;

Such a change can effectively avoid floating point arithmetic, so it can improve the efficiency of the Code.

This line of code can also continue to be improved as follows:

Gray= Hibyte (* Red + 151 * Green + * Blue);

of which 77,151, 28 are divided by 256 respectively, which is three coefficients above.

Similarly, it can be implemented as:

gray= (* Red + 151 * Green + * Blue) SHR 8;

This method realizes the shift operation, avoids the division and improves the efficiency.

For specific code efficiency issues, you can refer to the blog post "from the grayscale view of the basic image processing."

Summary of "turn" image grayscale method and its VC implementation

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.