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