Selective search是一種基於特征的目標檢測算法,在R-CNN中被用來生成候選區域。
論文鏈接:http://www.huppelen.nl/publications/selectiveSearchDraft.pdf
code鏈接:https://github.com/AlpacaDB/selectivesearch
原理簡介
在圖像中尋找物體,可以依據多種特征,例如顏色、紋理、形狀等。然而,這些特征並不能通用地用來尋找所有的物體,物體在圖像中的尺度也大小不一。為了兼顧各種尺度與特征,selective search的做法是先尋找尺寸較小的區域,然后逐漸將特征相近的小尺度的區域合並為大尺度區域,從而得到內部特征一致的物體圖像。
算法的流程為:
- 對圖像進行分割,得到較小的候選區域,使用的是“Felzenswalb and Huttenlocher”算法,這個算法的具體內容有待研究。
- 對每兩個相鄰的候選區域,計算其相似度,並將相似度最高的兩個區域合並,並計算該合並區域與所有相鄰區域的相似度,重復該步驟,直到沒有可以計算的相鄰區域。
其中,計算相似度的方法為:顏色越相近的區域相似度越大、紋理越相近的區域相似度越大、越小的區域相似度越大(實際上是權重越大)、區域的吻合度越大相似度越大。
顏色相近的評價標准是對各個通道計算顏色直方圖,然后取各個對應bins的直方圖最小值。
紋理相近的評價標准是計算各個區域的快速sift特征,然后對紋理直方圖的各個對應bins求最小值。
小區域的權重算法是s_size(ri, rj) = 1 - (size(ri) + size(rj)) / size(img)
。
區域吻合度指的是,兩個區域相鄰的邊的邊長差距不能太大,也就是合並時不能出現“斷崖”,表示方法是外接矩形的重合度應當盡可能大。算法為s_fill(ri, rj) = 1 - (size(bbox) - size(ri) - size(rj)) / size(img)
。
最終的相似度由這幾項加權得到。
除此之外,為了保證對各種情景下圖像的魯棒性,算法還使用了多種色彩空間變換,來增多色彩的特征數量。
代碼使用方法
給出的算法的函數原型為:
1 def selective_search( 2 im_orig, scale=1.0, sigma=0.8, min_size=50): 3 '''Selective Search 4 5 Parameters 6 ---------- 7 im_orig : ndarray 8 Input image 9 scale : int 10 Free parameter. Higher means larger clusters in felzenszwalb segmentation. 11 sigma : float 12 Width of Gaussian kernel for felzenszwalb segmentation. 13 min_size : int 14 Minimum component size for felzenszwalb segmentation. 15 Returns 16 ------- 17 img : ndarray 18 image with region label 19 region label is stored in the 4th value of each pixel [r,g,b,(region)] 20 regions : array of dict 21 [ 22 { 23 'rect': (left, top, width, height), 24 'labels': [...], 25 'size': component_size 26 }, 27 ... 28 ] 29 '''
在使用時,需要去除重復區域、過小的區域以及一些長寬比過大或過小的“畸形區域”。以下是一個使用的示例:
1 import skimage.data 2 import matplotlib.pyplot as plt 3 import matplotlib.patches as mpatches 4 import selectivesearch 5 6 7 def main(): 8 9 # loading astronaut image 10 img = skimage.data.astronaut() 11 12 # perform selective search 13 img_lbl, regions = selectivesearch.selective_search( 14 img, scale=500, sigma=0.9, min_size=10) 15 16 candidates = set() 17 for r in regions: 18 # excluding same rectangle (with different segments) 19 if r['rect'] in candidates: 20 continue 21 # excluding regions smaller than 2000 pixels 22 if r['size'] < 2000: 23 continue 24 # distorted rects 25 x, y, w, h = r['rect'] 26 if w / h > 1.2 or h / w > 1.2: 27 continue 28 candidates.add(r['rect']) 29 30 # draw rectangles on the original image 31 fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6)) 32 ax.imshow(img) 33 for x, y, w, h in candidates: 34 print(x, y, w, h) 35 rect = mpatches.Rectangle( 36 (x, y), w, h, fill=False, edgecolor='red', linewidth=1) 37 ax.add_patch(rect) 38 39 plt.show() 40 41 if __name__ == "__main__": 42 main()