如何在 OpenCV 和 C# 中进行霍夫变换?

How can I do Hough Transform in OpenCV and C#?

关于此 Opencv Tutorial,以下 C++ 代码片段:

vector<Vec4i> lines;
           // Find hough lines 
    HoughLinesP(edges, lines, 1, CV_PI / 180, 100, 100, 10);

           // Prepare blank mat with same sizes as image
    Mat Blank(image.rows, image.cols, CV_8UC3, Scalar(0, 0, 0));

       // Draw lines into image and Blank images
        for (size_t i = 0; i < lines.size(); i++)
         {
        Vec4i l = lines[i];

        line(image, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 0), 2, CV_AA);

         line(Blank, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255, 255, 255), 2, CV_AA);

         }

已被翻译成 C# 如下:

private void openFileDialogButton_Click(object sender, EventArgs e)
{
    try
    {
        const string filename = @"E:\___MSc in Computer Systems & Network\EMSC1,2,3\lena.png";

        Mat image = Cv2.ImRead(filename, LoadMode.GrayScale);

        Mat edges = new Mat();

        Cv2.Canny(image, edges, 95, 100);

        Cv2.ImWrite("edges.jpg", edges);

        Mat dx = new Mat();
        Mat dy = new Mat();

        Cv2.Sobel(edges, dx, MatType.CV_32F, 1, 0);
        Cv2.Sobel(edges, dy, MatType.CV_32F, 0, 1);

        Cv2.ImWrite("dx.jpg", dx);
        Cv2.ImWrite("dy.jpg", dy);

        Mat linesssssss = new Mat();

        Cv.HoughLines2((CvArr)linesssssss.ToIplImage(),
            (CvMat)edges,
            HoughLinesMethod.Standard,
            1,
            Math.PI / 180,
            100, 100, 10);
        //Cv2.HoughLinesP(edges, lines, 1, Math.PI / 180, 100, 100, 10);

        List<Vec4i> lines = IntPtrToList(linesssssss.Data);

        Mat Blank = new Mat(image.Rows, image.Cols, MatType.CV_8UC3, new Scalar(0, 0, 0));

        for (int i = 0; i < lines.Count; i++)
        {
            Vec4i l = lines[i];

            Cv2.Line(image, new OpenCvSharp.CPlusPlus.Point(l[0], l[1]), new OpenCvSharp.CPlusPlus.Point(l[2], l[3]), new Scalar(0, 0, 0), 2, Cv.AA);

            Cv2.Line(Blank, new OpenCvSharp.CPlusPlus.Point(l[0], l[1]), new OpenCvSharp.CPlusPlus.Point(l[2], l[3]), new Scalar(255, 255, 255), 2, Cv.AA);

        }

        //Cv2.ImWrite("houg.jpg", image);
        //Cv2.ImShow("Edges", image);
        //Cv2.ImWrite("houg2.jpg", Blank);
        //Cv2.ImShow("Edges Structure", Blank);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

不幸的是,它似乎不起作用。它给出了以下异常:

Nonpositive cols or rows

这是文章的原图:

基于 Github 的 sample code 对于 HoughLinesP:

    static void Main(string[] args)
    {
        string filename = "Alliance.jpg";

        Mat imageIn = Cv2.ImRead(filename, ImreadModes.GrayScale).Resize(new Size(800, 600));
        Mat edges = new Mat();

        Cv2.Canny(imageIn, edges, 95, 100);

        //HoughLinesP
        LineSegmentPoint[] segHoughP = Cv2.HoughLinesP(edges, 1, Math.PI / 180, 100, 100, 10);
        
        Mat imageOutP = imageIn.EmptyClone();

        foreach (LineSegmentPoint s in segHoughP)
            imageOutP.Line(s.P1, s.P2, Scalar.White, 1, LineTypes.AntiAlias, 0);

        using (new Window("Edges", WindowMode.AutoSize, edges))
        using (new Window("HoughLinesP", WindowMode.AutoSize, imageOutP))
        {
            Window.WaitKey(0);
        }
    }