python opencv minAreaRect 生成最小外接矩形

PHOTO EMBED

Sat Oct 08 2022 13:16:20 GMT+0000 (Coordinated Universal Time)

Saved by @linzao

cnt = np.array([[x1,y1],[x2,y2],[x3,y3],[x4,y4]]) # 必须是array数组的形式
rect = cv2.minAreaRect(cnt) # 得到最小外接矩形的(中心(x,y), (宽,高), 旋转角度)
box = cv2.cv.BoxPoints(rect) # 获取最小外接矩形的4个顶点坐标(ps: cv2.boxPoints(rect) for OpenCV 3.x)
box = np.int0(box)
# 画出来
cv2.drawContours(img, [box], 0, (255, 0, 0), 1)
cv2.imwrite('contours.png', img)
content_copyCOPY

https://blog.csdn.net/lanyuelvyun/article/details/76614872