OpenCV Quick Start Ten: Basic Transformation of Images

1. Scaling

1. Function API

def resize(InputArray src,
OutputArray dst,
Size dsize,
double fx = 0,
double fy = 0,
int interpolation = INTER_LINEAR
)

src: original image;
dsize: scaled image size;
dst: target image, but it has no meaning in Python. Generally do not pass parameters or set to None;
fx, fy:: and scaling in the y direction;
interpolation: interpolation method;
common interpolation scaling algorithm:
aaa

2. Code demo

import cv2
import numpy as np

dog = cv2.imread('./dog.jpeg')
#若fx.fy也写了优先使用dsize (x,y)
new = cv2.resize(dog,None, fx=0.3, fy=0.3, interpolation=cv2.INTER_AREA)
#shape(y,x,channels)
print(dog.shape)

cv2.imshow('dog', dog)
cv2.imshow('new', new)
cv2.waitKey(0)

2. Flip

1. Function API

def resize(InputArray src,flipcode)

src: input image;
flipcode: flip type.
aaa

2. Code demo

import cv2
import numpy as np

dog = cv2.imread('./dog.jpeg')
new = cv2.flip(dog, 0)
new2 = cv2.flip(dog, 1)
new3 = cv2.flip(dog, -1)

cv2.imshow('new', new)
cv2.imshow('dog', dog)
cv2.imshow('new2', new2)
cv2.imshow('new3', new3)
cv2.waitKey(0)

3. Rotation

1. Function API

def resize(InputArray src, rotateCode)

src: input image;
rotateCode: rotation method, as follows:
aaa

2. Code demo

import cv2
import numpy as np

dog = cv2.imread('./dog.jpeg')
new = cv2.rotate(dog, cv2.ROTATE_90_CLOCKWISE)#顺时针90°
new2 = cv2.rotate(dog, cv2.ROTATE_180)

cv2.imshow('dog', dog)
cv2.imshow('new', new)
cv2.imshow('new2', new2)
cv2.waitKey(0)

Four, affine transformation

To put it bluntly, affine transformation is the general term for image scaling, translation, and rotation.

1. Affine transformation API

def warpAffine(src, M, dsize, dst=None, flags=None, borderMode=None, borderValue=None)

src: input image;
M: operation matrix, 2 rows and 3 columns;
dsize: the size of the matrix after operation, that is, the size of the output image;
dst: output image;
flags: the combination of interpolation methods, the same as the interpolation in the resize function , you can check cv2.resize;
borderMode: border pixel extrapolation method;
borderValue: the borderValue value used in the case of a constant border; by default, it is 0.

2. Panning

aaa

3. Transformation Matrix API

def getRotationMatrix2D (Point2f center, double angle, double scale)

center: the center of rotation in the source image;
angle: the angle of rotation in degrees. A positive value means counterclockwise rotation (assuming that the coordinate origin is the upper left corner);
scale: zoom ratio.

4. Code demo

import cv2
import numpy as np

dog = cv2.imread('./dog.jpeg')
h, w, ch = dog.shape
#M = np.float32([[1, 0, 500], [0, 1, 300]])#x加500(右移),y加300  保证32位
# 旋转的角度为逆时针
# 中心点是 (x,y)
#M = cv2.getRotationMatrix2D((w/2, h/2), 15, 1.0)#15-逆时针旋转角度 1原尺寸
src = np.float32([[400, 300], [800, 300], [400, 1000]])
dst = np.float32([[200, 400], [600, 500], [150, 1100]])
#不明确角度
M = cv2.getAffineTransform(src, dst)#原坐标   现坐标  最好三个点

#如果想改变新图像的尺寸,需要修改dsize
new = cv2.warpAffine(dog, M, (w, h))

print(dog.shape)

cv2.imshow('dog', dog)
cv2.imshow('new', new)
cv2.waitKey(0)

Five, perspective transformation

1. Function API

def warpPerspective(InputArray src,OutputArray dst, InputArray M,Size dsize,int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT,const Scalar& borderValue=Scalar())

src: input image;
dst: output image;
M: matrix of perspective transformation;
dsize: size of output image;
flags=INTER_LINEAR: interpolation method of output image;
borderMode: pixel extrapolation method (BORDER_CONSTANT or BORDER_REPLICATE);
borderValue : The value used if the bounds are unchanged; by default it is equal to 0.

2. Transformation matrix

def getPerspectiveTransform(InputArray src,InputArray dst);

src: the coordinates of the quadrilateral vertices of the source image;
dst: the coordinates of the quadrilateral vertices corresponding to the target image.

3. Code demo

import cv2
import numpy as np

img = cv2.imread(r"C:\Users\DMr\Pictures\text\perspective.jpeg")

src = np.float32([[100, 1100], [2100, 1100], [0, 4000,], [2500, 3900]])
dst = np.float32([[0, 0], [2300, 0], [0, 3000], [2300, 3000]])
M = cv2.getPerspectiveTransform(src, dst)

new = cv2.warpPerspective(img, M, (2300, 3000))

cv2.imshow('orgin', img)
cv2.imshow('new', new)
cv2.waitKey(0)
cv2.imwrite('./math.png', new)

output
asc

Daily "Big Pie":
The meaning of life is always to expand rather than stick to it Never mind who I am today I want to be a better self

Guess you like

Origin blog.csdn.net/weixin_52051554/article/details/127497935