-
Notifications
You must be signed in to change notification settings - Fork 5
/
MotionDetector.java
97 lines (86 loc) · 3.13 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
package org.genericsystem.cv;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.genericsystem.cv.utils.NativeLibraryLoader;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;
public class MotionDetector {
static {
NativeLibraryLoader.load();
}
public static void main(String[] args) {
JFrame jframe = new JFrame("Motion Detector");
jframe.setResizable(false);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel vidpanel = new JLabel();
jframe.setContentPane(vidpanel);
Mat frame = new Mat();
VideoCapture camera = new VideoCapture(0);
camera.read(frame);
jframe.setSize(frame.width(), frame.height());
jframe.setVisible(true);
while (camera.read(frame)) {
Mat diffFrame = new Mat();
Core.absdiff(adjust(frame), new Scalar(255), diffFrame);
Imgproc.adaptiveThreshold(diffFrame, diffFrame, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 7, 3);
for (Rect rect : detection_contours(frame, diffFrame))
Imgproc.rectangle(frame, rect.br(), rect.tl(), new Scalar(0, 0, 255), 1);
ImageIcon image = new ImageIcon(mat2bufferedImage(frame));
vidpanel.setIcon(image);
vidpanel.repaint();
}
}
public static List<Rect> detection_contours(Mat frame, Mat diffFrame) {
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(diffFrame, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
double maxArea = 10;
List<Rect> rectangles = new ArrayList<>();
for (int i = 0; i < contours.size(); i++) {
MatOfPoint contour = contours.get(i);
double contourarea = Imgproc.contourArea(contour);
if (contourarea > maxArea) {
MatOfPoint2f contour2F = new MatOfPoint2f(contour.toArray());
Point[] result = new Point[4];
Imgproc.minAreaRect(contour2F).points(result);
Imgproc.drawContours(frame, Arrays.asList(new MatOfPoint(result)), 0, new Scalar(255, 0, 0), 1);
rectangles.add(Imgproc.boundingRect(contour));
Imgproc.drawContours(frame, contours, i, new Scalar(0, 255, 0));
}
}
return rectangles;
}
public static Mat adjust(Mat frame) {
Mat result = new Mat();
Imgproc.cvtColor(frame, result, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(result, result, new Size(3, 3), 0);
// Imgproc.Canny(result, result, 150d, 150d * 2, 3, true);
return result;
}
public static BufferedImage mat2bufferedImage(Mat image) {
MatOfByte bytemat = new MatOfByte();
Imgcodecs.imencode(".jpg", image, bytemat);
try {
return ImageIO.read(new ByteArrayInputStream(bytemat.toArray()));
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}