Bounding Box Sorting Algorithm For Text Detection And Object Detection From Left To Right And Top To Bottom

vignesh amudha
3 min readMar 8, 2020

--

Step1:

Take the minimum value of the sum of xmin and ymin, So that we get the points nearest to the origin.

Step2:

Find the threshold value to eliminate other bbox which does not lies in between the initial bbox I1 of x and y coordinates.

Notes: don't add threshold value. Instead of we can change the constant value according to the problem.

Threshold value = (ymax- ymin) + C

C is a constant value, which we set to adjust the threshold value.

ymax is the initial bbox I1

ymin is the initial bbox I2

Step3:

Then, sort the bbox according to the xmin axis of the filtered bbox.

Step4:

Save the sorted bbox in a separate final array and delete the filtered bbox in the original bbox array.

Step5:

Repeat the step1 to step4 until we get all the bbox sorted from left to right and top to bottom.

Code:

def sorting_bounding_box(points):
points = list(map(lambda x:[x[0],x[1][0],x[1][2]],points))
# print(points)points_sum = list(map(lambda x: [x[0],x[1],sum(x[1]),x[2] [1]],points))x_y_cordinate = list(map(lambda x: x[1],points_sum))final_sorted_list = []while True:try:new_sorted_text = []initial_value_A = [i for i in
sorted(enumerate(points_sum), key=lambda x:x[1][2])][0]
# print(initial_value_A)threshold_value = abs(initial_value_A[1][1][1] - initial_value_A[1][3])threshold_value = (threshold_value/2) + 5del points_sum[initial_value_A[0]]del x_y_cordinate[initial_value_A[0]]# print(threshold_value)A = [initial_value_A[1][1]]K = list(map(lambda x:[x,abs(x[1]-initial_value_A[1][1][1])],x_y_cordinate))K = [[count,i]for count,i in enumerate(K)]K = [i for i in K if i[1][1] <= threshold_value]if K != []:
sorted_K = list(map(lambda x:[x[0],x[1][0]],sorted(K,key=lambda x:x[1][1])))
B = []points_index = []for tmp_K in sorted_K:points_index.append(tmp_K[0])B.append(tmp_K[1])dist = distance.cdist(A,B)[0]d_index = [i for i in sorted(zip(dist,points_index), key=lambda x:x[0])]new_sorted_text.append(initial_value_A[1][0])index = []for j in d_index:new_sorted_text.append(points_sum[j[1]][0])index.append(j[1])for n in sorted(index, reverse=True):del points_sum[n]del x_y_cordinate[n]final_sorted_list.append(new_sorted_text)
else:
final_sorted_list.append([])
# print(new_sorted_text)except Exception as e:print(e)breakreturn final_sorted_list

--

--

Responses (1)