opencv图像处理之几何变换

1.缩放

  • cv2.resize()
  • 插值方法flag的值有:INTER_AREA、INTER_CUBIC、INTER_LINEAR等
def scale(self):
	#【1】使用缩放系数
    res1=cv2.resize(self.img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC)
	#【2】设定目标图像大小
    height,width=self.img.shape[:2]
    res2=cv2.resize(self.img,(2*width,2*height),interpolation=cv2.INTER_CUBIC)

    print(self.img.shape)
    print(res1.shape)
    print(res2.shape)

缩放前后的图像尺寸变化:放大了2倍。

(640, 640, 3)
(1280, 1280, 3)
(1280, 1280, 3)

2.平移

  • cv2.warpAffine(img,M,(width,height))

平移矩阵的形式:
M = [ 1 0 t x 0 1 t y ] M=\begin{bmatrix} 1 &0&t_x \\ 0 & 1&t_y \\ \end{bmatrix} M=[1001txty]

def translate(self):
    rows,cols,ch=self.img.shape
    M=np.float32([[1,0,100],[0,1,50]])#x方向平移100个像素,y方向平移50个像素
    dst=cv2.warpAffine(self.img,M,(cols,rows))#第二个参数是变换矩阵,第三个参数是输出图像的宽高。
    print(self.img.shape)
    print(dst.shape)

(640, 640, 3)
(640, 640, 3)

在这里插入图片描述

3.旋转

  • cv2.getRotationMatrix2D()
  • cv2.warpAffine()

旋转 θ \theta θ的矩阵:
M = [ c o s θ − s i n θ s i n θ c o s θ ] M=\begin{bmatrix} cos\theta & -sin\theta \\ sin\theta & cos\theta \\ \end{bmatrix} M=[cosθsinθsinθcosθ]
opencv还允许在任意位置旋转:
α = s c a l e ⋅ c o s θ \alpha=scale\cdot cos\theta α=scalecosθ β = s c a l e ⋅ s i n θ \beta=scale\cdot sin\theta β=scalesinθ M = [ α β ( 1 − α ) ⋅ c e n t e r . x − β ⋅ c e n t e r . y − β α β ⋅ c e n t e r . x + ( 1 − α ) ⋅ c e n t e r . x ] M=\begin{bmatrix} \alpha & \beta & (1-\alpha)\cdot center.x-\beta\cdot center.y \\ -\beta & \alpha & \beta \cdot center.x+(1-\alpha)\cdot center.x\\ \end{bmatrix} M=[αββα(1α)center.xβcenter.yβcenter.x+(1α)center.x]
除了 θ \theta θ,我们还需要指定center和scale。

def rotate(self):
    rows=self.img.shape[0]
    cols=self.img.shape[1]
    M=cv2.getRotationMatrix2D((cols/2,rows/2),45,0.6)
    #这里的第一个参数为旋转中心,第二个为旋转角度,第三个为旋转后的缩放因子
    
    dst=cv2.warpAffine(self.img,M,(2*cols,2*rows))
    #第三个参数是输出图像的尺寸中心
    
    print(self.img.shape)
    print(dst.shape)

(640, 640, 3)
(1280, 1280, 3)

在这里插入图片描述

4.仿射变换

  • cv2.getAffineTransform()
  • cv2.warpAffine()

在仿射变换中,原图中所有的平行线在结果图像中同样平行。为了创建这个矩阵我们需要从原图像中找到三个点以及他们在输出图像中的位置。然后cv2.getAffineTransform 会创建一个2x3 的矩阵,最后这个矩阵会被传给函数cv2.warpAffine。

def affine(self):
    rows,cols,ch=self.img.shape
    pts1=np.float32([[50,50],[200,50],[50,200]])
    pts2=np.float32([[10,100],[200,50],[100,240]])
    M=cv2.getAffineTransform(pts1,pts2)
    dst=cv2.warpAffine(self.img,M,(cols,rows))
    print(self.img.shape)
    print(dst.shape)

(640, 640, 3)
(640, 640, 3)

在这里插入图片描述
在这里插入图片描述

5.透视变换

  • cv2.getPerspectiveTransform()
  • cv2.warpPerspective()

对于视角变换,我们需要一个3x3 变换矩阵。在变换前后直线还是直线。要构建这个变换矩阵,你需要在输入图像上找4 个点,以及他们在输出图像上对应的位置。这四个点中的任意三个都不能共线。这个变换矩阵可以由函数cv2.getPerspectiveTransform() 构建。然后把这个矩阵传给函数cv2.warpPerspective。
我的这个图片案例其实效果不太明显。可以查看相机标定那里的效果。

def perspective(self):
    rows,cols,ch=self.img.shape
    pts1=np.float32([[56,65],[368,52],[28,387],[389,390]])
    pts2=np.float32([[0,0],[300,0],[0,300],[300,300]])
    M=cv2.getPerspectiveTransform(pts1,pts2)
    dst=cv2.warpPerspective(self.img,M,(300,300))
    print(self.img.shape)
    print(dst.shape)

(640, 640, 3)
(300, 300, 3)

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值