Page 1 of 1

Checkbox detection and its status checking

PostPosted: Wed Jul 27, 2016 7:43 am
by ashok

I have form which includes the checkboxes. I am trying to detect thee using blob process and Shapechecker for this. But I am unable to detect the checkboxes (Square/Rectangle). It detecting the squares but not all squares. I am using the below code to achieve this. Kindly help me to check the checked/unchecked status of the checkboxes.

Bitmap bitmap = new Bitmap(@"C:\OCRDemo\Source\transformed.jpg");
//Bitmap bitmap = (Bitmap)Bitmap.FromFile(@"C:\OCRDemo\Source\paper-form-2-transformed.jpg");
//Bitmap gray = AForge.Imaging.Image.CreateGrayscaleImage(200, 200);
Bitmap rgb = AForge.Imaging.Image.Clone(bitmap, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

// grayscale filter (BT709)
AForge.Imaging.Filters.Grayscale gr = new AForge.Imaging.Filters.Grayscale(0.2125, 0.7154, 0.0721);
bitmap = gr.Apply(bitmap);

AForge.Imaging.Filters.Threshold th = new AForge.Imaging.Filters.Threshold(100);
bitmap = th.Apply(bitmap);

// locate objects using blob counter
BlobCounter bc = new BlobCounter();
bc.FilterBlobs = true;
bc.MinWidth = 5;
bc.MinHeight = 5;
// set ordering options
bc.ObjectsOrder = ObjectsOrder.Size;
Blob[] blobs = bc.GetObjectsInformation();
SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

for (int i = 0, n = blobs.Length; i < n; i++)
List<IntPoint> edgePoints = bc.GetBlobsEdgePoints(blobs[i]);

//AForge.Point center;
//float radius;
List<IntPoint> cornerPoints;

if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints))
//Console.WriteLine(String.Format("X:{0}, Y:{1}, r:{2}", center.X, center.Y, radius));
// only do things if the corners form a rectangle
if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Rectangle)
// here i use the graphics class to draw an overlay, but you
// could also just use the cornerPoints list to calculate your
// x, y, width, height values.
List<System.Drawing.Point> Points = new List<System.Drawing.Point>();
foreach (var point in cornerPoints)
Points.Add(new System.Drawing.Point(point.X, point.Y));

//System.Drawing.Image img = System.Drawing.Image.FromStream(new MemoryStream(imgData));
//Bitmap img = new Bitmap(new Bitmap(img));

Bitmap tempBitmap = new Bitmap(bitmap.Width, bitmap.Height);

Graphics g = Graphics.FromImage(rgb);
g.DrawPolygon(new Pen(Color.Red, 5.0f), Points.ToArray());


// only do things if the corners form a rectangle
//if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Rectangle)
// bc.ExtractBlobsImage(bitmap, blobs[i], true);

Thanks in advance!