From 764c359330341ed28230c2e177fa9021edd53db4 Mon Sep 17 00:00:00 2001 From: "phishman3579@gmail.com" Date: Wed, 31 Aug 2011 02:48:25 +0000 Subject: [PATCH] Updated the aggregate brightness map code git-svn-id: https://android-motion-detection.googlecode.com/svn/trunk@16 996b93ed-3e4d-4b21-1044-74bd19560d1d --- .../jwetherell/motion_detection/Globals.java | 4 +-- .../motion_detection/detection/Comparer.java | 30 ++++++++++------- .../detection/Comparison.java | 32 ++++++++++++------- .../detection/MotionDetection.java | 10 +++--- .../motion_detection/detection/State.java | 22 ++++++------- 5 files changed, 57 insertions(+), 41 deletions(-) diff --git a/src/com/jwetherell/motion_detection/Globals.java b/src/com/jwetherell/motion_detection/Globals.java index 16c9db3..2bb2a2a 100644 --- a/src/com/jwetherell/motion_detection/Globals.java +++ b/src/com/jwetherell/motion_detection/Globals.java @@ -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; diff --git a/src/com/jwetherell/motion_detection/detection/Comparer.java b/src/com/jwetherell/motion_detection/detection/Comparer.java index ad7d16a..0adb7ca 100644 --- a/src/com/jwetherell/motion_detection/detection/Comparer.java +++ b/src/com/jwetherell/motion_detection/detection/Comparer.java @@ -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; @@ -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; } diff --git a/src/com/jwetherell/motion_detection/detection/Comparison.java b/src/com/jwetherell/motion_detection/detection/Comparison.java index b50c11b..a241916 100644 --- a/src/com/jwetherell/motion_detection/detection/Comparison.java +++ b/src/com/jwetherell/motion_detection/detection/Comparison.java @@ -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; } } } diff --git a/src/com/jwetherell/motion_detection/detection/MotionDetection.java b/src/com/jwetherell/motion_detection/detection/MotionDetection.java index 3dd7650..594f308 100644 --- a/src/com/jwetherell/motion_detection/detection/MotionDetection.java +++ b/src/com/jwetherell/motion_detection/detection/MotionDetection.java @@ -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; @@ -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(); diff --git a/src/com/jwetherell/motion_detection/detection/State.java b/src/com/jwetherell/motion_detection/detection/State.java index 8066015..7610008 100644 --- a/src/com/jwetherell/motion_detection/detection/State.java +++ b/src/com/jwetherell/motion_detection/detection/State.java @@ -10,7 +10,7 @@ * @author Justin Wetherell */ public class State { - private int[][] map = null; + private int[] map = null; private int width; private int height; private int average; @@ -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; } @@ -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");