OpenCv案例(三):基于OpenCVSharp识别试卷中下划线

需求:识别图片中填空题的下划线,并且高亮显示;

原图:

OpenCv案例(三):基于OpenCVSharp识别试卷中下划线_第1张图片

处理后图像如下:

OpenCv案例(三):基于OpenCVSharp识别试卷中下划线_第2张图片:

 代码部分:

 public Mat DetectLines(Mat src)
        {
            Mat mat = Cv2.ImRead(src, ImreadModes.Grayscale);
            Mat gaussianBlurMat = new Mat();
            Cv2.GaussianBlur(mat, gaussianBlurMat, new Size(3, 3), 15, 0, BorderTypes.Default);
            Cv2.ImShow("gaussianBlurMat", gaussianBlurMat);
            Mat laplanceMat = new Mat();
            //Cv2.Canny(gaussianBlurMat, cannyMat, ThresholdValue, ThresholdValue * 2, 3, false);
            Cv2.Laplacian(gaussianBlurMat, laplanceMat, MatType.CV_8UC1, 3, 3);
            Cv2.ImShow("Laplacian", laplanceMat);
            Mat binaryImg = new Mat();
            Mat morhpImage = new Mat();
            //binaryImage
            Cv2.Threshold(laplanceMat, binaryImg, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
            Cv2.ImShow("binary", binaryImg);

            //morphology operation    new Size(20, 1):一条横线
            Mat kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(60, 1), new Point(-1, -1));
            //开操作 先腐蚀 后膨胀
            Cv2.MorphologyEx(binaryImg, morhpImage, MorphTypes.Open, kernel, new Point(-1, -1));
            //Cv2.MorphologyEx(binaryImg, morhpImage, MorphTypes.Erode, kernel, new Point(-1, -1));
            //Cv2.ImShow("morphImg Erode", morhpImage);
            //Cv2.MorphologyEx(morhpImage, morhpImage, MorphTypes.Dilate, kernel, new Point(-1, -1));
            Cv2.ImShow("morphImg", morhpImage);

            //dilate img
            kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3), new Point(-1, -1));
            Cv2.Dilate(morhpImage, morhpImage, kernel);
            Cv2.ImShow("morph lines", morhpImage);

            LineSegmentPoint[] lines = Cv2.HoughLinesP(morhpImage, 1, Cv2.PI / 180, 30, 20.0, 0);

            Mat retMat = src.Clone();
            Cv2.CvtColor(retMat, retMat, ColorConversionCodes.GRAY2BGR);
            for (int i = 0; i < lines.Length; i++)
            {
                Cv2.Line(retMat, lines[i].P1, lines[i].P2, new Scalar(0, 0, 255), 2, LineTypes.Link8, 0);
            }
            Cv2.ImShow("out", retMat);
            return retMat;
        }

该案例的图像基本处理步骤如上,主要难点是如何去除非直线内容;针对不同图像,需要对上述算法中一些参数进行调整(若不理解部分算法参数含义,查找相关资料学习); 

你可能感兴趣的:(OpenCVSharp视觉学习,C#,opencv,计算机视觉,图像处理,视觉检测,机器学习)