def get_normalized_image(img, rr, debug = False):
box = cv2.boxPoints(rr)
extbox = cv2.boundingRect(box)
if extbox[2] * extbox[3] > img.shape[0] * img.shape[1]:
print("Too big proposal: {0}x{1}".format(extbox[2], extbox[3]))
return None, None
extbox = [extbox[0], extbox[1], extbox[2], extbox[3]]
extbox[2] += extbox[0]
extbox[3] += extbox[1]
extbox = np.array(extbox, np.int)
extbox[0] = max(0, extbox[0])
extbox[1] = max(0, extbox[1])
extbox[2] = min(img.shape[1], extbox[2])
extbox[3] = min(img.shape[0], extbox[3])
tmp = img[extbox[1]:extbox[3], extbox[0]:extbox[2]]
center = (tmp.shape[1] / 2, tmp.shape[0] / 2)
rot_mat = cv2.getRotationMatrix2D( center, rr[2], 1 )
if tmp.shape[0] == 0 or tmp.shape[1] == 0:
return None, rot_mat
if debug:
vis.draw_box_points(img, np.array(extbox, dtype="int"), color = (0, 255, 0))
cv2.imshow('scaled', img)
rot_mat[0,2] += rr[1][0] /2.0 - center[0]
rot_mat[1,2] += rr[1][1] /2.0 - center[1]
try:
norm_line = cv2.warpAffine( tmp, rot_mat, (int(rr[1][0]), int(rr[1][1])), borderMode=cv2.BORDER_REPLICATE )
except:
return None, rot_mat
return norm_line, rot_mat
评论列表
文章目录