我有一个在视频帧上应用过滤器后识别轮廓的代码。现在,在我的例子中,我得到了3个轮廓,我通过在它们周围画矩形来显示它们,我想做的是在这3个轮廓矩形周围画一个矩形。就像是一个更大的矩形,包含3个检测到的矩形。 这是我的简单代码检测和绘制矩形周围的轮廓。
im2, contours, hierarchy = cv2.findContours(canny_img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
try: hierarchy = hierarchy[0]
except: hierarchy = []
# computes the bounding box for the contour, and draws it on the frame,
for contour, hier in zip(contours, hierarchy):
(x,y,w,h) = cv2.boundingRect(contour)
if w > 80 and h > 80:
cv2.rectangle(frame, (x,y), (x+w,y+h), (255, 0, 0), 2)
cv2.imshow('Motion Detector',frame)
也许可以试试这样的:
从本质上讲,您需要跟踪什么是最小的x和y坐标,什么是最大的x和y坐标(包括宽度和高度),然后用这些坐标绘制一个矩形。
使用numpy:
相关问题 更多 >
编程相关推荐