以倾斜字体检测单词之间的空格(条形) [英] Detecting space(bar) between words in a slanted font

查看:332
本文介绍了以倾斜字体检测单词之间的空格(条形)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个python脚本来检测图像中编码的字母。该脚本使用 openCV的templateMatching 来匹配图像中嵌入的字符/字母。除了空格(空格键)字符外,检测工作正常。

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

是否有一些(简单/直接)方式来检测之间的空白在python中使用(或不使用)openCV的单词?

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

推荐答案

您可以沿倾斜的垂直线扫描空白区域


  1. 扫描整个图像

  2. count每行字体像素

  3. 如果没有计算像素,则找到间隙(绿线和蓝线)

  4. 计算加入的差距行数( w

  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)

如果更宽或等于阈值(在你的情况下为3)然后找到的差距是单词之间的差距(蓝线)

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

这我是如何用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;
    }

这是输出的样子:

I使用我自己的图片类图片,所以一些成员是:

xs,ys 是图像的大小,以像素为单位

p [y] [x] .dd 是(x,y)位置的像素为32位整数类型

clear(颜色) )清除整个图像

调整大小(xs,ys)将图像大小调整为新分辨率

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

[注释]

这使用固定的倾斜角度进行扫描,以确保您需要先找到倾斜角度而是沿着它扫描。

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

最后一个差距也应该是蓝色我忘记执行 if(w> = 3)... 如果最后 x 被处理,无论 i 。来源已经更新但图片不是。

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.

这篇关于以倾斜字体检测单词之间的空格(条形)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆