OpticalFlowDemo.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3. import org.opencv.core.*;
  4. import org.opencv.highgui.HighGui;
  5. import org.opencv.imgproc.Imgproc;
  6. import org.opencv.video.Video;
  7. import org.opencv.videoio.VideoCapture;
  8. class OptFlow {
  9. public void run(String[] args) {
  10. String filename = args[0];
  11. VideoCapture capture = new VideoCapture(filename);
  12. if (!capture.isOpened()) {
  13. System.out.println("Unable to open this file");
  14. System.exit(-1);
  15. }
  16. // Create some random colors
  17. Scalar[] colors = new Scalar[100];
  18. Random rng = new Random();
  19. for (int i = 0 ; i < 100 ; i++) {
  20. int r = rng.nextInt(256);
  21. int g = rng.nextInt(256);
  22. int b = rng.nextInt(256);
  23. colors[i] = new Scalar(r, g, b);
  24. }
  25. Mat old_frame = new Mat() , old_gray = new Mat();
  26. // Since the function Imgproc.goodFeaturesToTrack requires MatofPoint
  27. // therefore first p0MatofPoint is passed to the function and then converted to MatOfPoint2f
  28. MatOfPoint p0MatofPoint = new MatOfPoint();
  29. capture.read(old_frame);
  30. Imgproc.cvtColor(old_frame, old_gray, Imgproc.COLOR_BGR2GRAY);
  31. Imgproc.goodFeaturesToTrack(old_gray, p0MatofPoint,100,0.3,7, new Mat(),7,false,0.04);
  32. MatOfPoint2f p0 = new MatOfPoint2f(p0MatofPoint.toArray()) , p1 = new MatOfPoint2f();
  33. // Create a mask image for drawing purposes
  34. Mat mask = Mat.zeros(old_frame.size(), old_frame.type());
  35. while (true) {
  36. Mat frame = new Mat(), frame_gray = new Mat();
  37. capture.read(frame);
  38. if (frame.empty()) {
  39. break;
  40. }
  41. Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGR2GRAY);
  42. // calculate optical flow
  43. MatOfByte status = new MatOfByte();
  44. MatOfFloat err = new MatOfFloat();
  45. TermCriteria criteria = new TermCriteria(TermCriteria.COUNT + TermCriteria.EPS,10,0.03);
  46. Video.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, p1, status, err, new Size(15,15),2, criteria);
  47. byte StatusArr[] = status.toArray();
  48. Point p0Arr[] = p0.toArray();
  49. Point p1Arr[] = p1.toArray();
  50. ArrayList<Point> good_new = new ArrayList<>();
  51. for (int i = 0; i<StatusArr.length ; i++ ) {
  52. if (StatusArr[i] == 1) {
  53. good_new.add(p1Arr[i]);
  54. Imgproc.line(mask, p1Arr[i], p0Arr[i], colors[i],2);
  55. Imgproc.circle(frame, p1Arr[i],5, colors[i],-1);
  56. }
  57. }
  58. Mat img = new Mat();
  59. Core.add(frame, mask, img);
  60. HighGui.imshow("Frame", img);
  61. int keyboard = HighGui.waitKey(30);
  62. if (keyboard == 'q' || keyboard == 27) {
  63. break;
  64. }
  65. // Now update the previous frame and previous points
  66. old_gray = frame_gray.clone();
  67. Point[] good_new_arr = new Point[good_new.size()];
  68. good_new_arr = good_new.toArray(good_new_arr);
  69. p0 = new MatOfPoint2f(good_new_arr);
  70. }
  71. System.exit(0);
  72. }
  73. }
  74. public class OpticalFlowDemo {
  75. public static void main(String[] args) {
  76. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  77. new OptFlow().run(args);
  78. }
  79. }