已解决
C# OpenCvSharp 去除文字中的线条
来自网友在路上 168868提问 提问时间:2023-11-10 03:06:57阅读次数: 68
最佳答案 问答题库688位专家为你答疑解惑
效果
中间过程效果
项目
代码
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());}}
}
下载
Demo下载
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"C# OpenCvSharp 去除文字中的线条":http://eshow365.cn/6-36816-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!