Geometric transformation does not change the pixel value of the image, but only rearranges the pixels on the image plane. Proper geometric transformation can eliminate the negative influence of geometric distortion caused by the imaging Angle, perspective relation and lens itself to the greatest extent. As a pre-processing step in image processing, geometric transformation is one of the core tasks of image normalization.
A geometric transformation requires two operations: first, the operations required for spatial transformation, such as translation, scaling, rotation and orthographic projection, need to be used to represent the (pixel) mapping between the output image and the input image; In addition, grayscale interpolation algorithms are required, because the pixels of the output image may be mapped to the non-integer coordinates of the input image when computed according to this transformation relationship.
1. Picture clipping
import cv2
img = cv2.imread('image0.jpg'.1)
imgInfo = img.shape
dst = img[100:200.100:300]
# From 100px to 200px on the X axis and 100px to 300px on the y axis
cv2.imshow('image',img)
Before # shear
cv2.imshow('image',dst)
After # shear
cv2.waitKey(0)
Copy the code
The results of
Second, the picture shift
1. Mathematical principle of picture shift
X,y are the final coordinates of the picture (x,y), x0, y0 are the original coordinates of the picture, the translation size of ∆x, ∆y, the formula is as follows:
The matrix is transformed as follows:
2. Implementation method of OpencV
The warpAffine function converts the source image using the specified matrix:
𝚍 𝚜 𝚝 (X, y) = 𝚜 𝚛 𝚌 (𝙼 𝙼 𝙼 12 X + y + 13, 11 𝙼 21 X + y + 𝙼 𝙼 22, 23)
cv2.warpAffine(src,M,dsize[,dst[,flags[,borderMode[,borderValue]]]])
Parameters:
- SRC Input image.
- DST outputs images with dSIZE and the same type as SRC. Middle 2 by 3 transformation matrix.
- DSIZE Specifies the size of the output image.
- Flags the combination of interpolation methods and the optional flag WARP_INVERSE_MAP, which means that M is invert (𝚍𝚜𝚝 → 𝚜𝚛𝚌).
- BorderMode pixel interpolation; When borderMode = BORDER_TRANSPARENT, this means that the pixels in the target image that correspond to the “outlier” in the source image are not modified by the function.
- BorderValue The value to use if the boundary is constant; By default, it is 0.
import cv2
import numpy as np
img = cv2.imread('image0.jpg'.1)
cv2.imshow('src',img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
matShift = np.float32([[1.0.100], [0.1.200]])
dst = cv2.warpAffine(img,matShift,(height,width))
cv2.imshow('dst',dst)
# Pan the image
cv2.waitKey(0)
Copy the code
Results:
3. Implementation of Python
import cv2
import numpy as np
img = cv2.imread('image0.jpg'.1)
cv2.imshow('src',img)
imgInfo = img.shape
dst = np.zeros(img.shape,np.uint8)
height = imgInfo[0]
width = imgInfo[1]
for i in range(0,height):
for j in range(0,width- 100.):
dst[i,j+100]=img[i,j]
cv2.imshow('image',dst)
cv2.waitKey(0)
Copy the code
Three, picture zoom
In computers, images are stored as matrices, first column after column. So, an image with width × height × color channel = 480×256×3 will be stored in a 256×480×3 three-dimensional tensor. Image processing is also calculated in accordance with this idea (including the image processing under OpenCV), that is, height × width × color channel.
1, the mathematical principle of picture scaling
WarpAffine () scales with the matrix formula as follows.
2. Implementation method of OpencV
Cv2.resize () can scale images, but the Cv2.resize API is a minor exception. Because its parameter input is width × height × color channel
cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
Parameters:
- SRC – original
- DST – Target image. If dsize is not 0, the DST size is size. Otherwise, its size needs to be determined by the size of SRC, the parameters fx and fy. DST has the same type as the SRC image
- Dsize – Target image size.
When dsize is 0, it can be calculated by the following formula:
dsize = Size(round(fxsrc.cols) round(fysrc.rows))
Therefore, the parameter dsize and the parameter (fx, fy) cannot both be 0- Fx – Scale factor on the horizontal axis. When it is 0, the calculation is as follows: (double) dsie.width /src.cols
- Fy – Scale factor on the vertical axis. When it is 0, the calculation is as follows: (double) dsie.width /src.cols
- Interpolation method. There are 5 kinds:
- Inter_nearest-nearest interpolation method
- INTER_LINEAR – Bilinear interpolation (default)
- INTER_AREA – Resampling using Pixel area relation. This may be a better approach for image decimation. But when you zoom in, it’s similar to the nearest neighbor method.
- INTER_CUBIC – Cubic interpolation based on 4×4 pixel neighborhood
- INTER_LANCZOS4 – Lanczos interpolation based on 8×8 pixel neighborhoods
Difference method principle is introduced: https://blog.csdn.net/chaipp0607/article/details/65658736
# 1 load 2 info 3 resize 4 check
import cv2
img = cv2.imread('image0.jpg'.1)
imgInfo = img.shape
print(imgInfo)
height = imgInfo[0]
width = imgInfo[1]
mode = imgInfo[2]
# 1 Zoom out 2 equal ratio not 2:3
dstHeight = int(height*0.5)
dstWidth = int(width*0.5)
# Nearest neighboring interpolation Bilinear interpolation pixel relation resampling cubic interpolation
dst = cv2.resize(img,(dstWidth,dstHeight))
cv2.imshow('image',dst)
matScale = np.float32([[0.5.0.0], [0.0.7.0]])
dst = cv2.warpAffine(img,matScale,(int(width*0.5),int(height*0.7)))
cv2.imshow('dst2',dst2)
Image width *0.5, height *0.7
cv2.waitKey(0)
Copy the code
Results:
(547, 730, 3)
Copy the code
3. Implementation of Python
import cv2
import numpy as np
img = cv2.imread('image0.jpg'.1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dstHeight = int(height/2)
dstWidth = int(width/2)
dstImage = np.zeros((dstHeight,dstWidth,3),np.uint8)# 0-255
for i in range(0,dstHeight):Line #
for j in range(0,dstWidth):# column
iNew = int(i*(height*1.0/dstHeight))
jNew = int(j*(width*1.0/dstWidth))
dstImage[i,j] = img[iNew,jNew]
cv2.imshow('dst',dstImage)
cv2.waitKey(0)
Copy the code
4. Image mirroring
1. Mathematical principle of image mirroring
(1) Vertical turnover principle
(2) The principle of horizontal turnover
(3) 180 degree reversal principle
2. Implementation method of OpencV
import cv2
import numpy as np
img = cv2.imread('canton02.jpg'.1)
cv2.imshow('src',img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]
newImgInfo = (height*2,width,deep)
newImgInfo2 = (height,width*2,deep)
newImgInfo3 = (height*2,width*2,deep)
dst = np.zeros(newImgInfo,np.uint8)
# Vertical flip
cv2.imshow('dst',dst)
for i in range(0,height):
for j in range(0,width):
dst[i,j] = img[i,j]
# x , y=2*h-y-1
dst[height*2-i- 1,j] = img[i,j]
cv2.imshow('dst',dst)
# Flip horizontal
dst2 = np.zeros(newImgInfo2,np.uint8)
for i in range(0,height):
for j in range(0,width):
dst2[i,j] = img[i,j]
# x=2*w-x-1 , y
dst2[i,width*2-j- 1] = img[i,j]
cv2.imshow('dst2',dst2)
# Flip both horizontally and vertically
dst3 = np.zeros(newImgInfo3,np.uint8)
for i in range(0,height):
for j in range(0,width):
dst3[i,j] = img[i,j]
# x=2*w-x-1 , y=2*h-y-1
dst3[height*2-i- 1,width*2-j- 1] = img[i,j]
cv2.imshow('dst3',dst3)
cv2.waitKey(0)
Copy the code
4. Picture conversion
1. Mathematical principle of picture rotation
As shown in the figure above, r is the fixed distance from the origin, theta is the Angle between the original angular position of the point and the horizontal line, and theta is the Angle of rotation. Coordinate said
In polar coordinates, the original coordinates of a point are zero
The transformation equation is:
Transform to a matrix:
2. Implementation method of OpencV
Cv2. GetRotationMatrix2D function
The image is rotated around the origin, and then the center of the image moves to another position.
The center point of the image before and after the rotation is not in the same position, in order to get them in the same position, you need to shift the image again.
The rotation coordinate of the center of the image minus the rotation coordinate is the translation vector in the transformation matrix.
getRotationMatrix2D (Point2f center,double angle,double scale)
Parameters:
- Center The rotation center in the source image
- Angle Angle of rotation. Positive values indicate counterclockwise rotation (the origin of the coordinates is considered to be the upper left corner)
- Scale isotropic scale factor.
import cv2
import numpy as np
img = cv2.imread('canton02.jpg'.1)
cv2.imshow('src',img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
# 2 * 3
matRotate = cv2.getRotationMatrix2D((height*0.5,width*0.5),45.1)# mat rotate 1 center 2 angle 3 scale
25 # 100 * 100
dst = cv2.warpAffine(img,matRotate,(height,width))
cv2.imshow('dst',dst)
cv2.waitKey(0)
Copy the code
5. Affine transformation of pictures
1. Mathematical principle of image affine transformation
So let’s look at a GIF, just to give you an intuition of what an affine transformation is,
Affine transformation: An arbitrary affine transformation can be represented by multiplying by a matrix (linear transformation) followed by a vector (translation).
We can express this by affine transformation:
- Rotation (linear transformation)
- Translation (vector plus)
- Scaling operation (linear transformation)
Affine transformation is a linear transformation from two-dimensional coordinates (x0, y0) to two-dimensional coordinates (x,y), and its mathematical expression is as follows:
The corresponding homogeneous coordinate matrix is expressed as:
In image processing, affine transformation can be used to carry out translation, scaling, rotation and other operations on two-dimensional images. Please refer to the above sections for details.
2. Implementation method of OpencV
GetAffineTransform (InputArray SRC, InputArray DST)
Parameters:
- InputArray SRC: represents the three points of input
- InputArray dstL: indicates three points of output
import cv2
import numpy as np
img = cv2.imread('image0.jpg'.1)
cv2.imshow('src',img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
# SRC 3-> DST 3 (upper left corner, lower left corner, upper right corner)
matSrc = np.float32([[0.0], [0,height- 1],[width- 1.0]])
matDst = np.float32([[50.50], [300,height- 200.],[width- 300..100]])
# combination
matAffine = cv2.getAffineTransform(matSrc,matDst)# mat 1 src 2 dst
dst = cv2.warpAffine(img,matAffine,(width,height))
cv2.imshow('dst',dst)
cv2.waitKey(0)
Copy the code