PageRenderTime 282ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/cagcontactor/jcurious
Java | 94 lines | 67 code | 15 blank | 12 comment | 4 complexity | f7fa3521a34b0efe2725ec680586a0c6 MD5 | raw file
Possible License(s): GPL-2.0
  1. package se.caglabs.opencvsamples;
  2. /*
  3. * Because I believe that examples are the easiest way how to use JavaCV, I am
  4. * sending a sample based on http://dasl.mem.drexel.edu/~noahKuntz/openCVTut9.html
  5. *
  6. * burgetrm@gmail.com
  7. */
  8. import org.bytedeco.javacpp.BytePointer;
  9. import org.bytedeco.javacpp.FloatPointer;
  10. import org.bytedeco.javacpp.IntPointer;
  11. import org.bytedeco.javacpp.Loader;
  12. import org.bytedeco.javacv.*;
  13. import static org.bytedeco.javacpp.opencv_core.*;
  14. import static org.bytedeco.javacpp.opencv_imgproc.*;
  15. import static org.bytedeco.javacpp.opencv_video.*;
  16. import static org.bytedeco.javacpp.opencv_highgui.*;
  17. public class OpticalFlowTracker {
  18. private static final int MAX_CORNERS = 500;
  19. public static void main(String[] args) {
  20. // Load two images and allocate other structures
  21. IplImage imgA = cvLoadImage(
  22. "image0.png",
  23. CV_LOAD_IMAGE_GRAYSCALE);
  24. IplImage imgB = cvLoadImage(
  25. "image1.png",
  26. CV_LOAD_IMAGE_GRAYSCALE);
  27. CvSize img_sz = cvGetSize(imgA);
  28. int win_size = 15;
  29. // IplImage imgC = cvLoadImage("OpticalFlow1.png",
  30. // CV_LOAD_IMAGE_UNCHANGED);
  31. IplImage imgC = cvLoadImage(
  32. "image0.png",
  33. CV_LOAD_IMAGE_UNCHANGED);
  34. // Get the features for tracking
  35. IplImage eig_image = cvCreateImage(img_sz, IPL_DEPTH_32F, 1);
  36. IplImage tmp_image = cvCreateImage(img_sz, IPL_DEPTH_32F, 1);
  37. IntPointer corner_count = new IntPointer(1).put(MAX_CORNERS);
  38. CvPoint2D32f cornersA = new CvPoint2D32f(MAX_CORNERS);
  39. CvArr mask = null;
  40. cvGoodFeaturesToTrack(imgA, eig_image, tmp_image, cornersA,
  41. corner_count, 0.05, 5.0, mask, 3, 0, 0.04);
  42. cvFindCornerSubPix(imgA, cornersA, corner_count.get(),
  43. cvSize(win_size, win_size), cvSize(-1, -1),
  44. cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03));
  45. // Call Lucas Kanade algorithm
  46. BytePointer features_found = new BytePointer(MAX_CORNERS);
  47. FloatPointer feature_errors = new FloatPointer(MAX_CORNERS);
  48. CvSize pyr_sz = cvSize(imgA.width() + 8, imgB.height() / 3);
  49. IplImage pyrA = cvCreateImage(pyr_sz, IPL_DEPTH_32F, 1);
  50. IplImage pyrB = cvCreateImage(pyr_sz, IPL_DEPTH_32F, 1);
  51. CvPoint2D32f cornersB = new CvPoint2D32f(MAX_CORNERS);
  52. cvCalcOpticalFlowPyrLK(imgA, imgB, pyrA, pyrB, cornersA, cornersB,
  53. corner_count.get(), cvSize(win_size, win_size), 5,
  54. features_found, feature_errors,
  55. cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.3), 0);
  56. // Make an image of the results
  57. for (int i = 0; i < corner_count.get(); i++) {
  58. if (features_found.get(i) == 0 || feature_errors.get(i) > 550) {
  59. System.out.println("Error is " + feature_errors.get(i) + "/n");
  60. continue;
  61. }
  62. System.out.println("Got it/n");
  63. cornersA.position(i);
  64. cornersB.position(i);
  65. CvPoint p0 = cvPoint(Math.round(cornersA.x()),
  66. Math.round(cornersA.y()));
  67. CvPoint p1 = cvPoint(Math.round(cornersB.x()),
  68. Math.round(cornersB.y()));
  69. cvLine(imgC, p0, p1, CV_RGB(255, 0, 0),
  70. 2, 8, 0);
  71. }
  72. cvSaveImage(
  73. "image0-1.png",
  74. imgC);
  75. cvNamedWindow( "LKpyr_OpticalFlow", 0 );
  76. cvShowImage( "LKpyr_OpticalFlow", imgC );
  77. cvWaitKey(0);
  78. }
  79. }