Слияние сегментов слов, которые перекрываются или содержатся в других сегментах python
Я работаю над проектом, который включает сегментацию слов в изображениях, содержащих рукописный текст. Для этого я использую технику масштабного пространства для сегментации слов .
Одна из проблем - это перекрывающиеся сегменты, как показано на рисунке:
Я хочу объединить любые 2 перекрывающихся сегмента (сегмент, содержащийся в другом сегменте) в один (для всех таких сегментов в строке).
Ниже приведен код, который я пробовал:
def segment_cleaner(seg,dist = 20):
seg : segments as list of list of tuples in the form [[(x1,y1),(x1+w,y1+h)],[(x2,y2),(x2+w,y2+h)],...]
dist : represents the minimum distance between 2 segments, if the dist is less then merge the 2 segments
if len(seg) ==2:
return seg
else:
cleaned_seg = []
#arrange segments from left to right
sorteg_seg = sorted(seg, key=lambda x: x[0])
#loop through segments, if there is overlapp, one segment is contained inside other or two segements
#are less than the dist argument apart, combine them together
x_pointer = 0
for i in range(len(sorteg_seg)-1):
i_th_seg = sorteg_seg[i]
i_plus1th_seg = sorteg_seg[i+1]
#condition for containment
contained = i_th_seg[0][0] < i_plus1th_seg[0][0] and i_th_seg[0][1] < i_plus1th_seg[0][1] and i_th_seg[1][0] > i_plus1th_seg[1][0] and i_th_seg[1][1] > i_plus1th_seg[1][1]
if contained:
#ignore the smaller segement
print('remove contained rect.')
if i_th_seg[1][0]>x_pointer:
cleaned_seg.append(i_th_seg)
x_pointer = i_th_seg[1][0]
elif i_plus1th_seg[0][0] - i_th_seg[1][0] <= dist:
print('merge segements')
x_min = min(i_th_seg[0][0],i_plus1th_seg[0][0])
y_min = min(i_th_seg[0][1],i_plus1th_seg[0][1])
x_max = max(i_th_seg[1][0],i_plus1th_seg[1][0])
y_max = max(i_th_seg[1][1],i_plus1th_seg[1][1])
append_seg = [(x_min,y_min),(x_max,y_max)]
if x_max > x_pointer:
cleaned_seg.append(append_seg)
x_pointer = x_max
else:
if i_th_seg[1][0]>x_pointer:
cleaned_seg.append(i_th_seg)
if i_plus1th_seg[1][0]>x_pointer and i ==len(sorteg_seg)-2 :
cleaned_seg.append(i_plus1th_seg)
return cleaned_seg
Это не работает должным образом, поскольку сегменты динамически обновляются, когда мы просматриваем список сегментов. Цените любую помощь в этом.