opencv几何变换(3):cv2.warpAffine,cv2.resize,cv2.getRotationMatrix2D,cv2.getAffineTransform,cv2.getPerspe

tech2024-07-26  42

目录

 

1.图像平移:cv2.warpAffine(src,M,dsize,dst=None,flag=None,borderMode=None,borderValue=None)

2.图像缩放:cv2.resize(src,dsize,dst=None,fx=None,fy=None,interpolation=None)

3.图像旋转变换矩阵:M=cv2.getRotationMatrix2D(center,angle,scale)

4.图像仿射变换:M=cv2.getAffineTransform(src,dst)

5.透视变换:M=cv2.getPerspectiveTransform(src,dst)


1.图像平移:cv2.warpAffine(src,M,dsize,dst=None,flag=None,borderMode=None,borderValue=None)

src为输入图像;M为变换矩阵一般反映平移或旋转的关系,为InputArray类型的2x3的变换矩阵;dsize为输出图像的大小;flags是插值方法,默认值为cv2.INTER_LINEAR表示线性插值,此外还有cv2.INTER_AREA(区域插值),cv2.INTER_NEAREST(最近邻插值),cv2.INTER_CUBIC(三次样条插值),cv2.INTER_LANCZOS4(Lanczos插值),borderMode边界像素模式为int类型;boarderValue是边界填充值,默认情况下为0

import cv2 import numpy as np img = cv2.imread('img2.png') # 构造移动矩阵H # 在x轴方向移动多少距离,在y轴方向移动多少距离 H = np.float32([[1, 0, 50], [0, 1, 25]]) rows, cols = img.shape[:2] print(img.shape) print(rows, cols) # 注意这里rows和cols需要反置,即先列后行 res = cv2.warpAffine(img, H, (2*cols, 2*rows)) cv2.imshow('origin_picture', img) cv2.imshow('new_picture', res) cv2.waitKey(0) cv2.destroyAllWindows()

2.图像缩放:cv2.resize(src,dsize,dst=None,fx=None,fy=None,interpolation=None)

src为原图;dsize为输出图像尺寸,与比例因子二选一;fx为沿水平轴的比例因子;fy为沿垂直轴的比例因子;interpolation为插值方法

img = cv2.imread('img2.png') # 方法一:通过设置缩放比例,来对图像进行放大或缩小 res1 = cv2.resize(img, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC) height, width = img.shape[:2] # 方法二:直接设置图像的大小,不需要缩放因子 #cv2.INTER_NEAREST(最近邻插值) cv2.INTER_AREA (区域插值) cv2.INTER_CUBIC(三次样条插值) cv2.INTER_LANCZOS4(Lanczos插值) res2 = cv2.resize(img, (int(0.8*width), int(0.8*height)),interpolation=cv2.INTER_LANCZOS4) cv2.imshow('origin_picture', img)

3.图像旋转变换矩阵:M=cv2.getRotationMatrix2D(center,angle,scale)

center为图片的旋转中心;angle为旋转角度;scale为缩放比例,其中正值为逆时针,负值为顺时针,通过此操作得到变换矩阵M,再用cv2.warpAffine()进行旋转

import cv2 img=cv2.imread(r'D:\Downloads\img2.png',1) rows,cols=img.shape[:2] #参数1:旋转中心,参数2:旋转角度,参数3:缩放因子 #参数3正为逆时针,负值为正时针 M=cv2.getRotationMatrix2D((cols/2,rows/2),45,1) print(M) #第三个参数是输出图像的尺寸中心 dst=cv2.warpAffine(img,M,(cols,rows)) #dst=cv2.warpAffine(img,M,(cols,rows),borderValue=(255,255,255)) while(1): cv2.imshow('img', img) cv2.imshow('img1',dst) #0xFF==27 ESC if cv2.waitKey(1)&0xFF==27: break cv2.destroyAllWindows()

4.图像仿射变换:M=cv2.getAffineTransform(src,dst)

src和dst都是3*2矩阵,分别表示输入的3个点和输出的3个点,由此函数得到变换矩阵M

import cv2 import numpy as np #读取图片 src = cv2.imread(r'D:\Downloads\bird.png') #获取图像大小 rows, cols = src.shape[:2] #设置图像仿射变换矩阵 pos1 = np.float32([[50,50], [200,50], [50,200]]) pos2 = np.float32([[10,100], [200,50], [100,250]]) M = cv2.getAffineTransform(pos1, pos2) print(M) #图像仿射变换 result = cv2.warpAffine(src, M, (2*cols, 2*rows)) #显示图像 cv2.imshow("original", src) cv2.imshow("result", result) #等待显示 cv2.waitKey(0) cv2.destroyAllWindows()

5.透视变换:M=cv2.getPerspectiveTransform(src,dst)

透视变换(Perspective Transformation)是将成像投影到一个新的视平面(Viewing Plane),也称作投影映射(Projective Mapping)。src,dst分别为源图像和目标图像中的矩形四点坐标,得到变换矩阵M

import cv2 import numpy as np #读取图片 src = cv2.imread(r'D:\Downloads\bird.png') #获取图像大小 rows, cols = src.shape[:2] #设置图像透视变换矩阵 pos1 = np.array([[38,32],[50,32],[38,64],[50,64]],dtype = "float32") pos2 = np.array([[19,16],[25,16],[19,32],[25,32]],dtype = "float32") M = cv2.getPerspectiveTransform(pos1, pos2) #图像透视变换 result = cv2.warpPerspective(src, M, (cols,rows)) #显示图像 cv2.imshow("original", src) cv2.imshow("result", result) #等待显示 cv2.waitKey(0) cv2.destroyAllWindows()

 

 

最新回复(0)