Im using histogram and averages to find the most highest 1000 averages in a bmp but its not working

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
In my class the new class i have this code:

<pre class="prettyprint int ISampleGrabberCB.BufferCB(double sampleTime, IntPtr pBuffer, int bufferLen)
{
if (Form1.ExtractAutomatic == true)
{
using (var bitmap = new Bitmap(_width, _height, _width * 3, PixelFormat.Format24bppRgb, pBuffer))
{
if (!this.Secondpass)
{
long[] HistogramValues = Form1.GetHistogram(bitmap);
long t = Form1.GetTopLumAmount(HistogramValues, 1000);
Form1.averagesTest.Add(t);
}
else
{

if (_frameId > 0)
{
if (Form1.averagesTest[_frameId] / 1000.0 - Form1.averagesTest[_frameId - 1] / 1000.0 > 150.0) // Something is wrong with the averagesTest function in Form1.
{
count = 6;
}

if (count > 0)
{
bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
bitmap.Save(Path.Combine(_outFolder, _frameId.ToString("D6") + ".bmp"));
count --;
}
}
}

_frameId++;

if (_frameId % 100 == 0)
OnProgressChanged(_frameId);
}
}[/code]
Now in Form1 the function GetHistogram:

<pre class="prettyprint public static long[] GetHistogram(Bitmap b)
{
long[] myHistogram = new long[256];

BitmapData bmData = null;

try
{
bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

int scanline = bmData.Stride;

System.IntPtr Scan0 = bmData.Scan0;

unsafe
{
byte* p = (byte*)(void*)Scan0;

int nWidth = b.Width;
int nHeight = b.Height;

for (int y = 0; y < nHeight; y++)
{
for (int x = 0; x < nWidth; x++)
{
long Temp = 0;
Temp += p[0];
Temp += p[1];
Temp += p[2];

Temp = (int)Temp / 3;
myHistogram[Temp]++;

p += 4;
}
}
}

b.UnlockBits(bmData);
}
catch
{
try
{
b.UnlockBits(bmData);
}
catch
{

}
}

return myHistogram;
}[/code]
And the function <span style="font-size:small <span style="font-size:small GetTopLumAmount:

<pre class="prettyprint public static long GetTopLumAmount(long[] histogram, int amount)
{
if (histogram.Sum() < amount)
return -1;

long result = 0;
long counted = 0;

for (int i = histogram.Length - 1; i >= 0; i--)
{
if (counted < amount)
{
long current = Math.Min(histogram, amount - counted);
counted += current;

result += current * i;
}
else
break;
}

return result;
}[/code]
Now the calculation im doing that check if its larger then 150 is no working its never above 150:

<pre class="prettyprint if (Form1.averagesTest[_frameId] / 1000.0 - Form1.averagesTest[_frameId - 1] / 1000.0 > 150.0)[/code]
The main idea of my application is to get still lightnings images from a video file.

So im trying to get the 1000 highest or the 1000 averages from a bitmap . But the calculation is never working its never > 150.0

Now i remember it was working sometime before so im not sure why its not working now. This code should work for automatic mode while in manual mode its working but in this part when its automatic its not working.

I used a break point in the new class on the calculation line:
<pre class="prettyprint if (Form1.averagesTest[_frameId] / 1000.0 - Form1.averagesTest[_frameId - 1] / 1000.0 > 150.0)[/code]
But its never largr/above 150.0
What could be wrong here ? <hr class="sig danieli

View the full article
 
Back
Top