selectivesearch找词

根据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 坐标系。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值