PageRenderTime 123ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/curious-vision/src/test/java/se/caglabs/opencvsamples/HoughLines.java

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