Extracting text OpenCV

后端 未结 10 1852
臣服心动
臣服心动 2020-11-22 08:10

I am trying to find the bounding boxes of text in an image and am currently using this approach:

// calculate the local variances of the grayscale image
Mat          


        
10条回答
  •  有刺的猬
    2020-11-22 08:29

    You can utilize a python implementation SWTloc.

    Full Disclosure : I am the author of this library

    To do that :-

    First and Second Image

    Notice that the text_mode here is 'lb_df', which stands for Light Background Dark Foreground i.e the text in this image is going to be in darker color than the background

    from swtloc import SWTLocalizer
    from swtloc.utils import imgshowN, imgshow
    
    swtl = SWTLocalizer()
    # Stroke Width Transform
    swtl.swttransform(imgpaths='img1.jpg', text_mode = 'lb_df',
                      save_results=True, save_rootpath = 'swtres/',
                      minrsw = 3, maxrsw = 20, max_angledev = np.pi/3)
    imgshow(swtl.swtlabelled_pruned13C)
    
    # Grouping
    respacket=swtl.get_grouped(lookup_radii_multiplier=0.9, ht_ratio=3.0)
    grouped_annot_bubble = respacket[2]
    maskviz = respacket[4]
    maskcomb  = respacket[5]
    
    # Saving the results
    _=cv2.imwrite('img1_processed.jpg', swtl.swtlabelled_pruned13C)
    imgshowN([maskcomb, grouped_annot_bubble], savepath='grouped_img1.jpg')
    


    Third Image

    Notice that the text_mode here is 'db_lf', which stands for Dark Background Light Foreground i.e the text in this image is going to be in lighter color than the background

    from swtloc import SWTLocalizer
    from swtloc.utils import imgshowN, imgshow
    
    swtl = SWTLocalizer()
    # Stroke Width Transform
    swtl.swttransform(imgpaths=imgpaths[1], text_mode = 'db_lf',
                  save_results=True, save_rootpath = 'swtres/',
                  minrsw = 3, maxrsw = 20, max_angledev = np.pi/3)
    imgshow(swtl.swtlabelled_pruned13C)
    
    # Grouping
    respacket=swtl.get_grouped(lookup_radii_multiplier=0.9, ht_ratio=3.0)
    grouped_annot_bubble = respacket[2]
    maskviz = respacket[4]
    maskcomb  = respacket[5]
    
    # Saving the results
    _=cv2.imwrite('img1_processed.jpg', swtl.swtlabelled_pruned13C)
    imgshowN([maskcomb, grouped_annot_bubble], savepath='grouped_img1.jpg')
    

    You will also notice that the grouping done is not so accurate, to get the desired results as the images might vary, try to tune the grouping parameters in swtl.get_grouped() function.

提交回复
热议问题