在OpenCV Python中绘制一个包围所有轮廓的矩形

11

我有一个代码,可以在视频帧上应用滤镜后识别轮廓。 在我的情况下,我会得到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)

你尝试了什么?拿一张纸自己画出来,然后思考你使用的算法。 - Dan Mašek
你尝试从所有轮廓创建一个点集,并在该集上计算boundingRect吗?也许这能够解决问题。 - PSchn
似乎没有必要使用for contour, hier in zip(contours, hierarchy),因为您在for循环中没有使用hier... 可以简单地使用for contour in contours - Sabito stands with Ukraine
3个回答

17

也许可以尝试像这样:

im2, contours, hierarchy = cv2.findContours(canny_img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

try: hierarchy = hierarchy[0]
except: hierarchy = []

height, width, _ = canny_img.shape
min_x, min_y = width, height
max_x = max_y = 0

# 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)
    min_x, max_x = min(x, min_x), max(x+w, max_x)
    min_y, max_y = min(y, min_y), max(y+h, max_y)
    if w > 80 and h > 80:
        cv2.rectangle(frame, (x,y), (x+w,y+h), (255, 0, 0), 2)

if max_x - min_x > 0 and max_y - min_y > 0:
    cv2.rectangle(frame, (min_x, min_y), (max_x, max_y), (255, 0, 0), 2)

实质上,您需要跟踪最小的x和y坐标以及最大的x和y坐标(包括宽度和高度),然后只需使用这些坐标绘制一个矩形。


2
对于OpenCV 4,只需使用以下代码: contours, hierarchy = cv2.findContours(canny_img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) - GStav

13

使用numpy:

boxes = []
for c in cnts:
    (x, y, w, h) = cv2.boundingRect(c)
    boxes.append([x,y, x+w,y+h])

boxes = np.asarray(boxes)
left, top = np.min(boxes, axis=0)[:2]
right, bottom = np.max(boxes, axis=0)[2:]

cv2.rectangle(frame, (left,top), (right,bottom), (255, 0, 0), 2)

2

如果您想将所有内容都放在二进制图像中,可以从所有非零值生成点并对其应用算法。如下所示:

    points = cv2.findNonZero(thresholdImage)
    rect = cv2.minAreaRect(points)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接