opencv自带了运动目标检测的接口,这里测试一段视频检测效果1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BackgroundSubtractorMOG2 fgbg = Video.createBackgroundSubtractorMOG2();
Mat fgmask = new Mat();
//fgbg.apply(image, fgmask);
VideoCapture capture = new VideoCapture("images/target2.mp4");
if (!capture.isOpened()) {
System.err.println("--(!)Error opening video capture");
System.exit(0);
}
Mat frame = new Mat();
while (capture.read(frame)) {
if (frame.empty()) {
System.err.println("--(!) No captured frame -- Break!");
break;
}
//-- 3. Apply the classifier to the frame
fgbg.apply(frame, fgmask);
HighGui.imshow("原图", frame);
HighGui.imshow("检测目标", fgmask);
if (HighGui.waitKey(10) == 27) {
break;// escape
}
}
System.exit(0);
}