目标
- 学习对图像应用不同的几何变换,如平移、旋转、仿生变换等。
- 你将看到这些函数:cv.getPerspectiveTransform
变换
OpenCV提供了两个变换函数,cv.warpAffine和cv.warpPerspective,用它们可以进行各种变换。cv.warpAffine需要一个2x3变换矩阵,而cv.warpPerspective需要一个3x3变换矩阵作为输入。
缩放
缩放就是调整图像的大小。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('messi5.jpg')
res = cv.resize(img,None,fx=2, fy=2, interpolation = cv.INTER_CUBIC)
#OR
height, width = img.shape[:2]
res = cv.resize(img,(2*width, 2*height), interpolation = cv.INTER_CUBIC)
翻译
翻译是物体位置的移动。如果你知道在(x,y)方向上的移动,并让它成为(tx,ty),你可以创建变换矩阵M如下:
M
=
[
1
0
t
x
0
1
t
y
]
M=\left[ \begin{array}{c} \begin{matrix} 1& 0& t_x\\ \end{matrix}\\ \begin{matrix} 0& 1& t_y\\ \end{matrix}\\ \end{array} \right]
M=[10tx01ty]
你可以把它变成一个np.float32类型的Numpy数组,然后把它传给cv.warpAffine()函数。请看下面的例子,移位为(100,50):
import numpy as np
import cv2 as cv
img = cv.imread('messi5.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',dst)
cv.waitKey(0)
cv.destroyAllWindows()
警告
cv.warpAffine()函数的第三个参数是输出图像的大小,它应该是**(宽度,高度)**的形式。记住宽度=列数,高度=行数。
请看下面的结果:
旋转
图像旋转一个角度θ是通过以下形式的变换矩阵实现的:
M
=
[
cos
θ
−
sin
θ
sin
θ
cos
θ
]
M=\left[ \begin{matrix} \cos \theta& -\sin \theta\\ \sin \theta& \cos \theta\\ \end{matrix} \right]
M=[cosθsinθ−sinθcosθ]
但是OpenCV提供了可调节旋转中心的缩放旋转,因此你可以在你喜欢的任何位置进行旋转。修改后的变换矩阵是这样的
[
α
β
(
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
]
\left[ \begin{array}{c} \begin{matrix} \alpha& \beta& \left( 1−\alpha \right) ⋅center.x−\beta ⋅center.y\\ \end{matrix}\\ \begin{matrix} −\beta& \alpha& \beta ⋅center.x+\left( 1−\alpha \right) ⋅center.y\\ \end{matrix}\\ \end{array} \right]
[αβ(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
θ
\alpha =scale⋅cos\theta ,\\ \beta =scale⋅sin\theta
α=scale⋅cosθ,β=scale⋅sinθ
为了找到这个变换矩阵,OpenCV提供了一个函数,cv.getRotationMatrix2D。请看下面的例子,它将图像相对于中心旋转了90度而没有任何缩放。
img = cv.imread('messi5.jpg',0)
rows,cols = img.shape
# cols-1和rows-1是坐标极限。
M = cv.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),90,1)
dst = cv.warpAffine(img,M,(cols,rows))
请看结果:
仿生变换
在仿生变换中,原始图像中的所有平行线在输出图像中仍然是平行的。为了找到变换矩阵,我们需要输入图像中的三个点和它们在输出图像中的对应位置。然后cv.getAffineTransform将创建一个2x3的矩阵,并传递给cv.warpAffine。
看看下面的例子,也看看我选择的点(用绿色标记):
img = cv.imread('drawing.png')
rows,cols,ch = 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))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()
请看结果:
透视变换
对于透视变换,你需要一个3x3的变换矩阵。直线在变换后仍会保持直线。要找到这个变换矩阵,你需要输入图像上的4个点和输出图像上的相应点。在这4个点中,有3个不应该是相邻的。然后可以通过函数cv.getPerspectiveTransform找到变换矩阵。然后用这个3x3的变换矩阵应用cv.warpPerspective。
请看下面的代码:
img = cv.imread('sudoku.png')
rows,cols,ch = 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()
结果:
其他资源
《计算机视觉:算法与应用》,理查德-塞利斯基
(“Computer Vision: Algorithms and Applications”, Richard Szeliski)