Cant figure out why it dosent extract/save to hard disk the needed images/frames:

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
This is how i did it in another project just for testing and it did work:

<pre class="prettyprint counter = 0;
tempList = new List<long>();
long res = 0;
_fi = new DirectoryInfo(/*@"D:Frames_ExtractedVIDEO0007.avi"*/ @"D:Frames_ExtractedVIDEO0007.avi").GetFiles("*.bmp"); //@"D:Frames_ExtractedLightnings Israel 03042011 Youtube4Down.com.avi").GetFiles("*.bmp");
averages = new List<long>(_fi.Length);
for (int i = 0; i < _fi.Length; i++)
{
using (Bitmap myBitmaps = new Bitmap(_fi.FullName))
{
long[] HistogramValues = GetHistogram(myBitmaps);
res = GetTopLumAmount(HistogramValues, 1000);
averages.Add(res);

long[] HistogramValues123 = Form1.GetHistogram(myBitmaps);
if (histogramValuesList == null)
histogramValuesList = new List<long>(256);
histogramValuesList.AddRange(HistogramValues123);
list_of_histograms.Add(HistogramValues123);
}
}
this.averages1000_Control1.DrawAverages(averages.ToArray(), 1000.0);

for (int x = 1; x < averages.Count(); x++)
{
if (averages[x] / 1000.0 - averages[x - 1] / 1000.0 > 180.0) // This will show only the first frame wich containing a lightning inside the begining of a lightning.
//if (averages[x] / 1000.0 > 180.0) // This will show every frame with a lightning inside but it wont be automatic like we wanted.
{

Image iOld = pictureBox1.Image;
pictureBox1.Image = new Bitmap(_fi[x].FullName);
//pictureBox1.Image.Save(Path.Combine(@"d:temp_images", counter.ToString("D6") + ".bmp"));
counter++;
if (iOld != null)
iOld.Dispose();
}
}[/code]

Now in the original project im not using _fi wich is FileINfo[] like above and make GetFiles i need to use the frames in WmvAdapter.cs wich is extracting the frames from the video file.

So in the original project Form1 in the top form level i aded a new List wich im using in WmvAdapter.cs:

<pre class="prettyprint public static List<long> averagesTest = new List<long>();[/code]
Now i have in the Form1 also another two functions im using in the WmvAdapter.cs:

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

BitmapData bmData = null;

try
{
//Lock it fixed with 32bpp
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]++;

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

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

}
}

return myHistogram;
}


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]
And in the WmvAdapter.cs im using it all like this:

<pre class="prettyprint int ISampleGrabberCB.BufferCB(double sampleTime, IntPtr pBuffer, int bufferLen)
{
using (var bitmap = new Bitmap(_width, _height, _width * 3, PixelFormat.Format24bppRgb, pBuffer))
{
if (!this.Secondpass)
{
//get avg
/*double average = GetAveragePixelValue(bitmap);

if (AveragesList == null)
AveragesList = new List<double>();
//save avg
AveragesList.Add(average);*/

//***************************\
// for (int i = 0; i < (int)framesCounts(); i++)
// {
// get histogram values
long[] HistogramValues = Form1.GetHistogram(bitmap);
/* if (histogramValuesList == null)
histogramValuesList = new List<long>(256);
histogramValuesList.AddRange(HistogramValues);*/
long t = Form1.GetTopLumAmount(HistogramValues, 1000);

//***************************\
Form1.averagesTest.Add(t); // to add this list to a text file or binary file and read the averages from the file when its is Secondpass !!!!!

//}

}




else
{
/*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);



using (BinaryWriter binWriter =
new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
for (int i = 0; i < histogramValuesList.Count; i++)
{

binWriter.Write(histogramValuesList[(int)i]);

}
binWriter.Close();
}
}*/

for (int x = 1; x < Form1.averagesTest.Count; x++)
{
if (Form1.averagesTest[x] / 1000.0 - Form1.averagesTest[x - 1] / 1000.0 > 180.0)
{

bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
bitmap.Save(Path.Combine(_outFolder, _frameId.ToString("D6") + ".bmp"));
_frameId++;
}
}
}

_frameId++;

//let only report each 100 frames for performance
if (_frameId % 100 == 0)
OnProgressChanged(_frameId);



}



return 0;
}[/code]

But for some reason its saving to the hard disk almost every Bitmap. It should save only Bitmaps with lightning inside and it was working on the other test project example above.
But it dosent work here.
I just added now:
<pre class="prettyprint double fff = Form1.averagesTest[x] / 1000.0 - Form1.averagesTest[x - 1] / 1000.0;[/code]
And the first time the result is: -0.032999999999999474
Wich is < then 180
After some times the result is 229.332 and then its entering and saving the Bitmap to hard disk.

And it keep saving to the hard disk non stop frames/images and put all the lightnings images in the middle or in a gorup and then keep saving after it in the original state i mean the averagesTest List there are 4873 indexs and also the video file the frames
count are 4873.
But when its saving once its entering the saving it dosent stop i got it over 6000 images on hard disk most of them are black except the ones withl ightnings inside and i had to stop the program manualy.

I dont get it why it did work good in the example project above but it dosent work here.
<hr class="sig danieli

View the full article
 
Back
Top