如何将多个边界框合并到Python OpenCV中

发布于 2025-02-11 09:36:16 字数 1198 浏览 4 评论 0原文

我有python代码正在检测颜色。一旦检测到颜色,我就会找到轮廓并绘制它们。以下是原始图像:

”“在此处输入图像描述”

和以下是带有轮廓&的图像。它上面的边界框:

“在此处输入图像描述”

如您所见,有很多轮廓检测到,因此有多个边界框。有没有办法将这些边界框合并为一个。以下是代码

import cv2
import imutils
import numpy as np

image = cv2.imread("L00001.png")
image = imutils.resize(image, width=800)

hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_bound = np.array([45, 150, 20])
upper_bound = np.array([75, 305, 255])
origMask = cv2.inRange(hsv, lower_bound, upper_bound)
contours, h = cv2.findContours(origMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
    new = np.vstack(contours)
    area = cv2.contourArea(c)
    if area > 10:
        x, y, w, h = cv2.boundingRect(c)
        cv2.rectangle(image, (int(x), int(y)), (int(x + w), int(y + h)), (0, 0, 255), 2)

cv2.imshow("FRAME", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

I have python code which is detecting a color. Once the color is detected, I am finding the contours and drawing them. Below is the original image:

enter image description here

and below is the image with contours & bounding box on it:

enter image description here

As you can see there are lot of contours detected and thus there are multiple bounding box. Is there a way to merge these bounding box into one. Below is the code

import cv2
import imutils
import numpy as np

image = cv2.imread("L00001.png")
image = imutils.resize(image, width=800)

hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_bound = np.array([45, 150, 20])
upper_bound = np.array([75, 305, 255])
origMask = cv2.inRange(hsv, lower_bound, upper_bound)
contours, h = cv2.findContours(origMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
    new = np.vstack(contours)
    area = cv2.contourArea(c)
    if area > 10:
        x, y, w, h = cv2.boundingRect(c)
        cv2.rectangle(image, (int(x), int(y)), (int(x + w), int(y + h)), (0, 0, 255), 2)

cv2.imshow("FRAME", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小草泠泠 2025-02-18 09:36:16

我已经能够使用连接的组件分析来执行此操作。在此之前,我还应用了扩张,输出看起来令人满意

import cv2
import imutils
import numpy as np
from skimage import measure
from imutils import contours

image = cv2.imread("L00001.png")
image = imutils.resize(image, width=800)

hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_bound = np.array([45, 150, 20])
upper_bound = np.array([75, 305, 255])
origMask = cv2.inRange(hsv, lower_bound, upper_bound)
thresh = cv2.threshold(origMask, 200, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.erode(thresh, None, iterations=1)
thresh = cv2.dilate(thresh, None, iterations=6)

labels = measure.label(thresh, neighbors=4, background=0)
mask = np.zeros(thresh.shape, dtype="uint8")
for label in np.unique(labels):
    if label == 0:
        continue
    labelMask = np.zeros(thresh.shape, dtype="uint8")
    labelMask[labels == label] = 255
    numPixels = cv2.countNonZero(labelMask)
    if numPixels > 30:
        mask = cv2.add(mask, labelMask)

cnts, h = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for (i, c) in enumerate(cnts):
    area = cv2.contourArea(c)
    if area > 10:
        x, y, w, h = cv2.boundingRect(c)
        cv2.rectangle(image, (int(x), int(y)), (int(x + w), int(y + h)), (0, 0, 255), 2)

cv2.imshow("FRAME", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

”在此处输入图像说明”

I have been able to do this using connected component analysis. I also applied dilation before that and the output looks satisfactory

import cv2
import imutils
import numpy as np
from skimage import measure
from imutils import contours

image = cv2.imread("L00001.png")
image = imutils.resize(image, width=800)

hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_bound = np.array([45, 150, 20])
upper_bound = np.array([75, 305, 255])
origMask = cv2.inRange(hsv, lower_bound, upper_bound)
thresh = cv2.threshold(origMask, 200, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.erode(thresh, None, iterations=1)
thresh = cv2.dilate(thresh, None, iterations=6)

labels = measure.label(thresh, neighbors=4, background=0)
mask = np.zeros(thresh.shape, dtype="uint8")
for label in np.unique(labels):
    if label == 0:
        continue
    labelMask = np.zeros(thresh.shape, dtype="uint8")
    labelMask[labels == label] = 255
    numPixels = cv2.countNonZero(labelMask)
    if numPixels > 30:
        mask = cv2.add(mask, labelMask)

cnts, h = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for (i, c) in enumerate(cnts):
    area = cv2.contourArea(c)
    if area > 10:
        x, y, w, h = cv2.boundingRect(c)
        cv2.rectangle(image, (int(x), int(y)), (int(x + w), int(y + h)), (0, 0, 255), 2)

cv2.imshow("FRAME", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文