一、简介
这里主要介绍如何应用图像的几何变换,如平移,旋转,放射变换等。opencv提供2个转换函数分别是cv.warpAffine和cv.warpPerspecive,可以使用这2个函数实现各种转换。
二、缩放
opencv使用cv.resize()函数实现缩放,对于该函数的插值参数比较合适的设置是cv.INTER_AREA用于缩小,cv.INTER_CUBIC(较耗时),cv.INTER_LINEAR用于放大。默认情况下插值使用cv.INTER_LINEAR。
以下是示例代码:
import numpy as np
import cv2 as cv
img = cv.imread('./img/messi.jpg')
res = cv.resize(img,None,fx=0.5, fy=0.5, interpolation = cv.INTER_CUBIC)
#OR
#height, width = img.shape[:2]
#res = cv.resize(img,(0.5*width, 0.5*height), interpolation = cv.INTER_CUBIC)
cv.imshow("img", img)
cv.imshow("res", res)
cv.waitKey(0)
cv.destroyAllWindows()
三、平移
图像转换是移动目标位置,如果知道移动方向(x,y)以及需要移动到(
t
x
,
t
y
t_x, t_y
tx,ty),那么需要创建的矩阵M如下:
M
=
[
1
0
t
x
0
1
t
y
]
M=[\begin{matrix} 1 & 0 & t_x \\ 0 & 1 & t_y \end{matrix} ]
M=[1001txty]
以下为使用cv.warpAffine()函数实现示例:
cv.warpAffine() 计算原理:dst(x,y)=src(M11x+M12y+M13,M21x+M22y+M23)
import numpy as np
import cv2 as cv
img = cv.imread('./img/messi.jpg', 0)
rows, cols = img.shape
M = np.float32([[1,0,100], [0,1,50]])
dst = cv.warpAffine(img, M, (cols, rows))
cv.imshow('img', img)
cv.imshow('dst', dst)
cv.waitKey(0)
cv.destroyAllWindows()
四、旋转
旋转依然使用cv.warpAffine()函数,图片θ角度需要创建M矩阵如下:
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
.
y
]
M=[\begin{matrix} α & β & (1−α)⋅center.x−β⋅center.y \\ −β & α & β⋅center.x+(1−α)⋅center.y \end{matrix} ]
M=[α−ββα(1−α)⋅center.x−β⋅center.yβ⋅center.x+(1−α)⋅center.y]
其中:
α
=
s
c
a
l
e
⋅
c
o
s
θ
β
=
s
c
a
l
e
⋅
s
i
n
θ
α=scale⋅cosθ \\ β=scale⋅sinθ
α=scale⋅cosθβ=scale⋅sinθ
示例代码如下:
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('./img/messi.jpg', 0)
rows, cols = img.shape
# cols-1 and rows-1 are the coordinate limits.
M = cv.getRotationMatrix2D(((cols - 1) / 2.0, (rows - 1) / 2.0), 90, 1)
dst = cv.warpAffine(img, M, (cols,rows))
cv.imshow('img', img)
cv.imshow('dst', dst)
cv.waitKey(0)
cv.destroyAllWindows()
五、仿射
仿射变换要点:
1、变换前是直线,变换后依然是直线。
2、直线比例保持不变。
示例代码如下:
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('./img/messi.jpg', 0)
rows, cols = img.shape
pts1 = np.float32([[50,50], [200,50], [50,200]])
pts2 = np.float32([[10,100], [200,50], [100,250]])
M = cv.getAffineTransform(pts1, pts2)
dst = cv.warpAffine(img, M, (cols, rows))
cv.imshow('img', img)
cv.imshow('dst', dst)
cv.waitKey(0)
cv.destroyAllWindows()
六、透视
透视变换(Perspective Transformation)是指利用透视中心、像点、目标点三点共线的条件,按透视旋转定律使承影面(透视面)绕迹线(透视轴)旋转某一角度,破坏原有的投影光线束,仍能保持承影面上投影几何图形不变的变换。简而言之,就是将一个平面通过一个投影矩阵投影到指定平面上。
原理如下图所示:
透视需要4个图像输入点,使用cv.warpPerspective函数,示例代码如下:
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('./img/sudoku.png', 0)
rows, cols = 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 = cv.getPerspectiveTransform(pts1,pts2)
dst = cv.warpPerspective(img,M,(300,300))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()