imutils It's an image processing toolkit , It's right opencv Some methods of secondary processing were used , Make it easier to use . Compared with opencv
Learning difficulty of , As a result, many methods need a certain foundation , Novices may start slowly , and imutils It is convenient to use , Can help us understand opencv

This article will analyze it imutils Source code of , See how it's called opencv Methods . By the way, I'll learn , Here we mainly talk about several image functions in common use

<> translation

View source code :
def translate(image, x, y): # define the translation matrix and perform the
translation M = np.float32([[1, 0, x], [0, 1, y]]) shifted = cv2.warpAffine(
image, M, (image.shape[1], image.shape[0])) # return the translated image return
shifted
translate Meaning of each parameter :

* image: input image
* x: Horizontal movement , Positive means right
* y: Vertical movement , Positive means up
This function uses the opencv Inside warpAffine method , Let's look at the meaning and function of each parameter :

cv2.warpAffine(img,M,(rows,cols),flags=cv2.INTER_,borderMode=cv2.BORDER_REFLECT,borderValue=(0,0,0))

* img: input image
* M: Transformation matrix
* (rows,cols): The size of the output image
* flags: Combination of interpolation methods (int type )
* borderMode: Boundary pixel mode (int type )
* borderValue: Boundary fill value ; By default, it is 0
ad locum imutils Only the first three parameters are used , The translation function is mainly reflected in M in :
M=[10tx01ty]M= \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ \end{bmatrix} M=[1
0​01​tx​ty​​]

txt_xtx​ A positive value means moving to the right ,tyt_yty​ Positive indicates downward movement

through imutils After simplification , Just input x,yx,yx,y That's it , Internal adoption numpy The two numbers are directly transformed into a matrix

Examples of use :
# Move the image to the right 25, Move up 75 translated = imutils.translate(image, 25, -75)
Images before and after moving :

<> rotate

View source code :
def rotate(image, angle, center=None, scale=1.0): # grab the dimensions of the
image (h, w) = image.shape[:2] # if the center is None, initialize it as the
center of # the image if center is None: center = (w // 2, h // 2) # perform
the rotation M = cv2.getRotationMatrix2D(center, angle, scale) rotated = cv2.
warpAffine(image, M, (w, h)) # return the rotated image return rotated
rotate Meaning of each parameter :

* image: input image
* angle: Rotation angle
* center: Central coordinates ( No input required , Automatically from image size )
* scale: Scaling
This function uses the opencv Inside getRotationMatrix2D and warpAffine method , among warpAffine
It was already said in the last function , Let's take a look getRotationMatrix2D The meaning and function of each parameter :
cv2.getRotationMatrix2D(center, angle, scale)

* center: Center point coordinates
* angle: Rotation angle
* scale: Scaling
Its function is to obtain affine transformation matrix , Then by warpAffine Make a transformation , Transform information into warpAffine Understandable matrix

The image is rotated at a certain angle θ\thetaθ It is realized by the following transformation matrix :
[cosθ−sinθsinθcosθ]\begin{bmatrix} cos\theta & -sin\theta \\ sin\theta &
cos\theta \\ \end{bmatrix}[cosθsinθ​−sinθcosθ​]

however OpenCV Provides scalable rotation and adjustable center of rotation , So you can rotate anywhere you like . The modified transformation matrix is :
M=[αβ(1−α)⋅center.x−β⋅center.y−βαβ⋅center.x+(1−α)⋅center.y]M=\begin{bmatrix}
\alpha & \beta & (1-\alpha)\cdot center.x-\beta \cdot center.y \\ -\beta &
\alpha & \beta \cdot center.x+(1-\alpha)\cdot center.y \\ \end{bmatrix}M=[α−β​βα
​(1−α)⋅center.x−β⋅center.yβ⋅center.x+(1−α)⋅center.y​]

among :α=scale⋅cosθ,β=scale⋅sinθ\alpha=scale \cdot cos\theta,\beta=scale \cdot
sin\thetaα=scale⋅cosθ,β=scale⋅sinθ, The matrix change is getRotationMatrix2D Function of

through imutils After simplification , You can combine the two processes , Only input image and rotation angle

Examples of use :
# Circular rotation for angle in xrange(0, 90, 180, 270): # Rotate and show rotated = imutils.rotate(
bridge, angle=angle) cv2.imshow("Angle=%d" % (angle), rotated)
Rotated image :

<> Sizing

View source code :
def resize(image, width=None, height=None, inter=cv2.INTER_AREA): # initialize
the dimensions of the image to be resized and # grab the image size dim = None (
h, w) = image.shape[:2] # if both the width and height are None, then return the
# original image if width is None and height is None: return image # check to
see if the width is None if width is None: # calculate the ratio of the height
and construct the # dimensions r = height / float(h) dim = (int(w * r), height)
# otherwise, the height is None else: # calculate the ratio of the width and
construct the # dimensions r = width / float(w) dim = (width, int(h * r)) #
resize the image resized = cv2.resize(image, dim, interpolation=inter) # return
the resized image return resized
resize Meaning of each parameter :

* image: input image
* width: The width of the output image
* height: The height of the output image (width and height Just choose one , The other one is scaled )
* inter: interpolation method , The default is cv2.INTER_AREA
This function uses the opencv Inside resize method , Let's look at the meaning and function of each parameter :

* image: input image
* dim: Scaling
* interpolation: interpolation method
As you can see, it's just changing the form of the input parameters , If you know the scale, you can choose it directly opencv Of resize, If you know the width or height of the changed image, you can choose imutils Of
resize

Examples of use :
# Traverse adjust width for width in (400, 300, 200, 100): # Change and display resized = imutils.resize(
workspace, width=width) cv2.imshow("Width=%dpx" % (width), resized)
Picture after adjustment :

<> Skeletonization

View source code :
def skeletonize(image, size, structuring=cv2.MORPH_RECT): # determine the area
(i.e. total number of pixels in the image), # initialize the output
skeletonized image, and construct the # morphological structuring element area =
image.shape[0] * image.shape[1] skeleton = np.zeros(image.shape, dtype="uint8")
elem= cv2.getStructuringElement(structuring, size) # keep looping until the
erosions remove all pixels from the # image while True: # erode and dilate the
image using the structuring element eroded = cv2.erode(image, elem) temp = cv2.
dilate(eroded, elem) # subtract the temporary image from the original, eroded #
image, then take the bitwise '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, then #
break from the loop if area == area - cv2.countNonZero(image): break # return
the skeletonized image return skeleton
skeletonize Meaning of each parameter :

* image: input image
* size: The size of the structure element kernel
* structuring: The shape of the structure element kernel
Here, the structural element representation is like a “ Blackboard eraser ”, Erase the image as a blackboard from top to bottom , And change
This function uses more than one opencv Internal method , Let's take a look at them one by one :
cv2.getStructuringElement

* structuring: The shape of the structure element kernel
* size: The size of the structure element kernel
This is the definition “ Blackboard eraser ” Size and shape of , Changes are provided by subsequent methods
cv2.erode(image,kernel,iterations=1)

* image: input image
* kernel: kernel
* iterations: Number of iterations ( No input is allowed , Select the default number of times )
Here is the corrosion image , Narrow lines and remove noise , The degree of change depends on the size of the kernel
cv2.dilate(image,kernel,iterations=1)

* image: input image
* kernel: kernel
* iterations: Number of iterations ( No input is allowed , Select the default number of times )
Here is the expansion operation , Because the noise will be reduced by corrosion , You can then use the dilation operation to restore the line to its original size
cv2.subtract(image1,image2,dst=None,mask=None,dtype=None)

* image1: input image 1
* image2: input image 2
Here we subtract the two images , Remove the background
cv2.bitwise_or(image1,image2,mask=noArray())

* image1: input image 1
* image2: input image 2
* mask: Mask , Use selected image , Figure or object , For processed images ( In whole or in part ) Occlusion , To control the area or process of image processing
Here, each pixel value of the image is binary “ or ” operation ,1|1=1,1|0=0,0|1=0,0|0=0
Skeletonization is to construct objects in the image “ Topological skeleton ” The process of , It is assumed that the object is white on a black background

Examples of use :
gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY) skeleton = imutils.skeletonize(
gray, size=(3, 3)) cv2.imshow("Skeleton", skeleton)
Pictures before and after skeletonization :

<> Into Matplotlib Display format

View source code :
def opencv2matplotlib(image): # OpenCV represents images in BGR order;
however, Matplotlib # expects the image in RGB order, so simply convert from BGR
# to RGB and return return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
It's not so much a transformation Matplotlib format , In essence, the BGR Into RGB. stay Python Of OpenCV in , Image with BGR The order is expressed as
NumPy array . Use this cv2.imshow Function time , This method works properly , however Matplotlib Medium image press RGB Sequential arrangement .

Matplotlib Images in different order in :

<>Canny Automatic edge detection

View source code :
def auto_canny(image, sigma=0.33): # compute the median of the single channel
pixel intensities v = np.median(image) # apply automatic Canny edge detection
using the computed median lower = int(max(0, (1.0 - sigma) * v)) upper = int(min
(255, (1.0 + sigma) * v)) edged = cv2.Canny(image, lower, upper)
This function is used to help us use the cv2.Canny, Have a look first cv2.Canny Parameter structure of :
cv2.Canny(image,threshold1,threshold2,[, edges[,apertureSize[,L2gradient ]]])

* image: input image
* threshold1, threshold2: The threshold is used to detect the obvious edge in the image
The threshold is difficult to determine , If used directly cv2.Canny, It is not always possible to find a suitable value , but imutils By processing the data from the image itself , A relatively suitable threshold can be provided

It should be noted that , Only grayscale or single channel images can be read

Examples of use :
gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY) edgeMap = imutils.auto_canny(gray
) cv2.imshow("Original", logo) cv2.imshow("Automatic Edge Map", edgeMap)
Image before and after edge detection :

Technology
©2019-2020 Toolsou All rights reserved,
One is called “ Asking for the train ” A small village Finally got the train Spring Boot Lesson 16 :SpringBoot Implementation of multithreading with injection class Chrome OS, For programmers and Windows What does it mean ? Internet Marketing JAVA Convert a string to a numeric type I've been drinking soft water for three years ? What is the use of soft water and water softener You don't know ——HarmonyOS Talking about uni-app Page value transfer problem JavaScript Medium Call and ApplySparkSQL Achieve partition overlay write Character recognition technology of vehicle license plate based on Neural Network