Lesson3-1:OpenCV图像处理---几何变换

几何变换

学习目标

  • 掌握图像的缩放,平移,旋转等
  • 了解数字图像的仿射变换和透射变换

1 图像缩放

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

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

参数:

  • src : 输入图像
  • dsize: 绝对尺寸,直接指定调整后图像的大小
  • fx,fy: 相对尺寸,将dsize设置为None,然后将fxfy设置为比例因子即可
  • interpolation:插值方法,
    在这里插入图片描述
  1. 示例
import 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 图像平移

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

  1. API
cv.warpAffine(img,M,dsize)

参数:

  • img: 输入图像
  • M 2 ∗ 3 2∗3 23移动矩阵
    对于(x,y)处的像素点,要把它移动到 ( x + t x , y + t y ) (x+t_{x},y+t_{y}) (x+tx,y+ty)处时, M M M矩阵应如下设置:
    M = [ 1 0 t x 0 1 t y ] M= \begin{bmatrix} %%不用输入对齐方式,默认是居中对齐 1&0&t_{x}\\ 0&1&t_{y}\\ && \end{bmatrix} M= 1001txty

注意:将 M M M设置为 n p . f l o a t 32 np.float32 np.float32类型的 N u m p y Numpy Numpy数组。

  • dsize: 输出图像的大小

注意:输出图像的大小,它应该是(宽度,高度)的形式。请记住, w i d t h width width=列数, h e i g h t height height=行数。

  • 示例

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

import numpy as np
import 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 图像旋转

图像旋转是指图像按照某个位置转动一定角度的过程,旋转中图像仍保持这原始尺寸。图像旋转后图像的水平对称轴、垂直对称轴及中心坐标原点都可能会发生变换,因此需要对图像旋转中的坐标进行相应转换。
那图像是怎么进行旋转的呢?如下图所示:
在这里插入图片描述
假设图像逆时针旋转 θ θ θ,则根据坐标转换可得旋转转换为:
{ x ′ = r c o s ( α − θ ) y ′ = r s i n ( α − θ ) \begin{cases} x^{'} =rcos(α - θ)\\ y^{'} = rsin(α - θ) \end{cases} {x=rcos(αθ)y=rsin(αθ)

其中:
r = x 2 + y 2 , s i n α y x 2 + y 2 , c o s α x x 2 + y 2 r=\sqrt{x^{2}+y^{2}} , sinα\frac{y}{\sqrt{x^{2}+y^{2}}},cosα\frac{x}{\sqrt{x^{2}+y^{2}}} r=x2+y2 ,sinαx2+y2 ycosαx2+y2 x
带入上面的公式中,有:
{ x ′ = x c o s θ + y s i n θ y ′ = − x s i n θ + y c o s θ \begin{cases} x^{'} = x cosθ+ysinθ \\ y^{'} = -x sinθ+ycosθ \end{cases} {x=xcosθ+ysinθy=xsinθ+ycosθ
也可以写成:
[ x ′ y ′ 1 ] = [ x y 1 ] [ c o s θ − s i n θ 0 s i n θ c o s θ 0 0 0 1 ] \begin{bmatrix} x^{'} & y^{'} & 1 \\ \end{bmatrix}= \begin{bmatrix} x & y & 1 \\ \end{bmatrix} \begin{bmatrix} cosθ& -sinθ &0 \\ sinθ & cosθ & 0\\ 0 & 0 & 1 \end{bmatrix} [xy1]=[xy1] cosθsinθ0sinθcosθ0001
同时我们要修正原点的位置,因为原图像中的坐标原点在图像的左上角,经过旋转后图像的大小会有所变化,原点也需要修正。

假设在旋转的时候是以旋转中心为坐标原点的,旋转结束后还需要将坐标原点移到图像左上角,也就是还要进行一次变换。
在这里插入图片描述
[ x ′ ′ y ′ ′ 1 ] = [ x ′ y ′ 1 ] [ 1 0 0 0 − 1 0 l e f t t o p 1 ] \begin{bmatrix} x^{''} & y^{''} & 1 \\ \end{bmatrix}= \begin{bmatrix} x^{'} & y^{'} & 1 \\ \end{bmatrix} \begin{bmatrix} 1& 0 &0 \\ 0 & -1 & 0\\ left & top & 1 \end{bmatrix} [x′′y′′1]=[xy1] 10left01top001
= [ x y 1 ] = [ c o s θ − s i n θ 0 s i n θ c o s θ 0 0 0 1 ] [ 1 0 0 0 − 1 0 l e f t t o p 1 ] = \begin{bmatrix} x & y& 1 \\ \end{bmatrix}= \begin{bmatrix} cosθ& -sinθ &0 \\ sinθ & cosθ & 0\\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} 1& 0 &0 \\ 0 & -1 & 0\\ left & top & 1 \end{bmatrix} =[xy1]= cosθsinθ0sinθcosθ0001 10left01top001

在OpenCV中图像旋转首先根据旋转角度和旋转中心获取旋转矩阵,然后根据旋转矩阵进行变换,即可实现任意角度和任意中心的旋转效果。

  1. API
cv2.getRotationMatrix2D(center, angle, scale)

参数:

  • center:旋转中心
  • angle:旋转角度
  • scale:缩放比例

返回:

  • M:旋转矩阵

调用cv.warpAffine完成图像的旋转

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

# 2 图像旋转
rows,cols = img.shape[:2]
# 2.1 生成旋转矩阵
M = cv.getRotationMatrix2D((cols/2,rows/2),90,1)
# 2.2 进行旋转变换
dst = cv.warpAffine(img,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()

在这里插入图片描述

4 仿射变换

图像的仿射变换涉及到图像的形状位置角度的变化,是深度学习预处理中常到的功能,仿射变换主要是对图像的缩放,旋转,翻转和平移等操作的组合。

那什么是图像的仿射变换,如下图所示,图1中的点1, 2 和 3 与图二中三个点一一映射, 仍然形成三角形, 但形状已经大大改变,通过这样两组三点(感兴趣点)求出仿射变换, 接下来我们就能把仿射变换应用到图像中所有的点中,就完成了图像的仿射变换。
在这里插入图片描述
在OpenCV中,仿射变换的矩阵是一个 2 × 3 2×3 2×3的矩阵,
M = [ A B ] = [ a 00 b 01 b 0 a 10 b 11 b 1 ] M= \begin{bmatrix} A & B \\ \end{bmatrix}= \begin{bmatrix} a_{00} & b_{01} & b_{0} \\ a_{10} & b_{11} & b_{1} \end{bmatrix} M=[AB]=[a00a10b01b11b0b1]

其中左边的 2 × 2 2×2 2×2子矩阵 A A A是线性变换矩阵,右边的2×1子矩阵 B B B是平移项:
A = [ a 00 b 01 a 10 b 11 ] , B = [ b 0 b 1 ] A= \begin{bmatrix} a_{00} & b_{01} \\ a_{10} & b_{11} \end{bmatrix}, B= \begin{bmatrix} b_{0} \\ b_{1} \end{bmatrix} A=[a00a10b01b11],B=[b0b1]
对于图像上的任一位置 ( x , y ) (x,y) (x,y),仿射变换执行的是如下的操作:
T a f f i n e = A [ x y ] + B = M [ x y 1 ] T_{affine}=A \begin{bmatrix} x \\ y \end{bmatrix} +B=M \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} Taffine=A[xy]+B=M xy1

需要注意的是,对于图像而言,宽度方向是x,高度方向是y,坐标的顺序和图像像素对应下标一致。所以原点的位置不是左下角而是右上角,y的方向也不是向上,而是向下。

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

示例

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

# 2 仿射变换
rows,cols = img.shape[:2]
# 2.1 创建变换矩阵
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[100,100],[200,50],[100,250]])
M = cv.getAffineTransform(pts1,pts2)
# 2.2 完成仿射变换
dst = cv.warpAffine(img,M,(cols,rows))

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

在这里插入图片描述

5 透射变换

透射变换是视角变化的结果,是指利用透视中心、像点、目标点三点共线的条件,按透视旋转定律使承影面(透视面)绕迹线(透视轴)旋转某一角度,破坏原有的投影光线束,仍能保持承影面上投影几何图形不变的变换。
在这里插入图片描述
它的本质将图像投影到一个新的视平面,其通用变换公式为:
[ x ′ y ′ 1 ] = [ u v w ] [ a 00 a 01 a 02 a 10 a 11 a 12 a 20 a 21 a 22 ] \begin{bmatrix} x^{'} & y^{'} & 1 \\ \end{bmatrix}= \begin{bmatrix} u & v& w \\ \end{bmatrix} \begin{bmatrix} a_{00} & a_{01} & a_{02} \\ a_{10} & a_{11} & a_{12} \\ a_{20} & a_{21} & a_{22} \end{bmatrix} [xy1]=[uvw] a00a10a20a01a11a21a02a12a22
其中, ( u , v ) (u,v) (u,v)是原始的图像像素坐标, w w w取值为 1 1 1 ( x = x ′ / z ′ , y = y ′ / z ′ ) (x=x'/z',y=y'/z') (x=x/z,y=y/z)是透射变换后的结果。后面的矩阵称为透视变换矩阵,一般情况下,我们将其分为三部分:
T = [ a 00 a 01 a 02 a 10 a 11 a 12 a 20 a 21 a 22 ] = [ T 1 T 2 T 3 a 22 ] T= \begin{bmatrix} a_{00} & a_{01} & a_{02} \\ a_{10} & a_{11} & a_{12} \\ a_{20} & a_{21} & a_{22} \end{bmatrix}= \begin{bmatrix} T_{1} &T_{2} \\ T_{3} & a_{22} \\ \end{bmatrix} T= a00a10a20a01a11a21a02a12a22 =[T1T3T2a22]
其中: T 1 T_{1} T1表示对图像进行线性变换, T 2 T_{2} T2对图像进行平移, T 3 T_{3} T3表示对图像进行投射变换, a 22 a_{22} a22一般设为1.

在opencv中,我们要找到四个点,其中任意三个不共线,然后获取变换矩阵T,再进行透射变换。通过函数cv.getPerspectiveTransform找到变换矩阵,将cv.warpPerspective应用于此 3 x 3 3x3 3x3变换矩阵。

  1. 示例
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 读取图像
img = cv.imread("./image/image2.jpg")
# 2 透射变换
rows,cols = img.shape[:2]
# 2.1 创建变换矩阵
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)
# 2.2 进行变换
dst = cv.warpPerspective(img,T,(cols,rows))

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

在这里插入图片描述

6 图像金字塔

图像金字塔是图像多尺度表达的一种,最主要用于图像的分割,是一种以多分辨率来解释图像的有效但概念简单的结构。

图像金字塔用于机器视觉和图像压缩,一幅图像的金字塔是一系列以金字塔形状排列的分辨率逐步降低,且来源于同一张原始图的图像集合。其通过梯次向下采样获得,直到达到某个终止条件才停止采样。

金字塔的底部是待处理图像的高分辨率表示,而顶部是低分辨率的近似,层级越高,图像越小,分辨率越低。
在这里插入图片描述

  1. API
cv.pyrUp(img)       #对图像进行上采样
cv.pyrDown(img)        #对图像进行下采样
  1. 示例
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 图像读取
img = cv.imread("./image/image2.jpg")
# 2 进行图像采样
up_img = cv.pyrUp(img)  # 上采样操作
img_1 = cv.pyrDown(img)  # 下采样操作
# 3 图像显示
cv.imshow('enlarge', up_img)
cv.imshow('original', img)
cv.imshow('shrink', img_1)
cv.waitKey(0)
cv.destroyAllWindows()

在这里插入图片描述
总结

  1. 图像缩放:对图像进行放大或缩小
cv.resize()
  1. 图像平移:

指定平移矩阵后,调用cv.warpAffine()平移图像

  1. 图像旋转:

调用cv.getRotationMatrix2D获取旋转矩阵,然后调用cv.warpAffine()进行旋转

  1. 仿射变换:

调用cv.getAffineTransform将创建变换矩阵,最后该矩阵将传递给cv.warpAffine()进行变换

  1. 透射变换:

通过函数cv.getPerspectiveTransform()找到变换矩阵,将cv.warpPerspective()进行投射变换

  1. 金字塔

图像金字塔是图像多尺度表达的一种,使用的API:

cv.pyrUp(): 向上采样

cv.pyrDown(): 向下采样
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Next---YOLO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值