OpenCV-Python Development Guide (13) --- Perspective of Geometric Transformation

More complex affine changes

The previous blog post explained the two most basic affine transformations: translation and rotation. But OpenCV also provides us with the function cv2.getAffineTransform() to generate the transformation matrix M used by the affine function cv2.warpAffine().

The definition of this function is as follows:

def getAffineTransform(src, dst): 

src: Three point coordinates representing the input image

dst: Three point coordinates representing the output image

Those who have used PS know that we can use the shortcut key Ctrl+T to pull the image at will, because there are four points on the top, bottom, left, and right of the image to be responsible for positioning. The three coordinates in src are exactly the same as PS, namely the upper left corner, the upper right corner, and the lower left corner. There is only one point missing, but the positioning is more than sufficient. Of course, the mapping can only be a parallelogram, because one coordinate is missing.

Next, let's locate the image at will and achieve the pulling effect:

import cv2
import numpy as np

img = cv2.imread("4.jpg")
rows,cols,ch = img.shape
p1=np.float32([[0,0],[cols-1,0],[0,rows-1]])
p2=np.float32([[0,rows*0.33],[cols*0.85,rows*0.25],[cols*0.15,rows*0.7]])
M = cv2.getAffineTransform(p1,p2)
move_img = cv2.warpAffine(img, M, (cols, rows))
cv2.imshow("img", img)
cv2.imshow("move_img", move_img)
cv2.waitKey()
cv2.destroyAllWindows()

After running, the effect is as shown in the following figure:
Transform image
p1 is the coordinates of the original image, here is a little transformation. The coordinates of the original image are the upper left corner [0,0], the upper right corner [cols,0], and the lower left corner [0,rows]. The transformed coordinates are the upper left corner [0, rows 0.33], the upper right corner [cols 0.85, rows 0.25], and the lower left corner [cols 0.15, rows*0.7].

perspective

The reason to explain the above parallelogram affine is because our perspective is an arbitrary quadrilateral, and it is easier for us to understand the concept of perspective through the above code.

In OpenCV, perspective transformation is implemented by the function cv2.warpPerspective(), which is defined as follows:

def warpPerspective(src, M, dsize, dst=None, flags=None, borderMode=None, borderValue=None):

src: the original image to be perspective

M: 3*3 transformation matrix for transforming the perspective image

dsize: represents the size of the output image

The three parameters of flags, borderMode, and borderValue are similar to the previous ones. I won’t introduce them here.

Below, we transform the above image into an arbitrary quadrilateral, the specific code is as follows:

import cv2
import numpy as np

img = cv2.imread("4.jpg")
rows, cols, ch = img.shape
p1 = np.float32([[0, 0], [cols, 0], [0, rows], [cols, rows]])
p2 = np.float32([[0, rows * 0.33], [cols * 0.85, rows * 0.25], [cols * 0.15, rows * 0.7], [cols * 0.85, rows * 0.85]])
M = cv2.getPerspectiveTransform(p1, p2)
move_img = cv2.warpPerspective(img, M, (cols, rows))
cv2.imshow("img", img)
cv2.imshow("move_img", move_img)
cv2.waitKey()
cv2.destroyAllWindows()

The perspective function warpPerspective obtains the transformation matrix through getPerspectiveTransform. After running, the effect is as follows:
perspective

Guess you like

Origin blog.csdn.net/liyuanjinglyj/article/details/113802729