6、OpenCV4.4图像旋转及位移

对一个图像进行旋转和位移:

cv2.getRotationMatrix2D

cv2.warpAffine

# -*- coding: utf-8 -*-  
import cv2  
import numpy as np  
  
img = cv2.imread('1.png')  
x,y,c=img.shape  
rotMat = cv2.getRotationMatrix2D((x/2, y/2),45,1)  
#center – Center of the rotation in the source image.  
#angle – Rotation angle in degrees. Positive values   
#mean counter-clockwise rotation (the coordinate origin   
#is assumed to be the top-left corner).  
#scale – Isotropic scale factor.  
#map_matrix – The output affine transformation, 2x3 floating-point matrix.  
#在opencv中提供了cv2.getRotationMatrix2D函数获得变换矩阵。  
#第一参数指定旋转圆点;  
#第二个参数指定旋转角度;  
#第二个参数指定缩放比例;  
rotMat = np.float32([[1,0,100],[0,1,50]])  
#平移100 50  
afnImg = cv2.warpAffine(img,rotMat,(x,y))  
#dst(x,y)=src(a[0][0]*x+a[0][1]*y,a[1][0]*x+a[1][1]*y)  
#https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html?highlight=warpaffine#cv2.warpAffine  
#第三个参数是输出图像的大小。  
#第二个参数是变换矩阵;  
cv2.imshow('affine image',afnImg)  
cv2.imwrite('affine.png',afnImg)  
cv2.waitKey(0)  
cv2.destroyAllWindows()  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sxj731533730

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值