python-3.x - 为什么 NMSboxes 没有消除多个边界框?

标签 python-3.x opencv object-detection yolo opencv-python

首先这里是我的代码:

        image = cv2.imread(filePath)
        height, width, channels = image.shape
        
        # USing blob function of opencv to preprocess image
        blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416),
        swapRB=True, crop=False)
        #Detecting objects
        net.setInput(blob)
        outs = net.forward(output_layers)
        
        # Showing informations on the screen
        class_ids = []
        confidences = []
        boxes = []

        for out in outs:
            for detection in out:
                scores = detection[5:]
                class_id = np.argmax(scores)
                confidence = scores[class_id]
                if confidence > 0.7:
                    # Object detected
                    center_x = int(detection[0] * width)
                    center_y = int(detection[1] * height)
                    w = int(detection[2] * width)
                    h = int(detection[3] * height)

                    # Rectangle coordinates
                    x = int(center_x - w / 2)
                    y = int(center_y - h / 2)

                    boxes.append([x, y, w, h])
                    confidences.append(float(confidence))
                    class_ids.append(class_id)
                    
                indexes = cv2.dnn.NMSBoxes(boxes, confidences,score_threshold=0.4,nms_threshold=0.8,top_k=1)
                
        font = cv2.FONT_HERSHEY_PLAIN
        colors = np.random.uniform(0, 255, size=(len(classes), 3))
        labels = ['bicycle','car','motorbike','bus','truck']
        for i in range(len(boxes)):
            if i in indexes:
                label = str(classes[class_ids[i]])
                if label in labels:
                    x, y, w, h = boxes[i]
                    color = colors[class_ids[i]]
                    cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
                    cv2.putText(image, label, (x, y + 30), font, 2, color, 3)
        cv2.imshow(fileName,image)
我的问题是:不是 cv2.dnn.NMSBoxes假设消除多个边界框?那么为什么我仍然得到如下示例的输出:
sample 1 of multiple bounding blocks
sample 2 of multiple bounding blocks
我所期望的类似于以下内容:
enter image description here
我的代码做错了吗?有没有更好的选择?非常感谢您的帮助。

最佳答案

NMS的流程是这样的
输入 - 提案框列表 B,对应的置信度 S 和重叠阈值 N
输出 - 过滤后的提案列表 D
算法/步骤

  • 选择置信度得分最高的提议,将其从 B 中移除,并将其添加到最终提议列表 D。(最初 D 为空)
  • 现在将此提案与所有提案进行比较——计算该提案与其他提案的 IOU(交集)。如果 IOU 大于阈值 N,则从 B
  • 中删除该提案
  • 再次从 B 中剩余的提案中取出具有最高置信度的提案,将其从 B 中移除,并将其添加到 D
  • 再次用B中的所有proposals计算这个proposal的IOU,并剔除IOU高于阈值的框
  • 重复这个过程,直到 B
  • 中没有更多的提案为止。

    这里所指的阈值只不过是nms_threshold .
    cv2.dnn.NMSBoxes功能,nms_threshold是用于非极大值抑制的 IOU 阈值。
    所以如果你有一个很大的值,你会强制两个框有很高的重叠(通常不是这种情况),只有当它与另一个框的 IOU 大于 0.8 时,才会删除该框。由于通常没有这么多重叠,因此不会删除这些框。减少此值将更容易删除冗余检测
    希望这是有道理的
    您可以阅读有关非极大值抑制的更多信息 here

    关于python-3.x - 为什么 NMSboxes 没有消除多个边界框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66701910/

    相关文章:

    Python 有限差分样条

    缺少 OpenCV 透明 API UMat

    opencv - 将CvSeq转换为CvMat

    tensorflow - 将预训练模型生成的预测输出解码为人类可读的标签

    tensorflow - 将图形 (pb) 转换为 SavedModel 以用于 gcloud ml-engine predict

    python - 如何在Python中找到两个给定数组中非互质对的数量?

    python - Python 教程 - 我应该使用 2.x 还是 3.0?

    python-3.x - Airflow 测试任务有效,但在 dag 中运行失败

    c++ - 只检测 opencv 中较粗的水平直线

    python - 如何使用 python 和 opencv 检测图像中的水平线并获取其 y 坐标?