Hello to all, [INDENT]I am making an application which will only take images, convert it into binary image using a threshold. Now this binary image will be segmented horizontally on the basis of black pixels. In the attachment , this is the original image file. Now i have to segment it in these two segments i.e, and .
I am using the following code to do this, i am getting an exception ofinvalid parameters to the Rectangle in line 51 [/INDENT]
Please view the following code carefully and tell me the logical mistake please thanks in advance;
public ArrayList horizantalsegment()
{
ArrayList aa = new ArrayList();
int counter = 0, x1 = 0, x2 =0, y1 =0, y2 =0;
bool flagblack = false;
Bitmap bitmap = new Bitmap(srcimage.Width, srcimage.Height);
Bitmap bsegment = (Bitmap)srcimage.Clone ();
BitmapData srcdata = ((Bitmap)srcimage).LockBits(new Rectangle(0, 0, srcimage.Width, srcimage.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData destdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte clrvalue = 0;
unsafe
{
for (int i = 0; i < srcdata.Height; i++)
{
byte* ptr = (byte*)srcdata.Scan0 + (i * srcdata.Stride);
byte* dptr = (byte*)destdata.Scan0 + (i * srcdata.Stride);
for (int j = 0; j < srcdata.Width; j++)
{
clrvalue = 0;
clrvalue = (byte)(ptr[0] + ptr[1] + ptr[2]);
if (clrvalue == 0 && !flagblack)
{
y1 = i - 1;
flagblack = true;
}
else if (flagblack)
{
counter++;
}
if (counter == srcimage.Width - 1)
{
y2 = y1 + i + 2;
Bitmap segmentedimg = new Bitmap(bsegment.Width, y2 - y1);
BitmapData segmentdata = segmentedimg.LockBits(new Rectangle(0, 0, bsegment.Width, segmentedimg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData srcdataseg = bsegment.LockBits(new Rectangle(0, y1, bsegment.Width, y2 -y1), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
for (int x = 0 ; x < segmentedimg.Height; x++)
{
byte* srcptr = (byte*)srcdataseg.Scan0 + (x * srcdataseg.Stride);
byte* segptr = (byte*)segmentdata.Scan0 + (x * segmentdata.Stride);
for (int y = y1; y < y2 ; y++)
{
byte color = (byte)((srcptr[0] + srcptr[1] + srcptr[2]) / 3);
segptr[0] = segptr[1] = segptr[2] = color;
srcptr += 3;
segptr += 3;
}
}
x1 = 0;
x2 = 0;
y1 = 0;
y2 = 0;
flagblack = false;
segmentedimg.UnlockBits(segmentdata);
bsegment.UnlockBits(srcdataseg);
aa.Add(segmentedimg);
counter = 0;
}
ptr += 3;
dptr += 3;
}
counter = 0;
}
}
((Bitmap)srcimage).UnlockBits(srcdata);
bitmap.UnlockBits(destdata);
return aa;
}