Detecting space(bar) between words in a slanted font

好久不见. 提交于 2020-01-11 13:39:10

问题


I wrote a python script that detects alphabets encoded in an image. The script is using openCV's templateMatching to match characters/alphabets embedded in the image. The detection is working fine except for the space(spacebar) character.

Here is a sample image

Is there some (easy/direct)way to detect the whitespace between words using (or without using) openCV in python?


回答1:


You can scan for empty space along skewed vertical lines

  1. scan whole image
  2. count font pixels per line
  3. if no pixel counted then gap found (green and blue lines)
  4. count joined gap lines (w)

    if wider or equal to threshold (3 in your case) then the found gap is gap between words (Blue lines)

This is how I done it in C++:

int x,y,i,w;
picture pic0,pic1,pic2; // pic0 - original input image,pic1 output, pic2 temp

pic1=pic0;              // copy input image pic0 to pic2
pic2=pic0;              // copy input image pic0 to pic1
pic2.rgb2i();           // and convert to grayscale intensity

for (w=0,x=pic2.ys>>1;x<pic2.xs;x++)
    {
    // count pixels per skewed vertical line
    for (i=0,y=0;y<pic2.ys;y++)
     if (pic2.p[y][x-(y>>1)].dd<200) i++;
    if (!i) w++; // increment gap width
    if ((i)||(x==pic2.xs-1))
        {
        if (w>=3)   // if gap bigger then treshold
            {       // draw blue gap lines
            for (i=x,x-=w;x<i;x++)
             for (y=0;y<pic1.ys;y++)
              pic1.p[y][x-(y>>1)].dd=0x000000FF;
            }
        w=0;
        continue;
        }
    // if gap found draw green line
    for (y=0;y<pic1.ys;y++)
     pic1.p[y][x-(y>>1)].dd=0x0000FF00;
    }

This is how the output looks like:

I used my own picture class for images so some members are:
xs,ys are size of image in pixels
p[y][x].dd is pixel at (x,y) position as 32 bit integer type
clear(color) clears entire image
resize(xs,ys) resizes image to new resolution

[notes]

This uses fixed skew angle for scanning to make this robust you need first find the skew angle and then scan along it instead.

The last gap should be also blue I forget to execute the if (w>=3)... if last x is processed regardless of i. The source is already updated but image is not.



来源:https://stackoverflow.com/questions/31082073/detecting-spacebar-between-words-in-a-slanted-font

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!