图像的几何变换,缩放,平移,旋转、仿射和透视

本文详细介绍了使用OpenCV进行图像的几何变换,包括缩放、平移和旋转操作,并展示了如何实现仿射变换和透视变换,通过代码实例深入理解图像处理技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、图像的几何变换,缩放,平移,旋转

#图像的几何变换,缩放,平移,旋转、仿射、透视
import cv2 as cv
import numpy as np
img1 = cv.imread("E:\\python opencv\\demo2\\a.jpg")
cv.imshow("image", img1)

#缩放  cv.resize()
res = cv.resize(img1, None, fx=1, fy=2, interpolation=cv.INTER_CUBIC)
res1 = cv.resize(img1, None, fx=0.5, fy=1, interpolation=cv.INTER_AREA)
res2 = cv.resize(img1, None, fx=0.5, fy=0.5, interpolation=cv.INTER_LINEAR)
#或者
height, width = img1.shape[:2]
res3 = cv.resize(img1, (2*width, 2*height), interpolation=cv.INTER_LINEAR)
cv.imshow("res", res)
cv.imshow("res1", res1)
cv.imshow("res2", res2)
cv.imshow("res3", res3)

#平移 np.float32    cv.waroAffine
rows, cols = img1.shape[:2]
M = np.float32([[1, 0, 100], [0, 1, 50]])
dst = cv.warpAffine(img1, M, (cols, rows))
cv.imshow('dst', dst)

#旋转 (),旋转角度,比例
M1 = cv.getRotationMatrix2D(((cols-1)/2.0, (rows-1)/2.0), 90, 0.5)
dst1 = cv.warpAffine(img1, M1, (cols, rows))
cv.imshow('dst1', dst1)
cv.waitKey(0)
cv.destroyAllWindows()

二、仿射和透视

#仿射和透视,在输入图像和输出图像中找三个对应的点
#创建一个2*3转换矩阵,然后旋转
#透视就是找4个对应的点,生成3*3转换矩阵,然后旋转
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img=cv.imread("E:\\python opencv\\demo2\\a.jpg")
rows, cols, ch = img.shape
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[10, 100], [200, 50], [100, 250]])
M = cv.getAffineTransform(pts1, pts2)
dst = cv.warpAffine(img, M, (cols, rows))
plt.subplot(121), plt.imshow(img), plt.title('Input')
plt.subplot(122), plt.imshow(dst), plt.title('Output')
plt.show()
cv.waitKey()
cv.destroyAllWindows()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值