PYTHON-OPENCV Boundingrect Use note

Source: Internet
Author: User

The rectangular border (bounding Rectangle) is said to wrap the found shape with a minimum rectangle. There is also a rectangle with a rotation, the area will be smaller, the effect see

On the Code

First introduce the next Cv2.boundingrect (IMG) function

This function is very simple, IMG is a binary graph, that is, its parameters;

Returns four values, respectively, x,y,w,h;

X, y is the coordinates of the top left of the matrix, W,h is the width and height of the matrix

Then usecv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)画出矩行

参数解释

第一个参数:img是原图

第二个参数:(x,y)是矩阵的左上点坐标

第三个参数:(x+w,y+h)是矩阵的右下点坐标

第四个参数:(0,255,0)是画线对应的rgb颜色

第五个参数:2是所画的线的宽度

# with Green (0,255,0) to draw the smallest rectangle frame x, y, W, H=Cv2.boundingrect (CNT) Cv2.rectangle (IMG, (x, y), (x+w, Y+h), (0,255,0),2# in red indicates a rectangular frame with a rotation angle 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)
However, if used in Python, there is no vector or mat as input to the Boundingrect, the following error will appear:
    X, Y, W, h = cv2.boundingrect (landmarks) typeerror:points is not a numpy array, neither a scalar

The above landmark as input is a list,
Solution:
Therefore, we need to introduce numpy to his strong turn, the specific operation is as follows:

Import as  = cv2.boundingrect (np.array (landmarks))  + W, y + h), (0  25502)


This allows you to turn the list into an array.







PYTHON-OPENCV Boundingrect Use note

Related Article

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.