Motion Detection Code with JavaCV

901 views
Skip to first unread message

Angelo Marchesin

unread,
Oct 5, 2010, 10:31:03 AM10/5/10
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;
}
}

Ravi Teja Alluri

unread,
Feb 28, 2014, 5:06:37 AM2/28/14
to jav...@googlegroups.com
Hi Angelo Marchesin ,

can you tell what does sa(Sized Area ) and activeAlarms means i did not find any declaration type of them .

thanks.

James

unread,
May 19, 2014, 2:48:57 PM5/19/14
to jav...@googlegroups.com
Hi 

James

unread,
May 19, 2014, 2:51:54 PM5/19/14
to jav...@googlegroups.com
Hi I'm applying that source on android, but I couldn't grab frame

OpenCVFrameGrabber is working on android? 

Samuel Audet

unread,
May 20, 2014, 7:41:51 AM5/20/14
to jav...@googlegroups.com
On 05/20/2014 03:51 AM, James wrote:
> Hi I'm applying that source on android, but I couldn't grab frame
>
> OpenCVFrameGrabber is working on android?

It should be working with the latest SNAPSHOT yes:
https://oss.sonatype.org/content/repositories/snapshots//org/bytedeco/

Samuel
Reply all
Reply to author
Forward
0 new messages