1

enter image description here

As result, only just the last line was boxed as seen above.

The code starts from this post below.
Extracting text OpenCV
First, I start it from Diomedes Domínguez's C# code. I modified only one line because original code throws here.

var mask = new Mat(Mat.Zeros(bw.Size(), MatType.CV_8UC1));

System.ArgumentException: 'empty ranges
parameter: ranges'

So I added range.

var mask = new Mat(Mat.Zeros(bw.Size(), MatType.CV_8UC1), new Rect(0, 0, bw.Size().Width, bw.Size().Height));

So here is code what I've tried.

Mat large = new Mat(@"D:\cap_price.PNG");
            Mat rgb = new Mat(), small = new Mat(), grad = new Mat(), bw = new Mat(), connected = new Mat();

            // downsample and use it for processing
            Cv2.PyrDown(large, rgb);
            Cv2.CvtColor(rgb, small, ColorConversionCodes.BGR2GRAY);

            // morphological gradient
            var morphKernel = Cv2.GetStructuringElement(MorphShapes.Ellipse, new OpenCvSharp.Size(3, 3));
            Cv2.MorphologyEx(small, grad, MorphTypes.Gradient, morphKernel);
            // binarize
            Cv2.Threshold(grad, bw, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);

            // connect horizontally oriented regions
            morphKernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(9, 1));
            Cv2.MorphologyEx(bw, connected, MorphTypes.Close, morphKernel);
            // find contours
            var mask = new Mat(Mat.Zeros(bw.Size(), MatType.CV_8UC1));
            Cv2.FindContours(connected, out OpenCvSharp.Point[][] contours, out HierarchyIndex[] hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple, new OpenCvSharp.Point(0, 0));
            // filter contours
            var idx = 0;
            foreach (var hierarchyItem in hierarchy)
            {
                OpenCvSharp.Rect rect = Cv2.BoundingRect(contours[idx]);
                var maskROI = new Mat(mask, rect);
                maskROI.SetTo(new Scalar(0, 0, 0));
                // fill the contour
                Cv2.DrawContours(mask, contours, idx, Scalar.White, -1);

                // ratio of non-zero pixels in the filled region
                double r = (double)Cv2.CountNonZero(maskROI) / (rect.Width * rect.Height);
                if (r > .45 /* assume at least 45% of the area is filled if it contains text */
                     &&
                (rect.Height > 8 && rect.Width > 8) /* constraints on region size */
                                                    /* these two conditions alone are not very robust. better to use something 
                                                    like the number of significant peaks in a horizontal projection as a third condition */
                )
                {
                    Cv2.Rectangle(rgb, rect, new Scalar(0, 255, 0), 1);
                }
            }

            //rgb.SaveImage(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "rgb.jpg"));
            rgb.SaveImage(@"D:\rgb.jpg");

I want each text line to be boxed.

  • 1
    I've finally made it. ```Cv2.DrawContours(mask, contours, idx, Scalar.White, -1);``` should be ```Cv2.DrawContours(mask, contours, idx++, Scalar.White, -1);``` – Renny Hyunwoo Park Jul 08 '19 at 05:36

0 Answers0