I am working on image processing project in C#. I'm using OTSU thresholding method. I am getting this exception "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." I googled and searched stack-overflow for more than 2 days, but I didn't get the correct solution. I am getting this error only for few images, with other images its working fine...
public void Convert2GrayScaleFast(Bitmap bmp)
{
BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* p = (byte*)(void*)bmData.Scan0.ToPointer();
int stopAddress = (int)p + bmData.Stride * bmData.Height;
while ((int)p != stopAddress)
{
p[0] = (byte)(.299 * p[2] + .587 * p[1] + .114 * p[0]);
p[1] = p[0];
p[2] = p[0];
p += 3;
}
}
bmp.UnlockBits(bmData);
}
I'm getting the exception in this line
p[0] = (byte)(.299 * p[2] + .587 * p[1] + .114 * p[0]);
dont tell to bypass exception using Tools menu ->Options -> Debugging -> General -> Uncheck this option "Suppress JIT optimization on module load". I have tried all the methods but nothing worked for me..
Here is the image which throws the exception Thanks a lot if u help me to solve the problem.