Android OpenCV Drawing Hough Lines

后端 未结 3 1250
执念已碎
执念已碎 2020-12-04 22:58

I am trying to use OpenCV on an android phone to detect lines. I modified the \'Tutorial 1 Basic - 2. Use OpenCV Camera\' sample. I am also using Hough Line Transform as an

3条回答
  •  一生所求
    2020-12-04 23:24

    Here is my code for visual studio, hope it helps.

    void drawLines(Mat &input, const std::vector &lines) {
    for (int i = 0; i < lines.size(); i++) {
        float alpha = CV_PI/2-atan(input.rows/input.cols);
        float r_max;
        float r_min;
        float r = lines[i][0];
        float theta = lines[i][1];
        if (thetaCV_PI-alpha) {
            r_max = input.cols*cos(theta);
            r_min = input.rows*sin(theta);
            if (r > r_max) {
                Point pt1(input.cols, (r - input.cols*cos(theta)) / sin(theta));
                Point pt2((r - input.rows*sin(theta)) / cos(theta), input.rows);
                line(input, pt1, pt2, Scalar(255, 0, 0), 1);
            }
            else if (r < r_max && r > r_min) {
                Point pt1(r / cos(theta), 0);
                Point pt2((r - input.rows*sin(theta)) / cos(theta), input.rows);
                line(input, pt1, pt2, Scalar(255, 0, 0), 1);
            }
            else {
                Point pt1(r / cos(theta), 0);
                Point pt2(0, r / sin(theta));
                line(input, pt1, pt2, Scalar(255, 0, 0), 1);
            }
    
        }
        else {
                r_min = input.cols*cos(theta);
                r_max = input.rows*sin(theta);
                if (r > r_max) {
                    Point pt1(input.cols, (r - input.cols*cos(theta)) / sin(theta));
                    Point pt2((r - input.rows*sin(theta)) / cos(theta), input.rows);
                    line(input, pt1, pt2, Scalar(0, 0, 255), 1);
                }
                else if (r < r_max && r > r_min) {
                    Point pt1(input.cols, (r - input.cols*cos(theta)) / sin(theta));
                    Point pt2(0, r / sin(theta));
                    line(input, pt1, pt2, Scalar(0, 0, 255), 1);
                }
                else {
                    Point pt1(r / cos(theta), 0);
                    Point pt2(0, r / sin(theta));
                    line(input, pt1, pt2, Scalar(0, 0, 255), 1);
                }
        }
    
    }
    

    Here are 2 graphs about the logic about my code I posted.

    The explanation of alpha

    The explanation of alpha

    The explanation of r_max && r_min

    The explanation of r_max && r_min

提交回复
热议问题