import java.util.ArrayList; import java.util.Random; import org.opencv.core.*; import org.opencv.highgui.HighGui; import org.opencv.imgproc.Imgproc; import org.opencv.video.Video; import org.opencv.videoio.VideoCapture; class OptFlow { public void run(String[] args) { String filename = args[0]; VideoCapture capture = new VideoCapture(filename); if (!capture.isOpened()) { System.out.println("Unable to open this file"); System.exit(-1); } // Create some random colors Scalar[] colors = new Scalar[100]; Random rng = new Random(); for (int i = 0 ; i < 100 ; i++) { int r = rng.nextInt(256); int g = rng.nextInt(256); int b = rng.nextInt(256); colors[i] = new Scalar(r, g, b); } Mat old_frame = new Mat() , old_gray = new Mat(); // Since the function Imgproc.goodFeaturesToTrack requires MatofPoint // therefore first p0MatofPoint is passed to the function and then converted to MatOfPoint2f MatOfPoint p0MatofPoint = new MatOfPoint(); capture.read(old_frame); Imgproc.cvtColor(old_frame, old_gray, Imgproc.COLOR_BGR2GRAY); Imgproc.goodFeaturesToTrack(old_gray, p0MatofPoint,100,0.3,7, new Mat(),7,false,0.04); MatOfPoint2f p0 = new MatOfPoint2f(p0MatofPoint.toArray()) , p1 = new MatOfPoint2f(); // Create a mask image for drawing purposes Mat mask = Mat.zeros(old_frame.size(), old_frame.type()); while (true) { Mat frame = new Mat(), frame_gray = new Mat(); capture.read(frame); if (frame.empty()) { break; } Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGR2GRAY); // calculate optical flow MatOfByte status = new MatOfByte(); MatOfFloat err = new MatOfFloat(); TermCriteria criteria = new TermCriteria(TermCriteria.COUNT + TermCriteria.EPS,10,0.03); Video.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, p1, status, err, new Size(15,15),2, criteria); byte StatusArr[] = status.toArray(); Point p0Arr[] = p0.toArray(); Point p1Arr[] = p1.toArray(); ArrayList good_new = new ArrayList<>(); for (int i = 0; i