1. Introduction to imutils function
imutils is a package based on OPenCV to achieve the purpose of calling the OPenCV interface more concisely. It can easily implement a series of operations such as image translation, rotation, scaling, and skeletonization.
installation method:
pip install imutils
Before installation, make sure that numpy, scipy, matplotlib and opencv have been installed.
2. How to use imutils
2.1 Image translation
OpenCV also provides the realization of image translation. The translation matrix must be calculated first, and then the affine transformation is used to realize the translation. The image translation can be directly performed in imutils.
translated = imutils.translate(img,x,y)
parameter:
- img: the image to be moved
- x: the number of pixels moved along the x axis
- y: the number of pixels moving along the y axis
import cv2
import numpy as np
import matplotlib.pyplot as plt
import imutils
from pylab import mpl
mpl.rcParams['font.sans-serif']=['SimHei'] # 简体
mpl.rcParams['axes.unicode_minus']= False
img = cv2.imread('01.jpg')
img_translate = imutils.translate(img, 20, 30)
plt.figure(figsize=(20,8), dpi=100)
plt.subplot(1,2,1)
plt.imshow(img[:,:,::-1])
plt.title("原图")
plt.subplot(1,2,2)
plt.imshow(img_translate[:,:,::-1])
plt.title("平移后")
2.2 Image zoom
In OPenCV, the zooming of the picture should pay attention to ensure that the aspect ratio is maintained. In imutils, the aspect ratio of the original image is automatically maintained, and only the width weight and height can be specified.
img = cv2.imread('01.jpg')
img_resized = imutils.resize(img, height=200)
plt.figure(figsize=(20,8), dpi=100)
plt.subplot(1,2,1)
plt.imshow(img[:,:,::-1])
plt.title("原图")
plt.subplot(1,2,2)
plt.imshow(img_resized[:,:,::-1])
plt.title("缩放后")
2.3 Image rotation
The affine transformation is used when rotating in OpenCV. Here, the image rotation method is imutils.rotate()
followed by two parameters. The first is the image data, the second is the angle of rotation, and the rotation is counterclockwise. At the same time, imutils
another similar method is provided rotate_round()
, which rotates clockwise.
img = cv2.imread('01.jpg')
img_rotate1 = imutils.rotate(img, 90)
img_rotate2 = imutils.rotate(img, 180)
img_rotate3 = imutils.rotate_bound(img, 90)
plt.figure(figsize=(20,8), dpi=100)
plt.subplot(2,2,1)
plt.imshow(img[:,:,::-1])
plt.title("原图")
plt.subplot(2,2,2)
plt.imshow(img_rotate1[:,:,::-1])
plt.title("逆时针旋转90度")
plt.subplot(2,2,3)
plt.imshow(img_rotate2[:,:,::-1])
plt.title("逆时针旋转180度")
plt.subplot(2,2,4)
plt.imshow(img_rotate3[:,:,::-1])
plt.title("顺时针旋转180度")
2.4 Skeleton extraction
Skeleton extraction refers to the process of topological skeleton construction of objects in the picture. The method provided by imutils is skeletonize(). The second parameter is the size of the structure parameter (structuring element), which is equivalent to a granularity. The smaller the size, the longer the processing time.
import cv2
import imutils
# 1 图像读取
image= cv2.imread('./01.jpg')
# 2 灰度化
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 3 骨架提取
skeleton = imutils.skeletonize(gray, size=(3, 3))
# 4 图像展示
plt.figure(figsize=(10,8),dpi=100)
plt.subplot(121),plt.imshow(img[:,:,::-1]),plt.title('原图')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(skeleton,cmap="gray"),plt.title('骨架提取结果')
plt.xticks([]), plt.yticks([])
plt.show()