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

前端 未结 3 1753
我寻月下人不归
我寻月下人不归 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:38

    The lines are returned in the lines matrix, which has two columns in which each line returns the rho and theta parameters of the polar coordinates. These coordinates refer to the distance between the top-left corner of the image and the line rotation in radians, respectively. So, in order to use the show method you must create a Mat that will represent the lines using the line coordinates.

      public Mat getHoughTransform(Mat image, double rho, double theta, int threshold) {
        Mat result = Image.getInstance().getImage().clone();
        Mat lines = new Mat();
        Imgproc.HoughLines(image, lines, rho, theta, threshold);
    
        for (int i = 0; i < lines.cols(); i++) {
            double data[] = lines.get(0, i);
            double rho1 = data[0];
            double theta1 = data[1];
            double cosTheta = Math.cos(theta1);
            double sinTheta = Math.sin(theta1);
            double x0 = cosTheta * rho1;
            double y0 = sinTheta * rho1;
            Point pt1 = new Point(x0 + 10000 * (-sinTheta), y0 + 10000 * cosTheta);
            Point pt2 = new Point(x0 - 10000 * (-sinTheta), y0 - 10000 * cosTheta);
            Imgproc.line(result, pt1, pt2, new Scalar(0, 0, 255), 2);
        }
        return result;
    }
    

    And this is a easier way to do it. It involve the Imgproc.HoughLinesP method.

    public Mat getHoughPTransform(Mat image, double rho, double theta, int threshold) {
        Mat result = Image.getInstance().getImage().clone();
        Mat lines = new Mat();
        Imgproc.HoughLinesP(image, lines, rho, theta, threshold);
    
        for (int i = 0; i < lines.cols(); i++) {
            double[] val = lines.get(0, i);
            Imgproc.line(result, new Point(val[0], val[1]), new Point(val[2], val[3]), new Scalar(0, 0, 255), 2);
        }
        return result;
    }
    
    0 讨论(0)
  • 2021-01-13 11:49

    in Android or Java its the same idia.you must to do it in for (); to get the theta and row after you get start point and the end point ,you can core the line.(core.line) but dont forget to transfer the mat that you get the values from her to other. after this>>release the mat. hope thats its help u. r.i enjoy

    0 讨论(0)
  • 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<cv::Vec2f> 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);
    }
    
    0 讨论(0)
提交回复
热议问题