我有一个代码,可以在视频帧上应用滤镜后识别轮廓。 在我的情况下,我会得到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)
boundingRect
吗?也许这能够解决问题。 - PSchnfor contour, hier in zip(contours, hierarchy)
,因为您在for循环中没有使用hier
... 可以简单地使用for contour in contours
。 - Sabito stands with Ukraine