import cv2 import numpy as np import math def orientation ratio if rat

  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
import cv2
import numpy as np
import math
def orientation(ratio):
if ratio > 1 / 4:
return 0
elif ratio < - 1 / 4:
return 1
else:
return -1
def correct_point(orientation, arr):
if len(arr) > 0:
arr.sort(key=lambda item: item[0][0])
if orientation == 1:
return arr[len(arr) - 1][0]
elif orientation == 0:
return arr[0][0]
else:
return None
else:
return None
def clearColor(img, axes):
cols = cv2.split(img)
sum = np.zeros(cols[0].shape, np.float)
for i in cols:
sum = np.add(sum, i)
return [cv2.divide(cols[i].astype(float), sum) for i in axes]
cap = cv2.VideoCapture(0)
h, w = cap.read()[1].shape[:2]
norm_rect = ((w / 3, h / 2), (2 * w / 3, h))
while (1):
_, frame = cap.read()
cv2.rectangle(frame, norm_rect[0], norm_rect[1], (0, 255, 0), 2)
cv2.imshow('frame', frame)
k = cv2.waitKey(10)
if k >= 0:
break
cv2.destroyWindow('frame')
ret,frame = cap.read()
# setup initial location of window
r, h, c, w = h / 2, h / 2, w / 3, w / 3
# r,h,c,w = 250,90,400,125 # simply hardcoded the values
track_window = (c,r,w,h)
# set up the ROI for tracking
roi = frame[r:r+h, c:c+w]
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
# Setup the termination criteria, either 10 iteration or move by atleast 1 pt
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
c=0
while True:
_, frame = cap.read()
if frame is None:
continue
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
hue, sat, bri = cv2.split(hsv)
dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
ret, track_window = cv2.meanShift(dst, track_window, term_crit)
x,y,w,h = track_window
cx = x + w / 2
cy = y + h / 2
canny = cv2.Canny(hue, 50, 80, None, 3)
count, hie = cv2.findContours(canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
ccc = np.zeros(hue.shape[:2], np.uint8)
cv2.drawContours(ccc, count, -1, 255, 1)
ccc = cv2.dilate(ccc, np.ones((3, 3), np.uint8), iterations=5)
ccc = cv2.erode(ccc, np.ones((3, 3), np.uint8), iterations=1)
tmp1 = np.zeros(ccc.shape[:2], np.uint8)
tmp2 = np.zeros(ccc.shape[:2], np.uint8)
tmp = cv2.merge([ccc, tmp1, tmp2])
cv2.floodFill(tmp, None, (cx, cy), (0, 255))
tmp1 = cv2.split(tmp)[1]
tmp2 = np.copy(tmp1)
contours = cv2.findContours(tmp2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0]
th_area_min = 2000
th_area_max = 20000
tmp1 = cv2.cvtColor(tmp1, cv2.COLOR_GRAY2BGR)
k = []
max_area = 0
max_contour = []
for i in contours:
area = cv2.contourArea(i)
if max_area < area:
max_area = area
max_contour = i
if area < th_area_max and area > th_area_min:
cv2.drawContours(tmp1, [i], 0, (0, 255, 0), 1)
circle = cv2.minEnclosingCircle(i)
k = i
a = []
rows,cols = tmp1.shape[:2]
vx,vy,x,y = cv2.fitLine(max_contour, cv2.cv.CV_DIST_L2,0,0.01,0.01)
if vx > 0.0001 and max_area > 22000:
ratio = vy / vx
cv2.line(tmp1,(x,y),(x + 100*vx,y+100*vy), (0, 255, 0), 3)
else:
ratio = 0
for i in k:
i = i[0]
dist = math.sqrt((i[0] - circle[0][0]) ** 2 + (i[1] - circle[0][1]) ** 2)
if circle[1] - dist < 2:
a.append((i, dist))
point = correct_point(orientation(ratio), a)
if point is not None:
cv2.circle(tmp1, tuple(point), 5, (0, 0, 255), 2)
cv2.imshow('green', tmp1)
#cv2.imshow('3', ccc)
k = cv2.waitKey(10)
if k == 13:
cv2.imwrite('./photos/' + str(c) + '.png', cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY))
c += 1
if k == 27:
break
cv2.destroyAllWindows()
cap.release()