mirror of
https://github.com/qurator-spk/eynollah.git
synced 2025-06-08 19:59:56 +02:00
simplify, wrap extremely long lines
This commit is contained in:
parent
cfc65128b1
commit
335aa273a1
4 changed files with 1792 additions and 1984 deletions
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -27,35 +27,33 @@ def find_contours_mean_y_diff(contours_main):
|
|||
cy_main = [(M_main[j]["m01"] / (M_main[j]["m00"] + 1e-32)) for j in range(len(M_main))]
|
||||
return np.mean(np.diff(np.sort(np.array(cy_main))))
|
||||
|
||||
|
||||
def get_text_region_boxes_by_given_contours(contours):
|
||||
kernel = np.ones((5, 5), np.uint8)
|
||||
boxes = []
|
||||
contours_new = []
|
||||
for jj in range(len(contours)):
|
||||
x, y, w, h = cv2.boundingRect(contours[jj])
|
||||
|
||||
boxes.append([x, y, w, h])
|
||||
box = cv2.boundingRect(contours[jj])
|
||||
boxes.append(box)
|
||||
contours_new.append(contours[jj])
|
||||
|
||||
return boxes, contours_new
|
||||
|
||||
def filter_contours_area_of_image(image, contours, hierarchy, max_area, min_area):
|
||||
found_polygons_early = list()
|
||||
|
||||
found_polygons_early = []
|
||||
for jv,c in enumerate(contours):
|
||||
if len(c) < 3: # A polygon cannot have less than 3 points
|
||||
continue
|
||||
|
||||
polygon = geometry.Polygon([point[0] for point in c])
|
||||
area = polygon.area
|
||||
if area >= min_area * np.prod(image.shape[:2]) and area <= max_area * np.prod(image.shape[:2]) and hierarchy[0][jv][3] == -1: # and hierarchy[0][jv][3]==-1 :
|
||||
found_polygons_early.append(np.array([[point] for point in polygon.exterior.coords], dtype=np.uint))
|
||||
if (area >= min_area * np.prod(image.shape[:2]) and
|
||||
area <= max_area * np.prod(image.shape[:2]) and
|
||||
hierarchy[0][jv][3] == -1):
|
||||
found_polygons_early.append(np.array([[point]
|
||||
for point in polygon.exterior.coords], dtype=np.uint))
|
||||
return found_polygons_early
|
||||
|
||||
def filter_contours_area_of_image_tables(image, contours, hierarchy, max_area, min_area):
|
||||
found_polygons_early = list()
|
||||
|
||||
found_polygons_early = []
|
||||
for jv,c in enumerate(contours):
|
||||
if len(c) < 3: # A polygon cannot have less than 3 points
|
||||
continue
|
||||
|
@ -66,48 +64,59 @@ def filter_contours_area_of_image_tables(image, contours, hierarchy, max_area, m
|
|||
##print(np.prod(thresh.shape[:2]))
|
||||
# Check that polygon has area greater than minimal area
|
||||
# print(hierarchy[0][jv][3],hierarchy )
|
||||
if area >= min_area * np.prod(image.shape[:2]) and area <= max_area * np.prod(image.shape[:2]): # and hierarchy[0][jv][3]==-1 :
|
||||
if (area >= min_area * np.prod(image.shape[:2]) and
|
||||
area <= max_area * np.prod(image.shape[:2]) and
|
||||
# hierarchy[0][jv][3]==-1
|
||||
True):
|
||||
# print(c[0][0][1])
|
||||
found_polygons_early.append(np.array([[point] for point in polygon.exterior.coords], dtype=np.int32))
|
||||
found_polygons_early.append(np.array([[point]
|
||||
for point in polygon.exterior.coords], dtype=np.int32))
|
||||
return found_polygons_early
|
||||
|
||||
def find_new_features_of_contours(contours_main):
|
||||
|
||||
areas_main = np.array([cv2.contourArea(contours_main[j]) for j in range(len(contours_main))])
|
||||
M_main = [cv2.moments(contours_main[j]) for j in range(len(contours_main))]
|
||||
cx_main = [(M_main[j]["m10"] / (M_main[j]["m00"] + 1e-32)) for j in range(len(M_main))]
|
||||
cy_main = [(M_main[j]["m01"] / (M_main[j]["m00"] + 1e-32)) for j in range(len(M_main))]
|
||||
areas_main = np.array([cv2.contourArea(contours_main[j])
|
||||
for j in range(len(contours_main))])
|
||||
M_main = [cv2.moments(contours_main[j])
|
||||
for j in range(len(contours_main))]
|
||||
cx_main = [(M_main[j]["m10"] / (M_main[j]["m00"] + 1e-32))
|
||||
for j in range(len(M_main))]
|
||||
cy_main = [(M_main[j]["m01"] / (M_main[j]["m00"] + 1e-32))
|
||||
for j in range(len(M_main))]
|
||||
try:
|
||||
x_min_main = np.array([np.min(contours_main[j][:, 0, 0]) for j in range(len(contours_main))])
|
||||
|
||||
argmin_x_main = np.array([np.argmin(contours_main[j][:, 0, 0]) for j in range(len(contours_main))])
|
||||
|
||||
x_min_from_argmin = np.array([contours_main[j][argmin_x_main[j], 0, 0] for j in range(len(contours_main))])
|
||||
y_corr_x_min_from_argmin = np.array([contours_main[j][argmin_x_main[j], 0, 1] for j in range(len(contours_main))])
|
||||
|
||||
x_max_main = np.array([np.max(contours_main[j][:, 0, 0]) for j in range(len(contours_main))])
|
||||
|
||||
y_min_main = np.array([np.min(contours_main[j][:, 0, 1]) for j in range(len(contours_main))])
|
||||
y_max_main = np.array([np.max(contours_main[j][:, 0, 1]) for j in range(len(contours_main))])
|
||||
x_min_main = np.array([np.min(contours_main[j][:, 0, 0])
|
||||
for j in range(len(contours_main))])
|
||||
argmin_x_main = np.array([np.argmin(contours_main[j][:, 0, 0])
|
||||
for j in range(len(contours_main))])
|
||||
x_min_from_argmin = np.array([contours_main[j][argmin_x_main[j], 0, 0]
|
||||
for j in range(len(contours_main))])
|
||||
y_corr_x_min_from_argmin = np.array([contours_main[j][argmin_x_main[j], 0, 1]
|
||||
for j in range(len(contours_main))])
|
||||
x_max_main = np.array([np.max(contours_main[j][:, 0, 0])
|
||||
for j in range(len(contours_main))])
|
||||
y_min_main = np.array([np.min(contours_main[j][:, 0, 1])
|
||||
for j in range(len(contours_main))])
|
||||
y_max_main = np.array([np.max(contours_main[j][:, 0, 1])
|
||||
for j in range(len(contours_main))])
|
||||
except:
|
||||
x_min_main = np.array([np.min(contours_main[j][:, 0]) for j in range(len(contours_main))])
|
||||
|
||||
argmin_x_main = np.array([np.argmin(contours_main[j][:, 0]) for j in range(len(contours_main))])
|
||||
|
||||
x_min_from_argmin = np.array([contours_main[j][argmin_x_main[j], 0] for j in range(len(contours_main))])
|
||||
y_corr_x_min_from_argmin = np.array([contours_main[j][argmin_x_main[j], 1] for j in range(len(contours_main))])
|
||||
|
||||
x_max_main = np.array([np.max(contours_main[j][:, 0]) for j in range(len(contours_main))])
|
||||
|
||||
y_min_main = np.array([np.min(contours_main[j][:, 1]) for j in range(len(contours_main))])
|
||||
y_max_main = np.array([np.max(contours_main[j][:, 1]) for j in range(len(contours_main))])
|
||||
|
||||
x_min_main = np.array([np.min(contours_main[j][:, 0])
|
||||
for j in range(len(contours_main))])
|
||||
argmin_x_main = np.array([np.argmin(contours_main[j][:, 0])
|
||||
for j in range(len(contours_main))])
|
||||
x_min_from_argmin = np.array([contours_main[j][argmin_x_main[j], 0]
|
||||
for j in range(len(contours_main))])
|
||||
y_corr_x_min_from_argmin = np.array([contours_main[j][argmin_x_main[j], 1]
|
||||
for j in range(len(contours_main))])
|
||||
x_max_main = np.array([np.max(contours_main[j][:, 0])
|
||||
for j in range(len(contours_main))])
|
||||
y_min_main = np.array([np.min(contours_main[j][:, 1])
|
||||
for j in range(len(contours_main))])
|
||||
y_max_main = np.array([np.max(contours_main[j][:, 1])
|
||||
for j in range(len(contours_main))])
|
||||
# dis_x=np.abs(x_max_main-x_min_main)
|
||||
|
||||
return cx_main, cy_main, x_min_main, x_max_main, y_min_main, y_max_main, y_corr_x_min_from_argmin
|
||||
|
||||
def find_features_of_contours(contours_main):
|
||||
|
||||
|
||||
areas_main=np.array([cv2.contourArea(contours_main[j]) for j in range(len(contours_main))])
|
||||
M_main=[cv2.moments(contours_main[j]) for j in range(len(contours_main))]
|
||||
cx_main=[(M_main[j]['m10']/(M_main[j]['m00']+1e-32)) for j in range(len(M_main))]
|
||||
|
@ -118,14 +127,15 @@ def find_features_of_contours(contours_main):
|
|||
y_min_main=np.array([np.min(contours_main[j][:,0,1]) for j in range(len(contours_main))])
|
||||
y_max_main=np.array([np.max(contours_main[j][:,0,1]) for j in range(len(contours_main))])
|
||||
|
||||
|
||||
return y_min_main, y_max_main
|
||||
|
||||
def return_parent_contours(contours, hierarchy):
|
||||
contours_parent = [contours[i] for i in range(len(contours)) if hierarchy[0][i][3] == -1]
|
||||
contours_parent = [contours[i]
|
||||
for i in range(len(contours))
|
||||
if hierarchy[0][i][3] == -1]
|
||||
return contours_parent
|
||||
|
||||
def return_contours_of_interested_region(region_pre_p, pixel, min_area=0.0002):
|
||||
|
||||
# pixels of images are identified by 5
|
||||
if len(region_pre_p.shape) == 3:
|
||||
cnts_images = (region_pre_p[:, :, 0] == pixel) * 1
|
||||
|
@ -137,10 +147,9 @@ def return_contours_of_interested_region(region_pre_p, pixel, min_area=0.0002):
|
|||
ret, thresh = cv2.threshold(imgray, 0, 255, 0)
|
||||
|
||||
contours_imgs, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||
|
||||
contours_imgs = return_parent_contours(contours_imgs, hierarchy)
|
||||
contours_imgs = filter_contours_area_of_image_tables(thresh, contours_imgs, hierarchy, max_area=1, min_area=min_area)
|
||||
|
||||
contours_imgs = filter_contours_area_of_image_tables(thresh, contours_imgs, hierarchy,
|
||||
max_area=1, min_area=min_area)
|
||||
return contours_imgs
|
||||
|
||||
def do_work_of_contours_in_image(contour, index_r_con, img, slope_first):
|
||||
|
@ -148,7 +157,6 @@ def do_work_of_contours_in_image(contour, index_r_con, img, slope_first):
|
|||
img_copy = cv2.fillPoly(img_copy, pts=[contour], color=(1, 1, 1))
|
||||
|
||||
img_copy = rotation_image_new(img_copy, -slope_first)
|
||||
|
||||
img_copy = img_copy.astype(np.uint8)
|
||||
imgray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
|
||||
ret, thresh = cv2.threshold(imgray, 0, 255, 0)
|
||||
|
@ -158,7 +166,6 @@ def do_work_of_contours_in_image(contour, index_r_con, img, slope_first):
|
|||
cont_int[0][:, 0, 0] = cont_int[0][:, 0, 0] + np.abs(img_copy.shape[1] - img.shape[1])
|
||||
cont_int[0][:, 0, 1] = cont_int[0][:, 0, 1] + np.abs(img_copy.shape[0] - img.shape[0])
|
||||
|
||||
|
||||
return cont_int[0], index_r_con
|
||||
|
||||
def get_textregion_contours_in_org_image_multi(cnts, img, slope_first, map=map):
|
||||
|
@ -172,7 +179,6 @@ def get_textregion_contours_in_org_image_multi(cnts, img, slope_first, map=map):
|
|||
return tuple(zip(*results))
|
||||
|
||||
def get_textregion_contours_in_org_image(cnts, img, slope_first):
|
||||
|
||||
cnts_org = []
|
||||
# print(cnts,'cnts')
|
||||
for i in range(len(cnts)):
|
||||
|
@ -193,7 +199,6 @@ def get_textregion_contours_in_org_image(cnts, img, slope_first):
|
|||
ret, thresh = cv2.threshold(imgray, 0, 255, 0)
|
||||
|
||||
cont_int, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||
|
||||
cont_int[0][:, 0, 0] = cont_int[0][:, 0, 0] + np.abs(img_copy.shape[1] - img.shape[1])
|
||||
cont_int[0][:, 0, 1] = cont_int[0][:, 0, 1] + np.abs(img_copy.shape[0] - img.shape[0])
|
||||
# print(np.shape(cont_int[0]))
|
||||
|
@ -202,32 +207,23 @@ def get_textregion_contours_in_org_image(cnts, img, slope_first):
|
|||
return cnts_org
|
||||
|
||||
def get_textregion_contours_in_org_image_light_old(cnts, img, slope_first):
|
||||
|
||||
h_o = img.shape[0]
|
||||
w_o = img.shape[1]
|
||||
|
||||
img = cv2.resize(img, (int(img.shape[1]/3.), int(img.shape[0]/3.)), interpolation=cv2.INTER_NEAREST)
|
||||
##cnts = list( (np.array(cnts)/2).astype(np.int16) )
|
||||
#cnts = cnts/2
|
||||
cnts = [(i/ 3).astype(np.int32) for i in cnts]
|
||||
zoom = 3
|
||||
img = cv2.resize(img, (img.shape[1] // zoom,
|
||||
img.shape[0] // zoom),
|
||||
interpolation=cv2.INTER_NEAREST)
|
||||
cnts_org = []
|
||||
#print(cnts,'cnts')
|
||||
for i in range(len(cnts)):
|
||||
for cnt in cnts:
|
||||
img_copy = np.zeros(img.shape)
|
||||
img_copy = cv2.fillPoly(img_copy, pts=[cnts[i]], color=(1, 1, 1))
|
||||
img_copy = cv2.fillPoly(img_copy, pts=[(cnt / zoom).astype(int)], color=(1, 1, 1))
|
||||
|
||||
img_copy = rotation_image_new(img_copy, -slope_first)
|
||||
|
||||
img_copy = img_copy.astype(np.uint8)
|
||||
img_copy = rotation_image_new(img_copy, -slope_first).astype(np.uint8)
|
||||
imgray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
|
||||
ret, thresh = cv2.threshold(imgray, 0, 255, 0)
|
||||
|
||||
cont_int, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||
|
||||
cont_int[0][:, 0, 0] = cont_int[0][:, 0, 0] + np.abs(img_copy.shape[1] - img.shape[1])
|
||||
cont_int[0][:, 0, 1] = cont_int[0][:, 0, 1] + np.abs(img_copy.shape[0] - img.shape[0])
|
||||
# print(np.shape(cont_int[0]))
|
||||
cnts_org.append(cont_int[0]*3)
|
||||
cnts_org.append(cont_int[0] * zoom)
|
||||
|
||||
return cnts_org
|
||||
|
||||
|
@ -235,14 +231,11 @@ def do_back_rotation_and_get_cnt_back(contour_par, index_r_con, img, slope_first
|
|||
img_copy = np.zeros(img.shape)
|
||||
img_copy = cv2.fillPoly(img_copy, pts=[contour_par], color=(1, 1, 1))
|
||||
|
||||
img_copy = rotation_image_new(img_copy, -slope_first)
|
||||
|
||||
img_copy = img_copy.astype(np.uint8)
|
||||
img_copy = rotation_image_new(img_copy, -slope_first).astype(np.uint8)
|
||||
imgray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
|
||||
ret, thresh = cv2.threshold(imgray, 0, 255, 0)
|
||||
|
||||
cont_int, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||
|
||||
cont_int[0][:, 0, 0] = cont_int[0][:, 0, 0] + np.abs(img_copy.shape[1] - img.shape[1])
|
||||
cont_int[0][:, 0, 1] = cont_int[0][:, 0, 1] + np.abs(img_copy.shape[0] - img.shape[0])
|
||||
# print(np.shape(cont_int[0]))
|
||||
|
@ -264,7 +257,6 @@ def get_textregion_contours_in_org_image_light(cnts, img, slope_first, map=map):
|
|||
return [i*6 for i in contours]
|
||||
|
||||
def return_contours_of_interested_textline(region_pre_p, pixel):
|
||||
|
||||
# pixels of images are identified by 5
|
||||
if len(region_pre_p.shape) == 3:
|
||||
cnts_images = (region_pre_p[:, :, 0] == pixel) * 1
|
||||
|
@ -277,11 +269,11 @@ def return_contours_of_interested_textline(region_pre_p, pixel):
|
|||
contours_imgs, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||
|
||||
contours_imgs = return_parent_contours(contours_imgs, hierarchy)
|
||||
contours_imgs = filter_contours_area_of_image_tables(thresh, contours_imgs, hierarchy, max_area=1, min_area=0.000000003)
|
||||
contours_imgs = filter_contours_area_of_image_tables(
|
||||
thresh, contours_imgs, hierarchy, max_area=1, min_area=0.000000003)
|
||||
return contours_imgs
|
||||
|
||||
def return_contours_of_image(image):
|
||||
|
||||
if len(image.shape) == 2:
|
||||
image = np.repeat(image[:, :, np.newaxis], 3, axis=2)
|
||||
image = image.astype(np.uint8)
|
||||
|
@ -293,7 +285,6 @@ def return_contours_of_image(image):
|
|||
return contours, hierarchy
|
||||
|
||||
def return_contours_of_interested_region_by_min_size(region_pre_p, pixel, min_size=0.00003):
|
||||
|
||||
# pixels of images are identified by 5
|
||||
if len(region_pre_p.shape) == 3:
|
||||
cnts_images = (region_pre_p[:, :, 0] == pixel) * 1
|
||||
|
@ -305,14 +296,13 @@ def return_contours_of_interested_region_by_min_size(region_pre_p, pixel, min_si
|
|||
ret, thresh = cv2.threshold(imgray, 0, 255, 0)
|
||||
|
||||
contours_imgs, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||
|
||||
contours_imgs = return_parent_contours(contours_imgs, hierarchy)
|
||||
contours_imgs = filter_contours_area_of_image_tables(thresh, contours_imgs, hierarchy, max_area=1, min_area=min_size)
|
||||
contours_imgs = filter_contours_area_of_image_tables(
|
||||
thresh, contours_imgs, hierarchy, max_area=1, min_area=min_size)
|
||||
|
||||
return contours_imgs
|
||||
|
||||
def return_contours_of_interested_region_by_size(region_pre_p, pixel, min_area, max_area):
|
||||
|
||||
# pixels of images are identified by 5
|
||||
if len(region_pre_p.shape) == 3:
|
||||
cnts_images = (region_pre_p[:, :, 0] == pixel) * 1
|
||||
|
@ -325,9 +315,11 @@ def return_contours_of_interested_region_by_size(region_pre_p, pixel, min_area,
|
|||
contours_imgs, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||
|
||||
contours_imgs = return_parent_contours(contours_imgs, hierarchy)
|
||||
contours_imgs = filter_contours_area_of_image_tables(thresh, contours_imgs, hierarchy, max_area=max_area, min_area=min_area)
|
||||
contours_imgs = filter_contours_area_of_image_tables(
|
||||
thresh, contours_imgs, hierarchy, max_area=max_area, min_area=min_area)
|
||||
|
||||
img_ret = np.zeros((region_pre_p.shape[0], region_pre_p.shape[1], 3))
|
||||
img_ret = cv2.fillPoly(img_ret, pts=contours_imgs, color=(1, 1, 1))
|
||||
|
||||
return img_ret[:, :, 0]
|
||||
|
||||
|
|
|
@ -41,9 +41,7 @@ def dedup_separate_lines(img_patch, contour_text_interest, thetha, axis):
|
|||
y_max_cont = img_patch.shape[0]
|
||||
|
||||
xv = np.linspace(x_min_cont, x_max_cont, 1000)
|
||||
|
||||
textline_patch_sum_along_width = img_patch.sum(axis=axis)
|
||||
|
||||
first_nonzero = 0 # (next((i for i, x in enumerate(mada_n) if x), None))
|
||||
|
||||
y = textline_patch_sum_along_width[:] # [first_nonzero:last_nonzero]
|
||||
|
@ -52,11 +50,8 @@ def dedup_separate_lines(img_patch, contour_text_interest, thetha, axis):
|
|||
x = np.array(range(len(y)))
|
||||
|
||||
peaks_real, _ = find_peaks(gaussian_filter1d(y, 3), height=0)
|
||||
|
||||
if 1 > 0:
|
||||
|
||||
try:
|
||||
|
||||
y_padded_smoothed_e = gaussian_filter1d(y_padded, 2)
|
||||
y_padded_up_to_down_e = -y_padded + np.max(y_padded)
|
||||
y_padded_up_to_down_padded_e = np.zeros(len(y_padded_up_to_down_e) + 40)
|
||||
|
@ -67,7 +62,7 @@ def dedup_separate_lines(img_patch, contour_text_interest, thetha, axis):
|
|||
peaks_neg_e, _ = find_peaks(y_padded_up_to_down_padded_e, height=0)
|
||||
neg_peaks_max = np.max(y_padded_up_to_down_padded_e[peaks_neg_e])
|
||||
|
||||
arg_neg_must_be_deleted = np.array(range(len(peaks_neg_e)))[y_padded_up_to_down_padded_e[peaks_neg_e] / float(neg_peaks_max) < 0.3]
|
||||
arg_neg_must_be_deleted = np.arange(len(peaks_neg_e))[y_padded_up_to_down_padded_e[peaks_neg_e] / float(neg_peaks_max) < 0.3]
|
||||
diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)
|
||||
|
||||
arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
|
||||
|
@ -78,12 +73,11 @@ def dedup_separate_lines(img_patch, contour_text_interest, thetha, axis):
|
|||
|
||||
clusters_to_be_deleted = []
|
||||
if len(arg_diff_cluster) > 0:
|
||||
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0 : arg_diff_cluster[0] + 1])
|
||||
for i in range(len(arg_diff_cluster) - 1):
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1 : arg_diff_cluster[i + 1] + 1])
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1 :
|
||||
arg_diff_cluster[i + 1] + 1])
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1 :])
|
||||
|
||||
if len(clusters_to_be_deleted) > 0:
|
||||
peaks_new_extra = []
|
||||
for m in range(len(clusters_to_be_deleted)):
|
||||
|
@ -93,7 +87,6 @@ def dedup_separate_lines(img_patch, contour_text_interest, thetha, axis):
|
|||
for m1 in range(len(clusters_to_be_deleted[m])):
|
||||
peaks_new = peaks_new[peaks_new != peaks_e[clusters_to_be_deleted[m][m1] - 1]]
|
||||
peaks_new = peaks_new[peaks_new != peaks_e[clusters_to_be_deleted[m][m1]]]
|
||||
|
||||
peaks_neg_new = peaks_neg_new[peaks_neg_new != peaks_neg_e[clusters_to_be_deleted[m][m1]]]
|
||||
peaks_new_tot = []
|
||||
for i1 in peaks_new:
|
||||
|
@ -106,9 +99,10 @@ def dedup_separate_lines(img_patch, contour_text_interest, thetha, axis):
|
|||
peaks_new_tot = peaks_e[:]
|
||||
|
||||
textline_con, hierarchy = return_contours_of_image(img_patch)
|
||||
textline_con_fil = filter_contours_area_of_image(img_patch, textline_con, hierarchy, max_area=1, min_area=0.0008)
|
||||
textline_con_fil = filter_contours_area_of_image(img_patch,
|
||||
textline_con, hierarchy,
|
||||
max_area=1, min_area=0.0008)
|
||||
y_diff_mean = np.mean(np.diff(peaks_new_tot)) # self.find_contours_mean_y_diff(textline_con_fil)
|
||||
|
||||
sigma_gaus = int(y_diff_mean * (7.0 / 40.0))
|
||||
# print(sigma_gaus,'sigma_gaus')
|
||||
except:
|
||||
|
@ -126,10 +120,18 @@ def dedup_separate_lines(img_patch, contour_text_interest, thetha, axis):
|
|||
peaks, _ = find_peaks(y_padded_smoothed, height=0)
|
||||
peaks_neg, _ = find_peaks(y_padded_up_to_down_padded, height=0)
|
||||
|
||||
return x, y, x_d, y_d, xv, x_min_cont, y_min_cont, x_max_cont, y_max_cont, first_nonzero, y_padded_up_to_down_padded, y_padded_smoothed, peaks, peaks_neg, rotation_matrix
|
||||
return (x, y,
|
||||
x_d, y_d,
|
||||
xv,
|
||||
x_min_cont, y_min_cont,
|
||||
x_max_cont, y_max_cont,
|
||||
first_nonzero,
|
||||
y_padded_up_to_down_padded,
|
||||
y_padded_smoothed,
|
||||
peaks, peaks_neg,
|
||||
rotation_matrix)
|
||||
|
||||
def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
||||
|
||||
(h, w) = img_patch.shape[:2]
|
||||
center = (w // 2, h // 2)
|
||||
M = cv2.getRotationMatrix2D(center, -thetha, 1.0)
|
||||
|
@ -151,9 +153,7 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
y_max_cont = img_patch.shape[0]
|
||||
|
||||
xv = np.linspace(x_min_cont, x_max_cont, 1000)
|
||||
|
||||
textline_patch_sum_along_width = img_patch.sum(axis=1)
|
||||
|
||||
first_nonzero = 0 # (next((i for i, x in enumerate(mada_n) if x), None))
|
||||
|
||||
y = textline_patch_sum_along_width[:] # [first_nonzero:last_nonzero]
|
||||
|
@ -162,11 +162,8 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
x = np.array(range(len(y)))
|
||||
|
||||
peaks_real, _ = find_peaks(gaussian_filter1d(y, 3), height=0)
|
||||
|
||||
if 1>0:
|
||||
|
||||
try:
|
||||
|
||||
y_padded_smoothed_e= gaussian_filter1d(y_padded, 2)
|
||||
y_padded_up_to_down_e=-y_padded+np.max(y_padded)
|
||||
y_padded_up_to_down_padded_e=np.zeros(len(y_padded_up_to_down_e)+40)
|
||||
|
@ -178,27 +175,22 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
peaks_neg_e, _ = find_peaks(y_padded_up_to_down_padded_e, height=0)
|
||||
neg_peaks_max=np.max(y_padded_up_to_down_padded_e[peaks_neg_e])
|
||||
|
||||
arg_neg_must_be_deleted= np.array(range(len(peaks_neg_e)))[y_padded_up_to_down_padded_e[peaks_neg_e]/float(neg_peaks_max)<0.3 ]
|
||||
arg_neg_must_be_deleted= np.arange(len(peaks_neg_e))[y_padded_up_to_down_padded_e[peaks_neg_e]/float(neg_peaks_max)<0.3]
|
||||
diff_arg_neg_must_be_deleted=np.diff(arg_neg_must_be_deleted)
|
||||
|
||||
|
||||
|
||||
arg_diff=np.array(range(len(diff_arg_neg_must_be_deleted)))
|
||||
arg_diff_cluster=arg_diff[diff_arg_neg_must_be_deleted>1]
|
||||
|
||||
|
||||
peaks_new=peaks_e[:]
|
||||
peaks_neg_new=peaks_neg_e[:]
|
||||
|
||||
clusters_to_be_deleted=[]
|
||||
if len(arg_diff_cluster)>0:
|
||||
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0:arg_diff_cluster[0]+1])
|
||||
for i in range(len(arg_diff_cluster)-1):
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i]+1:arg_diff_cluster[i+1]+1])
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i]+1:
|
||||
arg_diff_cluster[i+1]+1])
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster)-1]+1:])
|
||||
|
||||
|
||||
if len(clusters_to_be_deleted)>0:
|
||||
peaks_new_extra=[]
|
||||
for m in range(len(clusters_to_be_deleted)):
|
||||
|
@ -208,7 +200,6 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
for m1 in range(len(clusters_to_be_deleted[m])):
|
||||
peaks_new=peaks_new[peaks_new!=peaks_e[clusters_to_be_deleted[m][m1]-1]]
|
||||
peaks_new=peaks_new[peaks_new!=peaks_e[clusters_to_be_deleted[m][m1]]]
|
||||
|
||||
peaks_neg_new=peaks_neg_new[peaks_neg_new!=peaks_neg_e[clusters_to_be_deleted[m][m1]]]
|
||||
peaks_new_tot=[]
|
||||
for i1 in peaks_new:
|
||||
|
@ -216,16 +207,14 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
for i1 in peaks_new_extra:
|
||||
peaks_new_tot.append(i1)
|
||||
peaks_new_tot=np.sort(peaks_new_tot)
|
||||
|
||||
|
||||
else:
|
||||
peaks_new_tot=peaks_e[:]
|
||||
|
||||
|
||||
textline_con,hierarchy=return_contours_of_image(img_patch)
|
||||
textline_con_fil=filter_contours_area_of_image(img_patch,textline_con,hierarchy,max_area=1,min_area=0.0008)
|
||||
textline_con_fil=filter_contours_area_of_image(img_patch,
|
||||
textline_con, hierarchy,
|
||||
max_area=1, min_area=0.0008)
|
||||
y_diff_mean=np.mean(np.diff(peaks_new_tot))#self.find_contours_mean_y_diff(textline_con_fil)
|
||||
|
||||
sigma_gaus=int( y_diff_mean * (7./40.0) )
|
||||
#print(sigma_gaus,'sigma_gaus')
|
||||
except:
|
||||
|
@ -234,60 +223,41 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
sigma_gaus=3
|
||||
#print(sigma_gaus,'sigma')
|
||||
|
||||
|
||||
y_padded_smoothed= gaussian_filter1d(y_padded, sigma_gaus)
|
||||
y_padded_up_to_down=-y_padded+np.max(y_padded)
|
||||
y_padded_up_to_down_padded=np.zeros(len(y_padded_up_to_down)+40)
|
||||
y_padded_up_to_down_padded[20:len(y_padded_up_to_down)+20]=y_padded_up_to_down
|
||||
y_padded_up_to_down_padded= gaussian_filter1d(y_padded_up_to_down_padded, sigma_gaus)
|
||||
|
||||
|
||||
peaks, _ = find_peaks(y_padded_smoothed, height=0)
|
||||
peaks_neg, _ = find_peaks(y_padded_up_to_down_padded, height=0)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try:
|
||||
neg_peaks_max=np.max(y_padded_smoothed[peaks])
|
||||
|
||||
|
||||
arg_neg_must_be_deleted= np.array(range(len(peaks_neg)))[y_padded_up_to_down_padded[peaks_neg]/float(neg_peaks_max)<0.42 ]
|
||||
|
||||
|
||||
arg_neg_must_be_deleted= np.arange(len(peaks_neg))[y_padded_up_to_down_padded[peaks_neg]/float(neg_peaks_max)<0.42]
|
||||
diff_arg_neg_must_be_deleted=np.diff(arg_neg_must_be_deleted)
|
||||
|
||||
|
||||
|
||||
arg_diff=np.array(range(len(diff_arg_neg_must_be_deleted)))
|
||||
arg_diff_cluster=arg_diff[diff_arg_neg_must_be_deleted>1]
|
||||
except:
|
||||
arg_neg_must_be_deleted=[]
|
||||
arg_diff_cluster=[]
|
||||
|
||||
|
||||
try:
|
||||
peaks_new=peaks[:]
|
||||
peaks_neg_new=peaks_neg[:]
|
||||
clusters_to_be_deleted=[]
|
||||
|
||||
|
||||
if len(arg_diff_cluster)>=2 and len(arg_diff_cluster)>0:
|
||||
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0:arg_diff_cluster[0]+1])
|
||||
for i in range(len(arg_diff_cluster)-1):
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i]+1:arg_diff_cluster[i+1]+1])
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i]+1:
|
||||
arg_diff_cluster[i+1]+1])
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster)-1]+1:])
|
||||
elif len(arg_neg_must_be_deleted)>=2 and len(arg_diff_cluster)==0:
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[:])
|
||||
|
||||
|
||||
|
||||
if len(arg_neg_must_be_deleted)==1:
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted)
|
||||
|
||||
|
||||
if len(clusters_to_be_deleted)>0:
|
||||
peaks_new_extra=[]
|
||||
for m in range(len(clusters_to_be_deleted)):
|
||||
|
@ -297,7 +267,6 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
for m1 in range(len(clusters_to_be_deleted[m])):
|
||||
peaks_new=peaks_new[peaks_new!=peaks[clusters_to_be_deleted[m][m1]-1]]
|
||||
peaks_new=peaks_new[peaks_new!=peaks[clusters_to_be_deleted[m][m1]]]
|
||||
|
||||
peaks_neg_new=peaks_neg_new[peaks_neg_new!=peaks_neg[clusters_to_be_deleted[m][m1]]]
|
||||
peaks_new_tot=[]
|
||||
for i1 in peaks_new:
|
||||
|
@ -321,11 +290,8 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
##plt.plot(y_padded_smoothed)
|
||||
##plt.plot(peaks_new_tot,y_padded_smoothed[peaks_new_tot],'*')
|
||||
##plt.show()
|
||||
|
||||
peaks=peaks_new_tot[:]
|
||||
peaks_neg=peaks_neg_new[:]
|
||||
|
||||
|
||||
else:
|
||||
peaks_new_tot=peaks[:]
|
||||
peaks=peaks_new_tot[:]
|
||||
|
@ -333,25 +299,19 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
except:
|
||||
pass
|
||||
|
||||
|
||||
mean_value_of_peaks=np.mean(y_padded_smoothed[peaks])
|
||||
std_value_of_peaks=np.std(y_padded_smoothed[peaks])
|
||||
peaks_values=y_padded_smoothed[peaks]
|
||||
|
||||
|
||||
peaks_neg = peaks_neg - 20 - 20
|
||||
peaks = peaks - 20
|
||||
|
||||
for jj in range(len(peaks_neg)):
|
||||
if peaks_neg[jj] > len(x) - 1:
|
||||
peaks_neg[jj] = len(x) - 1
|
||||
|
||||
for jj in range(len(peaks)):
|
||||
if peaks[jj] > len(x) - 1:
|
||||
peaks[jj] = len(x) - 1
|
||||
|
||||
|
||||
|
||||
textline_boxes = []
|
||||
textline_boxes_rot = []
|
||||
|
||||
|
@ -386,7 +346,6 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
1.1 * dis_to_next_down) ###-int(dis_to_next_down*1./2)
|
||||
|
||||
|
||||
|
||||
if point_down_narrow >= img_patch.shape[0]:
|
||||
point_down_narrow = img_patch.shape[0] - 2
|
||||
|
||||
|
@ -423,8 +382,6 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
if point_up_rot2<0:
|
||||
point_up_rot2=0
|
||||
|
||||
|
||||
|
||||
x_min_rot1=x_min_rot1-x_help
|
||||
x_max_rot2=x_max_rot2-x_help
|
||||
x_max_rot3=x_max_rot3-x_help
|
||||
|
@ -435,29 +392,24 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
point_down_rot3=point_down_rot3-y_help
|
||||
point_down_rot4=point_down_rot4-y_help
|
||||
|
||||
|
||||
|
||||
|
||||
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)],
|
||||
[int(x_max_rot2), int(point_up_rot2)],
|
||||
[int(x_max_rot3), int(point_down_rot3)],
|
||||
[int(x_min_rot4), int(point_down_rot4)]]))
|
||||
|
||||
textline_boxes.append(np.array([[int(x_min), int(point_up)],
|
||||
[int(x_max), int(point_up)],
|
||||
[int(x_max), int(point_down)],
|
||||
[int(x_min), int(point_down)]]))
|
||||
|
||||
elif len(peaks) < 1:
|
||||
pass
|
||||
|
||||
elif len(peaks) == 1:
|
||||
distances = [cv2.pointPolygonTest(contour_text_interest_copy, tuple(int(x) for x in np.array([xv[mj], peaks[0] + first_nonzero])), True)
|
||||
distances = [cv2.pointPolygonTest(contour_text_interest_copy,
|
||||
tuple(int(x) for x in np.array([xv[mj], peaks[0] + first_nonzero])), True)
|
||||
for mj in range(len(xv))]
|
||||
distances = np.array(distances)
|
||||
|
||||
xvinside = xv[distances >= 0]
|
||||
|
||||
if len(xvinside) == 0:
|
||||
x_min = x_min_cont
|
||||
x_max = x_max_cont
|
||||
|
@ -480,7 +432,6 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
|
||||
x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d
|
||||
|
||||
|
||||
if x_min_rot1<0:
|
||||
x_min_rot1=0
|
||||
if x_min_rot4<0:
|
||||
|
@ -490,7 +441,6 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
if point_up_rot2<0:
|
||||
point_up_rot2=0
|
||||
|
||||
|
||||
x_min_rot1=x_min_rot1-x_help
|
||||
x_max_rot2=x_max_rot2-x_help
|
||||
x_max_rot3=x_max_rot3-x_help
|
||||
|
@ -501,21 +451,14 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
point_down_rot3=point_down_rot3-y_help
|
||||
point_down_rot4=point_down_rot4-y_help
|
||||
|
||||
|
||||
|
||||
|
||||
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)],
|
||||
[int(x_max_rot2), int(point_up_rot2)],
|
||||
[int(x_max_rot3), int(point_down_rot3)],
|
||||
[int(x_min_rot4), int(point_down_rot4)]]))
|
||||
|
||||
textline_boxes.append(np.array([[int(x_min), int(y_min)],
|
||||
[int(x_max), int(y_min)],
|
||||
[int(x_max), int(y_max)],
|
||||
[int(x_min), int(y_max)]]))
|
||||
|
||||
|
||||
|
||||
elif len(peaks) == 2:
|
||||
dis_to_next = np.abs(peaks[1] - peaks[0])
|
||||
for jj in range(len(peaks)):
|
||||
|
@ -533,12 +476,12 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
except:
|
||||
point_up =peaks[jj] + first_nonzero - int(1. / 1.8 * dis_to_next)
|
||||
|
||||
distances = [cv2.pointPolygonTest(contour_text_interest_copy, tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])), True)
|
||||
distances = [cv2.pointPolygonTest(contour_text_interest_copy,
|
||||
tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])), True)
|
||||
for mj in range(len(xv))]
|
||||
distances = np.array(distances)
|
||||
|
||||
xvinside = xv[distances >= 0]
|
||||
|
||||
if len(xvinside) == 0:
|
||||
x_min = x_min_cont
|
||||
x_max = x_max_cont
|
||||
|
@ -556,8 +499,6 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
|
||||
x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d
|
||||
|
||||
|
||||
|
||||
if x_min_rot1<0:
|
||||
x_min_rot1=0
|
||||
if x_min_rot4<0:
|
||||
|
@ -577,21 +518,16 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
point_down_rot3=point_down_rot3-y_help
|
||||
point_down_rot4=point_down_rot4-y_help
|
||||
|
||||
|
||||
|
||||
|
||||
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)],
|
||||
[int(x_max_rot2), int(point_up_rot2)],
|
||||
[int(x_max_rot3), int(point_down_rot3)],
|
||||
[int(x_min_rot4), int(point_down_rot4)]]))
|
||||
|
||||
textline_boxes.append(np.array([[int(x_min), int(point_up)],
|
||||
[int(x_max), int(point_up)],
|
||||
[int(x_max), int(point_down)],
|
||||
[int(x_min), int(point_down)]]))
|
||||
else:
|
||||
for jj in range(len(peaks)):
|
||||
|
||||
if jj == 0:
|
||||
dis_to_next = peaks[jj + 1] - peaks[jj]
|
||||
# point_up=peaks[jj]+first_nonzero-int(1./3*dis_to_next)
|
||||
|
@ -615,12 +551,12 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
point_up = peaks[jj] + first_nonzero - int(1. / 1.9 * dis_to_next_up)
|
||||
point_down = peaks[jj] + first_nonzero + int(1. / 1.9 * dis_to_next_down)
|
||||
|
||||
distances = [cv2.pointPolygonTest(contour_text_interest_copy, tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])), True)
|
||||
distances = [cv2.pointPolygonTest(contour_text_interest_copy,
|
||||
tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])), True)
|
||||
for mj in range(len(xv))]
|
||||
distances = np.array(distances)
|
||||
|
||||
xvinside = xv[distances >= 0]
|
||||
|
||||
if len(xvinside) == 0:
|
||||
x_min = x_min_cont
|
||||
x_max = x_max_cont
|
||||
|
@ -647,7 +583,6 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
if point_up_rot2<0:
|
||||
point_up_rot2=0
|
||||
|
||||
|
||||
x_min_rot1=x_min_rot1-x_help
|
||||
x_max_rot2=x_max_rot2-x_help
|
||||
x_max_rot3=x_max_rot3-x_help
|
||||
|
@ -658,28 +593,23 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
|||
point_down_rot3=point_down_rot3-y_help
|
||||
point_down_rot4=point_down_rot4-y_help
|
||||
|
||||
|
||||
|
||||
|
||||
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)],
|
||||
[int(x_max_rot2), int(point_up_rot2)],
|
||||
[int(x_max_rot3), int(point_down_rot3)],
|
||||
[int(x_min_rot4), int(point_down_rot4)]]))
|
||||
|
||||
textline_boxes.append(np.array([[int(x_min), int(point_up)],
|
||||
[int(x_max), int(point_up)],
|
||||
[int(x_max), int(point_down)],
|
||||
[int(x_min), int(point_down)]]))
|
||||
|
||||
|
||||
return peaks, textline_boxes_rot
|
||||
|
||||
def separate_lines_vertical(img_patch, contour_text_interest, thetha):
|
||||
|
||||
thetha = thetha + 90
|
||||
contour_text_interest_copy = contour_text_interest.copy()
|
||||
x, y, x_d, y_d, xv, x_min_cont, y_min_cont, x_max_cont, y_max_cont, first_nonzero, y_padded_up_to_down_padded, y_padded_smoothed, peaks, peaks_neg, rotation_matrix = dedup_separate_lines(img_patch, contour_text_interest, thetha, 0)
|
||||
|
||||
x, y, x_d, y_d, xv, x_min_cont, y_min_cont, x_max_cont, y_max_cont, \
|
||||
first_nonzero, y_padded_up_to_down_padded, y_padded_smoothed, \
|
||||
peaks, peaks_neg, rotation_matrix = dedup_separate_lines(img_patch, contour_text_interest, thetha, 0)
|
||||
|
||||
# plt.plot(y_padded_up_to_down_padded)
|
||||
# plt.plot(peaks_neg,y_padded_up_to_down_padded[peaks_neg],'*')
|
||||
|
@ -693,8 +623,7 @@ def separate_lines_vertical(img_patch, contour_text_interest, thetha):
|
|||
|
||||
neg_peaks_max = np.max(y_padded_up_to_down_padded[peaks_neg])
|
||||
|
||||
arg_neg_must_be_deleted = np.array(range(len(peaks_neg)))[y_padded_up_to_down_padded[peaks_neg] / float(neg_peaks_max) < 0.42]
|
||||
|
||||
arg_neg_must_be_deleted = np.arange(len(peaks_neg))[y_padded_up_to_down_padded[peaks_neg] / float(neg_peaks_max) < 0.42]
|
||||
diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)
|
||||
|
||||
arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
|
||||
|
@ -705,17 +634,15 @@ def separate_lines_vertical(img_patch, contour_text_interest, thetha):
|
|||
clusters_to_be_deleted = []
|
||||
|
||||
if len(arg_diff_cluster) >= 2 and len(arg_diff_cluster) > 0:
|
||||
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0 : arg_diff_cluster[0] + 1])
|
||||
for i in range(len(arg_diff_cluster) - 1):
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1 : arg_diff_cluster[i + 1] + 1])
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1 :
|
||||
arg_diff_cluster[i + 1] + 1])
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1 :])
|
||||
elif len(arg_neg_must_be_deleted) >= 2 and len(arg_diff_cluster) == 0:
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[:])
|
||||
|
||||
if len(arg_neg_must_be_deleted) == 1:
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted)
|
||||
|
||||
if len(clusters_to_be_deleted) > 0:
|
||||
peaks_new_extra = []
|
||||
for m in range(len(clusters_to_be_deleted)):
|
||||
|
@ -725,7 +652,6 @@ def separate_lines_vertical(img_patch, contour_text_interest, thetha):
|
|||
for m1 in range(len(clusters_to_be_deleted[m])):
|
||||
peaks_new = peaks_new[peaks_new != peaks[clusters_to_be_deleted[m][m1] - 1]]
|
||||
peaks_new = peaks_new[peaks_new != peaks[clusters_to_be_deleted[m][m1]]]
|
||||
|
||||
peaks_neg_new = peaks_neg_new[peaks_neg_new != peaks_neg[clusters_to_be_deleted[m][m1]]]
|
||||
peaks_new_tot = []
|
||||
for i1 in peaks_new:
|
||||
|
@ -796,7 +722,6 @@ def separate_lines_vertical(img_patch, contour_text_interest, thetha):
|
|||
distances = np.array(distances)
|
||||
|
||||
xvinside = xv[distances >= 0]
|
||||
|
||||
if len(xvinside) == 0:
|
||||
x_min = x_min_cont
|
||||
x_max = x_max_cont
|
||||
|
@ -823,13 +748,16 @@ def separate_lines_vertical(img_patch, contour_text_interest, thetha):
|
|||
if point_up_rot2 < 0:
|
||||
point_up_rot2 = 0
|
||||
|
||||
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)], [int(x_max_rot2), int(point_up_rot2)], [int(x_max_rot3), int(point_down_rot3)], [int(x_min_rot4), int(point_down_rot4)]]))
|
||||
|
||||
textline_boxes.append(np.array([[int(x_min), int(point_up)], [int(x_max), int(point_up)], [int(x_max), int(point_down)], [int(x_min), int(point_down)]]))
|
||||
|
||||
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)],
|
||||
[int(x_max_rot2), int(point_up_rot2)],
|
||||
[int(x_max_rot3), int(point_down_rot3)],
|
||||
[int(x_min_rot4), int(point_down_rot4)]]))
|
||||
textline_boxes.append(np.array([[int(x_min), int(point_up)],
|
||||
[int(x_max), int(point_up)],
|
||||
[int(x_max), int(point_down)],
|
||||
[int(x_min), int(point_down)]]))
|
||||
elif len(peaks) < 1:
|
||||
pass
|
||||
|
||||
elif len(peaks) == 1:
|
||||
x_min = x_min_cont
|
||||
x_max = x_max_cont
|
||||
|
@ -856,10 +784,14 @@ def separate_lines_vertical(img_patch, contour_text_interest, thetha):
|
|||
if point_up_rot2 < 0:
|
||||
point_up_rot2 = 0
|
||||
|
||||
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)], [int(x_max_rot2), int(point_up_rot2)], [int(x_max_rot3), int(point_down_rot3)], [int(x_min_rot4), int(point_down_rot4)]]))
|
||||
|
||||
textline_boxes.append(np.array([[int(x_min), int(y_min)], [int(x_max), int(y_min)], [int(x_max), int(y_max)], [int(x_min), int(y_max)]]))
|
||||
|
||||
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)],
|
||||
[int(x_max_rot2), int(point_up_rot2)],
|
||||
[int(x_max_rot3), int(point_down_rot3)],
|
||||
[int(x_min_rot4), int(point_down_rot4)]]))
|
||||
textline_boxes.append(np.array([[int(x_min), int(y_min)],
|
||||
[int(x_max), int(y_min)],
|
||||
[int(x_max), int(y_max)],
|
||||
[int(x_min), int(y_max)]]))
|
||||
elif len(peaks) == 2:
|
||||
dis_to_next = np.abs(peaks[1] - peaks[0])
|
||||
for jj in range(len(peaks)):
|
||||
|
@ -874,11 +806,12 @@ def separate_lines_vertical(img_patch, contour_text_interest, thetha):
|
|||
point_down = img_patch.shape[0] - 2
|
||||
point_up = peaks[jj] + first_nonzero - int(1.0 / 1.8 * dis_to_next)
|
||||
|
||||
distances = [cv2.pointPolygonTest(contour_text_interest_copy, tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])), True) for mj in range(len(xv))]
|
||||
distances = [cv2.pointPolygonTest(contour_text_interest_copy,
|
||||
tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])), True)
|
||||
for mj in range(len(xv))]
|
||||
distances = np.array(distances)
|
||||
|
||||
xvinside = xv[distances >= 0]
|
||||
|
||||
if len(xvinside) == 0:
|
||||
x_min = x_min_cont
|
||||
x_max = x_max_cont
|
||||
|
@ -905,12 +838,16 @@ def separate_lines_vertical(img_patch, contour_text_interest, thetha):
|
|||
if point_up_rot2 < 0:
|
||||
point_up_rot2 = 0
|
||||
|
||||
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)], [int(x_max_rot2), int(point_up_rot2)], [int(x_max_rot3), int(point_down_rot3)], [int(x_min_rot4), int(point_down_rot4)]]))
|
||||
|
||||
textline_boxes.append(np.array([[int(x_min), int(point_up)], [int(x_max), int(point_up)], [int(x_max), int(point_down)], [int(x_min), int(point_down)]]))
|
||||
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)],
|
||||
[int(x_max_rot2), int(point_up_rot2)],
|
||||
[int(x_max_rot3), int(point_down_rot3)],
|
||||
[int(x_min_rot4), int(point_down_rot4)]]))
|
||||
textline_boxes.append(np.array([[int(x_min), int(point_up)],
|
||||
[int(x_max), int(point_up)],
|
||||
[int(x_max), int(point_down)],
|
||||
[int(x_min), int(point_down)]]))
|
||||
else:
|
||||
for jj in range(len(peaks)):
|
||||
|
||||
if jj == 0:
|
||||
dis_to_next = peaks[jj + 1] - peaks[jj]
|
||||
# point_up=peaks[jj]+first_nonzero-int(1./3*dis_to_next)
|
||||
|
@ -934,11 +871,12 @@ def separate_lines_vertical(img_patch, contour_text_interest, thetha):
|
|||
point_up = peaks[jj] + first_nonzero - int(1.0 / 1.9 * dis_to_next_up)
|
||||
point_down = peaks[jj] + first_nonzero + int(1.0 / 1.9 * dis_to_next_down)
|
||||
|
||||
distances = [cv2.pointPolygonTest(contour_text_interest_copy, tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])), True) for mj in range(len(xv))]
|
||||
distances = [cv2.pointPolygonTest(contour_text_interest_copy,
|
||||
tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])), True)
|
||||
for mj in range(len(xv))]
|
||||
distances = np.array(distances)
|
||||
|
||||
xvinside = xv[distances >= 0]
|
||||
|
||||
if len(xvinside) == 0:
|
||||
x_min = x_min_cont
|
||||
x_max = x_max_cont
|
||||
|
@ -965,14 +903,17 @@ def separate_lines_vertical(img_patch, contour_text_interest, thetha):
|
|||
if point_up_rot2 < 0:
|
||||
point_up_rot2 = 0
|
||||
|
||||
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)], [int(x_max_rot2), int(point_up_rot2)], [int(x_max_rot3), int(point_down_rot3)], [int(x_min_rot4), int(point_down_rot4)]]))
|
||||
|
||||
textline_boxes.append(np.array([[int(x_min), int(point_up)], [int(x_max), int(point_up)], [int(x_max), int(point_down)], [int(x_min), int(point_down)]]))
|
||||
|
||||
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)],
|
||||
[int(x_max_rot2), int(point_up_rot2)],
|
||||
[int(x_max_rot3), int(point_down_rot3)],
|
||||
[int(x_min_rot4), int(point_down_rot4)]]))
|
||||
textline_boxes.append(np.array([[int(x_min), int(point_up)],
|
||||
[int(x_max), int(point_up)],
|
||||
[int(x_max), int(point_down)],
|
||||
[int(x_min), int(point_down)]]))
|
||||
return peaks, textline_boxes_rot
|
||||
|
||||
def separate_lines_new_inside_tiles2(img_patch, thetha):
|
||||
|
||||
(h, w) = img_patch.shape[:2]
|
||||
center = (w // 2, h // 2)
|
||||
M = cv2.getRotationMatrix2D(center, -thetha, 1.0)
|
||||
|
@ -994,9 +935,7 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
|||
y_max_cont = img_patch.shape[0]
|
||||
|
||||
xv = np.linspace(x_min_cont, x_max_cont, 1000)
|
||||
|
||||
textline_patch_sum_along_width = img_patch.sum(axis=1)
|
||||
|
||||
first_nonzero = 0 # (next((i for i, x in enumerate(mada_n) if x), None))
|
||||
|
||||
y = textline_patch_sum_along_width[:] # [first_nonzero:last_nonzero]
|
||||
|
@ -1006,9 +945,7 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
|||
|
||||
peaks_real, _ = find_peaks(gaussian_filter1d(y, 3), height=0)
|
||||
if 1 > 0:
|
||||
|
||||
try:
|
||||
|
||||
y_padded_smoothed_e = gaussian_filter1d(y_padded, 2)
|
||||
y_padded_up_to_down_e = -y_padded + np.max(y_padded)
|
||||
y_padded_up_to_down_padded_e = np.zeros(len(y_padded_up_to_down_e) + 40)
|
||||
|
@ -1019,7 +956,7 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
|||
peaks_neg_e, _ = find_peaks(y_padded_up_to_down_padded_e, height=0)
|
||||
neg_peaks_max = np.max(y_padded_up_to_down_padded_e[peaks_neg_e])
|
||||
|
||||
arg_neg_must_be_deleted = np.array(range(len(peaks_neg_e)))[y_padded_up_to_down_padded_e[peaks_neg_e] / float(neg_peaks_max) < 0.3]
|
||||
arg_neg_must_be_deleted = np.arange(len(peaks_neg_e))[y_padded_up_to_down_padded_e[peaks_neg_e] / float(neg_peaks_max) < 0.3]
|
||||
diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)
|
||||
|
||||
arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
|
||||
|
@ -1030,12 +967,10 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
|||
|
||||
clusters_to_be_deleted = []
|
||||
if len(arg_diff_cluster) > 0:
|
||||
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0 : arg_diff_cluster[0] + 1])
|
||||
for i in range(len(arg_diff_cluster) - 1):
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1 : arg_diff_cluster[i + 1] + 1])
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1 :])
|
||||
|
||||
if len(clusters_to_be_deleted) > 0:
|
||||
peaks_new_extra = []
|
||||
for m in range(len(clusters_to_be_deleted)):
|
||||
|
@ -1045,7 +980,6 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
|||
for m1 in range(len(clusters_to_be_deleted[m])):
|
||||
peaks_new = peaks_new[peaks_new != peaks_e[clusters_to_be_deleted[m][m1] - 1]]
|
||||
peaks_new = peaks_new[peaks_new != peaks_e[clusters_to_be_deleted[m][m1]]]
|
||||
|
||||
peaks_neg_new = peaks_neg_new[peaks_neg_new != peaks_neg_e[clusters_to_be_deleted[m][m1]]]
|
||||
peaks_new_tot = []
|
||||
for i1 in peaks_new:
|
||||
|
@ -1053,12 +987,13 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
|||
for i1 in peaks_new_extra:
|
||||
peaks_new_tot.append(i1)
|
||||
peaks_new_tot = np.sort(peaks_new_tot)
|
||||
|
||||
else:
|
||||
peaks_new_tot = peaks_e[:]
|
||||
|
||||
textline_con, hierarchy = return_contours_of_image(img_patch)
|
||||
textline_con_fil = filter_contours_area_of_image(img_patch, textline_con, hierarchy, max_area=1, min_area=0.0008)
|
||||
textline_con_fil = filter_contours_area_of_image(img_patch,
|
||||
textline_con, hierarchy,
|
||||
max_area=1, min_area=0.0008)
|
||||
y_diff_mean = np.mean(np.diff(peaks_new_tot)) # self.find_contours_mean_y_diff(textline_con_fil)
|
||||
|
||||
sigma_gaus = int(y_diff_mean * (7.0 / 40.0))
|
||||
|
@ -1084,27 +1019,23 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
|||
try:
|
||||
neg_peaks_max = np.max(y_padded_smoothed[peaks])
|
||||
|
||||
arg_neg_must_be_deleted = np.array(range(len(peaks_neg)))[y_padded_up_to_down_padded[peaks_neg] / float(neg_peaks_max) < 0.24]
|
||||
|
||||
arg_neg_must_be_deleted = np.arange(len(peaks_neg))[y_padded_up_to_down_padded[peaks_neg] / float(neg_peaks_max) < 0.24]
|
||||
diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)
|
||||
|
||||
arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
|
||||
arg_diff_cluster = arg_diff[diff_arg_neg_must_be_deleted > 1]
|
||||
|
||||
clusters_to_be_deleted = []
|
||||
|
||||
if len(arg_diff_cluster) >= 2 and len(arg_diff_cluster) > 0:
|
||||
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0 : arg_diff_cluster[0] + 1])
|
||||
for i in range(len(arg_diff_cluster) - 1):
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1 : arg_diff_cluster[i + 1] + 1])
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1 :
|
||||
arg_diff_cluster[i + 1] + 1])
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1 :])
|
||||
elif len(arg_neg_must_be_deleted) >= 2 and len(arg_diff_cluster) == 0:
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[:])
|
||||
|
||||
if len(arg_neg_must_be_deleted) == 1:
|
||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted)
|
||||
|
||||
if len(clusters_to_be_deleted) > 0:
|
||||
peaks_new_extra = []
|
||||
for m in range(len(clusters_to_be_deleted)):
|
||||
|
@ -1114,7 +1045,6 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
|||
for m1 in range(len(clusters_to_be_deleted[m])):
|
||||
peaks_new = peaks_new[peaks_new != peaks[clusters_to_be_deleted[m][m1] - 1]]
|
||||
peaks_new = peaks_new[peaks_new != peaks[clusters_to_be_deleted[m][m1]]]
|
||||
|
||||
peaks_neg_new = peaks_neg_new[peaks_neg_new != peaks_neg[clusters_to_be_deleted[m][m1]]]
|
||||
peaks_new_tot = []
|
||||
for i1 in peaks_new:
|
||||
|
@ -1138,7 +1068,6 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
|||
# plt.plot(y_padded_smoothed)
|
||||
# plt.plot(peaks_new_tot,y_padded_smoothed[peaks_new_tot],'*')
|
||||
# plt.show()
|
||||
|
||||
peaks = peaks_new_tot[:]
|
||||
peaks_neg = peaks_neg_new[:]
|
||||
except:
|
||||
|
@ -1166,7 +1095,6 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
|||
# print(peaks_neg_true)
|
||||
for i in range(len(peaks_neg_true)):
|
||||
img_patch[peaks_neg_true[i] - 6 : peaks_neg_true[i] + 6, :] = 0
|
||||
|
||||
else:
|
||||
pass
|
||||
|
||||
|
@ -1346,14 +1274,14 @@ def separate_lines_vertical_cont(img_patch, contour_text_interest, thetha, box_i
|
|||
contours_imgs, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||
|
||||
contours_imgs = return_parent_contours(contours_imgs, hierarchy)
|
||||
contours_imgs = filter_contours_area_of_image_tables(thresh, contours_imgs, hierarchy, max_area=max_area, min_area=min_area)
|
||||
|
||||
contours_imgs = filter_contours_area_of_image_tables(thresh,
|
||||
contours_imgs, hierarchy,
|
||||
max_area=max_area, min_area=min_area)
|
||||
cont_final = []
|
||||
###print(add_boxes_coor_into_textlines,'ikki')
|
||||
for i in range(len(contours_imgs)):
|
||||
img_contour = np.zeros((cnts_images.shape[0], cnts_images.shape[1], 3))
|
||||
img_contour = cv2.fillPoly(img_contour, pts=[contours_imgs[i]], color=(255, 255, 255))
|
||||
|
||||
img_contour = img_contour.astype(np.uint8)
|
||||
|
||||
img_contour = cv2.dilate(img_contour, kernel, iterations=4)
|
||||
|
@ -1373,9 +1301,7 @@ def separate_lines_vertical_cont(img_patch, contour_text_interest, thetha, box_i
|
|||
##print(cont_final,'nadizzzz')
|
||||
return None, cont_final
|
||||
|
||||
|
||||
def textline_contours_postprocessing(textline_mask, slope, contour_text_interest, box_ind, add_boxes_coor_into_textlines=False):
|
||||
|
||||
textline_mask = np.repeat(textline_mask[:, :, np.newaxis], 3, axis=2) * 255
|
||||
textline_mask = textline_mask.astype(np.uint8)
|
||||
kernel = np.ones((5, 5), np.uint8)
|
||||
|
@ -1400,8 +1326,10 @@ def textline_contours_postprocessing(textline_mask, slope, contour_text_interest
|
|||
x_help = 30
|
||||
y_help = 2
|
||||
|
||||
textline_mask_help = np.zeros((textline_mask.shape[0] + int(2 * y_help), textline_mask.shape[1] + int(2 * x_help), 3))
|
||||
textline_mask_help[y_help : y_help + textline_mask.shape[0], x_help : x_help + textline_mask.shape[1], :] = np.copy(textline_mask[:, :, :])
|
||||
textline_mask_help = np.zeros((textline_mask.shape[0] + int(2 * y_help),
|
||||
textline_mask.shape[1] + int(2 * x_help), 3))
|
||||
textline_mask_help[y_help : y_help + textline_mask.shape[0],
|
||||
x_help : x_help + textline_mask.shape[1], :] = np.copy(textline_mask[:, :, :])
|
||||
|
||||
dst = rotate_image(textline_mask_help, slope)
|
||||
dst = dst[:, :, 0]
|
||||
|
@ -1412,7 +1340,6 @@ def textline_contours_postprocessing(textline_mask, slope, contour_text_interest
|
|||
# plt.show()
|
||||
|
||||
contour_text_copy = contour_text_interest.copy()
|
||||
|
||||
contour_text_copy[:, 0, 0] = contour_text_copy[:, 0, 0] - box_ind[0]
|
||||
contour_text_copy[:, 0, 1] = contour_text_copy[:, 0, 1] - box_ind[1]
|
||||
|
||||
|
@ -1423,12 +1350,12 @@ def textline_contours_postprocessing(textline_mask, slope, contour_text_interest
|
|||
# plt.imshow(img_contour)
|
||||
# plt.show()
|
||||
|
||||
img_contour_help = np.zeros((img_contour.shape[0] + int(2 * y_help), img_contour.shape[1] + int(2 * x_help), 3))
|
||||
|
||||
img_contour_help[y_help : y_help + img_contour.shape[0], x_help : x_help + img_contour.shape[1], :] = np.copy(img_contour[:, :, :])
|
||||
img_contour_help = np.zeros((img_contour.shape[0] + int(2 * y_help),
|
||||
img_contour.shape[1] + int(2 * x_help), 3))
|
||||
img_contour_help[y_help : y_help + img_contour.shape[0],
|
||||
x_help : x_help + img_contour.shape[1], :] = np.copy(img_contour[:, :, :])
|
||||
|
||||
img_contour_rot = rotate_image(img_contour_help, slope)
|
||||
|
||||
# plt.imshow(img_contour_rot_help)
|
||||
# plt.show()
|
||||
|
||||
|
@ -1454,12 +1381,13 @@ def textline_contours_postprocessing(textline_mask, slope, contour_text_interest
|
|||
# print('juzaa')
|
||||
if abs(slope) > 45:
|
||||
# print(add_boxes_coor_into_textlines,'avval')
|
||||
_, contours_rotated_clean = separate_lines_vertical_cont(textline_mask, contours_text_rot[ind_big_con], box_ind, slope, add_boxes_coor_into_textlines=add_boxes_coor_into_textlines)
|
||||
_, contours_rotated_clean = separate_lines_vertical_cont(
|
||||
textline_mask, contours_text_rot[ind_big_con], box_ind, slope,
|
||||
add_boxes_coor_into_textlines=add_boxes_coor_into_textlines)
|
||||
else:
|
||||
_, contours_rotated_clean = separate_lines(dst, contours_text_rot[ind_big_con], slope, x_help, y_help)
|
||||
|
||||
_, contours_rotated_clean = separate_lines(
|
||||
dst, contours_text_rot[ind_big_con], slope, x_help, y_help)
|
||||
except:
|
||||
|
||||
contours_rotated_clean = []
|
||||
|
||||
return contours_rotated_clean
|
||||
|
@ -1487,11 +1415,9 @@ def separate_lines_new2(img_path, thetha, num_col, slope_region, logger=None, pl
|
|||
# print(margin,'margin')
|
||||
# if margin<=4:
|
||||
# margin = int(0.08 * length_x)
|
||||
|
||||
# margin=0
|
||||
|
||||
width_mid = length_x - 2 * margin
|
||||
|
||||
nxf = img_path.shape[1] / float(width_mid)
|
||||
|
||||
if nxf > int(nxf):
|
||||
|
@ -1553,8 +1479,8 @@ def separate_lines_new2(img_path, thetha, num_col, slope_region, logger=None, pl
|
|||
img_int[:, :] = img_xline[:, :] # img_patch_org[:,:,0]
|
||||
|
||||
img_resized = np.zeros((int(img_int.shape[0] * (1.2)), int(img_int.shape[1] * (3))))
|
||||
|
||||
img_resized[int(img_int.shape[0] * (0.1)) : int(img_int.shape[0] * (0.1)) + img_int.shape[0], int(img_int.shape[1] * (1)) : int(img_int.shape[1] * (1)) + img_int.shape[1]] = img_int[:, :]
|
||||
img_resized[int(img_int.shape[0] * (0.1)) : int(img_int.shape[0] * (0.1)) + img_int.shape[0],
|
||||
int(img_int.shape[1] * (1.0)) : int(img_int.shape[1] * (1.0)) + img_int.shape[1]] = img_int[:, :]
|
||||
# plt.imshow(img_xline)
|
||||
# plt.show()
|
||||
img_line_rotated = rotate_image(img_resized, slopes_tile_wise[i])
|
||||
|
@ -1565,7 +1491,9 @@ def separate_lines_new2(img_path, thetha, num_col, slope_region, logger=None, pl
|
|||
img_patch_separated_returned = rotate_image(img_patch_separated, -slopes_tile_wise[i])
|
||||
img_patch_separated_returned[:, :][img_patch_separated_returned[:, :] != 0] = 1
|
||||
|
||||
img_patch_separated_returned_true_size = img_patch_separated_returned[int(img_int.shape[0] * (0.1)) : int(img_int.shape[0] * (0.1)) + img_int.shape[0], int(img_int.shape[1] * (1)) : int(img_int.shape[1] * (1)) + img_int.shape[1]]
|
||||
img_patch_separated_returned_true_size = img_patch_separated_returned[
|
||||
int(img_int.shape[0] * (0.1)) : int(img_int.shape[0] * (0.1)) + img_int.shape[0],
|
||||
int(img_int.shape[1] * (1.0)) : int(img_int.shape[1] * (1.0)) + img_int.shape[1]]
|
||||
|
||||
img_patch_separated_returned_true_size = img_patch_separated_returned_true_size[:, margin : length_x - margin]
|
||||
img_patch_ineterst_revised[:, index_x_d + margin : index_x_u - margin] = img_patch_separated_returned_true_size
|
||||
|
@ -1594,27 +1522,19 @@ def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100,
|
|||
img_int=np.zeros((img_patch_org.shape[0],img_patch_org.shape[1]))
|
||||
img_int[:,:]=img_patch_org[:,:]#img_patch_org[:,:,0]
|
||||
|
||||
|
||||
|
||||
max_shape=np.max(img_int.shape)
|
||||
img_resized=np.zeros((int( max_shape*(1.1) ) , int( max_shape*(1.1) ) ))
|
||||
|
||||
|
||||
onset_x=int((img_resized.shape[1]-img_int.shape[1])/2.)
|
||||
onset_y=int((img_resized.shape[0]-img_int.shape[0])/2.)
|
||||
|
||||
|
||||
#img_resized=np.zeros((int( img_int.shape[0]*(1.8) ) , int( img_int.shape[1]*(2.6) ) ))
|
||||
|
||||
|
||||
|
||||
#img_resized[ int( img_int.shape[0]*(.4)):int( img_int.shape[0]*(.4))+img_int.shape[0] , int( img_int.shape[1]*(.8)):int( img_int.shape[1]*(.8))+img_int.shape[1] ]=img_int[:,:]
|
||||
img_resized[ onset_y:onset_y+img_int.shape[0] , onset_x:onset_x+img_int.shape[1] ]=img_int[:,:]
|
||||
|
||||
#print(img_resized.shape,'img_resizedshape')
|
||||
#plt.imshow(img_resized)
|
||||
#plt.show()
|
||||
|
||||
if main_page and img_patch_org.shape[1] > img_patch_org.shape[0]:
|
||||
#plt.imshow(img_resized)
|
||||
#plt.show()
|
||||
|
@ -1623,7 +1543,6 @@ def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100,
|
|||
|
||||
angles = np.linspace(angle - 22.5, angle + 22.5, n_tot_angles)
|
||||
angle = get_smallest_skew(img_resized, sigma_des, angles, map=map, logger=logger, plotter=plotter)
|
||||
|
||||
elif main_page:
|
||||
#plt.imshow(img_resized)
|
||||
#plt.show()
|
||||
|
@ -1637,7 +1556,6 @@ def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100,
|
|||
else:
|
||||
angles = np.linspace(90, 12, n_tot_angles)
|
||||
angle = get_smallest_skew(img_resized, sigma_des, angles, map=map, logger=logger, plotter=plotter)
|
||||
|
||||
else:
|
||||
angles = np.linspace(-25, 25, int(0.5 * n_tot_angles) + 10)
|
||||
angle = get_smallest_skew(img_resized, sigma_des, angles, map=map, logger=logger, plotter=plotter)
|
||||
|
@ -1695,7 +1613,9 @@ def do_work_of_slopes_new(
|
|||
else:
|
||||
try:
|
||||
textline_con, hierarchy = return_contours_of_image(img_int_p)
|
||||
textline_con_fil = filter_contours_area_of_image(img_int_p, textline_con, hierarchy, max_area=1, min_area=0.00008)
|
||||
textline_con_fil = filter_contours_area_of_image(img_int_p, textline_con,
|
||||
hierarchy,
|
||||
max_area=1, min_area=0.00008)
|
||||
y_diff_mean = find_contours_mean_y_diff(textline_con_fil)
|
||||
if np.isnan(y_diff_mean):
|
||||
slope_for_all = MAX_SLOPE
|
||||
|
@ -1733,7 +1653,6 @@ def do_work_of_slopes_new(
|
|||
|
||||
return cnt_clean_rot, box_text, contour, contour_par, crop_coor, index_r_con, slope
|
||||
|
||||
|
||||
def do_work_of_slopes_new_curved(
|
||||
box_text, contour, contour_par, index_r_con,
|
||||
textline_mask_tot_ea, image_page_rotated, mask_texts_only, num_col, scale_par, slope_deskew,
|
||||
|
@ -1759,7 +1678,9 @@ def do_work_of_slopes_new_curved(
|
|||
else:
|
||||
try:
|
||||
textline_con, hierarchy = return_contours_of_image(img_int_p)
|
||||
textline_con_fil = filter_contours_area_of_image(img_int_p, textline_con, hierarchy, max_area=1, min_area=0.0008)
|
||||
textline_con_fil = filter_contours_area_of_image(img_int_p, textline_con,
|
||||
hierarchy,
|
||||
max_area=1, min_area=0.0008)
|
||||
y_diff_mean = find_contours_mean_y_diff(textline_con_fil)
|
||||
if np.isnan(y_diff_mean):
|
||||
slope_for_all = MAX_SLOPE
|
||||
|
@ -1788,7 +1709,8 @@ def do_work_of_slopes_new_curved(
|
|||
textline_biggest_region = mask_biggest * textline_mask_tot_ea
|
||||
|
||||
# print(slope_for_all,'slope_for_all')
|
||||
textline_rotated_separated = separate_lines_new2(textline_biggest_region[y: y+h, x: x+w], 0, num_col, slope_for_all,
|
||||
textline_rotated_separated = separate_lines_new2(textline_biggest_region[y: y+h, x: x+w], 0,
|
||||
num_col, slope_for_all,
|
||||
logger=logger, plotter=plotter)
|
||||
|
||||
# new line added
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue