Search code examples
c++visual-studioopencvhough-transform

Visualize the Result of a Hough Transformation


I am currently programming a laser 3D-sensor which provides me with edge points (X,Y,Z) in a 2D array as a result. The coordinates are used to perform a Hough-transform. The result in Rho and Theta is output to my console.
My question now is whether I can visualise this result, to check for correctness. I have thought of the library OpenCV, but I am not familiar with its programming and have only seen functions and examples that have transformed and visualised images specified by a file path. Maybe someone can tell me if OpenCV supports a function that allows the visualisation of the Rho and Theta values as raw input, or if there are other possibilities.


Many thanks in advance


Solution

  • You can draw your points in an OpenCv image as a contour , then apply Hough transform as follow:

    cv::Mat dst;
    cv::drawContours(dst, contours, -1, cv::Scalar(0, 255, 0), 2);
     // contours is your 2D vector)
    
    vector<Vec2f> lines; // will hold theta and rho
    HoughLines(dst, lines, 1, cv::CV_PI/180, 150, 0, 0 ); 
    for( size_t i = 0; i < lines.size(); i++ )
    {
     float rho = lines[i][0], theta = lines[i][1];
     std::cout << "rho = " << rho << " theta = " << theta << '\n';
    }