# Analysis of the principle of image clipping based on OpenCV in Python

Time：2020-7-27

This article mainly introduces Python through OpenCV image cutting principle analysis, the article through the example code introduced in detail, for everyone’s study or work has a certain reference learning value, need friends can refer to

Basic concepts of image clipping:
Image clipping refers to the removal of the areas outside the research area that we want in the image, which is often clipped according to the boundary of administrative division or research area. For example, for a 500 × 400 image, we only want the middle 250 × 200 area, and then we can use image clipping to remove the surrounding area.

In the actual development work, we often need to clip the image. According to the actual process of ERDAS image framing, we can divide the image framing clipping into two types: regular clipping and irregular clipping.

Regular framing clipping: it refers to that the boundary range of the clipped image is a rectangle. When clipping, we only need to use the coordinates of the upper left corner and the lower right corner to determine the clipping position of the image.

Irregular clipping: it means that the boundary of the cropped image is an arbitrary polygon, and a complete closed polygon area must be generated before clipping.

Opencv implementation of image clipping

Regular framing cutting:

In opencv, the image is regarded as matrix data, and we regard the image as a multidimensional list. Because the boundary range of regular framing clipping is a rectangle, we can implement the regular clipping of image according to the list slice. Now, let’s cut out the 250 × 200 area in the middle of the 500 × 400 image.

The calculation diagram is as follows:

``````import cv2
Img1 = img [100:300125:375] ා area to be reserved -- clipping
#Parameter 1 is the range of height and parameter 2 is the range of width

cv2.imwrite("linsi.jpg",img1)
cv2.waitKey(0)``````

design sketch:

Polygon Subset Image

For irregular clipping, a complete closed polygon region must be generated first. If we want to crop the image into a circle now, we have to generate a circle first. Opencv provides us with a special method circle for drawing circular graphs

In this paper, through the example code introduced in very detail, for everyone’s study or work has a certain reference learning value

## PHP uses redis to solve the problem of oversold

preface In the event of the second killing of goods, for example, the inventory of goods is only 100, but in the rush purchase activity, 200 people may rush to buy at the same time, so there is concurrency. If the order of 100 goods is completed and the inventory is 0, it may continue […]