Doing computer vision direction, in addition to the popular variety of deep learning algorithms, many times also need to know basic image processing methods.
Record some operations of opencv (image mapping transformations), which can be easily used later on
Let's start with a picture of the effect
Fig. 2 and Fig. 3 are the same method, only the transformation matrix is different, both are 3-point mapping transformations
Figure 4 uses a 4-point mapping transformation
A brief introduction to the principle
Images are known to be 3-dimensional (channel) matrices, and the first two dimensions are two-dimensional arrays filled with 1-byte (0-255) numbers. The size of the number represents the shade of the color.
We take the original graphs before transformation as x and y. The transformed graphs are u and v. Multiplying [x,y,1] by the transformation matrix gives us the corresponding new u and v. Different transformation matrices serve different purposes (different transformations)
So now it's the process of finding the different transformation matrices corresponding to the different transformations
There's a direct way to find this matrix in opencv.
Simply provide the mapping positions of the three points of the original map and the three points you want to transform (3 points of the original map, 3 points of the mapping) to find this transformation matrix
Of course, you'll find that no matter how you adjust the mapping points, you can't transform them arbitrarily.
Because when only three points are given, the transformed image is actually just an equivalent scale of the original image, and cannot be mapped at will.
Here opencv also provides four points and four mapping methods to find the corresponding transformation matrix, and finally get the effect of any mapping.
The code is as follows:
# coding=gbk import cv2 import numpy as np import as plt ['-serif']=['SimHei'] # Used to display Chinese labels properly ['axes.unicode_minus']=False # Used to display the negative sign normally img=(r"") img = img[:,:,[2,1,0]] cols,rows,ch= pts1 = np.float32([[0, 0], [cols - 1, 0], [0, rows - 1]]) # Three-point mapping pts2 = np.float32([[0, 0], [cols - 1, 0], [80, rows - 1]]) pts21 = np.float32([[0, 0], [cols - 1, 0], [0, rows - 1]]) pts22 = np.float32([[cols * 0.2, rows * 0.1], [cols * 0.9, rows * 0.2], [cols * 0.1, rows * 0.9]]) pts31 = np.float32([[0, 0], [cols - 1, 0], [0, rows - 1],[cols - 1,rows-1]]) # Four-point mapping pts32 = np.float32([[0, 0], [cols - 1, 0], [50, rows - 1],[cols - 50,rows-50]]) M = (pts1,pts2) # Find the transformation matrix of a three-point mapping M2= (pts21,pts22) M3 = (pts31,pts32) # Find the transformation matrix of the four-point mapping dst = (img,M,(rows+120,cols)) # Transformation functions for three-point mappings dst2 = (img,M2,(rows,cols)) dst3 = (img,M3,(rows+40,cols+50)) # Transformation functions for four-point mappings (221) (img) ("Original image.") (222) (dst) ("Projective transformations") (223) (dst2) ("Imitation of the original transformation.") (224) (dst3) ("Imitation of projective irregular transformations.") ()
The above is a small introduction to the python-image processing (mapping transform) detailed integration, I hope to help you, if you have any questions please leave me a message, I will reply to you in a timely manner. I would also like to thank you very much for your support of my website!