【发布时间】:2016-12-12 19:51:04
【问题描述】:
我正在寻找带有数字和字符的图像的轮廓,用于 OCR。所以,我需要从左到右对轮廓进行排序,同时逐行排序,即从上到下。现在,轮廓不是这样排序的。
例如,上图的轮廓是随机排序的。
我需要的是排序为 D,o,y,o,u,k,n,o,w,s,o,m,e,o,n,e,r,.(dot),i (不带点),c,h...等等。我尝试了几种方法,我们首先观察 y 坐标,然后使用一些键和 x 坐标。就像现在一样,我有以下排序代码。它适用于前 2 行。然后在第三行,排序不会发生。主要问题似乎出在诸如 i、j、?、(点)、(逗号)等字母中,其中(点)的 y 轴发生了变化,尽管属于同一行。那么有什么好的解决方案呢?
for ctr in contours:
if cv2.contourArea(ctr) > maxArea * areaRatio:
rect.append(cv2.boundingRect(cv2.approxPolyDP(ctr,1,True)))
#rect contains the contours
for i in rect:
x = i[0]
y = i[1]
w = i[2]
h = i[3]
if(h>max_line_height):
max_line_height = h
mlh = max_line_height*2
max_line_width = raw_image.shape[1] #width of the input image
mlw = max_line_width
rect = np.asarray(rect)
s = rect.astype( np.uint32 ) #prevent overflows
order= mlw*(s[:,1]/mlh)+s[:,0]
sort_order= np.argsort( order )
rect = rect[ sort_order ]
【问题讨论】:
-
请提供一个清晰的示例,说明第 3 行中的问题。
-
图像第三行的轮廓被排序为stress.ed,hav..i,n,g。等等。这些点随机出现在其他字母的位置,导致其他字母落在正确的排序位置。
标签: python sorting opencv contour