|
Opencv里的图像翻转,平移,旋转,缩放,仿射及透视功能
cv2.flip() # 图像翻转
cv2.getRotationMatrix2D() #取得旋转角度的Matrix
cv2.GetAffineTransform(src, dst, mapMatrix) #取得图像仿射的matrix
cv2.warpAffine() #图像仿射
cv2.getPerspectiveTransform(src, dst) #取得图像透视的matrix
cv2.warpPerspective() #图像透视
import cv2
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
# flipCode==0垂直翻转(沿X轴翻转),
# flipCode>0水平翻转(沿Y轴翻转),
# flipCode<0水平垂直翻转(先沿X轴翻转,再沿Y轴翻转,等价于旋转180°)
- image = cv2.imread("ai111.vip.jpg")
- # Flipped Horizontally 水平翻转
- h_flip = cv2.flip(image, 1)
- # Flipped Vertically 垂直翻转
- v_flip = cv2.flip(image, 0)
- # Flipped Horizontally & Vertically 水平垂直翻转
- hv_flip = cv2.flip(image, -1)
- # flipCode==0垂直翻转(沿X轴翻转),
- # flipCode>0水平翻转(沿Y轴翻转),
- # flipCode<0水平垂直翻转(先沿X轴翻转,再沿Y轴翻转,等价于旋转180°)
- plt.figure(figsize=(16,16))
- plt.subplot(221)
- # ::-1 本来是BGR 现在倒过来就是RGB
- plt.imshow(image[:,:,::-1])
- plt.title('original')
- plt.subplot(222)
- plt.imshow(h_flip[:,:,::-1])
- plt.title('horizontal flip')
- plt.subplot(223)
- plt.imshow(v_flip[:,:,::-1])
- plt.title(' vertical flip')
- plt.subplot(224)
- plt.imshow(hv_flip[:,:,::-1])
- plt.title('h_v flip')
- # 调整子图间距
- # plt.subplots_adjust(wspace=0.5, hspace=0.1)
- plt.subplots_adjust(top=0.8, bottom=0.38, left=0.10, right=0.95, hspace=0,
- wspace=0.35)
- plt.show()
复制代码
图像平移,旋转,缩放 仿射变换 cv2.warpAffine()
- img = cv2.imread('ai111.vip.jpg')
- rows,cols = img.shape[:2]
- # 定义平移矩阵,需要是numpy的float32类型
- # x轴平移200,y轴平移100, 2*3矩阵
- M = np.float32([[1, 0, 200], [0, 1, 100]])
- # 用仿射变换实现平移
- img_shift = cv2.warpAffine(img, M, (cols, rows), borderValue=(0, 0, 255))
- # 第一个参数旋转中心,第二个参数旋转角度,第三个参数:缩放比例, 生成一2*3的矩阵
- M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
- M1 = cv2.getRotationMatrix2D((cols/2,rows/2),180,1)
- M2 = cv2.getRotationMatrix2D((cols/2,rows/2),60,1)
- M3 = cv2.getRotationMatrix2D((cols/2,rows/2),60,0.5)
- print(M)
- '''
- [[ 6.123234e-17 1.000000e+00 1.500000e+02]
- [-1.000000e+00 6.123234e-17 6.500000e+02]]
- '''
- #第三个参数:变换后的图像大小 宽度,高度
- img_rotat90 = cv2.warpAffine(img,M,(cols,rows))
- img_rotat180 = cv2.warpAffine(img,M1,(cols,rows))
- img_rotat60 = cv2.warpAffine(img,M2,(cols,rows), borderValue=(255, 0, 0))
- img_rotat60_scale = cv2.warpAffine(img,M3,(cols,rows))
- plt.figure(figsize=(16,16))
- plt.subplot(321)
- plt.imshow(img[:,:,::-1])
- plt.subplot(322)
- plt.imshow(img_shift[:,:,::-1])
- plt.subplot(323)
- plt.imshow(img_rotat90[:,:,::-1])
- plt.subplot(324)
- plt.imshow(img_rotat180[:,:,::-1])
- plt.subplot(325)
- plt.imshow(img_rotat60[:,:,::-1])
- plt.subplot(326)
- plt.imshow(img_rotat60_scale[:,:,::-1])
- # plt.subplots_adjust(top=0.8, bottom=0.08, left=0.10, right=0.95, hspace=0,
- # wspace=0.35)
- plt.subplots_adjust(top=0.8, bottom=0.38, left=0.10, right=0.95, hspace=0,
- wspace=0.35)
- plt.show()
复制代码
图像的旋转加上拉升就是图像仿射变换,仿射变化也是需要一个M矩阵就可以,
但是由于仿射变换比较复杂,一般直接找很难找到这个矩阵,
opencv提供了根据变换前后三个点的对应关系来自动求解M。
这个函数是M=cv2.getAffineTransform(pos1,pos2),其中两个位置就是变换前后的对应位置关系。
输出的就是仿射矩阵M。然后在使用函数cv2.warpAffine()
- img = cv2.imread('ai111.vip.jpg')
- rows,cols = img.shape[:2]
- pts1 = np.float32([[50,50],[200,50],[50,200]])
- pts2 = np.float32([[10,100],[200,20],[100,250]])
- M = cv2.getAffineTransform(pts1,pts2)
- print('仿射变换需要的矩阵shape:', M.shape, M)
- #第三个参数:变换后的图像大小 宽高互调了
- res = cv2.warpAffine(img,M,(rows,cols))
- plt.figure(figsize=(8, 8))
- plt.subplot(121)
- plt.imshow(img[:,:,::-1])
- plt.subplot(122)
- plt.imshow(res[:,:,::-1])
- print(img.shape, res.shape)
- plt.show()
复制代码
透视变换 视角变换 Perspective
视角变换,需要一个3*3变换矩阵。在变换前后要保证直线还是直线。
构建此矩阵需要在输入图像中找寻 4个点,以及在输出图像中对应的位置。这四个点中的任意三个点不能共线。
- img=cv2.imread('test4.jpg')
- rows,cols,ch=img.shape
- pts1=np.float32([[(203, 720), (585, 460), (695, 460), (1127, 720)]])
- pts2=np.float32([[(320, 720), (320, 0), (960, 0), (960, 720)]])
- M=cv2.getPerspectiveTransform(pts1,pts2)
- print('透视变换需要的矩阵shape:', M.shape, M)
- # img.shape[1::-1] 这里就是宽高互换啊 warpPerspective格式不同 inter_linear
- dst=cv2.warpPerspective(img,M,img.shape[1::-1], flags=cv2.INTER_LINEAR)
- plt.figure(figsize=(16,16))
- plt.subplot(121),plt.imshow(img[:,:,::-1]),plt.title('Input')
- plt.subplot(122),plt.imshow(dst[:,:,::-1]),plt.title('Output')
- plt.show()
复制代码
|
|