简体   繁体   English

如何应用OpenCvSharp FitLine

[英]how to apply OpenCvSharp FitLine

I'm using OpenCvSharp with C# and cannot find how to apply FitLine method. 我在C#中使用OpenCvSharp ,找不到如何应用FitLine方法。

public static void Apply(Bitmap bitmap)
{
    using (Mat source = bitmap.ToMat())
    {
        var output = new List<OpenCvSharp.CPlusPlus.Vec4f>(); // I don't know which variable is accepted.
        Cv2.FitLine(source, OutputArray.Create(output), DistanceType.L2, 0, 0.01, 0.01);
        Debug.WriteLine(output);
    }
}

An exception occurred in FitLine method and the error message is as below. FitLine方法中发生异常,错误消息如下。

An exception of type 'OpenCvSharp.OpenCVException' occurred in OpenCvSharp.dll but was not handled in user code OpenCvSharp.dll中发生类型'OpenCvSharp.OpenCVException'的异常,但未在用户代码中处理

Additional information: (is2d || is3d) && (points.depth() == CV_32F || points.depth() == CV_32S) 附加信息:(is2d || is3d)&&(points.depth()== CV_32F || points.depth()== CV_32S)

I changed source using source.ConvertTo(source, MatType.CV_32F, 1.0 / 255.0); 我使用source.ConvertTo(source, MatType.CV_32F, 1.0 / 255.0);更改了source in order to match MatType CV_32F in error message but same error was shown. 为了在错误消息中匹配MatType CV_32F ,但显示了相同的错误。

As second trial, refer to this link , below code throws memory protection exception. 作为第二次尝试,请参考此链接 ,下面的代码引发内存保护异常。

    using (Mat source = bitmap.ToMat())
    {
        var input = new List<OpenCvSharp.CPlusPlus.Point2f>();
        for (int i = 0; i < source.Width; i++)
        {
            for (int j = 0; j < source.Height; j++)
            {
                if (source.At<byte>(i, j) > 0)
                    input.Add(new Point2f(i, j));
            }
        }
        var output = new List<OpenCvSharp.CPlusPlus.Vec4f>();
        Cv2.FitLine(InputArray.Create(input), OutputArray.Create(output), DistanceType.L2, 0, 0.01, 0.01);
        Debug.WriteLine(output);
        return source.ToBitmap();
    }

How can I apply FitLine correctly? 如何正确应用FitLine

I just mocked this test below based on this url http://answers.opencv.org/question/14547/fitline-always-crashes/?answer=14550#post-id-14550 and it seems to work. 我只是在下面根据此网址http://answers.opencv.org/question/14547/fitline-always-crashes/?answer=14550#post-id-14550嘲笑了该测试,它似乎有效。 This example is with OpenCvSharp 3.1. 此示例与OpenCvSharp 3.1一起使用。

List<Point2f> points = new List<Point2f>();
points.Add(new Point2f(3, 3));
points.Add(new Point2f(4, 4));
points.Add(new Point2f(5, 5));
Line2D line = Cv2.FitLine(points, DistanceTypes.L2, 0, 0.01, 0.01);

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

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