Skip to content

Commit

Permalink
Added support for multiple points for source of motion
Browse files Browse the repository at this point in the history
  • Loading branch information
tm1990 committed May 30, 2015
1 parent 9827348 commit 5686cd4
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 9 deletions.
Expand Up @@ -276,7 +276,7 @@ protected void detect() {
* @param image with the motion detected
*/
private void notifyMotionListeners(BufferedImage currentOriginal) {
WebcamMotionEvent wme = new WebcamMotionEvent(this, previousOriginal, currentOriginal, detectorAlgorithm.getArea(), detectorAlgorithm.getCog());
WebcamMotionEvent wme = new WebcamMotionEvent(this, previousOriginal, currentOriginal, detectorAlgorithm.getArea(), detectorAlgorithm.getCog(), detectorAlgorithm.getPoints());
for (WebcamMotionListener l : listeners) {
try {
l.motionDetected(wme);
Expand Down Expand Up @@ -437,4 +437,22 @@ public WebcamMotionDetectorAlgorithm getDetectorAlgorithm() {
return detectorAlgorithm;
}


public void setMaxMotionPoints(int i){
detectorAlgorithm.setMaxPoints(i);
}

public int getMaxMotionPoints(){
return detectorAlgorithm.getMaxPoints();
}


public void setPointRange(int i){
detectorAlgorithm.setPointRange(i);
}

public int getPointRange(){
return detectorAlgorithm.getPointRange();
}

}
Expand Up @@ -2,6 +2,7 @@

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.ArrayList;


/**
Expand Down Expand Up @@ -50,4 +51,36 @@ public interface WebcamMotionDetectorAlgorithm {
* @return Return percentage image fraction covered by motion
*/
double getArea();

/**
* Set the minimum range between each point detected
* @param i the range to set
*/
void setPointRange(int i);

/**
* Set the max amount of points that can be detected at one time
* @param i The amount of points that can be detected
*/
void setMaxPoints(int i);


/**
* Get the current minimum range between each point
* @return The current range
*/
int getPointRange();

/**
* Get the current max amount of points that can be detected at one time
* @return
*/
int getMaxPoints();


/**
* Returns the currently stored points that have been detected
* @return The current points
*/
ArrayList<Point> getPoints();
}
@@ -1,11 +1,12 @@
package com.github.sarxos.webcam;

import java.awt.Point;
import java.awt.image.BufferedImage;

import com.github.sarxos.webcam.util.jh.JHBlurFilter;
import com.github.sarxos.webcam.util.jh.JHGrayFilter;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

/**
* Default motion detector algorithm.
*/
Expand Down Expand Up @@ -71,6 +72,7 @@ public BufferedImage prepareImage(BufferedImage original) {

@Override
public boolean detect(BufferedImage previousModified, BufferedImage currentModified) {
Points.clear();
int p = 0;

int cogX = 0;
Expand All @@ -79,6 +81,7 @@ public boolean detect(BufferedImage previousModified, BufferedImage currentModif
int w = currentModified.getWidth();
int h = currentModified.getHeight();

int j = 0;
if (previousModified != null) {
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
Expand All @@ -88,10 +91,29 @@ public boolean detect(BufferedImage previousModified, BufferedImage currentModif
int pid = combinePixels(cpx, ppx) & 0x000000ff;

if (pid >= pixelThreshold) {
cogX += x;
cogY += y;
p += 1;
}
Point pp = new Point(x, y);
boolean keep = j < maxPoints;

if (keep) {
for (Point g : Points) {
if (g.x != pp.x || g.y != pp.y) {
if (pp.distance(g) <= range) {
keep = false;
break;
}
}
}
}

if (keep) {
Points.add(new Point(x, y));
j += 1;
}

cogX += x;
cogY += y;
p += 1;
}
}
}
}
Expand Down Expand Up @@ -198,5 +220,73 @@ private static int clamp(int c) {
return 255;
}
return c;
}
}


/**
* ArrayList to store the points for a detected motion
*/
ArrayList<Point> Points = new ArrayList<Point>();

/**
* The default minimum range between each point where motion has been detected
*/
public static final int DEFAULT_RANGE = 50;

/**
* The default for the max amount of points that can be detected at one time
*/
public static final int DEFAULT_MAX_POINTS = 100;

/**
* The current minimum range between points
*/
private int range = DEFAULT_RANGE;

/**
* The current max amount of points
*/
private int maxPoints = DEFAULT_MAX_POINTS;

/**
* Set the minimum range between each point detected
* @param i the range to set
*/
public void setPointRange(int i){
range = i;
}

/**
* Get the current minimum range between each point
* @return The current range
*/
public int getPointRange(){
return range;
}


/**
* Set the max amount of points that can be detected at one time
* @param i The amount of points that can be detected
*/
public void setMaxPoints(int i){
maxPoints = i;
}


/**
* Get the current max amount of points that can be detected at one time
* @return
*/
public int getMaxPoints(){
return maxPoints;
}

/**
* Returns the currently stored points that have been detected
* @return The current points
*/
public ArrayList<Point> getPoints(){
return Points;
}
}
Expand Up @@ -2,6 +2,7 @@

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.EventObject;


Expand Down Expand Up @@ -30,6 +31,18 @@ public WebcamMotionEvent(WebcamMotionDetector detector, double strength, Point c
this(detector, null, null, strength, cog);
}

/**
* Create detected motion event.
*
* @param detector
* @param strength
* @param cog center of motion gravity
* @param points list of all detected points
*/
public WebcamMotionEvent(WebcamMotionDetector detector, double strength, Point cog, ArrayList<Point> points) {
this(detector, null, null, strength, cog, points);
}

/**
* Create detected motion event.
*
Expand All @@ -46,6 +59,26 @@ public WebcamMotionEvent(WebcamMotionDetector detector, BufferedImage previousIm
this.strength = strength;
this.cog = cog;
}

/**
* Create detected motion event.
*
* @param detector
* @param previousImage
* @param currentImage
* @param strength
* @param cog center of motion gravity
* @param points list of all detected points
*/
public WebcamMotionEvent(WebcamMotionDetector detector, BufferedImage previousImage, BufferedImage currentImage, double strength, Point cog, ArrayList<Point> points) {
this(detector, previousImage, currentImage, strength, cog);
this.points = points;
}

private ArrayList<Point> points;
public ArrayList<Point> getPoints(){
return points;
}

/**
* Get percentage fraction of image covered by motion. 0 is no motion on
Expand Down Expand Up @@ -85,4 +118,5 @@ public BufferedImage getCurrentImage() {
public WebcamMotionDetector getSource() {
return (WebcamMotionDetector) super.getSource();
}

}

0 comments on commit 5686cd4

Please sign in to comment.