Python3+OpenCV(三):图像的几何变换



1、缩放

作用:调整图像的大小

cv2.resize(src, dst, dsize, fx=0, fy=0, interpolation=INTER_LINEAR)

src:原图像(输入)
dst:改变后的图像(输出)
dsize:输出图像大小
如果这个参数不为0,那么就代表将原图像缩放到这个Size(width,height)指定的大小;如果这个参数为0,那么原图像缩放之后的大小就要通过下面的公式来计算:dsize = Size(round(fxsrc.cols), round(fysrc.rows))
fx:width方向的缩放比例,如果它是0,那么它就会按照(double)dsize.width/src.cols来计算
fy:height方向的缩放比例,如果它是0,那么它就会按照(double)dsize.height/src.rows来计算
interpolation:指定插值的方式(图像缩放后重新计算像素的方式)
默认使用INTER_LINEAR(双线性插值)

import cv2
import numpy as np

# 图像的缩放
img = cv2.imread("D:/Study/digital image processing/test/Lena.bmp")
res_sf = cv2.resize(img,None,fx=0.5,fy=0.5,interpolation=cv2.INTER_LINEAR)
cv2.imshow("img",img)
cv2.imshow("res_sf",res_sf)

cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

2、平移

通过numpy来产生移动矩阵,并将其赋值给仿射函数cv2.warpAffine()
作用:改变图像的位置(沿着x方向tx距离,y方向ty距离)

cv2.warpAffine(src, M, dsize, dst=Node, flags=None, borderMode=None, borderValue=None)

src:输入图像
M:变换矩阵
dsize:输出图像的大小
flags:插值方法的组合
borderMode:边界像素模式
borderValue:边界填充值(默认情况下,它为0)

# 图像的平移
img = cv2.imread("D:/Study/digital image processing/test/Lena.bmp")
# 保存图像的垂直尺寸(高)和水平尺寸(宽)
height, width = img.shape[:2]
#图像沿x方向平移100,沿y方向平移100
M = np.float32([[1,0,100],[0,1,100]])
res_py = cv2.warpAffine(img,M,(height,width))
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(res_py)
plt.show()

在这里插入图片描述

3、旋转

作用:将图像按照一定的角度进行旋转,同时可根据参数的设定来对图像按比例进行缩放

cv2.getRotationMatrix2D(center, angle, scale)

center:旋转中心
angle:旋转角度
scale:缩放比例

# 图像的旋转
img = cv2.imread("D:/Study/digital image processing/test/Lena.bmp")
# 保存图像的垂直尺寸(高)和水平尺寸(宽)
height, width = img.shape[:2]
# 以(height/2,width/2)为中心旋转45° 图像缩放比例=1(大小不变)
M = cv2.getRotationMatrix2D((height/2,width/2),45,1)
res_xz = cv2.warpAffine(img,M,(height,width))
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(res_xz)
plt.show()

在这里插入图片描述

4、镜像

作用:图像翻转(水平翻转、垂直翻转)

cv2.flip(src, flipCode, dst=None)

flipCode=-1 水平垂直翻转
flipCode= 0 垂直翻转
flipCode= 1 水平翻转

# 图像镜像
img = cv2.imread("D:/Study/digital image processing/test/Lena.bmp")
res_y = cv2.flip(img,1,dst=None) # 水平镜像
res_x = cv2.flip(img,0,dst=None) # 垂直镜像
res_z = cv2.flip(img,-1,dst=None) # 对角镜像

plt.subplot(221)
plt.imshow(img)
plt.title("Original")
plt.xticks([]), plt.yticks([])
plt.subplot(222)
plt.imshow(res_y)
plt.title("res_y")
plt.xticks([]), plt.yticks([])
plt.subplot(223)
plt.imshow(res_x)
plt.title("res_x")
plt.xticks([]), plt.yticks([])
plt.subplot(224)
plt.imshow(res_z)
plt.title("res_z")
plt.xticks([]), plt.yticks([])
plt.show()

在这里插入图片描述

5、仿射

作用:给定三个点变换前和变换后的坐标,返回相应的变化矩阵

cv.getAffineTransform(src, dst)

src:输入图像的三个点坐标
dst:输出图像的三个点坐标
三个点分别对应左上角、右上角、左下角

# 图像的仿射
img = cv2.imread("D:/Study/digital image processing/test/rec1.bmp")
# 保存图像的垂直尺寸(高)和水平尺寸(宽)
height, width = img.shape[:2]
# 变换前坐标
pts1 = np.float32([[50,50],[200,50],[50,200]])
# 变化后坐标
pts2 = np.float32([[100,100],[200,50],[100,250]])
# 变化矩阵M
M = cv2.getAffineTransform(pts1,pts2)
dst = cv2.warpAffine(img,M,(height,width))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()

在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值