# 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
据我所知,这个算法是用来分离阿拉伯字母的,这些字母在书写时是通过一条水平线连接起来的(我对阿拉伯字母一无所知)。在
所以该算法假设给定的图像是水平对齐的(否则它将无法工作),并寻找具有类似黑色像素上键的区域。在
在构建了图像的垂直轮廓之后,只需找到单词中最常见的高度(图像中第二高的高度)。而不是只需将图像在特定高度的区域和其他区域之间分离。在
使用您的图像:
红线是您需要找到的第二个最常见的高度(可以用柱状图来完成)。在
绿线表示单个字符之间的分隔(因此这里将得到4个字符)。在
顺便说一句,你的图像比文章中使用的图像噪音大得多,失真程度更高,所以你应该找到一些数值范围来离散化你的高度值(例如用直方图)。在
伪代码(或未确认的未测试代码):
相关问题 更多 >
编程相关推荐