-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob.py
52 lines (42 loc) · 1.34 KB
/
blob.py
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
class Blob:
'''
Blob is an object that each Blob at least have 25pixels(min_pixels)
and minimum luminance of Blob is 180(tau)
'''
def __init__(self):
self.center = [0, 0]
# list of center coordinates
self.pixels = []
# coordinates of blob's pixels [(x, y)]
def add(self, x, y):
'''
this function add the pixel with coordination(x, y) to the Blob
'''
# update the center
self.center[0] = (self.center[0] * self.mass() + x) / (self.mass() + 1)
self.center[1] = (self.center[1] * self.mass() + y) / (self.mass() + 1)
self.pixels.append((x, y))
def mass(self):
'''
Returns the number of Blobs pixels
'''
return len(self.pixels)
def distanceTo(self, c):
'''
Returns distance between two Blobs
'''
dx = (c.center[0] - self.center[0]) ** 2
dy = (c.center[1] - self.center[1]) ** 2
return (dx + dy) ** 0.5
def __str__(self):
'''
Returns the number of Blobs pixels and the coordination of Blobs center
'''
return str(self.mass()) + ' (%s, %s)' % (str(self.center[0]), str(self.center[1]))
if __name__ == '__main__':
b = Blob()
print(b)
tau = 180
# color threshold
min_pixels = 25
# min of pixels to make a blob