Introduction of Functions1. warpaffine-Image radial transform (translation, rotation, scaling) function prototype: Warpaffine (SRC, M, dsize, Dst=none, Flags=none, Bordermode=none, Bordervalue=none) SRC: Original image matrix; M: Transform matrix; Dszie: Image size (size) other parameters are default. 2. flip-Image Flip Function prototype: Flip (src, Flipcode, Dst=none) SRE: Original image matrix; Flipcode: Flip Direction: 1: Flip horizontally, 0: Flip vertically;-1: Horizontal vertical rollover DST: Default
Example Walkthrough1. Read an Image: 1) Translate 25 pixels in the positive direction of the x-axis, 2) translate 50 pixels in the positive direction of the y-axis, 2, read an image: 1) translate the negative direction of the x-axis 50 pixels, 2) translate 90 pixels in the negative direction of the y-axis;
The code is as follows:
#encoding: utf-8Import NumPy as Npimport cv2image = Cv2.imread ("H:\\img\\lena.jpg") Cv2.imshow ("Original", image) M = Np.float32 ([[1,0,25],[0,1,50]]) #平移矩阵1: Pan to x positive direction -, pan to y positive direction -shifted = Cv2.warpaffine (Image,m, (image.shape[1],image.shape[0])) Cv2.imshow ("shifted and right", shifted) Cv2.waitkey (0) M = Np.float32 ([[1,0,-50],[0,1,-90]]) #平移矩阵2: Pan to x negative direction- -, pan to Y negative direction- -shifted = Cv2.warpaffine (Image,m, (image.shape[1],image.shape[0])) Cv2.imshow ("shifted up and left", shifted) Cv2.waitkey (0)
The results are as follows:
1. Original image
2, X-25,y-50
3, x-50,y-90
3, read an image: 1) Rotate 45 degrees, zoom 0.75;4, read an image: 1) rotation-45 degrees, zoom 1.25;
The code is as follows:
#encoding: Utf-8Import NumPy as Npimport cv2image = Cv2. Imread("H:\\img\\lena.jpg") Cv2. Imshow("Original", image) Cv2. Waitkey(0) (h,w) = image. Shape[:2]center = (w/2, H/2)#旋转45度, scaling 0.75M = Cv2. Getrotationmatrix(Center, $,0.75)#旋转缩放矩阵: (rotation center, rotation angle, zoom factor)rotated = Cv2. Warpaffine(Image,m, (w,h)) Cv2. Imshow("Rotated by Degrees", rotated) Cv2. Waitkey(0)#旋转-45 degrees, scaling 1.25M = Cv2. Getrotationmatrix(center,- $,1.25)#旋转缩放矩阵: (rotation center, rotation angle, zoom factor)rotated = Cv2. Warpaffine(Image,m, (w,h)) Cv2. Imshow("Rotated by-90 Degrees", rotated) Cv2. Waitkey(0)
The results are as follows:
1. Original image
2, theta-45,0.75
3, theta- -45,1.25
5, read an image: 1) horizontal flipping; 2) vertical flipping; 3) horizontal vertical flipping;
The code is as follows:
#encoding: Utf-8Import Cv2##图像翻转#Image = Cv2. Imread("H:\\img\\lena.jpg") Cv2. Imshow("Original", image) Cv2. Waitkey(0)#图像水平翻转flipped = Cv2. Flip(Image,1) Cv2. Imshow("flipped horizontally", flipped) Cv2. Waitkey(0)#图像垂直翻转flipped = Cv2. Flip(Image,0) Cv2. Imshow("flipped vertically", flipped) Cv2. Waitkey(0)#图像水平垂直翻转flipped = Cv2. Flip(image,-1) Cv2. Imshow("flipped horizontally & vertically", flipped) Cv2. Waitkey(0)
The results are as follows:
1. Original image
2. Flip horizontally
3, Vertical flip
4, Horizontal vertical flip
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Opencv-python (5)--Image geometric transformation