【opencv】Selective Search demo

參考 Selective Search算法與演示

我用的 jupyter notebook,所以要克服下 opencv 的 cv2.imshow() 問題,參考
opencv如何在jupyter notebook中顯示圖片

jupyter notebook 的安裝可以參考
本地遠程訪問Ubuntu16.04.3服務器上的Jupyter notebook
【Windows】TensorFlow GPU Configuration


import cv2
import matplotlib.pyplot as plt
if __name__ == '__main__':
    cv2.setUseOptimized(True);
    cv2.setNumThreads(4);

    # read image
    im = cv2.imread('/root/userfolder/Experiment/1.png')
    # resize image
    newHeight = 200
    newWidth = int(im.shape[1] * 200 / im.shape[0])
    im = cv2.resize(im, (newWidth, newHeight))
    
    #cv2.imshow("input", im)  
    # jupyter notebook 
    #img = im[:,:,::-1] # 必須爲 ::-1
    #plt.imshow(im)


    # 創建算法+設置輸入圖像
    ss = cv2.ximgproc.segmentation.createSelectiveSearchSegmentation()
    ss.setBaseImage(im)

    # 使用SS快速版本
    ss.switchToSelectiveSearchFast()

    # 執行SS
    rects = ss.process()
    print('Total Number of Region Proposals: {}'.format(len(rects)))

    # 推薦100個ROI
    numShowRects = 100
    imOut = im.copy()

    # 顯示前100個區域外接矩形框
    for i, rect in enumerate(rects):
        if i < numShowRects:
            x, y, w, h = rect
            cv2.rectangle(imOut, (x, y), (x + w, y + h), (0, 255, 0), 1, cv2.LINE_AA)
        else:
            break

    # show output
    """
    cv2.imshow("SS-Demo", imOut)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    """
    # jupyter notebook
    img = imOut[:,:,::-1] # 必須爲 ::-1
    plt.xticks(())
    plt.yticks(())
    plt.imshow(img)

處理前
在這裏插入圖片描述
處理後
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章