import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import org.opencv.video.BackgroundSubtractor;
import org.opencv.video.Video;
import org.opencv.videoio.VideoCapture;
import org.opencv.videoio.Videoio;
class BackgroundSubtraction {
public void run(String[] args) {
String input = args.length > 0 ? args[0] : "../data/vtest.avi";
boolean useMOG2 = args.length > 1 ? args[1] == "MOG2" : true;
BackgroundSubtractor backSub;
if (useMOG2) {
backSub = Video.createBackgroundSubtractorMOG2();
} else {
backSub = Video.createBackgroundSubtractorKNN();
}
VideoCapture capture = new VideoCapture(input);
if (!capture.isOpened()) {
System.err.println("Unable to open: " + input);
System.exit(0);
}
Mat frame = new Mat(), fgMask = new Mat();
while (true) {
capture.read(frame);
if (frame.empty()) {
break;
}
backSub.apply(frame, fgMask);
Imgproc.rectangle(frame,
new Point(10, 2),
new Point(100, 20),
new Scalar(255, 255, 255), -1);
String frameNumberString =
String.format(
"%d", (
int)capture.get(Videoio.CAP_PROP_POS_FRAMES));
Imgproc.putText(frame, frameNumberString,
new Point(15, 15), Core.FONT_HERSHEY_SIMPLEX, 0.5,
HighGui.imshow("Frame", frame);
HighGui.imshow("FG Mask", fgMask);
int keyboard = HighGui.waitKey(30);
if (keyboard == 'q' || keyboard == 27) {
break;
}
}
HighGui.waitKey();
System.exit(0);
}
}
public class BackgroundSubtractionDemo {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
new BackgroundSubtraction().run(args);
}
}
Point2i Point
定义 types.hpp:209
Scalar_< double > Scalar
定义 types.hpp:702