def _find_a_thing(self, c, min_height, max_height, min_width, max_width, max_distance, debug_img=None):
rect = cv2.minAreaRect(c)
box = cv2.cv.BoxPoints(rect) if is_cv2() else cv2.boxPoints(rect)
top,bottom,left,right,center = self.find_dimensions(np.int0(np.array(box)))
if top is None or left is None or center is None:
return None
vertical = self.find_distance(top, bottom)
horizontal = self.find_distance(left, right)
away = self.find_distance(center, None)
if vertical > horizontal:
height = vertical
width = horizontal
flipped = False
else:
height = horizontal
width = vertical
flipped = True
if height < min_height or height > max_height:
return None
if width < min_width or width > max_height:
return None
if away > max_distance:
return None
# This page was helpful in understanding angle
# https://namkeenman.wordpress.com/2015/12/18/open-cv-determine-angle-of-rotatedrect-minarearect/
angle = rect[2]
if rect[1][0] < rect[1][1]:
angle -= 90.0
if debug_img is not None:
x,y,w,h = cv2.boundingRect(c)
cv2.drawContours(debug_img, [c], -1, (0, 255, 0), 2)
cv2.drawContours(debug_img, [np.int0(np.array(box))], -1, (0, 0, 255), 2)
cv2.rectangle(debug_img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.circle(debug_img, top, 5, (255, 255, 0))
cv2.circle(debug_img, bottom, 5, (255, 255, 0))
cv2.circle(debug_img, left, 5, (255, 255, 0))
cv2.circle(debug_img, right, 5, (255, 255, 0))
cv2.circle(debug_img, center, 5, (255, 255, 0))
return Thing(height, width, center, angle)
评论列表
文章目录