可以用Cv2.MorphologyEx 形态学处理。
也可以用Cv2.Erode 腐蚀 或 Cv2.Dilate 膨胀。
using OpenCvSharp; using OpenCvSharp.Extensions; using DlibDotNet; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace app { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } /// <summary> /// 腐蚀 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnErode_Click(object sender, EventArgs e) { Mat src = Cv2.ImRead(@"img\erode.bmp"); Mat dest = new Mat(); Mat kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(5, 5), new OpenCvSharp.Point(-1, -1)); // Erode 腐蚀 //Cv2.MorphologyEx(src, dest, MorphTypes.Erode, kernel, new OpenCvSharp.Point(-1, -1), 5); Cv2.Erode(src, dest, kernel, new OpenCvSharp.Point(-1, -1), 5); Cv2.ImShow("原图", src); Cv2.ImShow("腐蚀", dest); } /// <summary> /// 膨胀 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDilate_Click(object sender, EventArgs e) { Mat src = Cv2.ImRead(@"img\dilation.bmp"); Mat dest = new Mat(); Mat kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(5, 5), new OpenCvSharp.Point(-1, -1)); // Dilate 膨胀 //Cv2.MorphologyEx(src, dest, MorphTypes.Dilate, kernel, new OpenCvSharp.Point(-1, -1), 1); Cv2.Dilate(src, dest, kernel, new OpenCvSharp.Point(-1, -1), 1); Cv2.ImShow("原图", src); Cv2.ImShow("膨胀", dest); } } }