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

I am currently trying to determine if there was a deployed intersection barrier on an image using HoughLines in OpenCV . I thought that my code would draw a line on my image while a barrier appears in it, but instead the error message "Mat data type is not compatible" appears. Can show me how to detect strings in Java using OpenCV?

 public class DetectLines { public static void main(String args[]) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat eventless = Highgui.imread("files/eventless.png"); Mat barrier = Highgui.imread("files/barrier/lc-00201.png"); Mat difference = new Mat(); Mat lines = new Mat(); Core.absdiff(eventless, barrier, difference); Mat grey = new Mat(); Imgproc.cvtColor(difference, grey, Imgproc.COLOR_BGR2GRAY); Imgproc.HoughLines(grey, lines, 5.0, 4.0, 7); Imshow ims1 = new Imshow("Lines"); ims1.showImage(lines); } } 
+5
source share
3 answers

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

  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 an easier way to do this. This is due to 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; } 
+9
source

in Android or Java its own idia.you need to do this in for (); get theta and row after you get the start point and end point, you can create a line. (core.line) but don’t forget to move the mat so that you get the values ​​from it. after that → release the mat. hope this helps uri enjoy

+1
source

Change: see Answer lordash, which is specific to Java.

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


Take a look at this tutorial and find the “Standard Hough Line Transformation” section.

lines should not be cv::Mat , but std::vector<cv::Vec2f> lines; which later you can visualize with code similar to the one shown 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); } 
-1
source

All Articles