標籤:順序 div 1.5 oat bre 視窗 大於 ati 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‘)
『python』電腦視覺_經典目標檢測演算法中的幾個概念