def _rotate_image(self, mat, angle, width, height):
big = max(width, height)
small = min(width, height)
center = (big / 2.0) - (small / 2.0)
trans = numpy.float32([[1, 0, 0], [0, 1, 0]])
trans2 = numpy.float32([[1, 0, 0], [0, 1, 0]])
if small == width:
trans[0, 2] = center
trans2[1, 2] = -center - 1
else:
trans[1, 2] = center
trans2[0, 2] = -center - 1
# first enlarge the image to a square, translating the pixels to the new center
mat = cv2.warpAffine(mat, trans, (big, big))
# then rotate on the new center
rot = cv2.getRotationMatrix2D((big / 2, big / 2), angle, 1)
mat = cv2.warpAffine(mat, rot, (big, big))
# finally translate back to the start and resize to the new size
return cv2.warpAffine(mat, trans2, (height, width))
评论列表
文章目录