简体   繁体   English

如何使用EmguCV和C#绘制直方图

[英]How to draw histogram using EmguCV and C#

I need to draw two types of histogram, namely monodimensional and tridimensional. 我需要绘制两种类型的直方图,即单维和立体。 I'm a newbie to EMGU and all of the samples I found on the net are in C++ or C. Are there any samples using C# and Emgucv? 我是EMGU的新手,我在网上找到的所有样本都是C ++或C.有没有使用C#和Emgucv的样本?

Thanks for helping. 谢谢你的帮助。

The following code will segment the RED GREEN and BLUE Histogram data and put them in an array of floats for whatever use you want. 以下代码将分割RED GREEN和BLUE Histogram数据,并将它们放入浮动数组中,以满足您的任何需要。

float[] BlueHist;
float[] GreenHist;
float[] RedHist;

Image<Bgr, Byte> img = new Image<Bgr, byte>("ImageFileName");

DenseHistogram Histo = new DenseHistogram(255, new RangeF(0, 255));

Image<Gray, Byte> img2Blue = img[0];
Image<Gray, Byte> img2Green = img[1];
Image<Gray, Byte> img2Red = img[2];


Histo.Calculate(new Image<Gray, Byte>[] { img2Blue }, true, null);
//The data is here
//Histo.MatND.ManagedArray
BlueHist = new float[256];
Histo.MatND.ManagedArray.CopyTo(BlueHist, 0);

Histo.Clear();

Histo.Calculate(new Image<Gray, Byte>[] { img2Green }, true, null);
GreenHist = new float[256];
Histo.MatND.ManagedArray.CopyTo(GreenHist, 0);

Histo.Clear();

Histo.Calculate(new Image<Gray, Byte>[] { img2Red }, true, null);
RedHist = new float[256];
Histo.MatND.ManagedArray.CopyTo(RedHist, 0);

and this will do the greyscale histogram: 这将执行灰度直方图:

float[] GrayHist;

Image<Gray, Byte> img_gray = new Image<Gray, byte>("ImageFileName");

Histo.Calculate(new Image<Gray, Byte>[] { img_gray }, true, null);
//The data is here
//Histo.MatND.ManagedArray
GrayHist = new float[256];
Histo.MatND.ManagedArray.CopyTo(GrayHist, 0);

Hope this helps, 希望这可以帮助,

Cheers, 干杯,

Chris 克里斯

[Edit] [编辑]

To draw the histogram you will need to use either you own or a designed controls such as Zedgraph (This is supplied with with EMGU) here is a very good article on codeproject that shows it's use. 要绘制直方图,您需要使用自己的或者设计的控件,例如Zedgraph(这是随EMGU提供的),这是一篇关于codeproject的非常好的文章,展示了它的用途。

http://www.codeproject.com/KB/graphics/zedgraph.aspx http://www.codeproject.com/KB/graphics/zedgraph.aspx

Cheers 干杯

Chris 克里斯

Displaying Histograms in Emgu is super easy and fun. 在Emgu中显示直方图非常简单有趣。 Just make a histogramBox control on your form, then call this in your loop and you are done. 只需在表单上创建一个histogramBox控件,然后在循环中调用它即可完成。

        histogramBox1.ClearHistogram();
        histogramBox1.GenerateHistograms(frame, 256);
        histogramBox1.Refresh();

Tridimensional histogram 立体直方图

Image<Bgr, Byte>[] inp = new Image<Bgr, byte>("fileName.jpg");
int nBins = 256;
DenseHistogram hist = new DenseHistogram(new int[] { nBins, nBins, nBins }, new RangeF[] { new RangeF(0, 255), new RangeF(0, 255), new RangeF(0, 255) });
hist.Calculate(inp.Split(), false, null);

// To get value of single bin
int b = 255; int g = 0; int r = 0;  //blue
int count = Convert.ToInt32(hist.MatND.ManagedArray.GetValue(b, g, r));  //count = no of pixels in color Bgr(b,g,r)

//To get all values in a single array
List<Tuple<Bgr, int>> histVal = new List<Tuple<Bgr, int>>(nBins * nBins * nBins);
for (int i = 0; i < nBins; i++)
    for (int j = 0; j < nBins; j++)
        for (int k = 0; k < nBins; k++)
            histVal.Add(new Tuple<Bgr, int>(new Bgr(i, j, k), Convert.ToInt32(hist.MatND.ManagedArray.GetValue(i, j, k))));

Monodimensional histogram 单维直方图

int nBins = 256;
float[] valHist = new float[nBins];
Image<Gray, Byte>[] inp = new Image<Gray, byte>("fileName.jpg");
DenseHistogram hist = new DenseHistogram(nBins, new RangeF(0, 255));
hist.Calculate(new Image<Gray, Byte>[] { inp }, true, null);
hist.MatND.ManagedArray.CopyTo(valHist,0);

It is important to follow the procedure to add the Emgu.CV.UI.dll to your toolbox in Windows Forms in order to use all of the Windows Forms controls that Emgu CV provides (HistogramBox included.) 请务必按照以下步骤将Emgu.CV.UI.dll添加到Windows窗体中的工具箱中,以便使用Emgu CV提供的所有Windows窗体控件(包括HistogramBox)。

First of all you need to open your form in designer view. 首先,您需要在设计器视图中打开表单。 From Toolbox, right click in the empty space of 'General' column. 在“工具箱”中,右键单击“常规”列的空白区域。 This should pop up a selection menu, where 'Choose Items' selection is available, see image below. 这将弹出一个选择菜单,其中“选择项目”选项可用,请参见下图。

设计师表单视图

Afterwards, click on 'Choose Items'; 之后,点击“选择项目”; you will see a 'Choose Toolbox Item' Dialog. 您将看到“选择工具箱项”对话框。 From there click the 'Browse..' button on the lower right corner of the dialog. 从那里单击对话框右下角的“浏览...”按钮。

在此输入图像描述

Select 'Emgu.CV.UI.dll' file from 'Open' dialog, click the 'Open' button. 从“打开”对话框中选择“Emgu.CV.UI.dll”文件,单击“打开”按钮。 Now you should notice the ImageBox control has been added to the 'Choose Toolbox Items' dialog. 现在您应该注意到ImageBox控件已添加到“选择工具箱项”对话框中。 Click 'Ok'. 单击“确定”。 Then you should note the following controls added to your Toolbox (Applies for version 3.10 of Emgu. Some other versions of Emgu may have other controls or lack the controls mentioned below.) 然后你应该注意到你的工具箱中添加了以下控件(适用于Emgu的3.10版本。某些其他版本的Emgu可能有其他控件或缺少下面提到的控件。)

  • HistogramBox HistogramBox
  • ImageBox ImageBox
  • MatrixBox MatrixBox
  • PanAndZoomPictureBox. PanAndZoomPictureBox。

ToolBoxControls

Then you should be able to drag and drop to your form as you see fit the Windows Forms controls that Emgu CV has built-it. 然后,您应该能够拖放到您的表单,因为您认为Emgu CV已经构建了它的Windows窗体控件。 Or you should be able to use them programmatically: 或者您应该能够以编程方式使用它们:

Form frm = new Form();
var img = CvInvoke.Imread(this.PictureBox.ImageLocation, Emgu.CV.CvEnum.LoadImageType.Grayscale).ToImage<Gray, Byte>();

HistogramBox histo = new HistogramBox();

histo.ClearHistogram();
histo.GenerateHistograms(img, 256);
histo.Dock = DockStyle.Fill;
histo.Refresh();

frm.Controls.Add(histo);

frm.ShowDialog(); 

This answer was inspired in the Add Image Box Control tutorial. 这个答案的灵感来自于添加图像框控件教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM