Skip to content

Instantly share code, notes, and snippets.

@six519
Created December 4, 2017 10:50
Show Gist options
  • Save six519/743e32c9879ffea299b0b175823adc88 to your computer and use it in GitHub Desktop.
Save six519/743e32c9879ffea299b0b175823adc88 to your computer and use it in GitHub Desktop.
Basic Motion Detection With OpenCV Java Sample Code
package com.ferdinandsilva;
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;
public class OpenCVTest {
public static void main(String args[]) {
//load library
System.load("/usr/local/Cellar/opencv/3.3.1_1/share/OpenCV/java/libopencv_java331.dylib");
Mat frame = new Mat();
Mat firstFrame = new Mat();
Mat gray = new Mat();
Mat frameDelta = new Mat();
Mat thresh = new Mat();
List<MatOfPoint> cnts = new ArrayList<MatOfPoint>();
VideoCapture camera = new VideoCapture();
camera.open(0); //open camera
//set the video size to 512x288
camera.set(3, 512);
camera.set(4, 288);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
camera.read(frame);
//convert to grayscale and set the first frame
Imgproc.cvtColor(frame, firstFrame, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(firstFrame, firstFrame, new Size(21, 21), 0);
while(camera.read(frame)) {
//convert to grayscale
Imgproc.cvtColor(frame, gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(gray, gray, new Size(21, 21), 0);
//compute difference between first frame and current frame
Core.absdiff(firstFrame, gray, frameDelta);
Imgproc.threshold(frameDelta, thresh, 25, 255, Imgproc.THRESH_BINARY);
Imgproc.dilate(thresh, thresh, new Mat(), new Point(-1, -1), 2);
Imgproc.findContours(thresh, cnts, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
for(int i=0; i < cnts.size(); i++) {
if(Imgproc.contourArea(cnts.get(i)) < 500) {
continue;
}
System.out.println("Motion detected!!!");
System.exit(0);
}
}
}
}
@iksheth
Copy link

iksheth commented Nov 4, 2019

Does it work ?

@arissa34
Copy link

Yes !

@spometun
Copy link

spometun commented Apr 27, 2023

yes, it works for me for Ubuntu 22.04 just with System.load(...) change
thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment