簡體   English   中英

如何使用EmguCV和C#繪制直方圖

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

我需要繪制兩種類型的直方圖,即單維和立體。 我是EMGU的新手,我在網上找到的所有樣本都是C ++或C.有沒有使用C#和Emgucv的樣本?

謝謝你的幫助。

以下代碼將分割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);

這將執行灰度直方圖:

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);

希望這可以幫助,

干杯,

克里斯

[編輯]

要繪制直方圖,您需要使用自己的或者設計的控件,例如Zedgraph(這是隨EMGU提供的),這是一篇關於codeproject的非常好的文章,展示了它的用途。

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

干杯

克里斯

在Emgu中顯示直方圖非常簡單有趣。 只需在表單上創建一個histogramBox控件,然后在循環中調用它即可完成。

        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);

請務必按照以下步驟將Emgu.CV.UI.dll添加到Windows窗體中的工具箱中,以便使用Emgu CV提供的所有Windows窗體控件(包括HistogramBox)。

首先,您需要在設計器視圖中打開表單。 在“工具箱”中,右鍵單擊“常規”列的空白區域。 這將彈出一個選擇菜單,其中“選擇項目”選項可用,請參見下圖。

設計師表單視圖

之后,點擊“選擇項目”; 您將看到“選擇工具箱項”對話框。 從那里單擊對話框右下角的“瀏覽...”按鈕。

在此輸入圖像描述

從“打開”對話框中選擇“Emgu.CV.UI.dll”文件,單擊“打開”按鈕。 現在您應該注意到ImageBox控件已添加到“選擇工具箱項”對話框中。 單擊“確定”。 然后你應該注意到你的工具箱中添加了以下控件(適用於Emgu的3.10版本。某些其他版本的Emgu可能有其他控件或缺少下面提到的控件。)

  • HistogramBox
  • ImageBox
  • MatrixBox
  • PanAndZoomPictureBox。

ToolBoxControls

然后,您應該能夠拖放到您的表單,因為您認為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(); 

這個答案的靈感來自於添加圖像框控件教程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM