代码如下:
#encoding:utf-8
import numpy as np
import cv2
image = cv2.imread("H:\\img\\lena.jpg")
cv2.imshow("Original",image)
M = np.float32([[1,0,25],[0,1,50]])#平移矩阵1:向x正方向平移25,向y正方向平移50
shifted = cv2.warpAffine(image,M,(image.shape[1],image.shape[0]))
cv2.imshow("Shifted Down and Right",shifted)
cv2.waitKey(0)
M = np.float32([[1,0,-50],[0,1,-90]])#平移矩阵2:向x负方向平移-50,向y负方向平移-90
shifted = cv2.warpAffine(image,M,(image.shape[1],image.shape[0]))
cv2.imshow("Shifted Up and Left",shifted)
cv2.waitKey(0)
结果如下:
1、原图像
2、x-25,y-50
3、x-50,y-90
代码如下:
#encoding:utf-8
import numpy as np
import cv2
image = cv2.imread("H:\\img\\lena.jpg")
cv2.imshow("Original",image)
cv2.waitKey(0)
(h,w) = image.shape[:2]
center = (w / 2,h / 2)
#旋转45度,缩放0.75
M = cv2.getRotationMatrix2D(center,45,0.75)#旋转缩放矩阵:(旋转中心,旋转角度,缩放因子)
rotated = cv2.warpAffine(image,M,(w,h))
cv2.imshow("Rotated by 45 Degrees",rotated)
cv2.waitKey(0)
#旋转-45度,缩放1.25
M = cv2.getRotationMatrix2D(center,-45,1.25)#旋转缩放矩阵:(旋转中心,旋转角度,缩放因子)
rotated = cv2.warpAffine(image,M,(w,h))
cv2.imshow("Rotated by -90 Degrees",rotated)
cv2.waitKey(0)
结果如下:
1、原图像
2、theta-45,0.75
3、theta- -45,1.25
代码如下:
#encoding:utf-8
import cv2
#
#图像翻转
#
image = cv2.imread("H:\\img\\lena.jpg")
cv2.imshow("Original",image)
cv2.waitKey(0)
#图像水平翻转
flipped = cv2.flip(image,1)
cv2.imshow("Flipped Horizontally", flipped)
cv2.waitKey(0)
#图像垂直翻转
flipped = cv2.flip(image,0)
cv2.imshow("Flipped Vertically", flipped)
cv2.waitKey(0)
#图像水平垂直翻转
flipped = cv2.flip(image,-1)
cv2.imshow("Flipped Horizontally & Vertically", flipped)
cv2.waitKey(0)