contours_in_same_horizon: simplify

- array instead of list operations
- return array of index pairs instead of list objects
This commit is contained in:
Robert Sachunsky 2025-10-24 01:51:59 +02:00
parent acee4c1bfe
commit 5d15941b35
2 changed files with 44 additions and 54 deletions

View file

@ -1315,47 +1315,42 @@ def combine_hor_lines_and_delete_cross_points_and_get_lines_features_back_new(
float(num_col_classifier)) float(num_col_classifier))
if len_lines_bigger_than_x_width_smaller_than_acolumn_width_per_column < 10: if len_lines_bigger_than_x_width_smaller_than_acolumn_width_per_column < 10:
args_hor=np.arange(len(slope_lines_hor)) args_hor=np.arange(len(slope_lines_hor))
all_args_uniq=contours_in_same_horizon(cy_main_hor) sep_pairs=contours_in_same_horizon(cy_main_hor)
#print(all_args_uniq,'all_args_uniq') if len(sep_pairs):
if len(all_args_uniq)>0: special_separators=[]
if type(all_args_uniq[0]) is list: contours_new=[]
special_separators=[] for pair in sep_pairs:
contours_new=[] merged_all=None
for dd in range(len(all_args_uniq)): some_args=args_hor[pair]
merged_all=None some_cy=cy_main_hor[pair]
some_args=args_hor[all_args_uniq[dd]] some_x_min=x_min_main_hor[pair]
some_cy=cy_main_hor[all_args_uniq[dd]] some_x_max=x_max_main_hor[pair]
some_x_min=x_min_main_hor[all_args_uniq[dd]]
some_x_max=x_max_main_hor[all_args_uniq[dd]]
#img_in=np.zeros(separators_closeup_n[:,:,2].shape) #img_in=np.zeros(separators_closeup_n[:,:,2].shape)
#print(img_p_in_ver.shape[1],some_x_max-some_x_min,'xdiff') #print(img_p_in_ver.shape[1],some_x_max-some_x_min,'xdiff')
diff_x_some=some_x_max-some_x_min diff_x_some=some_x_max-some_x_min
for jv in range(len(some_args)): for jv in range(len(some_args)):
img_p_in=cv2.fillPoly(img_in_hor, pts=[contours_lines_hor[some_args[jv]]], color=(1,1,1)) img_p_in=cv2.fillPoly(img_in_hor, pts=[contours_lines_hor[some_args[jv]]], color=(1,1,1))
if any(i_diff>(img_p_in_ver.shape[1]/float(3.3)) for i_diff in diff_x_some): if any(i_diff>(img_p_in_ver.shape[1]/float(3.3)) for i_diff in diff_x_some):
img_p_in[int(np.mean(some_cy))-5: img_p_in[int(np.mean(some_cy))-5:
int(np.mean(some_cy))+5, int(np.mean(some_cy))+5,
int(np.min(some_x_min)): int(np.min(some_x_min)):
int(np.max(some_x_max)) ]=1 int(np.max(some_x_max)) ]=1
sum_dis=dist_x_hor[some_args].sum() sum_dis=dist_x_hor[some_args].sum()
diff_max_min_uniques=np.max(x_max_main_hor[some_args])-np.min(x_min_main_hor[some_args]) diff_max_min_uniques=np.max(x_max_main_hor[some_args])-np.min(x_min_main_hor[some_args])
if (diff_max_min_uniques > sum_dis and if (diff_max_min_uniques > sum_dis and
sum_dis / float(diff_max_min_uniques) > 0.85 and sum_dis / float(diff_max_min_uniques) > 0.85 and
diff_max_min_uniques / float(img_p_in_ver.shape[1]) > 0.85 and diff_max_min_uniques / float(img_p_in_ver.shape[1]) > 0.85 and
np.std(dist_x_hor[some_args]) < 0.55 * np.mean(dist_x_hor[some_args])): np.std(dist_x_hor[some_args]) < 0.55 * np.mean(dist_x_hor[some_args])):
# print(dist_x_hor[some_args], # print(dist_x_hor[some_args],
# dist_x_hor[some_args].sum(), # dist_x_hor[some_args].sum(),
# np.min(x_min_main_hor[some_args]), # np.min(x_min_main_hor[some_args]),
# np.max(x_max_main_hor[some_args]),'jalibdi') # np.max(x_max_main_hor[some_args]),'jalibdi')
# print(np.mean( dist_x_hor[some_args] ), # print(np.mean( dist_x_hor[some_args] ),
# np.std( dist_x_hor[some_args] ), # np.std( dist_x_hor[some_args] ),
# np.var( dist_x_hor[some_args] ),'jalibdiha') # np.var( dist_x_hor[some_args] ),'jalibdiha')
special_separators.append(np.mean(cy_main_hor[some_args])) special_separators.append(np.mean(cy_main_hor[some_args]))
else:
img_p_in=img_in_hor
special_separators=[]
else: else:
img_p_in=img_in_hor img_p_in=img_in_hor
special_separators=[] special_separators=[]

View file

@ -14,21 +14,16 @@ from shapely.ops import unary_union, nearest_points
from .rotate import rotate_image, rotation_image_new from .rotate import rotate_image, rotation_image_new
def contours_in_same_horizon(cy_main_hor): def contours_in_same_horizon(cy_main_hor):
X1 = np.zeros((len(cy_main_hor), len(cy_main_hor))) """
X2 = np.zeros((len(cy_main_hor), len(cy_main_hor))) Takes an array of y coords, identifies all pairs among them
which are close to each other, and returns all such pairs
X1[0::1, :] = cy_main_hor[:] by index into the array.
X2 = X1.T """
sort = np.argsort(cy_main_hor)
X_dif = np.abs(X2 - X1) same = np.diff(cy_main_hor[sort] <= 20)
args_help = np.array(range(len(cy_main_hor))) # groups = np.split(sort, np.arange(len(cy_main_hor) - 1)[~same] + 1)
all_args = [] same = np.flatnonzero(same)
for i in range(len(cy_main_hor)): return np.stack((sort[:-1][same], sort[1:][same])).T
list_h = list(args_help[X_dif[i, :] <= 20])
list_h.append(i)
if len(list_h) > 1:
all_args.append(list(set(list_h)))
return np.unique(np.array(all_args, dtype=object))
def find_contours_mean_y_diff(contours_main): def find_contours_mean_y_diff(contours_main):
M_main = [cv2.moments(contours_main[j]) for j in range(len(contours_main))] M_main = [cv2.moments(contours_main[j]) for j in range(len(contours_main))]