『Python』图像金字塔、滑动窗口和非极大值抑制实现

图像金字塔

1.在从cv2.resize中,传入参数时先列后行的

2.使用了python中的生成器,调用时使用for i in pyramid即可

3.scaleFactor是缩放因子,需要保证缩放后的图不小于最小尺寸,对应神经网络就是训练尺寸

'''图像金字塔'''
def resize(img, scaleFactor):
    # cv2.resize先接收列后接收行,返回亦然
    return cv2.resize(img, (int(img.shape[1] * (1/scaleFactor)),
                            int(img.shape[0] * (1/scaleFactor))),
                      interpolation=cv2.INTER_AREA)
def pyramid(image, scale=1.5, minSize = (200, 80)):
    yield image

    while True:
        image = resize(image, scale)
        if image.shape[0]  < minSize[1] or image.shape[1] < minSize[0]:
            break
        yield  image

 滑动窗口

'''滑动窗口'''
def sliding_window(image, stepSize, windowSize):
    for y in range(0, image.shape[0], stepSize):
        for x in range(0, image.shape[1], stepSize):
            yield(x, y, image[y:y+windowSize[1], x:x+windowSize[0]])

 非极大值抑制

'''非极大值抑制'''
def non_max_suppression_fast(boxes, overlapThresh):
    # 如果没有box,返回空list
    if len(boxes) == 0:
        return []
    # 修改boxes的格式为float方便处理
    if boxes.dtype.kind == 'i':
        boxes = boxes.astype('float')
    # 使用pick收集boxes
    pick = []
    x1 = boxes[:, 0]
    y1 = boxes[:, 1]
    x2 = boxes[:, 2]
    y2 = boxes[:, 3]
    scores = boxes[:, 4]
    area = (x2 - x1 + 1) * (y2 - y1 + 1)
    # 按照score从小到大的顺序排序indexes
    idxs = np.argsort(scores) # [::-1],源程序有这么一个逆序,不过我觉得应该去掉

    while len(idxs) > 0:
        # 分配最后一个(得分最低)index给i,并使用pick收集这个index(即i)
        last = len(idxs) - 1
        i = idxs[last]
        pick.append(i)
        # 在得分大于当前i的boxes中,
        # 找到重合部分的左上点和右下点
        xx1 = np.maximum(x1[i], x1[idxs[:last]])
        yy1 = np.maximum(y1[i], y1[idxs[:last]])
        xx2 = np.minimum(x2[i], x2[idxs[:last]])
        yy2 = np.minimum(y2[i], y2[idxs[:last]])
        # 计算上面得到的重合面积
        w = np.maximum(0, xx2 - xx1 + 1)
        h = np.maximum(0, yy2 - yy1 + 1)
        # 计算重合度
        overlap = (w * h) / area[idxs[:last]]
        # 删除得分最高的项(循环开始已经收集了),
        # 删除
        idxs = np.delete(idxs, np.concatenate(([last],
                                               np.where(overlap > overlapThresh))))  # [0])))
        # 加上索引之后只删除一个得分最高的过重合矩形,所以不应该加索引

    return boxes[pick].astype('int')

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值