Search code examples
opencvhough-transform

OpenCV Hough strongest lines


Do the HoughLines or HoughLinesP functions in OpenCV return the list of lines in accumulator order like the HoughCircles function does? I would like to know the ordering of lines. It would also be very handy to get a the accumulator value for the lines so an intelligent and adaptive threshold could be used instead of a fixed one. Are either the ordering or the accumulator value available without rewriting OpenCV myself?


Solution

  • HoughTransform orders lines descending by number of votes. You can see the code here

    However, the vote count is lost as the function returns - the only way to have it is to modify OpenCV.

    The good news is that is not very complicated - I did it myself once. It's a metter of minutes to change the output from vector< Vec2f > to vector< Vec3f > and populate the last param with vote count.

    Also, you have to modify CvLinePolar to add the third parameter - hough is implemented in C, and there is a wrapper over it in C++, so you have to modify both the implementation and the wrapper.

    The main code to modify is here

     for( i = 0; i < linesMax; i++ )
     {
            CvLinePolar line;
            int idx = sort_buf[i];
            int n = cvFloor(idx*scale) - 1;
            int r = idx - (n+1)*(numrho+2) - 1;
            line.rho = (r - (numrho - 1)*0.5f) * rho;
            line.angle = n * theta;
    
            // add this line, and a field voteCount to CvLinePolar
            // DO NOT FORGET TO MODIFY THE C++ WRAPPER
            line.voteCount = accum[idx];          
    
            cvSeqPush( lines, &line );
     }