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

C# 如何使用EmguCV和C绘制直方图#,c#,emgucv,C#,Emgucv,我需要绘制两种柱状图,即一维柱状图和三维柱状图。 我是EGU的新手,我在网上找到的所有样本都是C++或C.的,有没有使用C和EGUCV? 感谢您的帮助。以下代码将分割红绿色和蓝色直方图数据,并将它们放入一个浮点数组中,以供您使用 float[] BlueHist; float[] GreenHist; float[] RedHist; Image<Bgr, Byte> img = new Image<Bgr, byte>("ImageFileName"); Dense

我需要绘制两种柱状图,即一维柱状图和三维柱状图。 我是EGU的新手,我在网上找到的所有样本都是C++或C.的,有没有使用C和EGUCV?
感谢您的帮助。

以下代码将分割红绿色和蓝色直方图数据,并将它们放入一个浮点数组中,以供您使用

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);
float[]BlueHist;
绿色主义者;
浮动[]RedHist;
图像img=新图像(“图像文件名”);
DenseShistogram Histo=新的DenseShistogram(255,新范围f(0255));
图像img2Blue=img[0];
图像img2Green=img[1];
图像img2Red=img[2];
历史计算(新图像[]{img2Blue},真,空);
//数据在这里
//历史博物馆
BlueHist=新浮点[256];
历史材料管理材料(BlueHist,0);
透明组织();
历史计算(新图像[]{img2Green},真,空);
GreenHist=新浮动[256];
历史材料管理材料(GreenHist,0);
透明组织();
历史计算(新图像[]{img2Red},真,空);
RedHist=新浮点[256];
历史匹配管理数据集复制到(RedHist,0);
这将生成灰度直方图:

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);
float[]灰色历史;
图像img_gray=新图像(“图像文件名”);
历史计算(新图像[]{img_gray},真,空);
//数据在这里
//历史博物馆
GrayHist=新浮点[256];
历史匹配管理数据集(灰色历史,0);
希望这有帮助

干杯

克里斯

[编辑]

要绘制直方图,您需要使用您自己的或设计的控件,如Zedgraph(这是随EMGU提供的),这是一篇关于codeproject的非常好的文章,展示了它的用法

干杯


Chris在Emgu中显示直方图非常简单有趣。只要在表单上创建一个Historogrambox控件,然后在循环中调用它,就完成了

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

三维直方图

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))));
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);
Image[]inp=新图像(“fileName.jpg”);
int-nBins=256;
DenseHistogram hist=新的DenseHistogram(新的int[]{nBins,nBins,nBins},新的RangeF[]{newrangef(0255),新的RangeF(0255),新的RangeF(0255)});
hist.Calculate(inp.Split(),false,null);
//获取单个存储单元的值
int b=255;int g=0;int r=0//蓝色
int count=Convert.ToInt32(hist.MatND.ManagedArray.GetValue(b,g,r))//计数=彩色Bgr(b、g、r)中的像素数
//获取单个数组中的所有值
List histVal=新列表(nBins*nBins*nBins);
对于(int i=0;i
一维直方图

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))));
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);
int-nBins=256;
float[]valHist=新的float[nBins];
Image[]inp=新图像(“fileName.jpg”);
DenseShistogram hist=新的DenseShistogram(nBins,新范围F(0255));
计算(新图像[]{inp},true,null);
历史匹配管理数据集CopyTo(valHist,0);

必须按照以下步骤将Emgu.CV.UI.dll添加到Windows窗体中的工具箱中,以便使用Emgu CV提供的所有Windows窗体控件(包括Historogrambox)

首先,您需要在designer视图中打开表单。从工具箱中,在“常规”列的空白处单击鼠标右键。这将弹出一个选择菜单,其中“选择项目”选项可用,请参见下图

然后,点击“选择项目”;您将看到一个“选择工具箱项”对话框。从那里单击对话框右下角的“浏览…”按钮

从“打开”对话框中选择“Emgu.CV.UI.dll”文件,单击“打开”按钮。 现在您应该注意到ImageBox控件已添加到“选择工具箱项”对话框中。单击“确定”。然后,您应该注意添加到工具箱中的以下控件(适用于3.10版Emgu。其他一些版本的Emgu可能有其他控件或缺少以下提到的控件。)

  • 组织染色体
  • 图像盒
  • MatrixBox
  • PanAndZoomPictureBox

然后,您应该能够按照自己认为合适的方式将其拖放到表单上,并使用Emgu CV构建的Windows表单控件。或者您应该能够以编程方式使用它们:

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(); 
Form frm=new Form();
var img=CvInvoke.Imread(this.PictureBox.ImageLocation,Emgu.CV.CvEnum.LoadImageType.Grayscale).ToImage();
HistogramBox histo=新的HistogramBox();
清晰直方图();
生成直方图(img,256);
历史船坞=船坞式填充;
刷新历史();
frm.对照组。添加(历史);
frm.ShowDialog();

这个答案来源于教程。

计算的第一个参数是图像数组。这是不是意味着我可以给它{img2Blue,img2Red,img2Green}它可以计算所有的值?你能演示一下这个用法吗?非常感谢。只需快速更正Chris的帖子
DenseHistogram Histo=newdensehistogram(255,newrangef(0255))应该是
DenseHistogram Histo=新的DenseHistogram(256,新的范围f(0,256))否则,您将丢失255位中的任何值,正如我刚刚创建by图像的拉普拉斯算子时所经历的那样。