HoughLines.java 3.3 KB

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