6

I was looking at Opencv Java documentation of Hough Transform.

The return value lines is in a Mat data type described as:

Output vector of lines. Each line is represented by a two-element vector (rho, theta). rho is the distance from the coordinate origin (0,0) (top-left corner of the image). theta is the line rotation angle in radians (0 ~ vertical line, pi/2 ~ horizontal line).

Curiously, this description matches the C++ interface's description, but the data type not: in C++ you can use a std::vector<cv::Vec2f> lines as described in this tutorial. In C++ the returned data representation, given the description, is straightforward, but in Java not.

So, in Java, how are the two-element vector represented/stored in the returned Mat?

Antonio
  • 19,451
  • 13
  • 99
  • 197

1 Answers1

6

Here's some code I used a while ago, in version 2.4.8 I think. matLines came from this:

Imgproc.HoughLinesP(matOutline, matLines, 1, Math.PI / 180, houghThreshCurrent, houghMinLength, houghMaxGap);

...

Point[] points = new Point[]{ new Point(), new Point() };
for (int x = 0; x < matLines.cols(); x++) {
   double[] vec = matLines.get(0, x);
   points[0].x = vec[0];
   points[0].y = vec[1];
   points[1].x = vec[2];
   points[1].y = vec[3];
   //...
}
medloh
  • 941
  • 12
  • 33
  • Thanks! I am actually not the one in need, let's see what the author of [this question](http://stackoverflow.com/questions/29493267/how-to-detect-lines-using-houghlines-in-opencv-java) says... – Antonio Apr 10 '15 at 13:26
  • I've taken the liberty to rewrite the loop to avoid unnecessary memory allocation in the loop; for big pictures, this can be a real resource hog. –  Sep 24 '16 at 23:20