将C ++代码转换为C#代码

时间:2016-04-02 06:17:18

标签: opencv opencvsharp

我试图将C ++ opencv代码demo drawContour functionality转换为C#。我在函数API Cv2.ApproxPolyDP中遇到了函数参数contours0 [k]和contours [k]的问题。我得到设计时编译器错误,说明某些参数对于Cv2.ApproxPolyDP调用无效。有问题的代码如下所示。在此先感谢您的帮助。

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(); 
    }
}

1 个答案:

答案 0 :(得分:0)

我需要编写C ++行&#34; approxPolyDP(Mat(contours0 [k]),contours [k],3,true);&#34;如C#line&#34; contours [k] = Cv2.ApproxPolyDP(contours0 [k],3,true);&#34;