# Basic installation and simple use of imutils

The imutils package includes a series of OpenCV + convenience functions that perform basic tasks such as translation, rotation, resizing, and skeleton extraction.

## 1. Installation

This package assumes that you have installed NumPy and opencv (if you plan to use the opencv2matplotlib function, you also have installed matplotlib).

To install the library, simply use the following command:

pip install imutils


## 2. Secondary encapsulation of OpenCV function

Let's go on and see what we can do with this bag.

### 2.1 translation

Translation refers to the translation of the image in the X or Y direction. To translate the image in OpenCV, you need to provide the translation amount of (x, y), expressed as (tx, ty) to construct the translation matrix M:
M = [ 1 0 t x 0 1 t y ] M = \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y\end{bmatrix} M=[10​01​tx​ty​​]
Then, you need to apply the function cv2. warpaffine.

Instead of manually constructing the translation matrix M, we just need to call the translate function of imutils.

import imutils
import cv2

cv2.imshow("src", img)
# Translate the image 25 pixels to the right and 75 pixels up
translate_img = imutils.translate(img, 25, -75)
cv2.imshow("translate_img", translate_img)
cv2.waitKey() ### 2.2 rotation

In OpenCV, image rotation is accomplished by calling cv2.getRotationMatrix2D and cv2.warpAffine. Further attention needs to be paid to providing the (x, y) coordinates of the image rotation point. These calls quickly increase computation, making the code cumbersome and less readable. The rotate function in imutils can help solve this problem.

import imutils
import cv2

cv2.imshow("src", img)
# Cycle through the angle to rotate the image
for angle in range(0, 360, 90):
# Rotate the image and display it
rotated = imutils.rotate(img, angle=angle)
cv2.imshow("Angle=%d" % (angle), rotated)
cv2.waitKey() ### 2.3 zoom

In OpenCV, the size of the image is adjusted by calling the cv2.resize function. However, special attention needs to be paid to imutils.resize to ensure that the aspect ratio is constant. The resize function of imutils maintains the aspect ratio and provides the keyword parameters width and height so that the image can be adjusted to the expected width / height, while (1) maintaining the aspect ratio and (2) ensuring that the size of the image does not have to be explicitly calculated by the developer.

Another optional keyword parameter, inter, can also be used to specify the interpolation method.

import imutils
import cv2

cv2.imshow("src", img)
# Loop over different widths to resize the image
for width in (400, 300, 200, 100):
# Resize the image and display it
resized = imutils.resize(img, width=width)
cv2.imshow("Width=%dpx" % (width), resized)
cv2.waitKey() ### 2.4 skeleton

Skeletonization is the process of constructing the "topological skeleton" of an object in an image, which is assumed to be a white object with a black background. OpenCV does not provide functions to explicitly construct skeletons, but provides morphological and binary functions to achieve this.

For convenience, the skeleton function of imutils can be used to construct the topological skeleton of the image.

The first parameter size is the size of the structured element kernel. An optional parameter, structuring, can be used to control structured elements - it defaults to cv2.MORPH_RECT, but can be any valid structural element.

import imutils
import cv2

cv2.imshow("src", logo)
# Skeleton extraction
gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY)
skeleton = imutils.skeletonize(gray, size=(3, 3))
cv2.imshow("Skeleton", skeleton)
cv2.waitKey() ### 2.5 Matplotlib display picture

In the Python binding of OpenCV, images are represented as NumPy arrays in BGR order. This works well when using cv2.imshow. However, if you plan to use Matplotlib, because the plt.imshow function assumes that the images are in RGB order. Calling cv2.cvtColor will solve this problem, or you can use the opencv2matplotlib function.

import imutils
import cv2
import matplotlib.pyplot as plt

cv2.imshow("src", logo)
# Incorrect usage: do not convert color space when displaying images
plt.figure("Incorrect")
plt.imshow(logo)
# Correct usage: convert the color space before using plt.imshow
plt.figure("Correct")
plt.imshow(imutils.opencv2matplotlib(logo))
plt.show()
cv2.waitKey() ## 3. Source code of imutils

### 3.1 translation

def translate(image, x, y):
# Define the translation matrix and perform the translation operation
M = np.float32([[1, 0, x], [0, 1, y]])
shifted = cv2.warpAffine(image, M, (image.shape, image.shape))

# Returns the panned image
return shifted


### 3.2 rotation

def rotate(image, angle, center=None, scale=1.0):
# Get image width and height
(h, w) = image.shape[:2]

# If center is None, it is initialized to the center of the image
if center is None:
center = (w // 2, h // 2)

# Rotation implementation
M = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, M, (w, h))

# Returns the rotated image
return rotated


### 3.3 zoom

def resize(image, width=None, height=None, inter=cv2.INTER_AREA):
# Initialize the size of the image to be resized and obtain the image size
dim = None
(h, w) = image.shape[:2]

# If both width and height are None, the original image is returned
if width is None and height is None:
return image

# Check whether the width is None
if width is None:
# Calculate the ratio of height and construction dimensions
r = height / float(h)
dim = (int(w * r), height)

# Otherwise, height is None
else:
# Calculate the ratio of width and construction dimensions
r = width / float(w)
dim = (width, int(h * r))

# Resize image
resized = cv2.resize(image, dim, interpolation=inter)

return resized


### 3.4 skeleton

def skeletonize(image, size, structuring=cv2.MORPH_RECT):
# Determine the area (i.e. the total number of pixels in the image),
# Initialize the output skeleton image and construct morphological structure elements
area = image.shape * image.shape
skeleton = np.zeros(image.shape, dtype="uint8")
elem = cv2.getStructuringElement(structuring, size)

# Continue the loop until all pixels are removed from the image
while True:
# Corrosion and expansion images using structural elements
eroded = cv2.erode(image, elem)
temp = cv2.dilate(eroded, elem)

# Subtracts the temporary image from the original image, and then takes an "or" between the skeleton and the temporary image
temp = cv2.subtract(image, temp)
skeleton = cv2.bitwise_or(skeleton, temp)
image = eroded.copy()

# If there are no more "white" pixels in the image, exit the loop
if area == area - cv2.countNonZero(image):
break

# Returns the result of skeleton extraction
return skeleton


### 3.5Matplotlib display picture

def opencv2matplotlib(image):
# OpenCV represents images in BGR order; However, Matplotlib expects images to be in RGB order, so it simply converts from BGR to RGB and returns
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)


### Reference catalogue

https://www.pyimagesearch.com/2015/02/02/just-open-sourced-personal-imutils-package-series-opencv-convenience-functions/

Tags: OpenCV

Posted on Thu, 14 Oct 2021 23:28:28 -0400 by pbsperry