def checkButton(self, img, x1, y1, x2, y2):
btn1 = img[y1:y2, x1:x2]
btn1 = cv2.cvtColor(btn1, cv2.COLOR_BGR2GRAY)
if self.thresh_change_trigger:
ret, mask = cv2.threshold(btn1, 0, 255, cv2.THRESH_BINARY_INV +
cv2.THRESH_OTSU)
self.thresh_val.setText(str(ret))
self.THRESH = ret
else:
ret, mask = cv2.threshold(btn1, self.THRESH, 255,
cv2.THRESH_BINARY_INV)
try:
(_, cnts, _) = cv2.findContours(mask, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
except Exception, e:
(cnts, _) = cv2.findContours(mask, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
ci = 0
max_area = 0
if cnts:
for i in range(len(cnts)):
cnt = cnts[i]
area = cv2.contourArea(cnt)
if(area > max_area):
max_area = area
ci = i
cnt = cnts[ci]
else:
cnt = None
self.flags.isSet_prev = self.flags.isSet_cur
if cnt is not None:
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 0), 1)
hull = cv2.convexHull(cnt)
cv2.drawContours(btn1, [hull], 0, (0, 0, 255), 1)
self.flags.isSet_cur = True
else:
cv2.rectangle(img, (x1, y1), (x2, y2), (188, 188, 137), 1)
self.flags.isSet_cur = False
return img
评论列表
文章目录