cv2.getRotationMatrix2D() and cv2.warpAffine()

 cv2.getRotationMatrix2D ()

The rotation matrix of the image is generally:

                                                                     M=\begin{pmatrix} cos(\theta) \qquad -sin(\theta)& \\ sin(\theta) \qquad cos(\theta) & \end{pmatrix}
But this simple matrix is ​​transformed at the origin. In order to be able to rotate and transform at any position, opencv uses another method:

                                                     M=\begin{pmatrix} \alpha & -\beta &(1-\alpha)center_x-\beta center_y \\ -\beta& \alpha &\beta center_x-(1-\alpha) center_y \\ \end{pmatrix}
In order to construct this matrix, opencv provides a function:
cv2.getRotationMatrix2D(), this function requires three parameters, the center of rotation, the angle of rotation, and the scale of the rotated image, such as the following example:

import cv2 
import matplotlib.pyplot as plt 
 
img = cv2.imread('flower.jpg') 
rows,cols = img.shape[:2] 
#第一个参数旋转中心,第二个参数旋转角度,第三个参数:缩放比例 
M = cv2.getRotationMatrix2D((cols/2,rows/2),45,1) 
#第三个参数:变换后的图像大小 
res = cv2.warpAffine(img,M,(rows,cols)) 
 
plt.subplot(121) 
plt.imshow(img) 
plt.subplot(122) 
plt.imshow(res)

cv2.warpAffine()

cv2.warpAffine(img,M,(rows,cols),flags=cv2.INTER_,borderMode=cv2.BORDER_REFLECT,borderValue=(255,255,255))

src-input image.
M-transformation matrix.
dsize-the size of the output image.
flags-a combination of interpolation methods (int type!)
borderMode-border pixel mode (int type!)
borderValue-(emphasis!) Border padding value; by default, it is 0.

Among the above parameters: M is an affine transformation matrix, which generally reflects the relationship of translation or rotation, and is a 2×3 transformation matrix of InputArray type.

Flags represents the interpolation method, the default is flags=cv2.INTER_LINEAR, which represents linear interpolation, in addition to: cv2.INTER_NEAREST (nearest neighbor interpolation) cv2.INTER_AREA (regional interpolation) cv2.INTER_CUBIC (cubic spline interpolation) cv2.INTER_LANCZOS4 (Lanczos Interpolation)

In daily affine transformation, when only the first three parameters are set, such as cv2.warpAffine(img,M,(rows,cols)) can achieve the basic affine transformation effect, but the phenomenon of "black border" can appear , As shown in the figure:

This often causes a lot of trouble for subsequent processing. The sixth parameter can select the fill color, and the default is black. The parameters for setting warpAffine in python are different from those in C++. The name of the parameter must be specified, as follows:

c++中:warpAffine(src,img,M,Size(a,b),1,0,Scalar(255,255,255));

In python:

cv2.warpAffine(img,M,(rows,cols),flags=cv2.INTER_,borderMode=cv2.BORDER_REFLECT,borderValue=(255,255,255))

One advantage in python is that you don’t need to set them in order, just specify the parameters you set, such as filling the border area of ​​the above picture with white:

cv2.warpAffine(img,M,(lengh,lengh),borderValue=(255,255,255))

from:

https://blog.csdn.net/weixin_41010198/article/details/88424163

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_27009517/article/details/112349980