import cv2
import numpy as np
from math import *
img=cv2.imread('C:/Users/xpp/Desktop/Lena.png')
img[:,:,0]=255
def rotate(image,angle):
height, width,channels=image.shape
#变换后新图像的大小是原图像旋转后的轮廓外接矩形,注意这个外接矩形的长和高也是水平和竖直的
heightNew=int(width*fabs(sin(radians(angle)))+height*fabs(cos(radians(angle))))
widthNew=int(height*fabs(sin(radians(angle)))+width*fabs(cos(radians(angle))))
matRotation=cv2.getRotationMatrix2D((width / 2, height / 2), angle, 1)#(2,3)
#变换矩阵的中心点相当于平移一样 原图像的中心点与新图像的中心点的相对位置
matRotation[0,2]+=(widthNew-width)/2
matRotation[1,2]+=(heightNew-height)/2
imgRotation=None
if channels==1:
imgRotation=cv2.warpAffine(img,matRotation,(widthNew,heightNew),borderValue=(255))
elif channels==3:
imgRotation=cv2.warpAffine(img,matRotation,(widthNew,heightNew),borderValue=(255,255,255))
return imgRotation
imgNew=rotate(img,60)
cv2.imshow('result',imgNew)
cv2.waitKey(0)
算法:图像保真旋转是旋转后的新图像包含了原图像所有内容,没有像素缺失的情况。 文献:马文伟, & 王强. (2018). 一种应用于图像旋转的图像保真的方法及装置. CN109034153A. 链接:https://blog.csdn.net/watkinsong/article/details/10212715?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param
本文分享自 图像处理与模式识别研究所 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!