根据selectivesearch 算法 加上iou 找图里面的汉字 -。- 干啥用大家都懂
更好的有 faster-rcnn 但是需要做深度学习什么的,而且也没那么多图片做深度学习。
# -*- coding: utf-8 -*- import skimage.data import matplotlib.pyplot as plt import matplotlib.patches as mpatches import selectivesearch rate = 0.2 mrate = 0.4 def main(): # loading astronaut image #img = skimage.data.astronaut() img = plt.imread("D:/png/4.jpeg") # 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 x, y, w, h = r['rect'] if w >15 and w<60 and h>15 and h <60: # if h > 30 and h <300 and w>10 and w<100: candidates.add(r['rect']) # candidates.add(r['rect']) candidates = filter(list(candidates)) # draw rectangles on the original image fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6)) ax.imshow(img) 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() def filter(candidates): candidates.sort(key=lambda x: (x[2]*x[3]) ,reverse=True) fs = set() while len(candidates) > 0: s = [] ious = [] for v in candidates: if 0 == len(s): s = v ious.append(0.) continue ious.append(IOU(s,v)) maxRate = 0. while True : maxRate = max(ious) if maxRate > mrate: candidates.pop(ious.index(maxRate)) ious.pop(ious.index(maxRate)) else: break index = 0 if maxRate > rate: index = ious.index(maxRate) i=0 for v in ious: if i == index : fs.add(s) if v > rate : candidates.pop(i) i = i-1 i = i + 1 fs.add(candidates[0]) candidates.pop(0) return fs def IOU (vec1, vec2): w = min(vec1[0]+vec1[2], vec2[0]+vec2[2]) - max(vec1[0], vec2[0]) h = min(vec1[1]+vec1[3], vec2[1]+vec2[3]) - max(vec1[1], vec2[1]) if w < 0 or h < 0: return 0. return float(w) * float(h) /(float(vec1[2]*vec1[3])) if __name__ == "__main__": main()
原始图片:
运算之后
左边的坐标是 x, y, w, h 坐标系。