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,18 +1315,16 @@ def combine_hor_lines_and_delete_cross_points_and_get_lines_features_back_new(
float(num_col_classifier))
if len_lines_bigger_than_x_width_smaller_than_acolumn_width_per_column < 10:
args_hor=np.arange(len(slope_lines_hor))
all_args_uniq=contours_in_same_horizon(cy_main_hor)
#print(all_args_uniq,'all_args_uniq')
if len(all_args_uniq)>0:
if type(all_args_uniq[0]) is list:
sep_pairs=contours_in_same_horizon(cy_main_hor)
if len(sep_pairs):
special_separators=[]
contours_new=[]
for dd in range(len(all_args_uniq)):
for pair in sep_pairs:
merged_all=None
some_args=args_hor[all_args_uniq[dd]]
some_cy=cy_main_hor[all_args_uniq[dd]]
some_x_min=x_min_main_hor[all_args_uniq[dd]]
some_x_max=x_max_main_hor[all_args_uniq[dd]]
some_args=args_hor[pair]
some_cy=cy_main_hor[pair]
some_x_min=x_min_main_hor[pair]
some_x_max=x_max_main_hor[pair]
#img_in=np.zeros(separators_closeup_n[:,:,2].shape)
#print(img_p_in_ver.shape[1],some_x_max-some_x_min,'xdiff')
@ -1356,9 +1354,6 @@ def combine_hor_lines_and_delete_cross_points_and_get_lines_features_back_new(
else:
img_p_in=img_in_hor
special_separators=[]
else:
img_p_in=img_in_hor
special_separators=[]
img_p_in_ver[img_p_in_ver == 255] = 1
sep_ver_hor = img_p_in + img_p_in_ver

View file

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