PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/samples/HoughLines.java

https://gitlab.com/cvtung/javacv
Java | 117 lines | 77 code | 13 blank | 27 comment | 12 complexity | 6bc8d01b05e3a4d99ad912a802e9d6a5 MD5 | raw file
  1. import javax.swing.JFrame;
  2. import org.bytedeco.javacpp.*;
  3. import org.bytedeco.javacv.*;
  4. import static org.bytedeco.javacpp.opencv_core.*;
  5. import static org.bytedeco.javacpp.opencv_imgproc.*;
  6. import static org.bytedeco.javacpp.opencv_imgcodecs.*;
  7. /**
  8. * C to Java translation of the houghlines.c sample provided in the c sample directory of OpenCV 2.1,
  9. * using the JavaCV Java wrapper of OpenCV 2.2 developped by Samuel Audet.
  10. *
  11. * @author Jeremy Nicola
  12. * jeremy.nicola@gmail.com
  13. */
  14. public class HoughLines {
  15. /**
  16. * usage: java HoughLines imageDir\imageName TransformType
  17. */
  18. public static void main(String[] args) {
  19. String fileName = args.length >= 1 ? args[0] : "pic1.png"; // if no params provided, compute the defaut image
  20. IplImage src = cvLoadImage(fileName, 0);
  21. IplImage dst;
  22. IplImage colorDst;
  23. CvMemStorage storage = cvCreateMemStorage(0);
  24. CvSeq lines = new CvSeq();
  25. CanvasFrame source = new CanvasFrame("Source");
  26. CanvasFrame hough = new CanvasFrame("Hough");
  27. OpenCVFrameConverter.ToIplImage sourceConverter = new OpenCVFrameConverter.ToIplImage();
  28. OpenCVFrameConverter.ToIplImage houghConverter = new OpenCVFrameConverter.ToIplImage();
  29. if (src == null) {
  30. System.out.println("Couldn't load source image.");
  31. return;
  32. }
  33. dst = cvCreateImage(cvGetSize(src), src.depth(), 1);
  34. colorDst = cvCreateImage(cvGetSize(src), src.depth(), 3);
  35. cvCanny(src, dst, 50, 200, 3);
  36. cvCvtColor(dst, colorDst, CV_GRAY2BGR);
  37. /*
  38. * apply the probabilistic hough transform
  39. * which returns for each line deteced two points ((x1, y1); (x2,y2))
  40. * defining the detected segment
  41. */
  42. if (args.length == 2 && args[1].contentEquals("probabilistic")) {
  43. System.out.println("Using the Probabilistic Hough Transform");
  44. lines = cvHoughLines2(dst, storage, CV_HOUGH_PROBABILISTIC, 1, Math.PI / 180, 40, 50, 10, 0, CV_PI);
  45. for (int i = 0; i < lines.total(); i++) {
  46. // Based on JavaCPP, the equivalent of the C code:
  47. // CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
  48. // CvPoint first=line[0], second=line[1]
  49. // is:
  50. Pointer line = cvGetSeqElem(lines, i);
  51. CvPoint pt1 = new CvPoint(line).position(0);
  52. CvPoint pt2 = new CvPoint(line).position(1);
  53. System.out.println("Line spotted: ");
  54. System.out.println("\t pt1: " + pt1);
  55. System.out.println("\t pt2: " + pt2);
  56. cvLine(colorDst, pt1, pt2, CV_RGB(255, 0, 0), 3, CV_AA, 0); // draw the segment on the image
  57. }
  58. }
  59. /*
  60. * Apply the multiscale hough transform which returns for each line two float parameters (rho, theta)
  61. * rho: distance from the origin of the image to the line
  62. * theta: angle between the x-axis and the normal line of the detected line
  63. */
  64. else if(args.length==2 && args[1].contentEquals("multiscale")){
  65. System.out.println("Using the multiscale Hough Transform"); //
  66. lines = cvHoughLines2(dst, storage, CV_HOUGH_MULTI_SCALE, 1, Math.PI / 180, 40, 1, 1, 0, CV_PI);
  67. for (int i = 0; i < lines.total(); i++) {
  68. CvPoint2D32f point = new CvPoint2D32f(cvGetSeqElem(lines, i));
  69. float rho=point.x();
  70. float theta=point.y();
  71. double a = Math.cos((double) theta), b = Math.sin((double) theta);
  72. double x0 = a * rho, y0 = b * rho;
  73. CvPoint pt1 = cvPoint((int) Math.round(x0 + 1000 * (-b)), (int) Math.round(y0 + 1000 * (a))), pt2 = cvPoint((int) Math.round(x0 - 1000 * (-b)), (int) Math.round(y0 - 1000 * (a)));
  74. System.out.println("Line spoted: ");
  75. System.out.println("\t rho= " + rho);
  76. System.out.println("\t theta= " + theta);
  77. cvLine(colorDst, pt1, pt2, CV_RGB(255, 0, 0), 3, CV_AA, 0);
  78. }
  79. }
  80. /*
  81. * Default: apply the standard hough transform. Outputs: same as the multiscale output.
  82. */
  83. else {
  84. System.out.println("Using the Standard Hough Transform");
  85. lines = cvHoughLines2(dst, storage, CV_HOUGH_STANDARD, 1, Math.PI / 180, 90, 0, 0, 0, CV_PI);
  86. for (int i = 0; i < lines.total(); i++) {
  87. CvPoint2D32f point = new CvPoint2D32f(cvGetSeqElem(lines, i));
  88. float rho=point.x();
  89. float theta=point.y();
  90. double a = Math.cos((double) theta), b = Math.sin((double) theta);
  91. double x0 = a * rho, y0 = b * rho;
  92. CvPoint pt1 = cvPoint((int) Math.round(x0 + 1000 * (-b)), (int) Math.round(y0 + 1000 * (a))), pt2 = cvPoint((int) Math.round(x0 - 1000 * (-b)), (int) Math.round(y0 - 1000 * (a)));
  93. System.out.println("Line spotted: ");
  94. System.out.println("\t rho= " + rho);
  95. System.out.println("\t theta= " + theta);
  96. cvLine(colorDst, pt1, pt2, CV_RGB(255, 0, 0), 3, CV_AA, 0);
  97. }
  98. }
  99. source.showImage(sourceConverter.convert(src));
  100. hough.showImage(houghConverter.convert(colorDst));
  101. source.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  102. hough.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  103. }
  104. }