C# OpenCvSharp實(shí)現(xiàn)去除文字中的線條
效果
處理步驟
1、Cv2.Resize圖片放大
2、Cv2.CvtColor轉(zhuǎn)灰度圖
3、Cv2.Threshold二值化
4、Cv2.HoughLinesP找直線
5、利用找出的直線制作一個(gè)mask圖
6、Cv2.Inpaint+制作的mask圖進(jìn)行圖像修補(bǔ)
7、逐像素處理文字邊緣的顏色
8、Cv2.BitwiseNot黑白反色
中間過(guò)程效果
項(xiàng)目
VS2022
.net framework 4.8
OpenCvSharp 4.8
Microsoft.ML.OnnxRuntime 1.16.2
代碼
/// <summary> /// Restores the selected region in an image using the region neighborhood. /// </summary> /// <param name="src">Input 8-bit, 16-bit unsigned or 32-bit float 1-channel or 8-bit 3-channel image.</param> /// <param name="inpaintMask">Inpainting mask, 8-bit 1-channel image. Non-zero pixels indicate the area that needs to be inpainted.</param> /// <param name="dst">Output image with the same size and type as src.</param> /// <param name="inpaintRadius">Radius of a circular neighborhood of each point inpainted that is considered by the algorithm.</param> /// <param name="flags">Inpainting method that could be cv::INPAINT_NS or cv::INPAINT_TELEA</param> public static void Inpaint(InputArray src, InputArray inpaintMask,OutputArray dst, double inpaintRadius, InpaintMethod flags)
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()); } } }
以上就是C# OpenCvSharp實(shí)現(xiàn)去除文字中的線條的詳細(xì)內(nèi)容,更多關(guān)于C# OpenCvSharp去除文字中線條的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
c# 以類名為參創(chuàng)建父類相同的類的實(shí)例代碼
下面小編就為大家?guī)?lái)一篇c# 以類名為參創(chuàng)建父類相同的類的實(shí)例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01c#實(shí)現(xiàn)輸出的字符靠右對(duì)齊的示例
下面小編就為大家分享一篇c#實(shí)現(xiàn)輸出的字符靠右對(duì)齊的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12C#事件中的兩個(gè)參數(shù)詳解(object sender,EventArgs e)
這篇文章主要介紹了C#事件中的兩個(gè)參數(shù)詳解(object sender,EventArgs e),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09