OpenCV Boundingrect and Boundingrect usage

Source: Internet
Author: User

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)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.