-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotiondetection.pde
81 lines (70 loc) · 1.87 KB
/
motiondetection.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
PShader modect;
PGraphics diffimg;
PImage prev;
boolean modect_preview = false;
boolean motioncheckpaused = true;
boolean firstcheck = true;
boolean motion = false;
int whitecount = 0;
int countNoMotion = -1;
float dummyval;
float modect_contrast = 5f;
int checkPauseDuration = 20;
int dectw = 256;
int decth = 144;
int maxNoMotion = 10;
int numHotPixels = 2;
void checkMotion() {
if(firstcheck) {
prev = cam.get(width/4, height/4, width/2, height/2);
firstcheck = false;
}
modect.set("destSampler", cam.get(width/4, height/4, width/2, height/2));
modect.set("srcSampler", prev);
modect.set("contrast", modect_contrast);
prev = cam.get(width/4, height/4, width/2, height/2);
diffimg.shader(modect);
diffimg.beginDraw();
diffimg.pushMatrix();
diffimg.translate(0, 0);
diffimg.noStroke();
diffimg.beginShape(QUAD);
diffimg.vertex(0, 0, 0, 0);
diffimg.vertex(dectw, 0, 1, 0);
diffimg.vertex(dectw, decth, 1, 1);
diffimg.vertex(0, decth, 0, 1);
diffimg.endShape();
diffimg.popMatrix();
diffimg.endDraw();
diffimg.resetShader();
resetShader();
diffimg.loadPixels();
thread("detectMotion");
}
void detectMotion() {
int whitecount = 0;
int s = diffimg.pixels.length/2;
int c;
for(int i=0; i < s; i++) {
c = (diffimg.pixels[s+i] >> 16) & 0xFF;
if(c > 128) {
whitecount++;
if(whitecount >= numHotPixels) break;
}
c = (diffimg.pixels[s-i] >> 16) & 0xFF;
if(c > 128) {
whitecount++;
if(whitecount >= numHotPixels) break;
}
}
println("modect: analyse motion: " +whitecount);
if(whitecount >= numHotPixels) {
motion = true;
pauseMotionCheck();
countNoMotion = -1;
firstcheck = true;
} else {
motion = false;
countNoMotion++;
}
}