图像的几何变换
3.图像的旋转
图像的旋转就是让图像按照某一点旋转到指定的角度。需要确定3个参数:图像的旋转中心、旋转角度和缩放因子。在openv中通过getRotationMatrix2D()函数来实现图像的旋转。
import cv2
import numpy as npimgpath = "images/img1.jpg"
img = cv2.imread(imgpath, 1)
img = cv2.resize(img, (0, 0), fx=0.5, fy=0.5)
height, width = img.shape[:2]# 得到变换矩阵
M = cv2.getRotationMatrix2D((width/2, height/2), 45, 1) #中心旋转45度,不缩放
# 旋转变换
translated_img = cv2.warpAffine(img, M, (width, height))
cv2.imshow('Original Image', img)
cv2.imshow('Translated Image', translated_img)
cv2.waitKey(0)
4.图像的镜像变换
图像的镜像变换分为两种:水平镜像和垂直镜像。在opencv中通过flip()函数实现。
import cv2
import numpy as npimgpath = "images/img1.jpg"
img = cv2.imread(imgpath, 1)
img = cv2.resize(img, (0, 0), fx=0.25, fy=0.25) x_img = cv2.flip(img, 1) #X轴,水平翻转
y_img = cv2.flip(img, 0) #Y洲,垂直翻转
xy_img = cv2.flip(img, -1)#水平垂直同时翻转
cv2.imshow("img", img)
cv2.imshow("x_img", x_img)
cv2.imshow("y_img", y_img)
cv2.imshow("xy_img", xy_img)
cv2.waitKey(0)