private void OtsuThreshold() { if (!CheckIfGrayscale()) { Greyscale(); } AForge.Imaging.Filters.OtsuThreshold filter = new AForge.Imaging.Filters.OtsuThreshold(); // apply the filter filter.ApplyInPlace(image); }
public static bool HaveLabelRF(Bitmap imagePassport) { bool haveLabel = false; // 1- grayscale image Bitmap grayImage = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(imagePassport); // 2 - Otsu thresholding AForge.Imaging.Filters.OtsuThreshold threshold = new AForge.Imaging.Filters.OtsuThreshold(); Bitmap binaryImage = threshold.Apply(grayImage); AForge.Imaging.Filters.Invert invertFilter = new AForge.Imaging.Filters.Invert(); Bitmap invertImage = invertFilter.Apply(binaryImage); // 3 - Blob counting BlobCounter blobCounter = new BlobCounter(); blobCounter.FilterBlobs = true; blobCounter.MinWidth = 10; blobCounter.MinWidth = 10; blobCounter.ProcessImage(invertImage); Blob[] blobs = blobCounter.GetObjectsInformation(); // 4 - check shape of each blob SimpleShapeChecker shapeChecker = new SimpleShapeChecker(); int count = 0; foreach (Blob blob in blobs) { List <AForge.IntPoint> edgePoint = blobCounter.GetBlobsEdgePoints(blob); // check if shape looks like a circle AForge.Point center; float radius; if (shapeChecker.IsCircle(edgePoint, out center, out radius)) { count++; Rectangle cloneRect = new Rectangle((int)(center.X - radius), (int)(center.Y - radius + 1.0), (int)(radius * 2) + 1, (int)(radius * 2) + 1); Bitmap RF = new Bitmap(binaryImage.Clone(cloneRect, binaryImage.PixelFormat), 32, 32); Bitmap RF2 = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(RF); Bitmap RF3 = threshold.Apply(RF2); if (CompareLabel(RF3) > 70.0) { haveLabel = true; } } } return(haveLabel); }