Skip to content

Commit

Permalink
Updated the RGB to HSL code to calucate correctly
Browse files Browse the repository at this point in the history
git-svn-id: https://android-motion-detection.googlecode.com/svn/trunk@9 996b93ed-3e4d-4b21-1044-74bd19560d1d
  • Loading branch information
[email protected] committed Aug 28, 2011
1 parent 7faf772 commit 28952dc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
8 changes: 5 additions & 3 deletions src/com/jwetherell/motion_detection/detection/Comparer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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;
Expand Down
21 changes: 9 additions & 12 deletions src/com/jwetherell/motion_detection/detection/MotionDetection.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,24 @@ 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) {
if (first==null || mPrevious==null) return false;
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) {
Expand All @@ -52,6 +47,8 @@ protected static boolean isDifferentComparingState(int[] first, int width, int h
Log.d(TAG, output);
}

mPreviousState = state;

return different;
}

Expand Down
22 changes: 7 additions & 15 deletions src/com/jwetherell/motion_detection/image/ImageProcessing.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -82,22 +82,15 @@ 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));
float range = maxComponent - minComponent;

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) {
Expand All @@ -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));
Expand Down

0 comments on commit 28952dc

Please sign in to comment.