2016-05-23 4 views
0

私はemgucvを使って基本的な幾何学的形状を特定するプロジェクトを行っています。私は五角形を識別するために次のコードを使用しています。しかし私がそれを実行すると、五角形と円も表示されます。私は五角形だけを出力したい。私は既に円、長方形、三角形を特定しています。このコードを使って五角形だけを表示する方法は?
ありがとうございます!あなたが見つかった輪郭(ギザギザ)エイリアスされ、任意の多角形近似(「スムージング」)は、あまりにも多くの輪郭を変更することができるので、形状を検出するために、輪郭のセグメントの数に依存しないでくださいEmguCvでペンタゴンと六角形を検出する方法

public Image<Bgr, Byte> My_Image; 
    public Image<Gray, Byte> grayImage; 
    public Image<Gray, Byte> cannyEdges;  

    grayImage = My_Image.Convert<Gray, byte>(); 



public void CannyEdgeDetection() 
     { 
      try 
      { 
       double cannyThresholdLinking = 120.0; 
       cannyEdges = grayImage.Canny(cannyThreshold, cannyThresholdLinking); 
       lines = cannyEdges.HoughLinesBinary(
        1, //Distance resolution in pixel-related units 
        Math.PI/45.0, //Angle resolution measured in radians. 
        20, //threshold 
        10, //min Line width 
        10 //gap between lines 
        )[0]; //Get the lines from the first channel 
      } 
      catch (Exception ex) 
      { 

       throw ex; 
      } 

     } 

    public void DetectPentagons() 
      { 

       try 
       { 

        CannyEdgeDetection(); 

        using (MemStorage storage = new MemStorage()) 
         for (
          Contour<Point> contours = cannyEdges.FindContours(
           Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, 
           Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL, 
           storage); 
          contours != null; 
          contours = contours.HNext) 
         { 
          Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage); 

          if (currentContour.Area > 100) 
          { 
           if (currentContour.Total == 5) 
           { 

            grayImage.Draw(contours, new Bgr(Color.Red), 10); 
            pictureBox2.Image = obj.grayImage.Bitmap; 
           } 
          } 
         } 

       } 

       catch (Exception ex) 
       { 
        throw ex; 
       } 

      } 
+0

サイドノート: 'catch(Exception ex) { throw ex; } 'は、スタックトレースを消去するため、役に立たないよりも悪いです。意味的に例外を処理しない場合は、try/catchブロックを完全に削除するだけです。ブレークポイントを設定したりログに記録したりする場合は、 'throw ex'ではなく' throw; 'を呼び出すだけで、スタックトレースはそのまま残ります。 – Blorgbeard

+0

それを得ました。他の図形の代わりにこのコードを使って五角形だけを表示する方法を知っていますか? – RuwanH

答えて

0

図形を検出するために使用できる機能の1つはcircularityです。円形度は、形状がどれくらい円に近いかを決定する要素です。

circularity ~= 1, shape is a circle 
circularity < 1, shape is convex 
circularity > 1, shape is concave 

それのための式は次のとおりです。

circularity = (4 * PI * contour_area)/contour_perimeter^2 

すべての形状がそれ自身の予想される真円度値を持ち、例えば、正方形は0.7853の真円度を持つべきである、とあなたが輪郭をフィルタリングするために、この値を使用することができます四角形の場合FindContoursメソッドから利用可能なすべての値を取得する必要があります。

関連する問題