【opencv】Selective Search demo

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bryant_meng/article/details/86540629

参考 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)

处理前
在这里插入图片描述
处理后
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/bryant_meng/article/details/86540629