Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,689 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,306 people online right now. Registration is fast and FREE... Join Now!




Image Processing Crash

 
Reply to this topicStart new topic

Image Processing Crash, Help! My simple program to process images from an AVI file causes

ejohns85
post 28 May, 2008 - 08:52 PM
Post #1


New D.I.C Head

*
Joined: 28 May, 2008
Posts: 1

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:


csharp

public static Color[,] GetRGBArray(Bitmap cool.gif
{
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(cool.gif;

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.

This post has been edited by PsychoCoder: 29 May, 2008 - 05:29 AM
User is offlineProfile CardPM

Go to the top of the page

thor78
post 28 May, 2008 - 10:30 PM
Post #2


D.I.C Head

Group Icon
Joined: 6 May, 2008
Posts: 106



Dream Kudos: 50
My Contributions


First time I've seen pointers used in any .NET program. biggrin.gif Try processing the AVI by batch. Maybe for every 200 bitmap frames. Get the last index and start the next batch from there.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:33AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month