opencv image translation, zoom, rotate, flip affine transformation

Image geometric transformation

In principle the image geometric transformation includes two types: 2x3 matrix of affine transformation (translation, scaling, rotation, flip), based on the perspective transformation 3x3 matrix.

Image translation

opencv to achieve image translation

Achieve image translation, we need to define a matrix follows, tx and ty are x and y translation distance direction:
For image translation matrix
the image using the affine transformation translation function cv.warpAffine () implemented

experiment

# 图像平移
import numpy as np
import cv2 as cv

img = cv.imread('paojie.jpg')

rows, cols = img.shape[:2]

# 定义平移矩阵,需要是numpy的float32类型
# x轴平移100,y轴平移50
M = np.float32([[1, 0, 100], [0, 1, 50]])
# 用仿射变换实现平移,第三个参数为dst的大小
dst = cv.warpAffine(img, M, (cols, rows))

cv.imshow('shift', dst)
cv.waitKey(0)
cv.destroyAllWindows()

Experimental results

Image translation results

Image scaling

opencv in the image scaling

Zoom is resize the image, use cv.resize () function to achieve image scaling. Yes, you can also zoom in accordance with the specified size scaling.

experiment

# 图像缩放
import numpy as np
import cv2 as cv

img = cv.imread('paojie.jpg')

# 按照指定的宽度、高度缩放图片
res = cv.resize(img, (132, 150))
# 按照比例缩放,如x,y方向均放大一倍
# res2 = cv.resize(img, None, fx=2, fy=2, interpolation=cv.INTER_CUBIC)

cv.imshow('shrink', res)
# cv.imshow('zoom', res2)
cv.waitKey(0)
cv.destroyAllWindows()

Experimental results

Image narrow results

Various interpolation mode

Reference: various interpolation methods described

Image rotation

Brief introduction

The same as the rotational translation, the affine transform is used, and therefore need to define a transformation matrix. Providing direct OpenCV cv.getRotationMatrix2D () function to generate the matrix, the function has three parameters:
Parameter 1: rotation center picture
parameter 2: rotational angle (n: counterclockwise, negative: clockwise)
Parameter 3: scaling, 0.5 is reduced by half

experiment

# 图像旋转
import numpy as np
import cv2 as cv

img = cv.imread('paojie.jpg')
rows,cols = img.shape[:2]

# 逆时针45°旋转图片并缩小一半,第一个参数为旋转中心
M = cv.getRotationMatrix2D((cols / 2, rows / 2), 45, 0.5)
# img:源图像;M:旋转仿射矩阵;(cols,rows):dst的大小
dst = cv.warpAffine(img, M, (cols, rows))

cv.imshow('rotation', dst)
cv.waitKey(0)
cv.destroyAllWindows()

Experimental results

Image rotation narrow results

Image flip

The image flip opencv

dst = cv2.flip (img, 1)
wherein the function of the second parameter is greater than 0, represents the image flipped horizontally (along the y axis); the second parameter is equal to 0, it represents a vertical flip image (x-axis); a first two parameters is less than 0, represents the image both horizontally inverted and vertically inverted.

experiment

# 图像翻转
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

img = cv.imread('paojie.jpg')

# 水平翻转
hor = cv.flip(img,1)
# 垂直翻转
ver = cv.flip(img,0)
# 水平垂直翻转
hor_ver = cv.flip(img,-1)

plt.figure(1)
plt.subplot(2,2,1)
plt.imshow(img)
plt.title('Original')
plt.xticks([]),plt.yticks([])

plt.subplot(2,2,2)
plt.imshow(hor)
plt.title('horizontal')
plt.xticks([]),plt.yticks([])

plt.subplot(2,2,3)
plt.imshow(ver)
plt.title('vertical')
plt.xticks([]),plt.yticks([])

plt.subplot(2,2,4)
plt.imshow(hor_ver)
plt.title('horizontal_and_vertical')
plt.xticks([]),plt.yticks([])

plt.show()

Experimental results

Flip the result of a variety of image
After reading the entire article, do not point a praise relax.

Guess you like

Origin www.cnblogs.com/wojianxin/p/12590295.html