المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : An exception occures when I process an image. please help me



C# Programming
12-29-2010, 01:40 PM
Hello
I am doing face detection in live camera, first I detect skin and then I copy that skin area from the original image and re-size to he 50(width) and 70(height), because my template is 50(width) by 70(height) size.
when I start so some frames are checked properly but after some moment (or some time at the first frame) the following exception is generated.
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
the above error is generated on below line in the code.
stor = ((rowTamplate[(h * pixelSize)] + rowTamplate[(h * pixelSize) + 1] + rowTamplate[(h * pixelSize) + 2]) / 3) - ((rowBitmap[(h * pixelSize)] + rowBitmap[(h * pixelSize) + 1] + rowBitmap[(h * pixelSize) + 2]) / 3);
I thought alot to understand this problem but I failed.
the actual code is here.
BitmapData templateData = template.LockBits(new Rectangle(0, 0, 50, 70), System.Drawing.Imaging.ImageLockMode.WriteOnly, template.PixelFormat)
bitmap = ImageResizer.ResizeImage(bitmap, 50, 70);//here bitmap is a region from original image where skin is present.and here I resize it to 50(width) by 70(height).
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, 50, 70), System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat);

double stor = 0;
for (int w = 0; w < 70; w++)
{
Byte* rowTamplate = (byte*)templateData.Scan0 + (w * templateData.Stride);
Byte* rowBitmap = (byte*)bitmapData.Scan0 + (w * bitmapData.Stride);
for (int h = 0; h < 50; h++)
{
stor = 0;
//the exception is generated on the fllowing line
stor = ((rowTamplate[(h * pixelSize)] + rowTamplate[(h * pixelSize) + 1] + rowTamplate[(h * pixelSize) + 2]) / 3) - ((rowBitmap[(h * pixelSize)] + rowBitmap[(h * pixelSize) + 1] + rowBitmap[(h * pixelSize) + 2]) / 3);
if (stor < 0)
{
stor *= -1;
}
PixelsCounter += (int)stor;
stor = 0;
}
}
thanks