0

我正在尝试将 C++ opencv 代码演示 drawContour 功能转换为 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(); 
    }
}
4

1 回答 1

0

我需要编写 C++ 行“approxPolyDP(Mat(contours0[k]), contours[k], 3, true);” 作为 C# 行“contours[k] = Cv2.ApproxPolyDP(contours0[k], 3, true);”

于 2016-04-07T05:07:20.150 回答