本文图像内容均来自这里,如果您对cv和dl感兴趣,请查看此连接获取详细介绍。

import numpy as np
import cv2 
def non_max_suppression_slow(boxes,overlapThresh):
    if len(boxes) == 0:
        return []
    pick = []
    # 对所有的框分别提取他们的左上角坐标和右下角坐标
    x1 = boxes[:,0]
    y1 = boxes[:,1]
    x2 = boxes[:,2]
    y2 = boxes[:,3]
    # 计算所有框的面积
    area = (x2-x1+1)*(y2-y1+1)
    # 按照右下角坐标排序获取排序后的索引
    idxs = np.argsort(y2)
    # 训练条件是框的数量大于0
    while len(idxs) > 0:
        last = len(idxs) - 1
        i = idxs[last]
        pick.append(i)
        suppress = [last]
        # 循换对每个框计算面积,计算重叠率,如果重叠率大于某一个值则将这个框的索引求出
        for pos in range(0,last):
            j = idxs[pos]
            xx1 = max(x1[i],x1[j])
            yy1 = max(y1[i],y1[j])
            xx2 = min(x2[i],x2[j])
            yy2 = min(y2[i],y2[j])
            w = max(0,xx2-xx1+1)
            h = max(0,yy2-yy1+1)
            overlap = float(w*h)/area[j]
            if overlap > overlapThresh:
                suppress.append(pos)
            # 统计的重叠率大于Thresh的,从idxs中删除
        idxs = np.delete(idxs,suppress)
    return boxes[pick]
    # 下面测试函数功能
images = [
    ("image_1.png", np.array([
        (239, 80, 454, 303),
        (219, 95, 443, 321),
        (264, 51, 489, 287),
        (172, 28, 431, 333)]))]
for (imagePath,boundingBoxes) in images:
    print("[{}] initial bounding boxes".format(len(boundingBoxes)))
    image = cv2.imread(imagePath)
    orig = image.copy()
    for (startX,startY,endX,endY) in boundingBoxes:
          cv2.rectangle(orig,(startX,startY),(endX,endY),(0,0,255),2)
    pick = non_max_suppression_slow(boundingBoxes,0.3)
    print("after applying non-maximum,{} bounding boxes".format(len(pick)))
    for (startX,startY,endX,endY) in pick:
          cv2.rectangle(image,(startX,startY),(endX,endY),(0,255,0),2)
          cv2.imshow('Original',orig)
          cv2.imshow("After NMS",image)
          cv2.waitKey()

NMS处理之后 NMS处理之前

results matching ""

    No results matching ""