目标检测:选择性搜索(selective search)

用selective search方法提取候选框最简单的方法

1、根据如下github连接下载相关文件,并安装相应包

https://github.com/AlpacaDB/selectivesearch

终端下执行:

pip install selectivesearch

2、运行example文件中运行脚本 example.py,其中的图像为自带的测试图像

3、修改example.py,读取自己的图像进行测试

# -*- coding: utf-8 -*-
from __future__ import (
    division,
    print_function,
)

import skimage.data
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import selectivesearch
from skimage import io           ##

def main():

    # loading astronaut image
    #img = skimage.data.astronaut()  #自带的测试图像
    img = io.imread('/home/qf/git/myfile2/selectivesearch-develop/example/test_00042.jpg')

    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(
        img, scale=500, sigma=0.9, min_size=10)

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:   #避免选取重复的框
            continue
        # excluding regions smaller than 2000 pixels
        if r['size'] < 20000:         #筛掉较小的框
            continue
        # distorted rects
        x, y, w, h = r['rect']
        if w / h < 0.25 or h / w < 0.25:  #筛掉纵横比小于1/4的框
            continue
        candidates.add(r['rect'])

    # draw rectangles on the original image
    fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
    ax.imshow(img)
###############################################
    #print(candidates)
    with open("box.txt","w") as f:#格式化字符串还能这么用!
        for x, y, w, h in candidates:
            f.writelines([str(x),' ',str(y),' ',str(x+w),' ',str(y+h),'\n'])
###############################################
    for x, y, w, h in candidates:
        print(x, y, w, h)
        rect = mpatches.Rectangle(
            (x, y), w, h, fill=False, edgecolor='red', linewidth=1)
        ax.add_patch(rect)

    plt.show()

if __name__ == "__main__":
    main()

猜你喜欢

转载自blog.csdn.net/qq_38096703/article/details/80960158