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
|
#!/bin/python
#python 3.6.0
import numpy
import cv2
import math
import sys
def thresh (img, minv, maxv) :
ret, thresh = cv2.threshold(img, minv, 255, cv2.THRESH_BINARY)
ret, ithresh = cv2.threshold(img, maxv, 255, cv2.THRESH_BINARY_INV)
return cv2.bitwise_and(thresh,ithresh)
img = cv2.imread(sys.argv[1])
#blur, kernel is the distance of pixels the rurrounding ones are
#29 seems to be upper lmit on effectiveness.
himg = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(himg)
# i think the range for red is 0 10, 170 179
#blue is 195-135, 40? really that much?
#hue only testing, not very effective.
# better with saturation
thresh = cv2.bitwise_and(cv2.bitwise_or(thresh(h,0,10),thresh(h,169,180)), thresh(s,60,255))
#thresh = cv2.bitwise_and(thresh(h,95,135), thresh(s,60,255))
#thresh = cv2.GaussianBlur(thresh,(29,29),0,0,cv2.BORDER_REFLECT_101)
cv2.imwrite("stallmask.jpg",thresh);
mask = cv2.merge((thresh,thresh,thresh))
masked = cv2.bitwise_and(himg, mask);
cv2.imwrite("stallmasked.jpg",cv2.cvtColor(masked,cv2.COLOR_HSV2BGR));
#only takes 1 cannel arrays?
im2, con, heir = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
v = 0
for i in range(0, len(con)) :
if (cv2.contourArea(con[i]) > 2000) :
a = cv2.contourArea(con[i])
cv2.drawContours(masked, con, i, (120,255,255), 5)
(cx, cy), r = cv2.minEnclosingCircle(con[i])
p = 1- ( a / (math.pi * math.pow(r, 2)))
c = (int(cx),int(cy))
cv2.circle(masked, c,int(r), (150,255,255), 5)
if (p < .35) :
print("yee")
#find find frst child
#her in list mode returns the array inside of another array
if ( heir[0][i][2] != -1 ):
j = heir[0][i][2]
while (j != -1) :
cv2.drawContours(masked, con, j, (160,255,255), 3)
cp = 0
cp = cp + cv2.contourArea(con[j])
j = heir[0][j][0]
cp = cp/a
else :
cp = 0
print ("child area perc")
print(cp)
if (cp > 0.0005) :
cv2.putText(masked, "p", c, cv2.FONT_HERSHEY_PLAIN, 10, (0,255,255),cv2.LINE_AA)
else :
cv2.putText(masked, 'c', c, cv2.FONT_HERSHEY_PLAIN, 10, (0,255,255),cv2.LINE_AA)
print(p)
v = v + 1
else :
cv2.drawContours(masked, con, i, (80,255,255), 1)
cv2.imwrite("stallmaskedcon.jpg",cv2.cvtColor(masked,cv2.COLOR_HSV2BGR))
print("properly sized contours")
print(v)
print("all contours")
print(len(con))
|