How to detect lines using HoughLines in OpenCV (Java)?

前端 未结 3 1779
我寻月下人不归
我寻月下人不归 2021-01-13 11:05

I\'m currently trying to detect whether a level crossing barrier has been deployed on an image using HoughLines in OpenCV. I thought my code would

3条回答
  •  一个人的身影
    2021-01-13 11:58

    Edit: See lordache's answer which is Java specific.

    Useful reading: How are two-element vector represented in a OpenCV Mat in Java?


    Check this tutorial and look for the section "Standard Hough Line Transform".

    lines should not be a cv::Mat, but a std::vector lines; that later on you can visualize with a code like the one showed in the link:

    for( size_t i = 0; i < lines.size(); i++ )
    {
      float rho = lines[i][0], theta = lines[i][1];
      Point pt1, pt2;
      double a = cos(theta), b = sin(theta);
      double x0 = a*rho, y0 = b*rho;
      pt1.x = cvRound(x0 + 1000*(-b));
      pt1.y = cvRound(y0 + 1000*(a));
      pt2.x = cvRound(x0 - 1000*(-b));
      pt2.y = cvRound(y0 - 1000*(a));
      line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA);
    }
    

提交回复
热议问题