def classify_monitor_contour_set(contours):
'''Not a general purpose function : given the expectation of a set of strongly related contours for one monitor...'''
# First pass : compute the center of mass of every contour
classified = {}
for (i,c) in enumerate(contours):
classified[i] = {}
classified[i]['contour'] = c
moments = M = cv2.moments(c)
classified[i]['com'] = (int(M['m10']/M['m00']), int(M['m01']/M['m00']))
rect = contour_to_monitor_coords(c)
(maxWidth, maxHeight, dest, Mwarp) = compute_warp(rect)
classified[i]['rect'] = rect
classified[i]['maxWidth'] = maxWidth
classified[i]['maxHeight'] = maxHeight
classified[i]['dest'] = dest
classified[i]['Mwarp'] = Mwarp
# Second pass : establish if c-o-m of every contour is within the first contour
reference_contour = contours[0]
for (i,c) in enumerate(contours):
classified[i]['coherent'] = cv2.pointPolygonTest(reference_contour, classified[i]['com'], False)
# Final pass : report on the set
print('$'*80)
for (i,c) in enumerate(contours):
print('%d : c-o-m %s : coherent : %d mw %d mh %d' % (i,
classified[i]['com'],
classified[i]['coherent'],
classified[i]['maxWidth'],
classified[i]['maxHeight'],
))
print('$'*80)
# From the contours coherent to the reference contour, build an average/best estimator
count = 0
rect = np.zeros((4, 2), dtype = "float32")
for (i,c) in enumerate(contours):
if classified[i]['coherent'] == 1:
count += 1
for j in range(0,4):
rect[j] += classified[i]['rect'][j]
#pdb.set_trace()
for j in range(0,4):
# BUG to show Alison
# rect[j] = (rect[j]/1.0*count).astype('uint8')
rect[j] = (rect[j]/(1.0*count)).astype('uint32')
time.sleep(2.5)
return rect
评论列表
文章目录