欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中OpenCVSharp實(shí)現(xiàn)輪廓檢測(cè)

 更新時(shí)間:2020年11月17日 10:53:24   作者:IT_BOY__  
這篇文章主要介紹了C#中OpenCVSharp實(shí)現(xiàn)輪廓檢測(cè),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

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é)果如下:

ysnshi

這是輪廓的結(jié)果圖

到此這篇關(guān)于C#中OpenCVSharp實(shí)現(xiàn)輪廓檢測(cè)的文章就介紹到這了,更多相關(guān)C# OpenCVSharp輪廓檢測(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論