Пример #1
0
        static CellLines DetectHorizontalLines(Mat binary, int hThreshold, int textSize)
        {
            CellLines horizontals = new CellLines();

            using (Mat horizontal = binary.Clone())
                using (Mat hKernel = Cv2.GetStructuringElement(shape: MorphShapes.Rect, ksize: new Size(hThreshold, SIZE_LINE)))
                    using (Mat kernelDilate = Cv2.GetStructuringElement(shape: MorphShapes.Rect, ksize: new Size(SIZE_DILATE, SIZE_DILATE)))
                    {
                        // horizontal
                        Cv2.Erode(src: horizontal, dst: horizontal, element: hKernel, anchor: new Point(-1, -1));
                        Cv2.Dilate(src: horizontal, dst: horizontal, element: hKernel, anchor: new Point(-1, -1));

                        // 중간에 살짝 끊어진 라인을 잇기 위해 라인을 확장시킨다.
                        Cv2.Dilate(src: horizontal, dst: horizontal, element: kernelDilate, anchor: new Point(-1, -1));

                        Point[][]        horizontalContours;
                        HierarchyIndex[] horizontalHierarchy;
                        Cv2.FindContours(image: horizontal, contours: out horizontalContours, hierarchy: out horizontalHierarchy, mode: RetrievalModes.External, method: ContourApproximationModes.ApproxSimple, offset: new Point(0, 0));

                        int  startX, startY, endX, endY, index = 0;
                        Rect rect;

                        for (int i = 0; i < horizontalHierarchy.Length; i++)
                        {
                            rect = Cv2.BoundingRect(curve: horizontalContours[i]);

                            startX = rect.X;
                            startY = rect.Y + (int)(rect.Height * 0.5);
                            endX   = rect.X + rect.Width;
                            endY   = startY;

                            if (rect.Width > textSize)
                            {
                                horizontals.Add(new CellLine(index: index++, startX: startX, startY: startY, endX: endX, endY: endY, thickness: rect.Height));
                            }
                        }
                    }

            return(new CellLines(horizontals.OrderBy(line => line.CenterY)));
        }