OpenCVSharp - 利用形态学操作去除水平线

OpenCVSharp 跑一遍OpenCV官方教程(全为手敲代码,如有雷同都是我的错)

利用形态学操作去除水平线

OpenCV教程链接:https://docs.opencv.org/4.5.0/dd/dd7/tutorial_morph_lines_detection.html

核心语句

  • erode()
  • dilate()
  • getStructuringElement()
using System;
using OpenCvSharp;

namespace ConsoleApp1
{
    class tutorial7 : ITutorial
    {

        public void Run()
        {

            Mat src = Cv2.ImRead("notes.png", ImreadModes.Color);

            if (src.Empty())
            {
                Console.WriteLine("Could not open or find the image!\n");
            }
            // 显示源图像
            Cv2.ImShow("src", src);

            // 如果不是灰度图则先将彩色图转换为灰度图
            Mat gray = new Mat();
            if (src.Channels() == 3)
            {
                Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);
            }
            else
            {
                gray = src;
            }
            // 显示灰度图
            show_wait_destroy("gray", gray);
            // 先将灰度图按位反转(~),再应用自适应阈值处理
            Mat bw = new Mat();
            Cv2.AdaptiveThreshold(~gray, bw, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 15, -2);

            // 显示二值图
            show_wait_destroy("binary", bw);

            // 拷贝两个变量以保存去除水平线和垂直线的图片
            Mat horizontal = bw.Clone();
            Mat vertical = bw.Clone();

            // 设置水平方向大小
            int horizontal_size = horizontal.Cols / 30;
            // 生成结构元素
            Mat horizontalStructure = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(horizontal_size, 1));
            // 腐蚀、膨胀操作
            Cv2.Erode(horizontal, horizontal, horizontalStructure, new Point(-1, -1));
            Cv2.Dilate(horizontal, horizontal, horizontalStructure, new Point(-1, -1));
            // 显示结果
            show_wait_destroy("horizontal", horizontal);

            // 设置垂直方向大小
            int vertical_size = vertical.Rows / 30;
            // 生成结构元素
            Mat verticalStructure = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, vertical_size));
            // 腐蚀、膨胀操作
            Cv2.Erode(vertical, vertical, verticalStructure, new Point(-1, -1));
            Cv2.Dilate(vertical, vertical, verticalStructure, new Point(-1, -1));

            // 显示结果
            show_wait_destroy("vertical", vertical);

            // 反转图像
            Cv2.BitwiseNot(vertical, vertical);
            show_wait_destroy("vertical_bit", vertical);
            // 按照以下步骤提取边缘并模糊图像
            // 1. 提取边缘
            // 2. 膨胀(边缘)
            // 3. 拷贝到 smooth变量
            // 4. 模糊处理 smooth
            // 5. 拷贝边缘部分

            // Step 1
            Mat edges = new Mat();
            Cv2.AdaptiveThreshold(vertical, edges, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 3, -2);
            show_wait_destroy("edges", edges);
            // Step 2
            Mat kernel = Mat.Ones(2, 2, MatType.CV_8UC1);
            Cv2.Dilate(edges, edges, kernel);
            show_wait_destroy("dilate", edges);
            // Step 3
            Mat smooth = new Mat();
            vertical.CopyTo(smooth);
            // Step 4
            Cv2.Blur(smooth, smooth, new Size(2, 2));
            // Step 5
            smooth.CopyTo(vertical, edges);
            // Show final result
            show_wait_destroy("smooth - final", vertical);
        }

        void show_wait_destroy(string winname, Mat img)
        {
            Cv2.ImShow(winname, img);
            Cv2.MoveWindow(winname, 500, 0);
            Cv2.WaitKey(0);
            Cv2.DestroyWindow(winname);
        }
    }
}

 

你可能感兴趣的:(OpenCVSharp,opencv)