Created
December 17, 2014 02:27
-
-
Save waldemarnt/5af86a6c7b788ec61119 to your computer and use it in GitHub Desktop.
MotionDetector with black and white diference with javacv(opencv)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.bytedeco.javacpp.Loader; | |
import org.bytedeco.javacpp.opencv_core.CvMemStorage; | |
import org.bytedeco.javacpp.opencv_core.CvPoint2D32f; | |
import org.bytedeco.javacpp.opencv_core.CvSeq; | |
import org.bytedeco.javacpp.opencv_core.CvSize2D32f; | |
import org.bytedeco.javacpp.opencv_core.IplImage; | |
import org.bytedeco.javacv.CanvasFrame; | |
import org.bytedeco.javacv.OpenCVFrameGrabber; | |
import static org.bytedeco.javacpp.opencv_core.*; | |
import static org.bytedeco.javacpp.opencv_imgproc.*; | |
import static org.bytedeco.javacpp.opencv_calib3d.*; | |
import static org.bytedeco.javacpp.opencv_objdetect.*; | |
import static org.bytedeco.javacpp.opencv_core.*; | |
import static org.bytedeco.javacpp.opencv_highgui.*; | |
import static org.bytedeco.javacpp.opencv_imgproc.*; | |
import static org.bytedeco.javacpp.opencv_core.*; | |
import static org.bytedeco.javacpp.opencv_imgproc.*; | |
import static org.bytedeco.javacpp.opencv_highgui.*; | |
public class MotionDetector { | |
public static void main(String[] args) throws Exception { | |
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0); | |
grabber.start(); | |
IplImage frame = 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 = grabber.grab()) != null) { | |
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(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) { | |
System.out.println("finded"); | |
CvPoint2D32f center = box.center(); | |
CvSize2D32f size = box.size(); | |
} | |
} | |
contour = contour.h_next(); | |
} | |
} | |
} | |
grabber.stop(); | |
canvasFrame.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment