7

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?

Thanks for helping.

gonzobrains
  • 7,856
  • 14
  • 81
  • 132

4 Answers4

7

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.

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.

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

Cheers

Chris

Chris
  • 3,462
  • 20
  • 32
  • The first paremeter of Calculate is an array of Image. Does it mean I can give it { img2Blue, img2Red,img2Green } and it calculates all? Can you demonstrate this usage? Thank you very much. – Gqqnbig Oct 18 '12 at 15:39
  • 2
    Just a quick correction on Chris's post `DenseHistogram Histo = new DenseHistogram(255, new RangeF(0, 255));` should be `DenseHistogram Histo = new DenseHistogram(256, new RangeF(0, 256));` Else you will be missing any values in your 255 bin as I just experienced when I created a laplacian of by image. – foofunner Mar 11 '13 at 03:43
5

Displaying Histograms in Emgu is super easy and fun. Just make a histogramBox control on your form, then call this in your loop and you are done.

        histogramBox1.ClearHistogram();
        histogramBox1.GenerateHistograms(frame, 256);
        histogramBox1.Refresh();
john k
  • 6,268
  • 4
  • 55
  • 59
1

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);
nisssal
  • 111
  • 4
1

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

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.

Designer Form View

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.

enter image description here

Select 'Emgu.CV.UI.dll' file from 'Open' dialog, click the 'Open' button. Now you should notice the ImageBox control has been added to the 'Choose Toolbox Items' dialog. 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.)

  • HistogramBox
  • ImageBox
  • MatrixBox
  • 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. 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.

Amadeus Sanchez
  • 2,375
  • 2
  • 25
  • 31