selective_search

本文参考自: 原文地址

import cv2
import selectivesearch
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np

#第二步:执行搜索工具,展示搜索结果
image2="images/test2.png"

#用cv2读取图片
img = cv2.imread(image2)

#白底黑字图 改为黑底白字图
img=255-img

#selectivesearch 调用selectivesearch函数 对图片目标进行搜索
img_lbl, regions =selectivesearch.selective_search(img, scale=500, sigma=0.9, min_size=20)

print ("第一个原始区域:",regions[0]) #{'labels': [0.0], 'rect': (0, 0, 585, 301), 'size': 160699} 第一个为原始图的区域
print ("总共区域数:",len(regions)) #共搜索到199个区域

# 接下来我们把窗口和图像打印出来,对它有个直观认识
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
ax.imshow(img)
for reg in regions:
    x, y, w, h = reg['rect']
    rect = mpatches.Rectangle((x, y), w, h,fill=False,edgecolor='red',linewidth=1)
    ax.add_patch(rect)
plt.show()


猜你喜欢

转载自blog.csdn.net/qq_39706019/article/details/81505038