C#中OpenCVSharp實(shí)現(xiàn)輪廓檢測(cè)
OpenCv提供了函數(shù) findContours()用于對(duì)物體輪廓進(jìn)行檢測(cè),該函數(shù)實(shí)現(xiàn)算法是由S.suzuki K.Abe于1985年發(fā)表的。OpenCVSharp封裝了這個(gè)函數(shù),有2個(gè)參數(shù)(contours,hierarchy)要做特別的說(shuō)明。
public static void FindContours(InputOutputArray image, out Point[][] contours, out HierarchyIndex[] hierarchy, RetrievalModes mode, ContourApproximationModes method, Point? offset = null);
解析:contours 的類型是Point[][],它相當(dāng)于OpenCV中的Vector<Vector<Point>> contours
,存儲(chǔ)多個(gè)輪廓,每個(gè)輪廓是由若干個(gè)點(diǎn)組成,可以在該函數(shù)前聲明Point[][] contours;
,在C#中沒(méi)有賦值的變量在用的時(shí)候是不允許的,因?yàn)樗禽敵龅慕Y(jié)果,可以不需要給它new空間,但必須在函數(shù)的參數(shù)中聲明是out;參數(shù)hierarchy為包含圖像拓?fù)浣Y(jié)構(gòu)的信息,它是HierarchyIndex[]類型,這是輸入的結(jié)果,同樣要在函數(shù)的參數(shù)中聲明為out。具體代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using OpenCvSharp; using OpenCvSharp.Extensions; namespace OpenCvSharp_03 { class Program { static void Main(string[] args) { Mat srcImage = Cv2.ImRead(@"D:\MyData\circle.jpg"); Mat dst_Image = MyFindContours(srcImage); Cv2.ImShow("srcImage:", srcImage); Cv2.ImShow("contours", dst_Image); Cv2.WaitKey(); } public static Mat MyFindContours(Mat srcImage) { //轉(zhuǎn)化為灰度圖 Mat src_gray = new Mat(); Cv2.CvtColor(srcImage, src_gray, ColorConversionCodes.RGB2GRAY); //濾波 Cv2.Blur(src_gray, src_gray, new Size(3, 3)); //Canny邊緣檢測(cè) Mat canny_Image = new Mat(); Cv2.Canny(src_gray, canny_Image, 100, 200); //獲得輪廓 Point[][] contours; HierarchyIndex[] hierarchly; Cv2.FindContours(canny_Image,out contours,out hierarchly, RetrievalModes.Tree,ContourApproximationModes.ApproxSimple,new Point(0,0)); //將結(jié)果畫(huà)出并返回結(jié)果 Mat dst_Image = Mat.Zeros(canny_Image.Size(),srcImage.Type()); Random rnd = new Random(); for (int i = 0; i < contours.Length; i++) { Scalar color = new Scalar(rnd.Next(0,255),rnd.Next(0,255),rnd.Next(0,255)); Cv2.DrawContours(dst_Image, contours, i, color, 2,LineTypes.Link8, hierarchly); } return dst_Image; } } }
我封裝好了MyFindContours()這個(gè)函數(shù),方便大家調(diào)用進(jìn)行測(cè)試
測(cè)試結(jié)果如下:
到此這篇關(guān)于C#中OpenCVSharp實(shí)現(xiàn)輪廓檢測(cè)的文章就介紹到這了,更多相關(guān)C# OpenCVSharp輪廓檢測(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
動(dòng)態(tài)改變gridview列寬度函數(shù)分享
通常用GridView綁定datatable,由于需要?jiǎng)討B(tài)綁定到不同的datatable所以需要?jiǎng)討B(tài)調(diào)整GridView的寬度。寫(xiě)了這個(gè)函數(shù)實(shí)現(xiàn)該功能2014-01-01visio二次開(kāi)發(fā)--判斷文檔是否已發(fā)生變化(變化就加星號(hào)*)
最近做一個(gè)故障樹(shù)診斷的項(xiàng)目,用visio二次開(kāi)發(fā),可以同時(shí)打開(kāi)多個(gè)繪制的故障樹(shù)圖形文檔。項(xiàng)目中需要實(shí)現(xiàn)判斷文檔是否發(fā)生變化,這是很多編輯軟件的基本功能,變化了就加個(gè)星號(hào)*2013-04-04C#采用mouse_event函數(shù)實(shí)現(xiàn)模擬鼠標(biāo)功能
這篇文章主要介紹了C#模擬鼠標(biāo)點(diǎn)擊小功能,通過(guò)代碼向大家做分析,需要的朋友可以參考下2015-07-07解決C#獲取鼠標(biāo)相對(duì)當(dāng)前窗口坐標(biāo)的實(shí)現(xiàn)方法
本篇文章是對(duì)在C#中獲取鼠標(biāo)相對(duì)當(dāng)前窗口坐標(biāo)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C#使用foreach語(yǔ)句遍歷二維數(shù)組的方法
這篇文章主要介紹了C#使用foreach語(yǔ)句遍歷二維數(shù)組的方法,實(shí)例分析了C#遍歷數(shù)組的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04