Search code examples
opencvemgucvhough-transform

Standard Hough Lines in EMGU CV


I am in need of using the standard Hough Transformation (instead of the using the HoughLinesBinary method which implements Probabilistic Hough Transform) and have attempted doing so by creating a custom version of the HoughLinesBinary method:

using (MemStorage stor = new MemStorage())
     {
        IntPtr lines = CvInvoke.cvHoughLines2(canny.Ptr, stor.Ptr, Emgu.CV.CvEnum.HOUGH_TYPE.CV_HOUGH_STANDARD, rhoResolution, (thetaResolution*Math.PI)/180, threshold, 0, 0);

        Seq<MCvMat> segments = new Seq<MCvMat>(lines, stor);
        List<MCvMat> lineslist = segments.ToList();
        foreach(MCvMat line in lineslist)
        {
           //Process lines: (rho, theta)
        }
     }

My problem is that I am unsure of what type is the sequence returned. I believe it should be MCvMat, due to reading the documentation that CvMat* is used in OpenCV, which also states that for STANDARD "the matrix must be (the created sequence will be) of CV_32FC2 type"

I am unclear as to what I would need to do to return and process that correct output data from the STANDARD hough lines (i.e. the 2x1 vector for each line giving the rho and theta information).

Any help would be greatly appreciated. Thank you

-Sal


Solution

  • I had the same problem myself a couple of days ago. This is how I solved it using marshalling. Please let me know if you find a simpler solution.

    using (MemStorage stor = new MemStorage())
    {
        IntPtr lines = CvInvoke.cvHoughLines2(canny.Ptr, stor.Ptr, Emgu.CV.CvEnum.HOUGH_TYPE.CV_HOUGH_STANDARD, rhoResolution, (thetaResolution*Math.PI)/180, threshold, 0, 0);
    
        int maxLines = 100;
        for(int i = 0; i < maxLines; i++)
        {
            IntPtr line = CvInvoke.cvGetSeqElem(lines, i);
            if (line == IntPtr.Zero)
            {
                // No more lines
                break;
            }
            PolarCoordinates coords = (PolarCoordinates)System.Runtime.InteropServices.Marshal.PtrToStructure(line, typeof(PolarCoordinates));
    
            // Do something with your Hough lines
        }
    }
    

    with a struct defined as follows:

    public struct PolarCoordinates
    {
        public float Rho;
        public float Theta;
    }