优秀的编程知识分享平台

网站首页 > 技术文章 正文

OpenCV学习之路-33. 图像的复合变换

nanyue 2024-08-08 18:46:36 技术文章 13 ℃

图像的复合变换是指对给定的图像连续进行多次上述的平移、旋转、翻转、缩放、错切等基本变换,也称为级联变换。

对给定图像按一定顺序执行若干次基本变换,其变换矩阵仍然可以用 3x3 阶变换矩阵表示。进一步地,对图像依次执行基本变换 F1、F2、…Fn 的变换矩阵分别为 M1、M2、…Mn,可以证明,以图像中心点为中心进行比例、旋转进行变换,则复合变换的变换矩阵 M 等于各基本变换矩阵依次相乘所得到的组合矩阵:

缩放:

旋转:

平移:

扭变:


镜变:

基本例程:1.42 图像的复合变换

# 1.42 图像的复合变换
img = cv2.imread("../images/imgLena.tif") # 读取彩色图像(BGR)
height, width = img.shape[:2] # 图片的高度和宽度
# (1) 缩放
fx, fy = 0.6, 0.6
MAZ = np.float32([[fx, 0, 0], [0, fy, 0]]) # 构造缩放变换矩阵
imgT1 = cv2.warpAffine(img, MAZ, (width, height)) # 仿射变换, 黑色填充
# (2) 平移
dx, dy = 50, 200 # dx=100 向右偏移量, dy=50 向下偏移量
MAT = np.float32([[1, 0, dx], [0, 1, dy]]) # 构造平移变换矩阵
imgT2 = cv2.warpAffine(imgT1, MAT, (width, height), borderValue=(0,255,255)) # 实现仿射变换
# (3) 旋转
theta = -30 * np.pi / 180 # 逆时针旋转 30°
cosTheta = np.cos(theta)
sinTheta = np.sin(theta)
MAR = np.float32([[cosTheta, -sinTheta, 0], [sinTheta, cosTheta, 0]]) # 构造旋转变换矩阵
imgT3 = cv2.warpAffine(imgT2, MAR, (width, height), borderValue=(255,255,0)) # 实现仿射变换
# (4) 扭曲
theta = -30 * np.pi / 180 # 逆时针扭变 30°
MAS = np.float32([[1, np.tan(theta), 0], [0, 1, 0]]) # 构造扭变变换矩阵
imgT4 = cv2.warpAffine(imgT3, MAS, (width, height), borderValue=(255,0,255)) # 实现仿射变换
plt.figure(figsize=(9,6))
plt.subplot(221), plt.axis('off'), plt.title("T1:Zoom")
plt.imshow(cv2.cvtColor(imgT1, cv2.COLOR_BGR2RGB)),
plt.subplot(222), plt.axis('off'), plt.title("T2:Translation")
plt.imshow(cv2.cvtColor(imgT2, cv2.COLOR_BGR2RGB))
plt.subplot(223), plt.axis('off'), plt.title("T3:Rotation")
plt.imshow(cv2.cvtColor(imgT3, cv2.COLOR_BGR2RGB))
plt.subplot(224), plt.axis('off'), plt.title("T4:Shear")
plt.imshow(cv2.cvtColor(imgT4, cv2.COLOR_BGR2RGB))
plt.show()

(本节完)

最后

【OpenCV学习之路】是针对Python OpenCV学习所打造的一场刷题狂欢party! 对基础知识把握不牢固的话,欢迎来学习嗷~喜欢的话就抓紧收藏起来吧!

如果对学习没有自制力或者没有一起学习交流的动力,欢迎私我“进群”,邀请你来我的学习交流群,我们一起交流学习,报团打卡!

Tags:

最近发表
标签列表