PageRenderTime 34ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/rlpark.plugin.opencv/src/rlpark/plugin/opencv/samples/HoughLines.java

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