2016-04-02 16 views
0

Ich versuche, den C++ - Opencv-Code demo drawContour functionality in C# zu konvertieren. Ich habe Probleme mit den Funktionsargumenten contours0 [k] und contours [k] in der Funktion API Cv2.ApproxPolyDP. Ich bekomme Design-Zeit-Compiler-Fehler, der besagt, dass einige der Parameter für Cv2.ApproxPolyDP-Aufruf ungültig sind. Der betreffende Code ist unten aufgeführt. Vielen Dank im Voraus für Ihre Hilfe.Konvertieren von C++ - Code in C# -Code

using System; 
using System.Collections.Generic; 
using System.Windows.Forms; 
using OpenCvSharp; 

class Program 
{ 
    const int w = 500; 
    static int levels = 3; 
    static Point[][] contours; 
    static HierarchyIndex[] hierarchy; 

    static void on_trackbar(int pos, object UseData) 
    { 
     Mat cnt_img = Mat.Zeros(w, w, MatType.CV_8UC3); 
     int _levels = levels - 3; 
     Cv2.DrawContours(cnt_img, contours, _levels <= 0 ? 3 : -1, Scalar.White, 
      3, LineTypes.AntiAlias, hierarchy, Math.Abs(_levels)); 
     Cv2.ImShow("contours", cnt_img); 
    } 

    static void Main() 
    { 
     Mat img = Mat.Zeros(w, w, MatType.CV_8UC1); 

     //Draw 6 faces 
     for(int i = 0; i < 6; i++) 
     { 
      int dx = (i % 2) * 250 - 30; 
      int dy = (i/2)*150; 
      Scalar white = Scalar.White; 
      Scalar black = Scalar.Black; 

      if(i == 0) 
      { 
       for(int j = 0; j <= 10; j++) 
       { 
        double angle = (j + 5) * Math.PI/21; 
        Cv2.Line(img, 
         new Point(Math.Round(dx + 100 + j * 10 - 80 * Math.Cos(angle), 0), 
          Math.Round(dy + 100 - 90 * Math.Sin(angle), 0)), 
         new Point(Math.Round(dx + 100 + j * 10 - 30 * Math.Cos(angle), 0), 
          Math.Round(dy + 100 - 30 * Math.Sin(angle), 0)), 
          white, 1); 
       } 
      } 

      Cv2.Ellipse(img, new Point(dx + 150, dy + 100), new Size(100, 70), 0, 0, 360, white); 
      Cv2.Ellipse(img, new Point(dx + 115, dy + 70), new Size(30, 20), 0, 0, 360, black); 
      Cv2.Ellipse(img, new Point(dx + 185, dy + 70), new Size(30, 20), 0, 0, 360, black); 
      Cv2.Ellipse(img, new Point(dx + 115, dy + 70), new Size(15, 15), 0, 0, 360, white); 
      Cv2.Ellipse(img, new Point(dx + 185, dy + 70), new Size(15, 15), 0, 0, 360, white); 
      Cv2.Ellipse(img, new Point(dx + 115, dy + 70), new Size( 5, 5), 0, 0, 360, black); 
      Cv2.Ellipse(img, new Point(dx + 185, dy + 70), new Size( 5, 5), 0, 0, 360, black); 
      Cv2.Ellipse(img, new Point(dx + 150, dy + 100), new Size(10, 5), 0, 0, 360, black); 
      Cv2.Ellipse(img, new Point(dx + 150, dy + 150), new Size(40, 10), 0, 0, 360, black); 
      Cv2.Ellipse(img, new Point(dx + 27, dy + 100), new Size(20, 35), 0, 0, 360, white); 
      Cv2.Ellipse(img, new Point(dx + 273, dy + 100), new Size(20, 35), 0, 0, 360, white); 
     } 

     //show the faces 
     Cv2.ImShow("image", img); 

     //Extract the contours so that 
     Point[][] contours0; 
     Cv2.FindContours(img, out contours0, out hierarchy, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple); 
     contours = new Point[contours0.Length][]; 

     for(int k = 0; k < contours0.Length; k++) 
      Cv2.ApproxPolyDP(contours0[k], contours[k], 3, true); // compiler error! 

     CvTrackbar Track = new CvTrackbar("levels+3", "contours", 3, 7, on_trackbar); 
     on_trackbar(0, 0); 

     Cv2.WaitKey(); 
    } 
} 
+0

Welche Fehler bekommen Sie? Erkläre die Probleme ein wenig mehr. –

+0

Ein Compiler-Syntaxfehler, der besagt: ungültiger Parametertyp wird an die Funktion Cv2.FindContours (...) und/oder Cv2.ApproxPolyDP (...) übergeben. –

Antwort

0

I erforderlich, um die C++ Zeile zu schreiben "approxPolyDP (Mat (contours0 [k]), Konturen [k], 3, true);" als C# -Linie "Konturen [k] = Cv2.ApproxPolyDP (Konturen0 [k], 3, wahr);"