這兩種方法在旋轉圖像時,可能會產生一些不同的效果:
rotate_image_new()旋轉后的圖像完全包含旋轉前的內容,并且填充邊界盡可能小文章來源:http://www.zghlxwxcb.cn/news/detail-682386.html
rotate_image() 保持原始圖像的大小,并根據填充選項決定是否填充邊界為白色。如果 if_fill_white 參數為 True,則填充邊界為白色;否則,邊界將保持原始圖像的值。這種方法可以更快速地旋轉圖像,但可能會導致旋轉后的圖像包含額外的空白區(qū)域或丟失部分圖像信息。文章來源地址http://www.zghlxwxcb.cn/news/detail-682386.html
def rotate_image_new(image, degree):
'''
旋轉圖片角度
'''
from math import *
# dividing height and width by 2 to get the center of the image
height, width = image.shape[:2]
heightNew = int(width * fabs(sin(radians(degree))) + height * fabs(cos(radians(degree))))
widthNew = int(height * fabs(sin(radians(degree))) + width * fabs(cos(radians(degree))))
matRotation = cv2.getRotationMatrix2D((width / 2, height / 2), degree, 1)
matRotation[0, 2] += (widthNew - width) / 2 # 重點在這步,目前不懂為什么加這步
matRotation[1, 2] += (heightNew - height) / 2 # 重點在這步
imgRotation = cv2.warpAffine(image, matRotation, (widthNew, heightNew), borderValue=(255, 255, 255))
return imgRotation
def rotate_image( image, angle,if_fill_white = False):
'''
順時針旋轉
'''
# dividing height and width by 2 to get the center of the image
height, width = image.shape[:2]
# get the center coordinates of the image to create the 2D rotation matrix
center = (width / 2, height / 2)
# using cv2.getRotationMatrix2D() to get the rotation matrix
rotate_matrix = cv2.getRotationMatrix2D(center=center, angle=angle, scale=1)
# rotate the image using cv2.warpAffine
if not if_fill_white:
rotated_image = cv2.warpAffine(src=image, M=rotate_matrix, dsize=(width, height) )
else:
color = (255, 255) if len(image.shape)==2 else (255, 255,255)
rotated_image = cv2.warpAffine(src=image, M=rotate_matrix, dsize=(width, height), borderValue=color)
return rotated_image
到了這里,關于旋轉圖片兩種方法的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!