The rectangular border (bounding Rectangle) is said to wrap the found shape with a minimal rectangle. There is also a rotating rectangle, the area will be smaller, the effect is shown below
First introduce the next Cv2.boundingrect (IMG) function
This function is very simple, IMG is a binary graph, which is its parameters;
Returns four values, respectively, x,y,w,h;
X,y is the coordinate of the upper-left point of the Matrix, W,h is the width and height of the matrix
Then use Cv2.rectangle (IMG, (x,y), (X+w,y+h), (0,255,0), 2) to draw a moment line
Parameter explanation
First parameter: IMG is the original artwork
The second parameter: (X,Y) is the upper-left point coordinate of the matrix
The third parameter: (X+W,Y+H) is the lower-right coordinate of the matrix
The fourth parameter: (0,255,0) is the corresponding RGB color of the drawing line
Fifth parameter: 2 is the width of the line being drawn
# with green (0, 255, 0) to draw the smallest rectangular frame
x, y, W, h = cv2.boundingrect (CNT)
Cv2.rectangle (IMG, (x, y), (x+w, Y+h), (0, 255, 0), 2)
# A rectangular frame with a rotating angle in red
rect = Cv2.minarearect (cnt)
box = cv2.cv.BoxPoints (rect)
box = np.int0 (box)
Cv2.drawcontours (IMG, [box], 0, (0, 0, 255), 2)
cv2.imwrite (' contours.png ', IMG)