Tutorial Hough Lines
[platform/upstream/opencv.git] / samples / java / tutorial_code / ImgTrans / HoughLine / HoughLines.java
1 /**
2  * @file HoughLines.java
3  * @brief This program demonstrates line finding with the Hough transform
4  */
5
6 import org.opencv.core.*;
7 import org.opencv.core.Point;
8 import org.opencv.highgui.HighGui;
9 import org.opencv.imgcodecs.Imgcodecs;
10 import org.opencv.imgproc.Imgproc;
11
12 class HoughLinesRun {
13
14     public void run(String[] args) {
15         // Declare the output variables
16         Mat dst = new Mat(), cdst = new Mat(), cdstP;
17
18         //! [load]
19         String default_file = "../../../../data/sudoku.png";
20         String filename = ((args.length > 0) ? args[0] : default_file);
21
22         // Load an image
23         Mat src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
24
25         // Check if image is loaded fine
26         if( src.empty() ) {
27             System.out.println("Error opening image!");
28             System.out.println("Program Arguments: [image_name -- default "
29                     + default_file +"] \n");
30             System.exit(-1);
31         }
32         //! [load]
33
34         //! [edge_detection]
35         // Edge detection
36         Imgproc.Canny(src, dst, 50, 200, 3, false);
37         //! [edge_detection]
38
39         // Copy edges to the images that will display the results in BGR
40         Imgproc.cvtColor(dst, cdst, Imgproc.COLOR_GRAY2BGR);
41         cdstP = cdst.clone();
42
43         //! [hough_lines]
44         // Standard Hough Line Transform
45         Mat lines = new Mat(); // will hold the results of the detection
46         Imgproc.HoughLines(dst, lines, 1, Math.PI/180, 150); // runs the actual detection
47         //! [hough_lines]
48         //! [draw_lines]
49         // Draw the lines
50         for (int x = 0; x < lines.rows(); x++) {
51             double rho = lines.get(x, 0)[0],
52                     theta = lines.get(x, 0)[1];
53
54             double a = Math.cos(theta), b = Math.sin(theta);
55             double x0 = a*rho, y0 = b*rho;
56             Point pt1 = new Point(Math.round(x0 + 1000*(-b)), Math.round(y0 + 1000*(a)));
57             Point pt2 = new Point(Math.round(x0 - 1000*(-b)), Math.round(y0 - 1000*(a)));
58             Imgproc.line(cdst, pt1, pt2, new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0);
59         }
60         //! [draw_lines]
61
62         //! [hough_lines_p]
63         // Probabilistic Line Transform
64         Mat linesP = new Mat(); // will hold the results of the detection
65         Imgproc.HoughLinesP(dst, linesP, 1, Math.PI/180, 50, 50, 10); // runs the actual detection
66         //! [hough_lines_p]
67         //! [draw_lines_p]
68         // Draw the lines
69         for (int x = 0; x < linesP.rows(); x++) {
70             double[] l = linesP.get(x, 0);
71             Imgproc.line(cdstP, new Point(l[0], l[1]), new Point(l[2], l[3]), new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0);
72         }
73         //! [draw_lines_p]
74
75         //! [imshow]
76         // Show results
77         HighGui.imshow("Source", src);
78         HighGui.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst);
79         HighGui.imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP);
80         //! [imshow]
81
82         //! [exit]
83         // Wait and Exit
84         HighGui.waitKey();
85         System.exit(0);
86         //! [exit]
87     }
88 }
89
90 public class HoughLines {
91     public static void main(String[] args) {
92         // Load the native library.
93         System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
94         new HoughLinesRun().run(args);
95     }
96 }