Skip to content

Commit

Permalink
Updated the aggregate brightness map code
Browse files Browse the repository at this point in the history
git-svn-id: https://android-motion-detection.googlecode.com/svn/trunk@16 996b93ed-3e4d-4b21-1044-74bd19560d1d
  • Loading branch information
[email protected] committed Aug 31, 2011
1 parent cf986f1 commit 764c359
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/com/jwetherell/motion_detection/Globals.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.jwetherell.motion_detection;

public class Globals {
public static boolean USE_RGB = false;
public static boolean USE_LUMA = true;
public static boolean USE_RGB = true;
public static boolean USE_LUMA = false;
public static boolean USE_STATE = false;
public static boolean SAVE_PREVIOUS = false;
public static boolean SAVE_ORIGINAL = false;
Expand Down
30 changes: 19 additions & 11 deletions src/com/jwetherell/motion_detection/detection/Comparer.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,27 @@ public Comparison compare(State s1, State s2) {
if (xPixelsPerBox <= 0) xPixelsPerBox = 1;
int yPixelsPerBox = (int)(Math.floor(s1.getHeight() / yBoxes));
if (yPixelsPerBox <= 0) yPixelsPerBox = 1;

// Boxes
int[][] variance = new int[yBoxes][xBoxes];

// set to a different by default, if a change is found then flag non-match
boolean different = false;
// loop through whole image and compare individual blocks of images
int ty = 0;
int tx = 0;
int b1 = 0;
int b2 = 0;
int diff = 0;
for (int y = 0; y < yBoxes; y++) {
StringBuilder output = new StringBuilder();
if (debugMode > 0) output.append("|");
ty = y*yPixelsPerBox;
for (int x = 0; x < xBoxes; x++) {
int tx = x*xPixelsPerBox;
int b1 = aggregateMapArea(s1.getMap(), tx, ty, xPixelsPerBox, yPixelsPerBox);
int b2 = aggregateMapArea(s2.getMap(), tx, ty, xPixelsPerBox, yPixelsPerBox);
int diff = Math.abs(b1 - b2);
tx = x*xPixelsPerBox;
b1 = aggregateMapArea(s1.getMap(), tx, ty, xPixelsPerBox, yPixelsPerBox);
b2 = aggregateMapArea(s2.getMap(), tx, ty, xPixelsPerBox, yPixelsPerBox);
diff = Math.abs(b1 - b2);
variance[y][x] = diff;
// the difference in a certain region has passed the threshold value
if (diff > leniency) different = true;
Expand All @@ -74,25 +80,27 @@ public Comparison compare(State s1, State s2) {
return (new Comparison(s1, s2, variance, different));
}

private static int aggregateMapArea(int[][] map, int ox, int oy, int w, int h) {
if (map==null) return Integer.MIN_VALUE;
private static int aggregateMapArea(int[] map, int ox, int oy, int w, int h) {
if (map==null) throw new NullPointerException();

int t = 0;
int ty = 0;
int tx = 0;
for (int y = 0; y < h; y++) {
int ty = oy+y;
ty = oy+y;
for (int x = 0; x < w; x++) {
int tx = ox+x;
t += map[ty][tx];
tx = ox+x;
t += map[ty+tx];
}
}
return (t/(w*h));
}

public int getComparex() {
public int getCompareX() {
return comparex;
}

public int getComparey() {
public int getCompareY() {
return comparey;
}

Expand Down
32 changes: 21 additions & 11 deletions src/com/jwetherell/motion_detection/detection/Comparison.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,51 @@ public class Comparison {
private boolean different = false;

public Comparison(State s1, State s2, int[][] variance, boolean different) {
if (variance==null) throw new NullPointerException();

this.s1 = s1;
this.s2 = s2;
this.different = different;

if (variance==null) return;
this.variance = variance;
this.height = variance.length;
this.width = variance[0].length;
}

public void getChangeIndicator(int[] data, int width, int height, Comparer comparer) {
int bx = (width / this.width);
int by = (height / this.height);
int xBoxes = comparer.getCompareX();
if (xBoxes > s1.getWidth()) xBoxes = s1.getWidth();
int yBoxes = comparer.getCompareY();
if (yBoxes > s1.getHeight()) yBoxes = s1.getHeight();

int xPixelsPerBox = (int)(Math.floor(s1.getWidth() / xBoxes));
if (xPixelsPerBox <= 0) xPixelsPerBox = 1;
int yPixelsPerBox = (int)(Math.floor(s1.getHeight() / yBoxes));
if (yPixelsPerBox <= 0) yPixelsPerBox = 1;

int tx = 0;
int ty = 0;
for (int y = 0; y < by; y++) {
ty = (int)(y * by);
for (int x = 0; x < bx; x++) {
tx = (int)(x * bx);
for (int y = 0; y < yBoxes; y++) {
ty = (int)(y * yPixelsPerBox);
for (int x = 0; x < xBoxes; x++) {
tx = (int)(x * xPixelsPerBox);
if (variance[y][x] > comparer.getLeniency()) {
paintArea(data,tx,ty,bx,by);
paintArea(data,tx,ty,xPixelsPerBox,yPixelsPerBox);
}
}
}
}

private static void paintArea(int[] data, int ox, int oy, int w, int h) {
if (data==null) return;
if (data==null) throw new NullPointerException();

int ty = 0;
int tx = 0;
for (int y = 1; y <= h; y++) {
ty = oy*y;
for (int x = 0, xy=ty; x < w; x++, xy++) {
data[xy] = Color.RED;
for (int x = 0; x < w; x++) {
tx = ox+x;
data[ty+tx] = Color.RED;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ public abstract class MotionDetection {
private static final String TAG = "MotionDetection";

//Specific settings
private static final int mThreshold = 10000; //Number of different pixels
private static final int mPixelThreshold = 50; //Difference in pixel
private static final int mPixelThreshold = 50; //Difference in pixel (RGB & LUMA)
private static final int mThreshold = 10000; //Number of different pixels (RGB & LUMA)
private static final int mLeniency = 100; //Difference of aggregate map (State)
private static final int mDebugMode = 2; //State based debug (State)

private static int[] mPrevious = null;
private static int mPreviousWidth = 0;
Expand All @@ -35,8 +37,8 @@ protected static boolean isDifferentComparingState(int[] first, int width, int h
if (mPreviousState==null) mPreviousState = new State(mPrevious, mPreviousWidth, mPreviousHeight);
State state = new State(first, width, height);

Comparer ic = new Comparer((width/10), (height/10), 25);
ic.setDebugMode(1);
Comparer ic = new Comparer((width/50), (height/50), mLeniency);
ic.setDebugMode(mDebugMode);
Comparison c = ic.compare(state,mPreviousState);

boolean different = c.isDifferent();
Expand Down
22 changes: 9 additions & 13 deletions src/com/jwetherell/motion_detection/detection/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author Justin Wetherell <[email protected]>
*/
public class State {
private int[][] map = null;
private int[] map = null;
private int width;
private int height;
private int average;
Expand All @@ -23,27 +23,23 @@ public State(State other) {
}

public State(int[] data, int width, int height) {
if (data==null) return;
if (data==null) throw new NullPointerException();

this.map = data.clone();
this.width = width;
this.height = height;

// setup brightness map
map = new int[height][width];


// build map and stats
average = 0;
for (int y = 0, xy=0; y < this.height; y++) {
for (int x = 0; x < this.width; x++, xy++) {
int lum = data[xy];
map[y][x] = lum;
average += lum;
average += data[xy];;
}
}
average = (average / (this.width * this.height));
}

public int[][] getMap() {
public int[] getMap() {
return map;
}

Expand All @@ -59,10 +55,10 @@ public int getHeight() {
public String toString() {
StringBuilder output = new StringBuilder();
output.append("h="+height+" w="+width+"\n");
for (int y = 0; y < height; y++) {
for (int y = 0, xy = 0; y < height; y++) {
output.append('|');
for (int x = 0;x < width; x++) {
output.append(map[y][x]);
for (int x = 0;x < width; x++, xy++) {
output.append(map[xy]);
output.append('|');
}
output.append("\n");
Expand Down

0 comments on commit 764c359

Please sign in to comment.