Guest User

Untitled

a guest
May 22nd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. // Create an image to hold a grayscale version
  2. IplImage grayImage = IplImage.create(grabbedImage.width(), grabbedImage.height(),
  3. opencv_core.IPL_DEPTH_8U, 1);
  4.  
  5. IplImage cannyOutput = IplImage.create(grabbedImage.width(), grabbedImage.height(),
  6. opencv_core.IPL_DEPTH_8U, 1);
  7. IplImage color_dst = IplImage.create(grabbedImage.width(), grabbedImage.height(),
  8. opencv_core.IPL_DEPTH_8U, 3);
  9.  
  10. //
  11. IplImage tmpl = cvLoadImage("vision_target.png");
  12.  
  13. IplImage tmplp = IplImage.create(tmpl.width(), tmpl.height(),IPL_DEPTH_8U, 1);
  14. cvCvtColor(tmpl, tmplp, CV_RGB2GRAY);
  15. ObjectFinder matcher = new ObjectFinder(tmplp);
  16.  
  17. CvSeq lines = new CvSeq();
  18.  
  19. while ((grabbedImage = grabber.grab()) != null && keepGoing) {
  20. if (grabbedImage != null) {
  21.  
  22.  
  23.  
  24. // Convert image to grayscale.
  25. cvCvtColor(grabbedImage, grayImage, CV_BGR2GRAY);
  26.  
  27.  
  28. /*double[] results = matcher.find(grayImage);
  29. if (results != null) {
  30. System.out.print("Hooray!");
  31.  
  32. for (int i = 0; i < results.length; i++) {
  33. System.out.print(" " + (int)results[i]);
  34. }
  35.  
  36. for (int i = 0; i < results.length; i+=2) {
  37. cvCircle(grabbedImage, cvPoint((int)results[i], (int)results[i + 1]),3, CvScalar.YELLOW, -1, CV_AA,
  38. 0);
  39.  
  40. }
  41.  
  42. System.out.println();
  43. } else {
  44. System.out.println("Fail :(");
  45. }*/
  46.  
  47. // Run the pattern matcher
  48. /*CvSeq faces = cvHaarDetectObjects(grayImage, cascade, storage, 1.1, 1, 0);
  49.  
  50. // iterate over the discovered faces and draw yellow
  51. // rectangles around them.
  52. for (int i = 0; i < faces.total(); i++) {
  53. CvRect r = new CvRect(cvGetSeqElem(faces, i));
  54. cvRectangle(grabbedImage, cvPoint(r.x(), r.y()),
  55. cvPoint(r.x() + r.width(), r.y() + r.height()), CvScalar.YELLOW, 1, CV_AA,
  56. 0);
  57. }*/
  58.  
  59. cvCanny(grayImage, cannyOutput, 100, 230, 3);
  60. cvCvtColor( cannyOutput, color_dst, CV_GRAY2BGR );
  61. lines = cvHoughLines2(cannyOutput, storage, CV_HOUGH_PROBABILISTIC, 1, Math.PI / 180, 50, 10, 3);
  62.  
  63. for (int i = 0; i <= lines.total(); i++) {
  64. // from JavaCPP, the equivalent of the C code:
  65. // CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
  66. // CvPoint first=line[0], second=line[1]
  67. // is:
  68. // CvPoint first=line.position(0), secon=line.position(1);
  69.  
  70. Pointer line = cvGetSeqElem(lines, i);
  71.  
  72. if (line == null || line.isNull())
  73. break;
  74.  
  75. CvPoint pt1 = new CvPoint(line).position(0);
  76. CvPoint pt2 = new CvPoint(line).position(1);
  77.  
  78. System.out.println("Line spotted: ");
  79. System.out.println("\t pt1: " + pt1);
  80. System.out.println("\t pt2: " + pt2);
  81. cvLine(color_dst, pt1, pt2, CV_RGB(255, 0, 0), 3, CV_AA, 0); // draw the segment on the image
  82. }
  83. /* HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );
  84. for( size_t i = 0; i < lines.size(); i++ )
  85. {
  86. line( color_dst, Point(lines[i][0], lines[i][1]),
  87. Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );
  88. }*/
  89.  
  90.  
  91. // Display it
  92. frame.showImage(color_dst);
  93. }
  94. }
  95.  
  96. // Clean up
  97. grabber.release();
Add Comment
Please, Sign In to add comment