我使用了以下代码来旋转图像(initial image)并进行一些处理:
def rotate_image(mat, angle):
"""
Rotates an image (angle in degrees) and expands image to avoid cropping
"""
height, width = mat.shape[:2] # image shape has 3 dimensions
image_center = (width/2, height/2) # getRotationMatrix2D needs coordinates in reverse order (width, height) compared to shape
rotation_mat = cv2.getRotationMatrix2D(image_center, angle, 1.)
# rotation calculates the cos and sin, taking absolutes of those.
abs_cos = abs(rotation_mat[0,0])
abs_sin = abs(rotation_mat[0,1])
# find the new width and height bounds
bound_w = int(height * abs_sin + width * abs_cos)
bound_h = int(height * abs_cos + width * abs_sin)
# subtract old image center (bringing image back to origo) and adding the new image center coordinates
rotation_mat[0, 2] += bound_w/2 - image_center[0]
rotation_mat[1, 2] += bound_h/2 - image_center[1]
# rotate image with the new bounds and translated rotation matrix
rotated_mat = cv2.warpAffine(mat, rotation_mat, (bound_w, bound_h))
return rotated_mat
ModifiedVersionRotation = rotate_image(img, 35)
cv2.imwrite("lenarot.jpg", ModifiedVersionRotation)
这个函数在图像上添加了黑色边框,这样它就不会在旋转rotated image时被裁剪,这正是我真正需要的。但是,我如何旋转回图像并删除黑色边框呢?
1条答案
按热度按时间vohkndzv1#
对于旋转回来,我们可以计算逆变换,并将其应用于旋转后的图像:
示例:
完整代码示例:
ModifiedVersionRotation
:unrotated_img
:原始图像:
“恢复”的图像看起来很模糊,因为每个扭曲(向前和向后)都应用了双线性插值,使图像有点模糊。