/
dtsDiffMotion.pde
108 lines (93 loc) · 2.59 KB
/
dtsDiffMotion.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
class DiffMotion
{
private float avgX;
private float avgY;
private color curColor;
private color prvColor;
private PImage lastFrame;
int streamWidth, streamHeight, thresh;
float threshX, threshY, dampX, dampY;
GSCapture vStream = null;
DiffMotion(GSCapture _vStream, int _streamWidth, int _streamHeight)
{
vStream = _vStream;
threshX = threshY = 2;
dampX = dampY = 0.5;
thresh = 32;
streamWidth = _streamWidth;
streamHeight = _streamHeight;
lastFrame = createImage(streamWidth, streamHeight, RGB);
}
void init()
{
if(vStream!=null)
vStream.start();
}
PVector processFrame()
{
int diffSum = 0;
ArrayList<PVector> diffPixels = new ArrayList();
if(vStream.available())
{
vStream.read();
vStream.loadPixels();
lastFrame.loadPixels();
for(int x=0;x<streamWidth;x++)
{
for (int y=0;y<streamHeight;y++)
{
int i = y*streamWidth+x;
curColor = vStream.pixels[i];
prvColor = lastFrame.pixels[i];
// (1) ----------------------------
int r = (curColor>>16)&0xFF;
int g = (curColor>>8)&0xFF;
int b = curColor&0xFF;
int oldR = (prvColor>>16)&0xFF;
int oldG = (prvColor>>8)&0xFF;
int oldB = prvColor&0xFF;
// -----------------------------(1)
int diffR = max(0,(r-oldR)-thresh);
int diffG = max(0,(g-oldG)-thresh);
int diffB = max(0,(b-oldB)-thresh);
int tempSum = diffR+diffG+diffB;
if(tempSum>0)
{
diffSum += tempSum;
diffPixels.add(new PVector(x,y));
}
lastFrame.pixels[i] = curColor;
}
}
}
PVector avgPoint = avgDiffPixels(diffPixels);
// (2) -----------------------
float diffX = avgPoint.x - avgX;
float diffY = avgPoint.y - avgY;
if(abs(diffX)>threshX)
avgX+=diffX*dampX;
if(abs(diffY)>threshY)
avgY+=diffY*dampY;
// -------------------------(2)
return new PVector(avgX, avgY);
}
PVector avgDiffPixels(ArrayList<PVector> arr)
{
float sumx=0;
float sumy=0;
for(int i=0;i<arr.size();i++)
{
PVector c = (PVector)arr.get(i);
sumx+=c.x;
sumy+=c.y;
}
return new PVector(sumx/arr.size(),sumy/arr.size());
}
void finished()
{
vStream.stop();
vStream.delete();
}
}
//(1) http://processing.org/learning/library/framedifferencing.html
//(2) http://processing.org/learning/basics/easing.html