介绍
如何在c#中使用OpenCVSharp实现一个轮廓检测功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
OpenCv提供了函数findContours()用于对物体轮廓进行检测,该函数实现算法是由S。铃木K。安倍于1985年发表的.OpenCVSharp封装了这个函数,有2个参数(轮廓、层次)要做特别的说明。
公共静态孔隙FindContours (InputOutputArray形象,[][]轮廓点, 出HierarchyIndex[]层级,RetrievalModes模式, ContourApproximationModes方法,Point& # 63;抵消=null);
解析:轮廓的类型是点[][],它相当于OpenCV中的<代码> Vector
使用系统; 使用System.Collections.Generic; 使用来; 使用text; 使用System.Threading.Tasks; 使用OpenCvSharp; 使用OpenCvSharp.Extensions; 名称空间OpenCvSharp_03 { 类项目 { 静态void Main (string [] args) { 垫srcImage=Cv2.ImRead (@" D: \ MyData \ circle.jpg"); 垫dst_Image=MyFindContours (srcImage); Cv2.ImShow (“srcImage:“srcImage); Cv2.ImShow (“contours" dst_Image); Cv2.WaitKey (); } 公共静态垫MyFindContours(垫srcImage) {//转化为灰度图 新垫垫src_gray=(); Cv2。CvtColor (srcImage src_gray ColorConversionCodes.RGB2GRAY);//滤波 Cv2。模糊(src_gray src_gray,新的大小(3、3));//精明的边缘检测 新垫垫canny_Image=(); Cv2。精明的(src_gray canny_Image, 100, 200);//获得轮廓 点[][]轮廓; HierarchyIndex[]教主; Cv2。FindContours (canny_Image轮廓,教主,RetrievalModes.Tree, ContourApproximationModes。ApproxSimple,新的点(0,0));//将结果画出并返回结果 垫dst_Image=Mat.Zeros (canny_Image.Size (), srcImage.Type ()); 随机rnd=new随机(); for (int i=0;我& lt;contours.Length;我+ +) { 标量颜色=新的标量(rnd.Next (0255), rnd.Next (0255), rnd.Next (0255); Cv2。DrawContours (dst_Image轮廓,我,颜色,2,线型。Link8,教主); } 返回dst_Image; } } }
我封装好了MyFindContours()这个函数,方便大家调用进行测试
测试结果如下:
看完上述内容,你们掌握如何在c#中使用OpenCVSharp实现一个轮廓检测功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!