-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputerVisionBlobDetectionImprovedLifeSpan_processing.pde
More file actions
148 lines (132 loc) · 4.46 KB
/
ComputerVisionBlobDetectionImprovedLifeSpan_processing.pde
File metadata and controls
148 lines (132 loc) · 4.46 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import processing.video.*;
Capture video;
ArrayList<Blob> currentBlobs = new ArrayList<>();
ArrayList<Blob> storedBlobs = new ArrayList<>();
int idCounter = 0;
int colConstant = 150;
color c = color(255, 0, 0);
void setup(){
size(1100, 620);
video = new Capture(this, width, height);
video.start();
}
void captureEvent(Capture video){
video.read();
}
void draw(){
video.loadPixels();
image(video, 0, 0);
//to clear the old currentBlobs
currentBlobs.clear();
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
if(distSq(red(c), green(c), blue(c), red(video.get(x, y)), green(video.get(x, y)), blue(video.get(x, y))) < colConstant*colConstant){
// to make the blob bigger or smaller
boolean exist = false;
for(Blob b : currentBlobs){
if(b.isNear(x, y)){
b.add(x, y);
exist = true;
break;
}
}
// if there are no currentBlobs or no near currentBlobs we create a new blob
if(!exist || currentBlobs.isEmpty()){
currentBlobs.add(new Blob(x, y));
}
}
}
}
// to remove all the small current Blobs
//////////////////////////////////////////////////////////////////////////////////////
for(int i = 0; i < currentBlobs.size(); i++){
if(currentBlobs.get(i).area() < 1500){
currentBlobs.remove(i);
}
}
//////////////////////////////////////////////////////////////////////////////////////
// Match the stored blobs with the current blobs
//////////////////////////////////////////////////////////////////////////////////////
// if there are no stored blobs and there is current blobs
if(storedBlobs.isEmpty() && currentBlobs.size() > 0){
for(Blob b : currentBlobs){
b.setID(++idCounter);
b.resetLife();
storedBlobs.add(b);
}
println("add new blob");
}
else if(storedBlobs.size() <= currentBlobs.size()){ // In case we have the same number or less blobs each frame
for(Blob sB : storedBlobs){ // the nested loops are to find the closest blob for each stored blob from the current blobs
float minDist = 100000;
Blob closestBlob = null;
for(Blob cB : currentBlobs){
PVector sBCenter = sB.getCenter();
PVector cBCenter = cB.getCenter();
float dist = PVector.dist(sBCenter, cBCenter);
if(dist < minDist){
minDist = dist;
closestBlob = cB;
}
}
if(closestBlob != null){
sB.become(closestBlob);
sB.resetLife();
closestBlob.isTaken = true;
}
}
if(storedBlobs.size() < currentBlobs.size()){// In case we added
for(Blob b : currentBlobs){
if(!b.isTaken){
b.setID(++idCounter);
storedBlobs.add(b);
}
}
}
}
else if(storedBlobs.size() > currentBlobs.size()){ // In case we removed
for(Blob cB : currentBlobs){ // the nested loops are to find the closest blob for each stored blob from the current blobs
float minDist = 100000;
Blob closestBlob = null;
for(Blob sB : storedBlobs){
PVector sBCenter = sB.getCenter();
PVector cBCenter = cB.getCenter();
float dist = PVector.dist(sBCenter, cBCenter);
if(dist < minDist){
minDist = dist;
closestBlob = sB;
}
}
if(closestBlob != null){
closestBlob.become(cB);
closestBlob.resetLife();
closestBlob.isAlone = false;
}
}
for(int i = 0; i < storedBlobs.size(); i++){
Blob temp = storedBlobs.get(i);
if(temp.isAlone){
if(temp.isDead()){
storedBlobs.remove(i);
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////
// display each stored blob
//////////////////////////////////////////////////////////////////////////////////////
for(Blob b : storedBlobs){
if(b.getLifeSpan() == b.lifeSpanConstant){
b.displayId();
b.isAlone = true; // crutial to reset isAlone and make it true
}
}
//////////////////////////////////////////////////////////////////////////////////////
textAlign(LEFT);
textSize(24);
text("S: " + storedBlobs.size(), 10, 25);
text("C: " + currentBlobs.size(), 10, 50);
}
float distSq(float x1, float y1, float z1, float x2, float y2, float z2){ // as this function doesn't have sqrt so it is much faster than dist()
return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) + (z1 - z2)*(z1 - z2);
}