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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40203932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:16:17  来源:igfitidea点击:

Drawing a rectangle around all contours in OpenCV Python

pythonpython-2.7opencv

提问by Moeed Kundi

i have a code which identifies contours after applying filters on video frames. Now in my case i get 3 contours and i show them by drawing rectangles around them, what i want to do is drawing a rectangle around all these 3 contour rectangles. like it will be a larger rectangle, containing 3 detected rectangles. Here's my simple code of detecting and drawing rectangles around contours.

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

回答by pzp

Maybe try something like this:

也许尝试这样的事情:

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)

Essentially you want to keep track of what the smallest x and y coordinates are and what the largest x and y coordinates (including the width and height) are, and then just draw a rectangle with those coordinates.

本质上,您想跟踪最小的 x 和 y 坐标是什么以及最大的 x 和 y 坐标(包括宽度和高度)是多少,然后用这些坐标绘制一个矩形。

回答by lorenzo

Using numpy:

使用 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 = np.min(boxes[:,0])
top = np.min(boxes[:,1])
right = np.max(boxes[:,2])
bottom = np.max(boxes[:,3])

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

回答by Bart

If you want to box everything in a binary image, you can generate points from all non zero values and apply the algorithms on that. This is done as seen below:

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

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