[EmguCV | C #] using CvInvoke draw their own color histogram - Histogram (Hitsogram) Series (4)

Chinese New Year is over, although still a student so in fact there are two weeks of vacation, but in order not to tears, taking advantage of the holidays more use to enrich themselves, so start early return to the state, and this has been finally put to say it-yourself draw guess the color histogram of articles written out.

In it before [EmguCV | WinForm] EmguCV built using the Histogram tool to draw a histogram (Histogram) - histogram (Histogram) Series (1) chapter, can help us through EmguCV presenting a built-in HistogramBox and HistogramViewer histogram information of the image

, but sometimes, we still hope that can be directly used to render the color of the color distribution of an image, but this practice is coming to this part.


Foreword


Chinese New Year is over, although still a student so in fact there are two weeks of vacation, but in order not to tears, taking advantage of the holidays more use to enrich themselves, so start early return to the state, and this has been finally put to say it-yourself draw guess the color histogram of articles written out.

In it before [EmguCV | WinForm] EmguCV built using the Histogram tool to draw a histogram (Histogram) - histogram (Histogram) Series (1) chapter, can help us through EmguCV presenting a built-in HistogramBox and HistogramViewer histogram of the image information

But sometimes, we still hope that can be directly used to render the color of the color distribution of an image, but this practice is coming to this part.

PS: it is recommended to read [EmguCV | C #] using EmguCV of DenseHistogram class computing and record the image histogram - histogram (Histogram) Series (2)

Square values ​​plotted in FIG.


1. The present results plotted

This article will teach how to manually draw the value of the finished image Quadrant follows:

hsv_h_histogram

The picture shows the view of the color distribution of hue hue, because I put Bin value is set to 16, so the color change interval after quantization is relatively simple - as to what Bin can refer to the previous article [EmguCV | C #] using EmguCV of DenseHistogram class computing and record the image histogram - histogram (histogram) series (2)

However, because the two-dimensional information can be rendered relatively small, so when drawing such a histogram of the image, I am using the HSV color space, because the HSV color space can be separated out hues, see pure color basic color (visible light), while the original RGB color space, because the color is mixed, it is difficult to show (following schematic RGB color space)

RGB_color_solid_cube

In addition Benpian draw will match [EmguCV | C #] Calculated using EmguCV of CvInvoke Square (Histogram) - histogram (Histogram) series (3) article, using CvInvoke to calculate the value of the image information to the Square DenseHistogarm, then pick Next we look at how to draw it

2. Draw way

When we do draw the value of the Quadrant, in fact, it is the computed data DenseHistogarm pick out one by one to obtain good information about each Bin calculated, and draw it into an Image Image patterns, so when the final presentation is in fact presenting a picture.

Let's look at this side draw Hue Hue histogram:

1. initialization parameters

Here we need to care about is MAX_VALUE, this variable is used to store the maximum value of the cumulative histogram block found, for example, side view of the chalk value image, the left block of red color is accumulated up to , assuming he is 300, is shown in this figure, there are 300 falls on the red block color.

And then this value can be used as is drawn to the FIGS., For calculating the color coordinates of the display position of the blocks on the image; h_bins is achieved when we calculate the color previously set interval setting block .

float max_value = 0.0f;
int[] a1 = new int[100];
int[] b1 = new int[100];
float ax = 0;
int h_bins = histDense.BinDimension[0].Size;

2. Obtain a block histogram maximum and minimum cumulative value (Bin)

CvInvoke.cvGetMinMaxHistValue(histDense, ref ax, ref max_value, a1, b1);

3. Draw a setting image histogram data to make the width and height, and initializes

//设定值方图图像显示的宽高
int height = 240;
int width = 800;
IntPtr hist_img = CvInvoke.cvCreateImage(new System.Drawing.Size(width, height), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 3);
CvInvoke.cvZero(hist_img);

4. Calculate the image displayed on the bin width

This is to allow the number of Bin we originally set can be displayed on a reasonable width of the image we want to set, for example, here we want to show to the 800 * 240 images, because wide is 800, in order to be let me set the 50 reasonable bins full display, so to be calculated

int bin_w = width / (h_bins); //h_bin设定为50

5. sequentially obtain each data value Bin block side in FIG danger and calculating the position of the image and the color to

Bin sequentially searching each block, and retrieve the value of bin (the accumulated value of the color blocks), and calculate if the display image 240 on a high, and a position to display color to be displayed

for (int h = 0; h < h_bins; h++)
{

       / 取得直方图的统计数据,计算值方图中的所有颜色中最高统计的数值,作为实际显示时的图像高 */
      //取得值方图的数值位置,以便之后存成文件
      double bin_val = CvInvoke.cvQueryHistValue_1D(histDense, h);

      //计算取得的bin值要显示在240高的图像上时的位置
      int intensity = (int)System.Math.Round(bin_val * height / max_value);

      / 取得现在抓取的直方图的hue颜色,并为了显示成图像可观看,转换成RGB色彩 */
      CvInvoke.cvRectangle(hist_img, new System.Drawing.Point(h * bin_w, height),
                       new System.Drawing.Point((h + 1) * bin_w, height - intensity),
                       HueToBgr(h * 180.0d / h_bins), -1, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, 0);

}

Wherein, HueToBgr method is as follows:

It used to convert the RGB color value into hue

/// 
       /// hue色相转换成rgb color
       /// 
       /// 
       /// 
  
  
       private static MCvScalar HueToBgr(double hue)
       {
           int[] rgb = new int[3];
           int p, sector;
           int[,] sector_data = { { 0, 2, 1 }, { 1, 2, 0 }, { 1, 0, 2 }, { 2, 0, 1 }, { 2, 1, 0 }, { 0, 1, 2 } };
           hue *= 0.033333333333333333333333333333333f;
           sector = (int)Math.Floor(hue);
           p = (int)Math.Round(255 * (hue - sector));
           //p ^= sector & 1 ? 255 : 0;
           if ((sector & 1) == 1) p ^= 255;
           else p ^= 0;
           rgb[sector_data[sector, 0]] = 255;
           rgb[sector_data[sector, 1]] = 0;
           rgb[sector_data[sector, 2]] = p;
           MCvScalar scalar = new MCvScalar(rgb[2], rgb[1], rgb[0], 0);
           return scalar;
       }

6. The resulting image is converted into a value Square use image EmguCV Patterns

We originally used in the calculation of the deposit type is Intptr, refer to [EmguCV | OpenCV | C #] conversion support access OpenCV Iplmage IntPtr type of content type EmguCV save the image of the written

The following is one way:

Image
  
  
   
    hist_emgu_img = new Image
   
   
    
    (new System.Drawing.Size(width, height));
CvInvoke.cvCopy(hist_img, hist_emgu_img.Ptr, IntPtr.Zero);
return hist_emgu_img;
   
   
  
  

3. Draw a histogram HS

We come here with a quick look at how to draw a histogram HS, as to why the need to use S (saturation) it?

We will likely want to increase the analysis time can have brightness, image stability through the identification of saturation, but if the points are too thin, because the degree of detail increases too far, but will only make identification of decline.

1. initialization parameters

Here more s_bin, because we will need to see changes in saturation

float max_value = 0.0f;
int[] a1 = new int[100];
int[] b1 = new int[100];
float ax = 0;
int h_bins = histDense.BinDimension[0].Size;
int s_bins = histDense.BinDimension[1].Size;

2. Obtain a block histogram maximum and minimum cumulative value (Bin)

CvInvoke.cvGetMinMaxHistValue(histDense, ref ax, ref max_value, a1, b1);

3. Draw a setting image histogram data to make the width and height, and initializes

Because in addition to show Bin hue, we also need to show saturation, it may be more than the original width, if more than just a calculation in out wide as the size of the displayed image

//设定值方图图像显示的宽高
int height = 300;
int width;
//如果设定的bins超过窗口设定的显示范围,另外给予可以符合用额外的弹出窗口显示的值,因为要同时看到h与s的bin值,显示的图像宽可能会太宽
if (h_bins * s_bins > 800)
{
       width = h_bins * s_bins * 2;
}
else
{
      width = 800;
}

IntPtr hist_img = CvInvoke.cvCreateImage(new System.Drawing.Size(width, height), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 3);
CvInvoke.cvZero(hist_img);

4. Initialize the data used to store the color value of the Square, is converted into the RGB values ​​to be displayed out of the space

Because our original histogram is Hsv space, but when we want to draw the image point of view, RGB space as the recorded (Image Type), in order to be able to do the right show, so it is necessary to do a color space conversion after us

//用来存放从Hsv转回RGB图像时用的空间
IntPtr hsv_color = CvInvoke.cvCreateImage(new System.Drawing.Size(1, 1), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 3);
IntPtr rgb_color = CvInvoke.cvCreateImage(new System.Drawing.Size(1, 1), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 3);

5. Calculate the image displayed on the bin width

int bin_w = width / (h_bins * s_bins);

6. Bin sequentially made for each block in the data value and calculating Square danger is to position the image and the color

for (int h = 0; h < h_bins; h++)
{
       for (int s = 0; s < s_bins; s++)
      {
            int i = h * s_bins + s;

           / 取得直方图的统计数据,计算值方图中的所有颜色中最高统计的数值,作为实际显示时的图像高 */
            //取得值方图的数值位置,以便之后存成文件
           double bin_val = CvInvoke.cvQueryHistValue_2D(histDense, h, s);
           int intensity = (int)System.Math.Round(bin_val * height / max_value);

            / 取得现在抓取的直方图的hue颜色,并为了显示成图像可观看,转换成RGB色彩 */
            CvInvoke.cvSet2D(hsv_color, 0, 0, new Emgu.CV.Structure.MCvScalar(h * 180.0f / h_bins, s * 255.0f / s_bins, 255, 0)); //这边用来计算色相与饱和度的统计数据转换到图像上 hsv_color的数值
            CvInvoke.cvCvtColor(hsv_color, rgb_color, COLOR_CONVERSION.CV_HSV2BGR);   //在把hsv颜色空间转换为RGB
            Emgu.CV.Structure.MCvScalar color = CvInvoke.cvGet2D(rgb_color, 0, 0);
            CvInvoke.cvRectangle(hist_img, new System.Drawing.Point(i * bin_w, height), new System.Drawing.Point((i + 1) * bin_w, height - intensity), color, -1, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, 0);
      }
}

7. The obtained value is converted to an image side in FIG using image EmguCV Patterns

We originally used in the calculation of the deposit type is Intptr, refer to [EmguCV | OpenCV | C #] conversion support access OpenCV Iplmage IntPtr type of content type EmguCV save the image of the written

The following is one way:

Image
  
  
   
    hist_emgu_img = new Image
   
   
    
    (new System.Drawing.Size(width, height));
CvInvoke.cvCopy(hist_img, hist_emgu_img.Ptr, IntPtr.Zero);
return hist_emgu_img;

   
   
  
  

show result

hsv_hs_histogram

Experience


I hope this can help people who want to present colors for a help =)

Also give yourself a record

In this example program files

Reference data:

Color histogram calculation, display, processing, contrast and back projection (How to Use Histogram? Calculate, Show, Process, Compare and BackProject)


The article describes the concept, if not correct the wrong section, welcome to inform correct me thank you =)

Also to be reproduced, please attach the source thanks

Original: Large column  [EmguCV | C #] using CvInvoke draw their own color histogram - Histogram (Hitsogram) Series (4)


Guess you like

Origin www.cnblogs.com/petewell/p/11457738.html
Recommended