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; }