Opencv image geometric processing and image rotation python implementation

Opencv's geometric processing of images and python implementation of rotating images

Introduction: In the field of deep learning, we often use translation, rotation, mirroring and other operations for data augmentation; in the traditional CV field, due to some shooting angle problems, we need to correct the image, and geometric transformation is this process. Therefore, it is necessary to understand and learn geometric transformation.

  • Most of the principles of geometric transformation are similar, but the transformation matrix is ​​different. Therefore, we take the most commonly used translation and rotation as examples for learning.
    The transformation method for a pixel position is as follows
    Insert picture description here

The T in the formula is the transformation matrix, where (v, w) are the original coordinates and (x, y) are the transformed coordinates. Different transformations correspond to different matrices. Common transformation matrices and their functions are as follows:

Insert picture description here

Coordinate system transformation

  • Transformation center. For zooming and translation, the origin of the image coordinates (the upper left corner of the image is the origin) can be used as the center for transformation . This does not require coordinate system transformation, and can be directly calculated in general form. For rotation and offset , the origin is generally based on the image center , which involves coordinate system conversion.
  • The origin of the image coordinates is at the upper left corner of the image, horizontally to the right is the X axis, and vertically downward is the Y axis. The common coordinate system in mathematics textbooks is based on the image center as the origin, the horizontal right is the X axis, and the vertical upward is the Y axis, which is called the Cartesian coordinate system. Look at the picture below:

Insert picture description here

Therefore, for rotation and offset, 3 steps (3 transformations) are required:

  • 1. Convert the input original image image coordinates to Cartesian coordinate system;
  • 2. Perform rotation calculations. The rotation matrix has been given before;
  • 3. Convert the Cartesian coordinates of the rotated image back to the image coordinates.

Therefore, according to the 3 steps (3 transformations) mentioned above, the transformation form of rotation (clockwise rotation) is, there are 3 matrices in 3 transformations:

Insert picture description here

Reverse mapping

  • In Gonzalez's "Digital Image Processing_Third Edition", it is very clear that forward mapping is to directly calculate the spatial position of the corresponding pixel of the output image according to the original image with a transformation formula. Then this will cause a problem: it may be There are multiple pixel coordinates mapped to the same position of the output image, or some positions of the output image may not match the corresponding input image pixel at all, that is, they are not mapped, resulting in regular holes (black honeycomb) . A better way is to use Inverse Mapping: scan the position (x, y) of the output image, calculate the position (v, w) corresponding to the input image through Image (the inverse matrix of T), and use interpolation The method determines the gray value of the position in the output image.

Interpolation

  • After adopting reverse mapping, the value of the position of the output image needs to be determined by the interpolation method, so the interpolation algorithm needs to be selected. Usually there are nearest neighbor interpolation, bilinear interpolation, bicubic interpolation, etc. OpencV uses bilinear interpolation by default.

1.Opencv interface call

The following is the geometric transformation interface call of the image in opencv

import cv2
import numpy as np

Image translation

#读取图片
img = cv2.imread('E:/Machine Learning/OpenCV/task2/task2.jpg',)
#getRotationMatrix2D有三个参数,第一个为旋转中心,第二个为旋转角度,第三个为缩放比例
M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
rows,cols,channel=img.shape
move=np.float32([[1,0,100],[0,1,50]])
dst = cv2.warpAffine(img, move, (rows,cols))
cv2.imshow('original', img)
cv2.imshow('result', dst)
cv2.imwrite('E:/Machine Learning/OpenCV/task2/1.jpg',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

Insert picture description here

Insert picture description here

Image rotation

#getRotationMatrix2D有三个参数,第一个为旋转中心,第二个为旋转角度,第三个为缩放比例
M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
dst = cv2.warpAffine(img, M, (rows,cols))
cv2.imshow('original', img)
cv2.imshow('result', dst)
cv2.imwrite('E:/Machine Learning/OpenCV/task2/2.jpg',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

Insert picture description here

Affine transformation

  • Affine transformation is a linear transformation from two-dimensional coordinates to two-dimensional coordinates and maintains the "flatness" of the two-dimensional graphics. Lines that were parallel before conversion remain parallel after conversion. As shown below:
pst1=np.float32([[50,50],[0,100],[200,100]])
pst2=np.float32([[10,100],[200,70],[150,300]])
M=cv2.getAffineTransform(pst1,pst2)
dst=cv2.warpAffine(img,M,(rows,cols))
cv2.imshow('original', img)
cv2.imshow('result', dst)
cv2.imwrite('E:/Machine Learning/OpenCV/task2/3.jpg',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

Insert picture description here

summary

  • 1. In the traditional CV field, due to the camera shooting angle problem, we need to correct the image. The processing methods involved include translation, rotation, affine and perspective.
  • 2. The image geometric transformation process is to use the transformation matrix to map the image pixel coordinates.
  • 3. To rotate the image, you need to specify the center of the circle. Here you need to transform the coordinate system to the Cartesian coordinate system with the rotation center as the origin. After the mapping is completed, it will change back to the coordinate system of the image.
  • 4. In the mapping process, backward mapping is better, and forward mapping may have mapping overlaps and voids. Generally, bilinear interpolation is used for mapping gray levels.

2. The python code implementation of image rotation

import numpy as np
import cv2
import matplotlib as mpl
import matplotlib.pyplot as plt
# 排除警告信息
import warnings
# matplotlib画图常见参数设置
mpl.rcParams["font.family"] = "SimHei" 
# 设置字体
mpl.rcParams["axes.unicode_minus"]=False 
# 用来正常显示负号
plt.rcParams['font.sans-serif']=['SimHei'] 
# 用来正常显示中文标签# 嵌入式显示图形
%matplotlib inline
warnings.filterwarnings("ignore")
#读取图片
img = cv2.imread('E:/Machine Learning/OpenCV/task2/task2.jpg',cv2.IMREAD_GRAYSCALE)
rows,cols=img.shape
#设置旋转角度,np.sin()使用弧度计算
rote=45
pi_rote=np.pi*45/180

#变换矩阵
n=cols/2
m=rows/2
change_ax=np.matrix([[1,0,0],[0,-1,0],[-n,m,1]])
rote_img=np.matrix([[np.cos(pi_rote),-np.sin(pi_rote),0],[np.sin(pi_rote),np.cos(pi_rote),0],[0,0,1]])
change_back=np.matrix([[1,0,0],[0,-1,0],[n,m,1]])
T1=np.matmul(change_ax,rote_img)
T2=np.matmul(T1,change_back)
T=T2.I

#构建一个同样规格的图片
img1 = np.ones((rows,cols), np.uint8)*255

#利用变换矩阵,算该图片像素对应的灰度
for i in range(cols):
    for j in range(rows):
        rloc=[i,j,1]
        oloc=np.matmul(rloc,T)
        x,y= np.ceil(oloc[0,0]).astype(int), np.ceil(oloc[0,1]).astype(int)
        if (x<0 or x>cols-1) or(y<0 or y>rows-1):
            cor=255
        else:
            cor=img.item(x,y)
            img1.itemset((i,j),cor)
        
#显示变换后的图像
plt.subplot(1,2,1)
plt.title('原始图')
plt.imshow(img)
plt.subplot(1,2,2)
plt.title('旋转45度')
plt.imshow(img1)
plt.show()

Insert picture description here

Guess you like

Origin blog.csdn.net/hu_hao/article/details/105711592