diff --git a/src/com/jwetherell/motion_detection/detection/Comparer.java b/src/com/jwetherell/motion_detection/detection/Comparer.java index e930e29..47484b9 100644 --- a/src/com/jwetherell/motion_detection/detection/Comparer.java +++ b/src/com/jwetherell/motion_detection/detection/Comparer.java @@ -32,6 +32,7 @@ public void setDebugMode(int m) { // compare two images. public Comparison compare(State s1, State s2) { if (s1==null || s2==null) return null; + if (s1.getWidth()!=s2.getWidth() || s1.getHeight()!=s2.getHeight()) return null; int cx = comparex; if (cx > s1.getWidth()) cx = s1.getWidth(); @@ -54,8 +55,9 @@ public Comparison compare(State s1, State s2) { if (debugMode > 0) output.append("|"); ty = y*by; for (int x = 0; x < cx; x++) { - int b1 = aggregateMapArea(s1.getMap(), x*bx, ty, bx, by); - int b2 = aggregateMapArea(s2.getMap(), x*bx, ty, bx, by); + int tx = x*bx; + int b1 = aggregateMapArea(s1.getMap(), tx, ty, bx, by); + int b2 = aggregateMapArea(s2.getMap(), tx, ty, bx, by); int diff = Math.abs(b1 - b2); variance[y][x] = diff; // the difference in a certain region has passed the threshold value @@ -73,7 +75,7 @@ public Comparison compare(State s1, State s2) { private static int aggregateMapArea(int[][] map, int ox, int oy, int w, int h) { if (map==null) return Integer.MIN_VALUE; - + int t = 0; for (int i = 0; i < h; i++) { int ty = oy+i; diff --git a/src/com/jwetherell/motion_detection/detection/MotionDetection.java b/src/com/jwetherell/motion_detection/detection/MotionDetection.java index 4336da7..5a63a70 100644 --- a/src/com/jwetherell/motion_detection/detection/MotionDetection.java +++ b/src/com/jwetherell/motion_detection/detection/MotionDetection.java @@ -21,10 +21,10 @@ public abstract class MotionDetection { private static int[] mPrevious = null; private static int mPreviousWidth = 0; private static int mPreviousHeight = 0; - private static State mPreviousStatus = null; + private static State mPreviousState = null; public static int[] getPrevious() { - return (mPrevious!=null)?mPrevious.clone():null; + return ((mPrevious!=null)?mPrevious.clone():null); } protected static boolean isDifferentComparingState(int[] first, int width, int height) { @@ -32,18 +32,13 @@ protected static boolean isDifferentComparingState(int[] first, int width, int h if (first.length != mPrevious.length) return true; if (mPreviousWidth != width || mPreviousHeight != height) return true; - if (mPreviousStatus==null) { - mPreviousStatus = new State(mPrevious, width, height); - return false; - } + if (mPreviousState==null) mPreviousState = new State(mPrevious, mPreviousWidth, mPreviousHeight); - State state = new State(first, (width/10), (height/10)); - Comparer ic = new Comparer(20, 20, 0); + State state = new State(first, width, height); + Comparer ic = new Comparer((width/10), (height/10), 0); ic.setDebugMode(1); - Comparison c = ic.compare(mPreviousStatus, state); - - mPreviousStatus = new State(mPrevious, width, height); - + Comparison c = ic.compare(mPreviousState, state); + boolean different = c.isDifferent(); String output = "isDifferent="+different; if (different) { @@ -52,6 +47,8 @@ protected static boolean isDifferentComparingState(int[] first, int width, int h Log.d(TAG, output); } + mPreviousState = state; + return different; } diff --git a/src/com/jwetherell/motion_detection/image/ImageProcessing.java b/src/com/jwetherell/motion_detection/image/ImageProcessing.java index 7499953..c4564a8 100644 --- a/src/com/jwetherell/motion_detection/image/ImageProcessing.java +++ b/src/com/jwetherell/motion_detection/image/ImageProcessing.java @@ -60,14 +60,14 @@ public static float[] convertToHSL(int r, int g, int b) { } } - //convert to 0-360 + //convert to 0-360 (degrees) HSL[H] *= 60; if (HSL[H]<0) HSL[H] += 360; - //convert to 0-100 + //convert to 0-100 (percent) HSL[S] *= 100; - HSL[L] *= 100; - + HSL[L] *= 100; + return HSL; } @@ -82,7 +82,7 @@ public static float getBrightnessAtPoint(int pixel) { float red = r / 255; float green = g / 255; float blue = b / 255; - float h=0,s=0,l=0; + float h=0,l=0; float minComponent = Math.min(red, Math.min(green, blue)); float maxComponent = Math.max(red, Math.max(green, blue)); @@ -90,14 +90,7 @@ public static float getBrightnessAtPoint(int pixel) { l = (maxComponent + minComponent) / 2; - if(range == 0) { // Monochrome image - h = s = 0; - } else { - s = (l > 0.5) ? - range / (2 - range) - : - range / (maxComponent + minComponent); - + if(range > 0) { if(red == maxComponent) { h = (blue - green) / range; } else if(green == maxComponent) { @@ -112,8 +105,7 @@ public static float getBrightnessAtPoint(int pixel) { if (h<0) h += 360; //convert to 0-100 - s *= 100; - l *= 100; + l *= 100; //Convert the HSL into a single "brightness" representation return (float)(l * 0.5 + ((h / 360) * 50));