-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcentroid.py
65 lines (57 loc) · 1.82 KB
/
centroid.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import cv2
def bimg(img):
'''
need the binary video for contour length calculation here the image is already gray which is mask
:param img: image is masked hsv image in this case(which is already gray)
:return: the binary of it
'''
_, maskbin = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
return maskbin
def xlen(a):
'''
:param a: it is the masked image for the specific color (in this case mask)
:return: the weighted x length of all the contours
'''
contours, _ = cv2.findContours(bimg(a), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
i = 0
cx = 0
totalarea=0
while (i<len(contours)-1):
area=cv2.contourArea(contours[i]) #selecting the ith contour
M=cv2.moments(contours[i])
totalarea=totalarea+area
if(area!=0):
''' here x is the respective weighted X cordinate of the contour '''
x=area*(int(M['m10']/M['m00']))
cx=cx+x
i=i+1
else:
i=i+1
if(totalarea!=0):
return cx/totalarea
else:
return 0
def ylen(a):
'''
:param a: it is the masked image for the specific color (in this case mask)
:return: the weighted x length of all the contours
'''
contours, _ = cv2.findContours(bimg(a), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
i = 0
cy = 0
totalarea=0
while (i < len(contours) - 1):
area = cv2.contourArea(contours[i]) # selecting the ith contour
M = cv2.moments(contours[i])
totalarea=totalarea+area
if(area!=0):
''' here x is the respective weighted Y cordinate of the contour '''
y = area * (int(M['m01'] / M['m00']))
cy = cy + y
i = i + 1
else:
i=i+1
if(totalarea!=0):
return cy/totalarea
else:
return 0