同样要使用cv2.warpAffine()进行仿射变换。

 

一、方法

 

步骤一:可以通过函数cv2.getRotationMatrix2D()获取转换矩阵

在使用之前,可以通过函数cv2.getRotationMatrix2D()获取转换矩阵。

 

M = cv2.getRotationMatrix(center, angle, scale)

 

center: 旋转中心

angle:旋转角度。正数表示逆时针旋转,负数表示顺时针旋转。

scale: 缩放尺度。

 

例如:要以图中心为圆心,逆时针旋转45度,并将目标图像缩小为原来的0.6倍,则可以使用下列语句生成M:

 

M = cv2.getRotationMatrix((height/2, width/2), 45, 0.6)

 

步骤二:调用cv2.warpAffine()进行仿射变换

res = cv2.warpAffine(img, M, (width, height))

 

二、代码

仿射变换(2):旋转

 

 

 

结果:

 

仿射变换(2):旋转

 

 

 

 

相关文章: