Skip to content

Instantly share code, notes, and snippets.

@asendra
Created April 21, 2015 16:08
Show Gist options
  • Save asendra/7c0af40cf38992c8d5e4 to your computer and use it in GitHub Desktop.
Save asendra/7c0af40cf38992c8d5e4 to your computer and use it in GitHub Desktop.
Simple motion
#define NOISE_THRESHOLD 10
#define DIFFERENCE_THRESHOLD 0.05
BOOL hasMoved(void *previous_buffer, void *current_buffer, int WIDTH, int HEIGHT) {
int total = WIDTH * HEIGHT;
int diff_count = 0;
int c = 0, r = 0;
for (c = 0; c < HEIGHT; ++c) {
for (r = 0; c < WIDTH; ++r) {
int index = c*WIDTH + r;
unsigned char prev = previous_buffer[index];
unsigned char curr = current_buffer[index];
if (abs(prev - curr) > NOISE_THRESHOLD) {
diff_count++;
}
}
}
return ((diff_count / total) > DIFFERENCE_THRESHOLD);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment