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 img = cv2.imread("500x400.jpg") 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)
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