1、图像缩放
缩放是对图像的⼤⼩进⾏调整,即使图像放⼤或缩⼩。
cv2.resize(src,dsize,fx=0,fy=0,interpolation=cv2.INTER_LINEAR)
参数: src : 输⼊图像 dsize: 绝对尺⼨,直接指定调整后图像的⼤⼩ fx,fy: 相对尺⼨,将 dsize 设置为 None ,然后将 fx 和 fy 设置为⽐例因⼦即可
interpolation :插值⽅法,cv.INTER_AREA用于缩小,cv.INTER_CUBIC和cv.INTER_LINEAR**用于缩放。默认情况下,出于所有调整大小的目的,使用的插值方法为cv.INTER_LINEAR。
def test01():
# 显示中文设置字体
mpl.rcParams['font.family'] = 'SimHei'
img = cv.imread('cat.jpg')
print(img.shape) # (236, 236, 3)
# 绝对尺寸
height, width = img.shape[:2]
img01 = cv.resize(img,(2*width, 2*height),interpolation=cv.INTER_CUBIC)
print(img01.shape) # (100, 100, 3)
# 相对尺寸
img02 = cv.resize(img,None,fx=0.5,fy=0.5,interpolation=cv.INTER_AREA)
print(img02.shape) # (354, 354, 3)
plt.subplot(131),plt.imshow(img[:,:,::-1]),plt.title('原图')
plt.subplot(132),plt.imshow(img01[:,:,::-1]),plt.title('放大后')
plt.subplot(133),plt.imshow(img02[:,:,::-1]),plt.title('缩小后')
plt.show()
2、图像平移
图像平移就是将图像沿着x轴和y轴方向移动一定的距离
cv.warpAffine(img,M,(cols,rows))
参数:
img: 输⼊图像 M : 2∗3 移动矩阵
(cols,rows)输出图像的大小,其形式应为(width,height),
width
=列数,height
=行数。
def test02():
img = cv.imread('cat.jpg')
print(img.dtype)
# 平移矩阵
M = np.float32([[1,0,80],[0,1,50]])
img01 = cv.warpAffine(img,M,(img.shape[1],img.shape[0]))
# 图像显示
# 显示中文设置字体
mpl.rcParams['font.family'] = 'SimHei'
_,axes = plt.subplots(1,2)
axes[0].imshow(img)
axes[0].set_title('原图')
axes[1].imshow(img01)
axes[1].set_title("平移后")
plt.show()
3 、图像旋转
图像旋转是指图像按照某个位置转动⼀定⻆度的过程,旋转中图像仍保持这原始尺
⼨。图像旋转后图像的⽔平对称轴、垂直对称轴及中⼼坐标原点都可能会发⽣变
换,因此需要对图像旋转中的坐标进⾏相应转换。
在OpenCV中图像旋转⾸先根据旋转⻆度和旋转中⼼获取旋转矩阵,然后根据旋转
矩阵进⾏变换,即可实现任意⻆度和任意中⼼的旋转效果。
cv2.getRotationMatrix2D(center, angle, scale)
参数:
center:旋转中⼼ angle:旋转⻆度 scale:缩放⽐例
返回: M:旋转矩阵
调⽤cv.warpAffine完成图像的旋转
def test03():
img = cv.imread('cat.jpg')
# 创建旋转矩阵
M = cv.getRotationMatrix2D((img.shape[1]/2, img.shape[0]/2), 45, 0.8)
# 旋转变换
img01 = cv.warpAffine(img, M, (img.shape[1],img.shape[0]))
mpl.rcParams['font.family'] = 'SimHei'
# 图像展示
_,axes = plt.subplots(1,2)
axes[0].imshow(img[:,:,::-1])
axes[0].set_title("原图")
axes[1].imshow(img01[:,:,::-1])
axes[1].set_title("旋转后")
plt.show()
4、仿射变换
图像的仿射变换涉及到图像的形状位置⻆度的变化,是深度学习预处理中常到的功
能,仿射变换主要是对图像的缩放,旋转,翻转和平移等操作的组合。
在仿射变换中,原始图像中的所有平行线在输出图像中仍将平行。为了找到变换矩阵,我们需要输入图像中的三个点及其在输出图像中的对应位置。然后cv.getAffineTransform将创建一个2x3矩阵,该矩阵将传递给cv.warpAffine。
def test04():
img = cv.imread('cat.jpg')
# 创建变换矩阵
pst1 = np.float32([[50,50],[200,50],[50,200]])
pst2 = np.float32([[100,100],[200,50],[100,250]])
M = cv.getAffineTransform(pst1,pst2)
# 仿射变换
img01 = cv.warpAffine(img,M,(img.shape[1],img.shape[0]))
# 图像展示
mpl.rcParams['font.family'] = 'SimHei'
_,axes = plt.subplots(1,2)
axes[0].imshow(img[:,:,::-1])
axes[0].set_title("原图")
axes[1].imshow(img01[:,:,::-1])
axes[1].set_title("仿射变换后")
plt.show()
5 、透射变换
透射变换是视⻆变化的结果,是指利⽤透视中⼼、像点、⽬标点三点共线的条件,
按透视旋转定律使承影⾯(透视⾯)绕迹线(透视轴)旋转某⼀⻆度,破坏原有的
投影光线束,仍能保持承影⾯上投影⼏何图形不变的变换。
要找到此变换矩阵,需要在输入图像上有4个点,在输出图像上需要相应的点。在这四个点中,其中三个不应共线。然后可以通过函数cv.getPerspectiveTransform找到变换矩阵。然后将cv.warpPerspective应用于此3x3转换矩阵。
def test05():
img = cv.imread('cat.jpg')
# 创建变换矩阵
pts1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])
pts2 = np.float32([[100, 145], [300, 100], [80, 290], [310, 300]])
T = cv.getPerspectiveTransform(pts1,pts2)
# 进行变换
img01 = cv.warpPerspective(img,T,(img.shape[1],img.shape[0]))
# 图像展示
mpl.rcParams['font.family'] = 'SimHei'
_,axes = plt.subplots(1,2)
axes[0].imshow(img[:,:,::-1])
axes[0].set_title("原图")
axes[1].imshow(img01[:,:,::-1])
axes[1].set_title("透射变换后")
plt.show()
6、图像金字塔
具有不同分辨率的图像集称为“图像金字塔”(因为当它们堆叠在底部时,最高分辨率的图像位于顶部,最低分辨率的图像位于顶部时,看起来像金字塔)。
def test06():
img = cv.imread('cat.jpg')
lower_reso = cv.pyrUp(img)
higher_reso = cv.pyrDown(lower_reso)
# 图像展示
mpl.rcParams['font.family'] = 'SimHei'
_,axes = plt.subplots(1,3)
axes[0].imshow(img[:,:,::-1])
axes[0].set_title("img")
axes[1].imshow(lower_reso[:,:,::-1])
axes[1].set_title("lower_reso")
axes[2].imshow(higher_reso[:,:,::-1])
axes[2].set_title("higher_reso")
plt.show()
img不等于higher_reso,因为img一旦降低了分辨率,就会丢失信息