C# OpenCvSharp 去除文字中的线条

目录

效果

处理步骤

中间过程效果

项目

代码


 

效果

图片

处理步骤

1、Cv2.Resize图片放大

2、Cv2.CvtColor转灰度图

3、Cv2.Threshold二值化

4、Cv2.HoughLinesP找直线

5、利用找出的直线制作一个mask图

6、Cv2.Inpaint+制作的mask图进行图像修补

7、逐像素处理文字边缘的颜色

8、Cv2.BitwiseNot黑白反色

中间过程效果

图片

图片

项目

图片

代码

using OpenCvSharp;
using System;
using System.Drawing;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
 
namespace OpenCvSharp_Demo
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
 
        string image_path = "";
 
        private void Form1_Load(object sender, EventArgs e)
        {
            image_path = "1.png";
            pictureBox1.Image = new Bitmap(image_path);
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
 
            Mat image = new Mat(image_path);
            Cv2.Resize(image, image, new OpenCvSharp.Size(), 5, 5, InterpolationFlags.Cubic);
            Cv2.ImShow("image", image);
 
            Mat gray = new Mat();
            Cv2.CvtColor(image, gray, ColorConversionCodes.BGR2GRAY);
            Cv2.ImShow("gray", gray);
 
            Mat binary = new Mat();
            Cv2.Threshold(gray, binary, 70, 255, ThresholdTypes.BinaryInv);
            Cv2.ImShow("binary", binary);
 
            LineSegmentPoint[] res = Cv2.HoughLinesP(binary,
                rho: 1,
                theta: Math.PI / 180.0,
            threshold: 100,
                minLineLength: image.Cols - 50,
                maxLineGap: 50);
 
            //Console.WriteLine("res.Length:" + res.Length);
 
            Mat mask = Mat.Zeros(image.Rows, image.Cols, MatType.CV_8UC1);
            for (int i = 0; i < res.Length; i++)
            {
                Scalar color = Scalar.RandomColor();
                Cv2.Line(mask, res[i].P1, res[i].P2,
                    color: Scalar.White,
                    thickness: 11,
                    lineType: LineTypes.Link8);
            }
            Cv2.ImShow("mask", mask);
 
            Mat dst = new Mat();
            Cv2.Inpaint(binary, mask, dst, 5, InpaintMethod.Telea);
            Cv2.ImShow("Inpaint", dst);
 
            for (int r = 0; r < dst.Rows; r++)
            {
                for (int c = 0; c < dst.Cols; c++)
                {
                    byte p = dst.At<byte>(r, c);
                    if (p > 50)
                    {
                        dst.Set<byte>(r, c, 255);
                    }
                    else
                    {
                        dst.Set<byte>(r, c, 0);
                    }
                }
            }
 
            Cv2.ImShow("black background", dst);
            //黑白反色
            Cv2.BitwiseNot(dst, dst);
 
            Cv2.ImShow("dst", dst);
            Cv2.ImWrite("dst.jpg", dst);
 
            pictureBox2.Image = new Bitmap(dst.ToMemoryStream());
 
        }
 
    }
}

引入地址 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值