RLPark 1.0.0
Reinforcement Learning Framework in Java
|
00001 package rlpark.plugin.opencv.samples; 00002 00003 /* 00004 * I developed some code for recognize motion detections with JavaCV. 00005 * Actually, it works with an array of Rect, performing, every cicle, an 00006 * intersection test with area of difference with the rect of interests 00007 * (this array is callad "sa", stands for SizedArea). I hope could this 00008 * helps someone. 00009 * 00010 * Feel free to ask about any question regarding the code above, cheers! 00011 * 00012 * Angelo Marchesin <marchesin.angelo@gmail.com> 00013 */ 00014 00015 import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U; 00016 import static com.googlecode.javacv.cpp.opencv_core.cvAbsDiff; 00017 import static com.googlecode.javacv.cpp.opencv_imgproc.CV_GAUSSIAN; 00018 import static com.googlecode.javacv.cpp.opencv_imgproc.CV_RGB2GRAY; 00019 import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor; 00020 import static com.googlecode.javacv.cpp.opencv_imgproc.cvSmooth; 00021 00022 import com.googlecode.javacv.CanvasFrame; 00023 import com.googlecode.javacv.OpenCVFrameGrabber; 00024 import com.googlecode.javacv.cpp.opencv_core.IplImage; 00025 00026 public class MotionDetector { 00027 public static void main(String[] args) throws Exception { 00028 OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0); 00029 grabber.start(); 00030 00031 IplImage frame = grabber.grab(); 00032 IplImage image = null; 00033 IplImage prevImage = null; 00034 IplImage diff = null; 00035 00036 CanvasFrame canvasFrame = new CanvasFrame("Some Title"); 00037 canvasFrame.setCanvasSize(frame.width(), frame.height()); 00038 00039 while (canvasFrame.isVisible() && (frame = grabber.grab()) != null) { 00040 cvSmooth(frame, frame, CV_GAUSSIAN, 9, 9, 2, 2); 00041 if (image == null) { 00042 image = IplImage.create(frame.width(), frame.height(), IPL_DEPTH_8U, 1); 00043 cvCvtColor(frame, image, CV_RGB2GRAY); 00044 } else { 00045 prevImage = IplImage.create(frame.width(), frame.height(), IPL_DEPTH_8U, 1); 00046 prevImage = image; 00047 image = IplImage.create(frame.width(), frame.height(), IPL_DEPTH_8U, 1); 00048 cvCvtColor(frame, image, CV_RGB2GRAY); 00049 } 00050 00051 if (diff == null) { 00052 diff = IplImage.create(frame.width(), frame.height(), IPL_DEPTH_8U, 1); 00053 } 00054 00055 if (prevImage != null) { 00056 // perform ABS difference 00057 cvAbsDiff(image, prevImage, diff); 00058 // do some threshold for wipe away useless details 00059 // cvThreshold(diff, diff, 64, 255, CV_THRESH_BINARY); 00060 canvasFrame.showImage(diff); 00061 } 00062 } 00063 grabber.stop(); 00064 canvasFrame.dispose(); 00065 } 00066 }