forked from bytedeco/javacv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MotionDetector.java
101 lines (87 loc) · 4.37 KB
/
MotionDetector.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* I developed some code for recognize motion detections with JavaCV.
* Actually, it works with an array of Rect, performing, every cicle, an
* intersection test with area of difference with the rect of interests
* (this array is callad "sa", stands for SizedArea). I hope could this
* helps someone.
*
* Feel free to ask about any question regarding the code above, cheers!
*
* Angelo Marchesin <[email protected]>
*/
import org.bytedeco.javacpp.*;
import org.bytedeco.javacv.*;
import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_imgproc.*;
import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_imgproc.*;
public class MotionDetector {
public static void main(String[] args) throws Exception {
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
grabber.start();
IplImage frame = converter.convert(grabber.grab());
IplImage image = null;
IplImage prevImage = null;
IplImage diff = null;
CanvasFrame canvasFrame = new CanvasFrame("Some Title");
canvasFrame.setCanvasSize(frame.width(), frame.height());
CvMemStorage storage = CvMemStorage.create();
while (canvasFrame.isVisible() && (frame = converter.convert(grabber.grab())) != null) {
cvClearMemStorage(storage);
cvSmooth(frame, frame, CV_GAUSSIAN, 9, 9, 2, 2);
if (image == null) {
image = IplImage.create(frame.width(), frame.height(), IPL_DEPTH_8U, 1);
cvCvtColor(frame, image, CV_RGB2GRAY);
} else {
prevImage = IplImage.create(frame.width(), frame.height(), IPL_DEPTH_8U, 1);
prevImage = image;
image = IplImage.create(frame.width(), frame.height(), IPL_DEPTH_8U, 1);
cvCvtColor(frame, image, CV_RGB2GRAY);
}
if (diff == null) {
diff = IplImage.create(frame.width(), frame.height(), IPL_DEPTH_8U, 1);
}
if (prevImage != null) {
// perform ABS difference
cvAbsDiff(image, prevImage, diff);
// do some threshold for wipe away useless details
cvThreshold(diff, diff, 64, 255, CV_THRESH_BINARY);
canvasFrame.showImage(converter.convert(diff));
// recognize contours
CvSeq contour = new CvSeq(null);
cvFindContours(diff, storage, contour, Loader.sizeof(CvContour.class), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
while (contour != null && !contour.isNull()) {
if (contour.elem_size() > 0) {
CvBox2D box = cvMinAreaRect2(contour, storage);
// test intersection
if (box != null) {
CvPoint2D32f center = box.center();
CvSize2D32f size = box.size();
/* for (int i = 0; i < sa.length; i++) {
if ((Math.abs(center.x - (sa[i].offsetX + sa[i].width / 2))) < ((size.width / 2) + (sa[i].width / 2)) &&
(Math.abs(center.y - (sa[i].offsetY + sa[i].height / 2))) < ((size.height / 2) + (sa[i].height / 2))) {
if (!alarmedZones.containsKey(i)) {
alarmedZones.put(i, true);
activeAlarms.put(i, 1);
} else {
activeAlarms.remove(i);
activeAlarms.put(i, 1);
}
System.out.println("Motion Detected in the area no: " + i +
" Located at points: (" + sa[i].x + ", " + sa[i].y+ ") -"
+ " (" + (sa[i].x +sa[i].width) + ", "
+ (sa[i].y+sa[i].height) + ")");
}
}
*/
}
}
contour = contour.h_next();
}
}
}
grabber.stop();
canvasFrame.dispose();
}
}