How to calculate 1000 values average of image using histogram ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I know to calculate 256 values and create histogram of it.

<div style="background-color:white; color:black
<pre><span style="color:blue public <span style="color:blue static <span style="color:blue long[] GetHistogram(Bitmap b)
{
<span style="color:blue long[] myHistogram = <span style="color:blue new <span style="color:blue long[256];

BitmapData bmData = <span style="color:blue null;

<span style="color:blue try
{
<span style="color:green //Lock it fixed with 32bpp
bmData = b.LockBits(<span style="color:blue new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

<span style="color:blue int scanline = bmData.Stride;

System.IntPtr Scan0 = bmData.Scan0;

<span style="color:blue unsafe
{
<span style="color:blue byte* p = (<span style="color:blue byte*)(<span style="color:blue void*)Scan0;

<span style="color:blue int nWidth = b.Width;
<span style="color:blue int nHeight = b.Height;

<span style="color:blue for (<span style="color:blue int y = 0; y < nHeight; y++)
{
<span style="color:blue for (<span style="color:blue int x = 0; x < nWidth; x++)
{
<span style="color:blue long Temp = 0;
Temp += p[0];
Temp += p[1];
Temp += p[2];

Temp = (<span style="color:blue int)Temp / 3;
myHistogram[Temp]++;

<span style="color:green //we do not need to use any offset, we always can increment by pixelsize when
<span style="color:green //locking in 32bppArgb - mode
p += 4;
}
}
}

b.UnlockBits(bmData);
}
<span style="color:blue catch
{
<span style="color:blue try
{
b.UnlockBits(bmData);
}
<span style="color:blue catch
{

}
}

<span style="color:blue return myHistogram;
}
[/code]


<pre>if (FramesToSave != null && FramesToSave.Contains(_frameId))
{
bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
bitmap.Save(Path.Combine(_outFolder, _frameId.ToString("D6") + ".bmp"));
// get histogram values
long[] HistogramValues = Form1.GetHistogram(bitmap);
if (histogramValuesList == null)
histogramValuesList = new List<long>(256);
histogramValuesList.AddRange(HistogramValues);[/code]

Then after calculating the histogram of each frame(image) im using the LIST<> of arrays and drawing each histogram to a graph.

<div style="background-color:white; color:black
<pre><span style="color:blue long[] tt = list_of_histograms[myTrackPanelss1.trackBar1.Value];
histogramControl1.DrawHistogram(tt);
[/code]
<pre>i need ot buld a function.[/code]

In the function i need that it will get a number for example 100000 values from an image then using histogram calculation to get the highest 1000 values.
So the function will get histogarm and a number and return the 1000 highest values.

I didnt realy understand but i need to get a number and histogram and in the return to get a number ich is the highest 1000 values of the frame(image).
<
danieli<br/>

View the full article
 
Back
Top