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