public List <Rectangle> DetectLetters(Image <Bgr, Byte> img) { var hash = img.GetHashCode(); List <Rectangle> rects = new List <Rectangle>(); var imgGray = img.Convert <Gray, Byte>(); var imgSobel = imgGray.Sobel(1, 0, 3).Convert <Gray, Byte>(); var imgRes = new Image <Gray, byte>(imgSobel.Size); CvInvoke.Threshold(imgSobel, imgRes, 160, 255, ThresholdType.Binary | ThresholdType.Otsu); var element = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(2, 2), new Point(-1, -1)); CvInvoke.Dilate(imgRes, imgRes, element, new Point(0, 0), 1, BorderType.Default, new MCvScalar(0)); CvInvoke.Erode(imgRes, imgRes, element, new Point(0, 0), 12, BorderType.Default, new MCvScalar(0)); using (Mat hierachy = new Mat()) using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint()) { CvInvoke.FindContours(imgRes, contours, hierachy, RetrType.Tree, ChainApproxMethod.ChainApproxNone); for (int i = 0; i < contours.Size; i++) { Rectangle rectangle = CvInvoke.BoundingRectangle(contours[i]); var area = rectangle.Width * rectangle.Height; if (area > 1400 && rectangle.Width < img.Width * 0.7 && rectangle.Width > rectangle.Height * 1.5) { rects.Add(rectangle); } } } return(rects); }
/// <summary> /// Find the bounding rectangle for the specific array of points /// </summary> /// <param name="points">The collection of points</param> /// <returns>The bounding rectangle for the array of points</returns> public static Rectangle BoundingRectangle(PointF[] points) { using (VectorOfPointF ptVec = new VectorOfPointF(points)) return(CvInvoke.BoundingRectangle(ptVec)); }