[OpenCV-Python] 14 Geometric transformation

OpenCV-Python: IV Image Processing in OpenCV

14 Geometric transformation

Goals:
  • Learn to perform various transformations on images, such as movement, rotation, affine transformation, etc.
  • The function to be learned is: cv2.getPerspectiveTransform.

Transformation:
OpenCV provides two transformation functions, cv2.warpAffine and cv2.warpPerspective, using these two functions you can implement all types of transformations. The parameter received by cv2.warpAffine is a 2 × 3 transformation matrix, while the parameter received by cv2.warpPerspective is a 3 × 3 transformation matrix.

14.1 Extended zoom

Extend zoom only changes the size of the image. The function cv2.resize() provided by OpenCV can achieve this function. The size of the image can be set manually by yourself, and you can also specify the zoom factor. We can choose to use different interpolation methods. We recommend using cv2.INTER_AREA when scaling, and we recommend using v2.INTER_CUBIC (slow) and v2.INTER_LINEAR when expanding. By default, the interpolation method used for all operations that change the size of the image is cv2.INTER_LINEAR. You can use any of the following methods to change the size of the image:

import cv2
import numpy as np
img=cv2.imread('messi5.jpg')
# 下面的 None 本应该是输出图像的尺寸,但是因为后边我们设置了缩放因子
# 因此这里为 None
res=cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC)
#OR
# 这里呢,我们直接设置输出图像的尺寸,所以不用设置缩放因子
height,width=img.shape[:2]
res=cv2.resize(img,(2*width,2*height),interpolation=cv2.INTER_CUBIC)
while(1):
    cv2.imshow('res',res)
    cv2.imshow('img',img)
    if cv2.waitKey(1) & 0xFF == 27:
        break
cv2.destroyAllWindows()
# Resize(src, dst, interpolation=CV_INTER_LINEAR)

14.2 Pan

Translation is to change the position of the object. If you want to move in the (x, y) direction and the distance to move is (tx, ty), you can construct the movement matrix in the following way:

Insert picture description here

You can build this matrix using a Numpy array (data type is np.float32), and then pass it to the function cv2.warpAffine(). Take a look at the following example, it has been moved (100,50) pixels.

import cv2
import numpy as np

img = cv2.imread('messi5.jpg',0)
rows,cols = img.shape

M = np.float32([[1,0,100],[0,1,50]])
dst = cv2.warpAffine(img,M,(cols,rows))

cv2.imshow('img',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

img

Warning: The third parameter of the function cv2.warpAffine() is the size of the output image, and its format should be the image (width, height). It should be remembered that the width of the image corresponds to the number of columns, and the height corresponds to the number of rows.
Here is the result:

14.3 Rotation

For an image rotation angle θ, the following form of rotation matrix needs to be used.
      M = \begin{bmatrix} cos\theta & -sin\theta \ sin\theta & cos\theta   \end{bmatrix}
But OpenCV allows you to rotate anywhere, but the form of the rotation matrix should be modified
to
      

Among them:
      \begin{array}{l} \alpha =  scale \cdot \cos \theta , \ \beta =  scale \cdot \sin \theta \end{array}
In order to construct this rotation matrix, OpenCV provides a function: cv2.getRotationMatrix2D.
The following example is to rotate the image by 90 degrees without zooming.

import cv2
import numpy as np
img=cv2.imread('messi5.jpg',0)
rows,cols=img.shape
# 这里的第一个参数为旋转中心,第二个为旋转角度,第三个为旋转后的缩放因子
# 可以通过设置旋转中心,缩放因子,以及窗口大小来防止旋转后超出边界的问题
M=cv2.getRotationMatrix2D((cols/2,rows/2),45,0.6)
# 第三个参数是输出图像的尺寸中心
dst=cv2.warpAffine(img,M,(2*cols,2*rows))
while(1):
    cv2.imshow('img',dst)
    if cv2.waitKey(1)&0xFF==27:
        break
cv2.destroyAllWindows()   

Here is the result:

14.4 Affine transformation

In the affine transformation, all parallel lines in the original image are also parallel in the result image. In order to create this matrix we need to find three points from the original image and their positions in the output image. Then cv2.getAffineTransform will create a 2x3 matrix, and finally this matrix will be passed to the function cv2.warpAffine.
Let’s take a look at the example below and the points I selected (the points marked in green)

img = cv2.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 = cv2.getAffineTransform(pts1,pts2)

dst = cv2.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()

Here is the result:

14.5 Perspective transformation

For viewing angle transformation, we need a 3x3 transformation matrix. The straight line is still straight before and after the transformation. To construct this transformation matrix, you need to find 4 points on the input image and their corresponding positions on the output image. Any three of these four points cannot be collinear. This transformation matrix can be constructed with the function cv2.getPerspectiveTransform(). Then pass this matrix to the function cv2.warpPerspective.
code show as below:

img = cv2.imread('sudokusmall.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 = cv2.getPerspectiveTransform(pts1,pts2)

dst = cv2.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()

The results are as follows:

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-JZdb8x9w-1611974598281)(http://opencv-python-tutroals.readthedocs.io/en/latest/_images /perspective.jpg)]
For more information, please pay attention to the official account:
img

Guess you like

Origin blog.csdn.net/yegeli/article/details/113417730