Angelo Marchesin
unread,Oct 5, 2010, 10:31:03 AM10/5/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to javacv
Hi to all,
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!
CanvasFrame canvasFrame = new CanvasFrame("Some Title");
canvasFrame.setCanvasSize(800, 600);
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
grabber.start();
IplImage frame = grabber.grab();
IplImage image = null;
IplImage prevImage = null;
IplImage diff = null;
IplImage finalImage = IplImage.create(frame.width,
frame.height, IPL_DEPTH_8U, 3);
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);
// recognize contours
CvSeq.PointerByReference contourPointer = new
CvSeq.PointerByReference();
int sizeofCvContour =
com.sun.jna.Native.getNativeSize(CvContour.ByValue.class);
cvFindContours(diff, storage, contourPointer,
sizeofCvContour, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
CvSeq contour = contourPointer.getStructure();
while(contour != null) {
if (contour.elem_size > 0) {
CvBox2D box = cvMinAreaRect2(contour,
storage);
CvPoint2D32f center = box.center;
CvSize2D32f size = box.size;
// test intersection
if(box != null) {
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;
}
}