其他分享
首页 > 其他分享> > 图像处理(四):图像的几何变换

图像处理(四):图像的几何变换

作者:互联网

图像处理笔记总目录


1 图像缩放

缩放是对图像的大小进行调整,即使图像放大或缩小。

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

参数:

插值含义
cv2.INTER_LINEAR双线性插值法
cv2.INTER_NEAREST最近邻插值
cv2.INTER_AREA像素区域重采样(默认)
cv2.INTER_CUBIC双三次插值

示例:

import cv2.cv2 as cv
# 1. 读取图片
img1 = cv.imread("./image/dog.jpeg")
# 2.图像缩放
# 2.1 绝对尺寸
rows,cols = img1.shape[:2]
res = cv.resize(img1,(2*cols,2*rows),interpolation=cv.INTER_CUBIC)

# 2.2 相对尺寸
res1 = cv.resize(img1,None,fx=0.5,fy=0.5)

# 3 图像显示
# 3.1 使用opencv显示图像(不推荐)
cv.imshow("orignal",img1)
cv.imshow("enlarge",res)
cv.imshow("shrink)",res1)
cv.waitKey(0)

# 3.2 使用matplotlib显示图像
fig,axes=plt.subplots(nrows=1,ncols=3,figsize=(10,8),dpi=100)
axes[0].imshow(res[:,:,::-1])
axes[0].set_title("绝对尺度(放大)")
axes[1].imshow(img1[:,:,::-1])
axes[1].set_title("原图")
axes[2].imshow(res1[:,:,::-1])
axes[2].set_title("相对尺度(缩小)")
plt.show()

在这里插入图片描述

2 图像平移

图像平移将图像按照指定方向和距离,移动到相应的位置。

API:cv.warpAffine(img,M,dsize)
参数:

示例:需求是将图像的像素点移动(50,100)的距离

import numpy as np
import cv2.cv2 as cv
import matplotlib.pyplot as plt
# 1. 读取图像
img1 = cv.imread("./image/image2.jpg")

# 2. 图像平移
rows,cols = img1.shape[:2]
M = M = np.float32([[1,0,100],[0,1,50]])# 平移矩阵
dst = cv.warpAffine(img1,M,(cols,rows))

# 3. 图像显示
fig,axes=plt.subplots(nrows=1,ncols=2,figsize=(10,8),dpi=100)
axes[0].imshow(img1[:,:,::-1])
axes[0].set_title("原图")
axes[1].imshow(dst[:,:,::-1])
axes[1].set_title("平移后结果")
plt.show()

在这里插入图片描述

3 图像旋转

标签:imshow,axes,cv2,几何变换,图像处理,图像,img1,cv
来源: https://blog.csdn.net/weixin_45707277/article/details/121185423