Ed

Ed

  • NA
  • 3
  • 0

Simple Image Processing causes Program to Crash

May 29 2008 12:47 AM

Hello,

I am writing a C# program which performs some basic computer vision on an AVI video.

The first step is to extract a Bitmap object from the AVI file for each frame - and this code works perfectly on its own.

In order to process each bitmap, I then go into unsafe mode, and use a pointer to cycle through the bitmap data. I then store the RGB values of each pixel into a Color[,] array, so that I can then read the RGB values at any location in the bitmap [x, y]. Here is that function that returns this array:

public static Color[,] GetRGBArray(Bitmap b)
{
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;

Color[,] array = new Color[b.Width, b.Height];

unsafe
{
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - b.Width * 3;
int nWidth = b.Width * 3;
for (int y = 0; y < b.Height; ++y)
{
for (int x = 0; x < b.Width; ++x)
{
array[x, y] = Color.FromArgb(p[2], p[1], p[0]) ;

p = p + 3;
}
p += nOffset;
}
}
return array;
}

This function works perfectly well too, on its own at least.

Next, I add time as a dimension. I have another array, called data, which is an array of an array (jagged arrray). It is of type Color[i][x,y], where i is the frame number, and [x, y] are the coordinates on the bitmap. As the program grabs bitmaps from the AVI, I call the GetRGBArray() function for each bitmap, and the result is stored in the data array:

data[i] = GetRGBArray(b);

where i is the number of the current frame, and b is the bitmap object for the current frame, that I have grabbed from the AVI file.

From this, I can access the Color data for every pixel for every frame.

This all works fine functionally. Here is the problem. The AVI video has 1000 frames in it, however, after the program has been running for about 300 frames, it starts to slow down, and at about 400 frames, it just completely stops, and I don't know why.

There must be some trash of some sort that is piling up as I run the program, and once it gets to a certain point it starts slowing the program down. It doesn't matter where abouts in the video file I start from, after 300 frames it starts to be affected.

Any ideas? It is driving me mad!

Thank you for any help on this

Ed.


Answers (1)