简体   繁体   English

如何在 OpenCVSharp 中使用 OutputArray?

[英]How do I use OutputArray in OpenCVSharp?

I am trying to use the function Cv2.ApproxPolyDP in OpenCVSharp.我想使用的功能Cv2.ApproxPolyDP在OpenCVSharp。 It should return a List<Point> ( Vector<Point> in C++) for the approxCurve parameter.应该为 approxCurve 参数返回一个List<Point> (C++ 中的Vector<Point> )。 This parameter is of type OutputArray .此参数的类型为OutputArray I can't seem to make heads or tails of that, and documentation is sparse.我似乎无法对此做出正面或反面,而且文档很少。

There seems to be no constructor for an OutputArray nor can I convert one to a List<Point> .似乎没有OutputArray构造函数,我也不能将其转换为List<Point> Can someone let me know (preferably with code) how I can call Cv2.ApproxPolyDP and get a List<Point> back for the approxCurve parameter?有人可以让我知道(最好是代码)我如何调用Cv2.ApproxPolyDP并为 approxCurve 参数获取一个List<Point>

Thanks!谢谢!

The current version says "The type should match the type of the input curve".当前版本说“类型应该与输入曲线的类型相匹配”。

You should be able to convert the OutputArray to a Mat, and then use ToArray() to get the list of points.您应该能够将 OutputArray 转换为 Mat,然后使用 ToArray() 获取点列表。

Does this work?这行得通吗?

OutputArray output;
Cv2.ApproxPolyDP(..., output, ...);
var list = output.ToMat().ToArray();

Edit: Just realised ToArray() is an extension method we wrote.编辑:刚刚意识到 ToArray() 是我们编写的扩展方法。 This may help (without the conversion to CV_64F).这可能会有所帮助(无需转换为 CV_64F)。

    public static double[,] To2DArray(this Mat mat)
    {
        mat.ConvertTo(mat, MatType.CV_64F);
        if (mat.Rows <= 1)
        {
            m_Log.WarnFormat("1D Mat converted to 2D array, size={0}x{1}", mat.Rows, mat.Cols);
        }
        double[,] arr = new double[mat.Rows, mat.Cols];
        mat.GetArray(0, 0, arr);

        return arr;
    }

    public static double[] ToArray(this Mat mat)
    {
        double[,] array2d = mat.To2DArray();

        int rowCount = array2d.GetLength(0);

        double[] array = new double[rowCount];
        for (int i = 0; i < rowCount; i++)
        {
            array[i] = array2d[i, 0];
        }
        return array;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM