『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')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM