垂直投影和水平投影

2024-09-28 21:19:06 发布

您现在位置:Python中文网/ 问答频道 /正文

在那篇论文中,我试图实现下面的ocr算法。在

https://arxiv.org/ftp/arxiv/papers/1707/1707.00800.pdf

我对那部分感到困惑:

enter image description here

enter image description here

我构建了一幅图像的垂直剖面图:

env = np.sum(img, axis=1)

enter image description here

这就是我得到的

enter image description here

我想找一个算法的清晰解释,也许用一个伪代码


Tags: httpsorg图像env算法imgpdfnp
1条回答
网友
1楼 · 发布于 2024-09-28 21:19:06

据我所知,这个算法是用来分离阿拉伯字母的,这些字母在书写时是通过一条水平线连接起来的(我对阿拉伯字母一无所知)。在

所以该算法假设给定的图像是水平对齐的(否则它将无法工作),并寻找具有类似黑色像素上键的区域。在

在构建了图像的垂直轮廓之后,只需找到单词中最常见的高度(图像中第二高的高度)。而不是只需将图像在特定高度的区域和其他区域之间分离。在

使用您的图像:

enter image description here

红线是您需要找到的第二个最常见的高度(可以用柱状图来完成)。在

绿线表示单个字符之间的分隔(因此这里将得到4个字符)。在

顺便说一句,你的图像比文章中使用的图像噪音大得多,失真程度更高,所以你应该找到一些数值范围来离散化你的高度值(例如用直方图)。在

伪代码(或未确认的未测试代码):

# Discretize the y values to n_bins (noisier image will mean you can use less bins):
height_hist = np.histogram(y, bins=n_bins)

# Find bin with the second largest number of values:
bin = np.argsort(height_hist[0])[-2]

# Get the limit values of the bin:
y_low, y_high = height_hist[1][bin], height_hist[1][bin+1]

# Go over the vertical projection values and separate to characters:

zero = y[0] # Assuming the first projected value is outside of the word
char_list = []
i = 0
inside_char = False
while i < len(y):
    if y[i] != zero:
        start = i # start of char

        # Find end of current char:
        for j in range(i, len(y)):
            if y_low<=y[i] and  y[i]<=y_high:
                end = j # end of char
                char_list.append([start, end]) # add to char list
                i = end

        # Find the start of the next char:
        for j in range(i, len(y)):
            if y_low>y[i] or  y[i]>y_high:
                i = j
    else:
        i += 1

相关问题 更多 >