OpenCvSharp函数:Dilate膨胀、GetStructuringElement获取形态操作的结构元素、Erode腐蚀
Dilate膨脹
函數(shù)說明:用特定的結(jié)構(gòu)元素膨脹圖像。膨脹可以看成是最大值濾波,即用最大值替換中心像素點(diǎn)。該函數(shù)就地模式,可以指定迭代次數(shù),多通道圖像的話,每個通道分開處理。
用3x3矩形結(jié)構(gòu)元素膨脹
//函數(shù)原型 void Dilate(InputArray src,OutputArray dst,InputArray? element,Point? anchor = null,int iterations = 1,BorderTypes borderType = BorderTypes.Constant,Scalar? borderValue = null)參數(shù) | 說明 |
InputArray src | 輸入圖像,通道數(shù)可以任意,類型必須為CV_8U,CV_16U, CV_16S, CV_32F或CV_64F。 |
OutputArray dst | 輸出圖像,與輸入圖像有相同的大小、類型和通道數(shù)。(支持就地模式) |
InputArray? element | 用于膨脹的結(jié)構(gòu)元素。為null或空Mat時,默認(rèn)使用3x3矩形結(jié)構(gòu)元素。可通過GetStructuringElement函數(shù)生成。 |
Point? anchor = null | 錨點(diǎn),結(jié)構(gòu)元素的錨點(diǎn)。默認(rèn)為(-1,-1),表示結(jié)構(gòu)元素的中心點(diǎn) |
int iterations = 1 | 膨脹的次數(shù) |
BorderTypes borderType = BorderTypes.Constant | 邊界填充方式。不支持Wrap方式 |
Scalar? borderValue = null | 當(dāng)邊界填充方式為Constant時的填充值,默認(rèn)是Scalar.All(double.MaxValue) |
GetStructuringElement獲取形態(tài)操作的結(jié)構(gòu)元素
函數(shù)說明:返回一個指定大小、形狀的結(jié)構(gòu)元素,用于膨脹、腐蝕或其它形態(tài)操作。
//函數(shù)原型1 Mat GetStructuringElement(MorphShapes shape,Size ksize)//函數(shù)原型2 Mat GetStructuringElement(MorphShapes shape, Size ksize, Point anchor)參數(shù) | 說明 |
MorphShapes shape | 形態(tài)形狀:Rect矩形、Cross十字、Ellipse橢圓 |
Size ksize | 元素大小 |
Point anchor | 元素內(nèi)的錨定位置。默認(rèn)值(-1,-1)表示錨點(diǎn)位于中心。注意,只有十字形元素的形狀取決于錨點(diǎn)位置。在其他情況下,錨只是調(diào)節(jié)形態(tài)學(xué)操作的結(jié)果被轉(zhuǎn)移了多少。 |
返回值Mat | 結(jié)構(gòu)元素矩陣 |
Erode腐蝕
函數(shù)說明:用特定的結(jié)構(gòu)元素腐蝕圖像。腐蝕可以看成是最小值濾波,即用最小值替換中心像素點(diǎn)。
該函數(shù)就地模式,可以指定迭代次數(shù),多通道圖像的話,每個通道分開處理。
用3x3矩形結(jié)構(gòu)元素腐蝕
//函數(shù)原型 void Erode(InputArray src,OutputArray dst,InputArray? element,Point? anchor = null,int iterations = 1,BorderTypes borderType = BorderTypes.Constant,Scalar? borderValue = null)參數(shù) | 說明 |
InputArray src | 輸入圖像,通道數(shù)可以任意,類型必須為CV_8U,CV_16U, CV_16S, CV_32F或CV_64F。 |
OutputArray dst | 輸出圖像,與輸入圖像有相同的大小、類型和通道數(shù)。 (支持就地模式) |
InputArray? element | 用于腐蝕的結(jié)構(gòu)元素。為null或空Mat時,默認(rèn)使用3x3矩形結(jié)構(gòu)元素。可通過GetStructuringElement函數(shù)生成。 |
Point? anchor = null | 錨點(diǎn),結(jié)構(gòu)元素的錨點(diǎn)。 默認(rèn)為(-1,-1),表示結(jié)構(gòu)元素的中心點(diǎn) |
int iterations = 1 | 腐蝕次數(shù) |
BorderTypes borderType = BorderTypes.Constant | 邊界填充方式。 不支持Wrap方式 |
Scalar? borderValue = null | 當(dāng)邊界填充方式為Constant時的填充值,默認(rèn)是Scalar.All(double.MaxValue) |
圖像示例
原圖
膨脹與腐蝕
定位下劃線位置
源碼示例
public void Run() {TestDilateAndErode();MorphologyDemo();LocateGapFilling(); }/// <summary> /// 定位下劃線 /// </summary> /// <exception cref="Exception"></exception> private void LocateGapFilling() {using (var src = Cv2.ImRead(ImagePath.GapFilling, ImreadModes.Grayscale)) {if (src.Empty()) throw new Exception($"圖像打開有誤:{ImagePath.GapFilling}");Cv2.Threshold(src, src, 0, 255, ThresholdTypes.Otsu | ThresholdTypes.BinaryInv);Cv2.ImShow("GapFilling", src);using var dst = new Mat();var element = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 1));//可消除部分垂直線Cv2.Erode(src, dst, element);ShowMat("Location", element, dst);Cv2.WaitKey();Cv2.DestroyAllWindows();} }/// <summary> /// 膨脹、腐蝕 /// </summary> /// <exception cref="Exception"></exception> private void MorphologyDemo() {using (var src = Cv2.ImRead(ImagePath.Morphology, ImreadModes.Grayscale)) {if (src.Empty()) throw new Exception($"圖像打開有誤:{ImagePath.Morphology}");Cv2.Threshold(src, src, 0, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary);Cv2.ImShow("gray", src);using var dst = new Mat();var element = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 15));//膨脹Cv2.Dilate(src, dst, element);ShowMat("Dilate Rect", element, dst);//腐蝕Cv2.Erode(src, dst, element);ShowMat("Erode Rect", element, dst);Cv2.WaitKey();Cv2.DestroyAllWindows();} }private void ShowMat(string winNameFix, Mat element, Mat dst) {Cv2.ImShow($"{winNameFix},{element.Size()}", dst); }/// <summary> /// 演示Dilate與Erode,細(xì)看其中的不同。 /// 注意:一幅圖像膨脹后再腐蝕的圖像,不一定與原圖一樣。 /// </summary> private void TestDilateAndErode() {using (var mat = new Mat(11, 15, MatType.CV_8UC1, new byte[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0})) {Helper.Dump(mat);var element = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));//膨脹Cv2.Dilate(mat, mat, null, iterations: 2);Console.WriteLine("Dilate X 2");Dump(mat);using var dst = new Mat();//加上空白邊緣Cv2.CopyMakeBorder(mat, dst, 1, 1, 1, 1, BorderTypes.Constant, new Scalar(0, 0, 0));Cv2.Erode(mat, mat, null);Console.WriteLine("The first time of Erode");Dump(mat);Cv2.Erode(mat, mat, null);//可連接腐蝕多次//Cv2.Erode(mat,mat,null,iterations: 2);Console.WriteLine("The second time of Erode");Dump(mat);//加了空白邊緣的圖像Console.WriteLine("dst");Dump(dst);//對比前面的腐蝕結(jié)果//腐蝕Cv2.Erode(dst, dst, null);Console.WriteLine("ErodeX1");Dump(dst);Cv2.Erode(dst, dst, null);Console.WriteLine("ErodeX2");Dump(dst);} }#region Dump /// <summary> /// 控制臺顯示矩陣 /// </summary> /// <param name="mat">待顯示的矩陣</param> /// <param name="dumpChannel">要顯示通道</param> public static void Dump(Mat mat, FormatType formatType = FormatType.Default) {Dump(mat, new OpenCvSharp.Range(0, mat.Channels() - 1), formatType); } /// <summary> /// 控制臺顯示矩陣 /// </summary> /// <param name="mat">等顯示的矩陣</param> /// <param name="channelRange">要顯示的通道范圍</param> public static void Dump(Mat mat, OpenCvSharp.Range channelRange, FormatType formatType = FormatType.Default) {var nChannels = mat.Channels();if (nChannels == 1) {Console.WriteLine(mat.Dump(formatType));return;}var splits = mat.Split();for (int c = channelRange.Start; c <= channelRange.End; c++) {if (c < nChannels) {Console.WriteLine(splits[c].Dump(formatType));}else {Console.WriteLine($"通道值{c}必須小于{nChannels}");}} } #endregionOpenCvSharp函數(shù)示例(目錄)
參考
https://docs.opencv.org/
https://edu.csdn.net/learn/38286/608282
總結(jié)
以上是生活随笔為你收集整理的OpenCvSharp函数:Dilate膨胀、GetStructuringElement获取形态操作的结构元素、Erode腐蚀的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 渗透测试之防火墙
- 下一篇: 如本科技上海分公司乔迁新址,加速长三角地