示例#1
0
        static void GetInsideLine(CellLines horizontalSamples, CellLines verticalSamples, int lineDistance, ref CellLines horizontals, ref CellLines verticals, out CellLines horizontalRests, out CellLines verticalRests)
        {
            horizontalRests = new CellLines();
            verticalRests   = new CellLines();

            bool isContain;

            // 가장 외각선과 맞닿는 부분이 있는 라인과 맞닿은 라인은 인정한다.
            foreach (CellLine horizontal in horizontalSamples)
            {
                isContain = false;

                foreach (CellLine vertical in verticals)
                {
                    if (horizontal.StartY >= vertical.StartY && horizontal.EndY <= vertical.EndY)
                    {
                        if (Math.Abs(horizontal.StartX - vertical.StartX) <= lineDistance || Math.Abs(horizontal.EndX - vertical.EndX) <= lineDistance)
                        {
                            isContain = true;
                            horizontals.Add(horizontal);
                            break;
                        }
                    }
                }

                if (!isContain)
                {
                    horizontalRests.Add(horizontal);
                }
            }

            // 가장 외각선과 맞닿는 부분이 있는 라인과 맞닿은 라인은 인정한다.
            foreach (CellLine vertical in verticalSamples)
            {
                isContain = false;

                foreach (CellLine horizontal in horizontals)
                {
                    if (vertical.StartX >= horizontal.StartX && vertical.EndX <= horizontal.EndX)
                    {
                        if (Math.Abs(vertical.StartY - horizontal.StartY) <= lineDistance || Math.Abs(vertical.EndY - horizontal.EndY) <= lineDistance)
                        {
                            isContain = true;
                            verticals.Add(vertical);
                            break;
                        }
                    }
                }

                if (!isContain)
                {
                    verticalRests.Add(vertical);
                }
            }
        }
示例#2
0
        static CellLines GetVerticalAllInBox(CellLine top, CellLine bottom, CellLine left, CellLine right, CellLines verticals, int lineDistance, ref Dictionary <int, int> duplicateDic)
        {
            CellLines verticalsAll = new CellLines();

            foreach (CellLine line in verticals)
            {
                if (!duplicateDic.ContainsKey(line.Index) &&
                    line.StartX >= left.StartX &&
                    line.EndX <= right.EndX &&
                    line.StartY >= top.StartY - lineDistance &&
                    line.EndY <= bottom.StartY + lineDistance)
                {
                    verticalsAll.Add(line);
                }
            }

            return(verticalsAll);
        }
示例#3
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)));
        }
示例#4
0
        static void RemoveNoiseLine(CellLine top, CellLine bottom, CellLine left, CellLine right, CellLines innerHorizontals, CellLines innerVerticals, int lineDistance, ref Dictionary <int, int> duplicateDic, out CellLines horizontalsFinal, out CellLines verticalsFinal)
        {
            // 최종적으로 찾은 것을 duplicateDic에 추가한다. (이렇게 하면 박스 안의 박스는 별도로 처리 가능)
            horizontalsFinal = new CellLines();
            verticalsFinal   = new CellLines();

            CellLines horizontals     = new CellLines();
            CellLines verticals       = new CellLines();
            CellLines horizontalFirst = new CellLines();
            CellLines verticalFirst   = new CellLines();

            // 일단 가장 외각선과 맞닿는 부분이 있는 라인만 인정한다.
            foreach (CellLine line in innerHorizontals)
            {
                if (Math.Abs(line.StartX - left.StartX) <= lineDistance || Math.Abs(line.EndX - right.EndX) <= lineDistance)
                {
                    horizontals.Add(line);
                }
                else
                {
                    horizontalFirst.Add(line);
                }
            }

            // 일단 가장 외각선과 맞닿는 부분이 있는 라인만 인정한다.
            foreach (CellLine line in innerVerticals)
            {
                if (Math.Abs(line.StartY - top.StartY) <= lineDistance || Math.Abs(line.EndY - bottom.EndY) <= lineDistance)
                {
                    verticals.Add(line);
                }
                else
                {
                    verticalFirst.Add(line);
                }
            }

            // 가장 외각선과 맞닿는 부분이 있는 라인과 맞닿은 라인은 인정한다.
            // Data Sheet 같이 복잡한 테이블을 처리하기 위해 4번 돌린다.
            CellLines horizontalSecond, verticalSecond, horizontalThird, verticalThird, horizontalFourth, verticalFourth;

            GetInsideLine(horizontalSamples: horizontalFirst, verticalSamples: verticalFirst, lineDistance: lineDistance, horizontals: ref horizontals, verticals: ref verticals, horizontalRests: out horizontalSecond, verticalRests: out verticalSecond);
            GetInsideLine(horizontalSamples: horizontalSecond, verticalSamples: verticalSecond, lineDistance: lineDistance, horizontals: ref horizontals, verticals: ref verticals, horizontalRests: out horizontalThird, verticalRests: out verticalThird);
            GetInsideLine(horizontalSamples: horizontalThird, verticalSamples: verticalThird, lineDistance: lineDistance, horizontals: ref horizontals, verticals: ref verticals, horizontalRests: out horizontalFourth, verticalRests: out verticalFourth);

            bool matchStart, matchEnd;

            // top, bottom을 수평선에 추가한다.
            horizontals.Add(top);
            horizontals.Add(bottom);

            // left, right를 수직선에 추가한다.
            verticals.Add(left);
            verticals.Add(right);

            // 양 끝점이 모두 vertical과 붙어 있어야 line으로 인정한다. Data Sheet의 경우 단순 밑줄이 마치 Line처럼 잡힘
            foreach (CellLine horizontal in horizontals)
            {
                matchStart = false;
                matchEnd   = false;

                foreach (CellLine vertical in verticals)
                {
                    if (horizontal.StartY >= vertical.StartY && horizontal.EndY <= vertical.EndY)
                    {
                        if (Math.Abs(horizontal.StartX - vertical.StartX) <= lineDistance)
                        {
                            matchStart = true;
                        }
                        else if (Math.Abs(horizontal.EndX - vertical.EndX) <= lineDistance)
                        {
                            matchEnd = true;
                        }
                    }

                    if (matchStart && matchEnd)
                    {
                        horizontalsFinal.Add(horizontal);

                        // top, bottom이 추가 되었기 때문에 체크
                        if (!duplicateDic.ContainsKey(horizontal.Index))
                        {
                            duplicateDic.Add(horizontal.Index, horizontal.Index);
                        }

                        break;
                    }
                }
            }

            // 양 끝점이 모두 horizontal과 붙어 있어야 line으로 인정한다. Data Sheet의 경우 단순 밑줄이 마치 Line처럼 잡힘
            foreach (CellLine vertical in verticals)
            {
                matchStart = false;
                matchEnd   = false;

                foreach (CellLine horizontal in horizontals)
                {
                    if (vertical.StartX >= horizontal.StartX && vertical.EndX <= horizontal.EndX)
                    {
                        if (Math.Abs(vertical.StartY - horizontal.StartY) <= lineDistance)
                        {
                            matchStart = true;
                        }
                        else if (Math.Abs(vertical.EndY - horizontal.EndY) <= lineDistance)
                        {
                            matchEnd = true;
                        }
                    }

                    if (matchStart && matchEnd)
                    {
                        verticalsFinal.Add(vertical);

                        // left, right가 추가 되었기 때문에 체크
                        if (!duplicateDic.ContainsKey(vertical.Index))
                        {
                            duplicateDic.Add(vertical.Index, vertical.Index);
                        }

                        break;
                    }
                }
            }
        }