“This is the 29th day of my participation in the Gwen Challenge in November. See details of the event: The Last Gwen Challenge in 2021”

Geometric transformation of the image

Geometric transformation mainly includes zooming, translation, rotation, affine transformation, perspective transformation and image clipping. The two key functions that perform these geometric transformations are cv2.warpaffine () and cv2.warpPerspective().

The cv2.warpaffine () function transforms the source image using the following 2 x 3 transformation matrix:


d s t ( x . y ) = s r c ( M 11 x + M 12 y + M 13 . M 21 x + M 22 y + M 23 ) dst(x,y)=src(M_{11}x+M_{12}y+M_{13}, M_{21}x+M_{22}y+M_{23})

The cv2.warpPerspective() function transforms the source image using the following 3 x 3 transformation matrix:


d s t ( x . y ) = s r c ( M 11 x + M 12 y + M 13 M 31 x + M 32 y + M 33 . M 21 x + M 22 y + M 23 M 31 x + M 32 y + M 33 ) dst(x,y)=src(\frac {M_{11}x+M_{12}y+M_{13}} {M_{31}x+M_{32}y+M_{33}}, \frac {M_{21}x+M_{22}y+M_{23}} {M_{31}x+M_{32}y+M_{33}})

Next, we’ll look at the most common geometric transformation techniques.

Scale the image

When scaling an image, you can call cv2.resize() directly using the scaled image size:

# specify the size of the image after scaling
resized_image = cv2.resize(image, (width * 2, height * 2), interpolation=cv2.INTER_LINEAR)
Copy the code

In addition to the above usage, you can also provide both the fx and fy values for the scaling factor. For example, if you want to shrink the image by 2x:

# Use scale factor
dst_image = cv2.resize(image, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
Copy the code

If you want to enlarge the image, the best method is to use the Cv2.inter_CUBIC interpolation method (which is time-consuming) or Cv2.inter_Linear. If you want to zoom out the image, the general method is to use cv2.inter_linear. The five interpolation methods provided by OpenCV are shown in the table below:

The interpolation method The principle of
cv2.INTER_NEAREST Nearest neighbor interpolation
cv2.INTER_LINEAR Bilinear interpolation
cv2.INTER_AREA Resampling using pixel area relations
cv2.INTER_CUBIC Cubic interpolation based on 4×4 pixel neighborhood
cv2.INTER_LANCZOS4 Sinusoidal interpolation

Display the zoomed image:

def show_with_matplotlib(color_img, title, pos) :
    # Convert BGR image to RGB
    img_RGB = color_img[:,:,::-1]
    ax = plt.subplot(1.3, pos)
    plt.imshow(img_RGB)
    plt.title(title,fontsize=8)
    # plt.axis('off')
show_with_matplotlib(image, 'Original image'.1)
show_with_matplotlib(dst_image, 'Resized image'.2)
show_with_matplotlib(dst_image_2, 'Resized image 2'.3)
plt.show()
Copy the code

The zooming of the image can be observed through the coordinate system:

Pan image

To translate the object, we need to create a 2 x 3 transformation matrix using the NumPy array, which provides the translation distances in pixels in the X and y directions:

M = np.float32([[1.0, x], [0.1, y]])
Copy the code

It corresponds to the following transformation matrix:


[ 1 0 t x 0 1 t y ] \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \end{bmatrix}

After creating this matrix, call the cv2.warpaffine () function:

dst_image = cv2.warpAffine(image, M, (width, height))
Copy the code

The cv2.warpaffine () function transforms the source image using the supplied M-matrix. The third argument (width, height) is used to determine the size of the output image.

For example, if the image is to be shifted 200 pixels in the X direction and 30 pixels in the Y direction:

height, width = image.shape[:2]
M = np.float32([[1.0.200], [0.1.30]])
dst_image_1 = cv2.warpAffine(image, M, (width, height))
Copy the code

Translation can also be negative, in which case, the movement is in the opposite direction:

M = np.float32([[1.0, -200], [0.1, -30]])
dst_image_2 = cv2.warpAffine(image, M, (width, height))
Copy the code

The display picture is as follows:

Rotate the image

To rotate the image, you need to first construct the 2 x 3 transformation matrix using the cv.getrotationMatrix2D () function. The matrix rotates the image at the desired Angle, in degrees, where a positive value indicates a counterclockwise rotation. The center of rotation and the scale factor can also be adjusted. Using these elements, the transformation matrix is computed as follows:


[ Alpha. Beta. ( 1 a ) c e n t e r . x Beta. c e n t e r . y Beta. Alpha. Beta. c e n t e r . x ( 1 Alpha. ) c e n t e r . y ] \begin{bmatrix} \alpha & \beta & (1-a)\cdot center.x-\beta\cdot center.y \\ -\beta & \alpha & \beta\cdot center.x-(1-\alpha)\cdot center.y \end{bmatrix}

Among them:


Alpha. = s c a l e c o s Theta. . Beta. = s c a l e s i n Theta. \alpha=scale\cdot cos\theta, \beta=scale\cdot sin\theta

The following example builds the M-transform matrix to rotate 180 degrees relative to the center of the image with a scaling factor of 1(no scaling). Then, apply this M-matrix to the image, as shown below:

height, width = image.shape[:2]
M = cv2.getRotationMatrix2D((width / 2.0, height / 2.0), 180.1)
dst_image = cv2.warpAffine(image, M, (width, height))
Copy the code

Next, rotate with a different center of rotation:

M = cv2.getRotationMatrix2D((width/1.5, height/1.5), 30.1)
dst_image_2 = cv2.warpAffine(image, M, (width, height))
Copy the code

Display the rotated image:

2.4 Affine transformation of images

In the affine transformation, the cv2.getaffineTransform () function is first used to construct the 2 x 3 transformation matrix, which will be obtained from the corresponding coordinates in the input image and the transformation image. Finally, the m-matrix is passed to cv2.warpaffine () :

pts_1 = np.float32([[135.45], [385.45], [135.230]])
pts_2 = np.float32([[135.45], [385.45], [150.230]])
M = cv2.getAffineTransform(pts_1, pts_2)
dst_image = cv2.warpAffine(image_points, M, (width, height))
Copy the code

Affine transformations are transformations of reserved points, lines and planes. In addition, parallel lines will remain parallel after this transformation. However, affine transformations do not preserve both the distance and the Angle between the pixels.

The result of affine transformation can be observed in the following image:

2.5 Perspective transformation of images

For perspective transformation, first you need to use cv2 getPerspectiveTransform () function to create a 3 x 3 transformation matrix. This function requires four pairs of points (the coordinates of the quadrilateral in the source and output images) from which the function computes the perspective transformation matrix. Then, pass the m-matrix to cv2.warpPerspective() :

pts_1 = np.float32([[450.65], [517.65], [431.164], [552.164]])
pts_2 = np.float32([[0.0], [300.0], [0.300], [300.300]])
M = cv2.getPerspectiveTransform(pts_1, pts_2)
dst_image = cv2.warpPerspective(image, M, (300.300)
Copy the code

The perspective transformation effect is as follows:

2.6 Cropping An Image

You can crop the image using NumPy slices:

dst_image = image[80:200.230:330]
Copy the code

The clipping result is as follows: