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.
You can scan for empty space along skewed vertical lines
scan whole image
count font pixels per line
if no pixel counted then gap found (green and blue lines)
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.