mirror of
https://github.com/qurator-spk/eynollah.git
synced 2026-04-30 19:22:03 +02:00
get_marginals(): reduce indentation
This commit is contained in:
parent
c18deb0722
commit
9571ce3474
1 changed files with 109 additions and 102 deletions
|
|
@ -58,117 +58,124 @@ def get_marginals(text_mask, early_layout, num_col, slope_deskew,
|
||||||
# plt.legend()
|
# plt.legend()
|
||||||
# plt.show()
|
# plt.show()
|
||||||
|
|
||||||
if max_text_thickness_percent >= 14:
|
if max_text_thickness_percent < 14:
|
||||||
text_mask_d_y_rev = np.max(text_mask_d_y) - text_mask_d_y
|
return
|
||||||
|
|
||||||
region_sum_0 = gaussian_filter1d(text_mask_d_y, 1)
|
text_mask_d_y_rev = np.max(text_mask_d_y) - text_mask_d_y
|
||||||
first_nonzero = region_sum_0.nonzero()[0][0] # outer left
|
region_sum_0 = gaussian_filter1d(text_mask_d_y, 1)
|
||||||
last_nonzero = region_sum_0.nonzero()[0][-1] # outer right
|
first_nonzero = region_sum_0.nonzero()[0][0] # outer left
|
||||||
mid_point = 0.5 * (last_nonzero + first_nonzero)
|
last_nonzero = region_sum_0.nonzero()[0][-1] # outer right
|
||||||
one_third_right = (last_nonzero - mid_point) / 3.0
|
mid_point = 0.5 * (last_nonzero + first_nonzero)
|
||||||
one_third_left = (mid_point - first_nonzero) / 3.0
|
one_third_right = (last_nonzero - mid_point) / 3.0
|
||||||
|
one_third_left = (mid_point - first_nonzero) / 3.0
|
||||||
|
|
||||||
# rs: constrain the distance at least 2 characters at 12pt, retrieve height and prominence
|
# rs: constrain the distance at least 2 characters at 12pt, retrieve height and prominence
|
||||||
peaks, props = find_peaks(text_mask_d_y_rev, height=0, prominence=0, distance=30)
|
peaks, props = find_peaks(text_mask_d_y_rev, height=0, prominence=0, distance=30)
|
||||||
peaks_orig = np.copy(peaks)
|
peaks_orig = np.copy(peaks)
|
||||||
# rs: also calculate the product of prominence and height (for final selection)
|
# rs: also calculate the product of prominence and height (for final selection)
|
||||||
scores = np.zeros(peaks.max() + 1)
|
scores = np.zeros(peaks.max() + 1)
|
||||||
scores[peaks] = props['prominences'] * props['peak_heights']
|
scores[peaks] = props['prominences'] * props['peak_heights']
|
||||||
|
|
||||||
peaks = peaks[(peaks > first_nonzero) & (peaks < last_nonzero)]
|
peaks = peaks[(peaks > first_nonzero) & (peaks < last_nonzero)]
|
||||||
peaks = peaks[region_sum_0[peaks] < min_text_thickness]
|
peaks = peaks[region_sum_0[peaks] < min_text_thickness]
|
||||||
|
|
||||||
if num_col == 1:
|
if num_col == 1:
|
||||||
peaks_right = peaks[peaks > mid_point]
|
peaks_right = peaks[peaks > mid_point]
|
||||||
peaks_left = peaks[peaks < mid_point]
|
peaks_left = peaks[peaks < mid_point]
|
||||||
if num_col == 2:
|
elif num_col == 2:
|
||||||
peaks_right = peaks[peaks > mid_point + one_third_right]
|
peaks_right = peaks[peaks > mid_point + one_third_right]
|
||||||
peaks_left = peaks[peaks < mid_point - one_third_left]
|
peaks_left = peaks[peaks < mid_point - one_third_left]
|
||||||
|
else:
|
||||||
|
# should not happen, anyway
|
||||||
|
return
|
||||||
|
|
||||||
if len(peaks_left) == 0:
|
if len(peaks_left) == 0:
|
||||||
if len(peaks_right) == 0:
|
if len(peaks_right) == 0:
|
||||||
# plt.figure()
|
# plt.figure()
|
||||||
# ax1 = plt.subplot(2, 1, 1, title='text_mask_d (deskewed text+sep mask)')
|
# ax1 = plt.subplot(2, 1, 1, title='text_mask_d (deskewed text+sep mask)')
|
||||||
# ax1.imshow(text_mask_d, aspect='auto')
|
# ax1.imshow(text_mask_d, aspect='auto')
|
||||||
# ax1.vlines([first_nonzero], 0, height, label='first_nonzero', colors='r')
|
# ax1.vlines([first_nonzero], 0, height, label='first_nonzero', colors='r')
|
||||||
# ax1.vlines([last_nonzero], 0, height, label='last_nonzero', colors='r')
|
# ax1.vlines([last_nonzero], 0, height, label='last_nonzero', colors='r')
|
||||||
# ax1.vlines(peaks_left, 0, height, label='peaks_left', colors='orange')
|
# ax1.vlines(peaks_left, 0, height, label='peaks_left', colors='orange')
|
||||||
# ax1.vlines(peaks_right, 0, height, label='peaks_right', colors='orange')
|
# ax1.vlines(peaks_right, 0, height, label='peaks_right', colors='orange')
|
||||||
# ax2 = plt.subplot(2, 1, 2, title='text_mask_d_y (smoothed)', sharex=ax1)
|
# ax2 = plt.subplot(2, 1, 2, title='text_mask_d_y (smoothed)', sharex=ax1)
|
||||||
# ax2.plot(list(range(width)), region_sum_0)
|
# ax2.plot(list(range(width)), region_sum_0)
|
||||||
# ax2.hlines(min_text_thickness, 0, width, colors='g',
|
# ax2.hlines(min_text_thickness, 0, width, colors='g',
|
||||||
# label='min_text_thickness=%d' % min_text_thickness)
|
# label='min_text_thickness=%d' % min_text_thickness)
|
||||||
# ax2.scatter(peaks_orig, region_sum_0[peaks_orig], label='peaks')
|
# ax2.scatter(peaks_orig, region_sum_0[peaks_orig], label='peaks')
|
||||||
# plt.legend()
|
# plt.legend()
|
||||||
# plt.show()
|
# plt.show()
|
||||||
return
|
|
||||||
point_right = peaks_right[np.argmax(scores[peaks_right])]
|
|
||||||
#point_left = first_nonzero
|
|
||||||
point_left = 0
|
|
||||||
elif len(peaks_right) == 0:
|
|
||||||
point_left = peaks_left[np.argmax(scores[peaks_left])]
|
|
||||||
#point_right = last_nonzero
|
|
||||||
point_right = width - 1
|
|
||||||
elif scores[peaks_left].max() < scores[peaks_right].max():
|
|
||||||
point_right = peaks_right[np.argmax(scores[peaks_right])]
|
|
||||||
#point_left = first_nonzero
|
|
||||||
point_left = 0
|
|
||||||
else:
|
|
||||||
point_left = peaks_left[np.argmax(scores[peaks_left])]
|
|
||||||
#point_right = last_nonzero
|
|
||||||
point_right = 0
|
|
||||||
|
|
||||||
main_mask_d[:, point_left: point_right] = 1
|
|
||||||
if not np.any(main_mask_d):
|
|
||||||
return
|
return
|
||||||
|
point_right = peaks_right[np.argmax(scores[peaks_right])]
|
||||||
|
#point_left = first_nonzero
|
||||||
|
point_left = 0
|
||||||
|
elif len(peaks_right) == 0:
|
||||||
|
point_left = peaks_left[np.argmax(scores[peaks_left])]
|
||||||
|
#point_right = last_nonzero
|
||||||
|
point_right = width - 1
|
||||||
|
elif scores[peaks_left].max() < scores[peaks_right].max():
|
||||||
|
point_right = peaks_right[np.argmax(scores[peaks_right])]
|
||||||
|
#point_left = first_nonzero
|
||||||
|
point_left = 0
|
||||||
|
else:
|
||||||
|
point_left = peaks_left[np.argmax(scores[peaks_left])]
|
||||||
|
#point_right = last_nonzero
|
||||||
|
point_right = 0
|
||||||
|
|
||||||
# plt.figure()
|
main_mask_d[:, point_left: point_right] = 1
|
||||||
# ax1 = plt.subplot(2, 2, 1)
|
if not np.any(main_mask_d):
|
||||||
# ax1.title.set_text('text_mask_d (deskewed text+table mask)')
|
return
|
||||||
# ax1.imshow(text_mask_d)
|
|
||||||
# ax1.vlines(peaks_left, 0, height, label='peaks_left', colors='b')
|
|
||||||
# ax1.vlines(peaks_right, 0, height, label='peaks_right', colors='b')
|
|
||||||
# ax1.vlines([first_nonzero], 0, height, label='first_nonzero', colors='g')
|
|
||||||
# ax1.vlines([last_nonzero], 0, height, label='last_nonzero', colors='g')
|
|
||||||
# ax1.vlines([point_left], 0, height, label='point_left', colors='r')
|
|
||||||
# ax1.vlines([point_right], 0, height, label='point_right', colors='r')
|
|
||||||
# ax2 = plt.subplot(2, 2, 2, title='main_mask_d (deskewed main mask)', sharey=ax1)
|
|
||||||
# ax2.imshow(main_mask_d)
|
|
||||||
# ax3 = plt.subplot(2, 2, 3, title='text_mask_d_y (projection for minima)', sharex=ax1)
|
|
||||||
# ax3.plot(list(range(width)), text_mask_d_y)
|
|
||||||
# ax3.set_aspect('auto')
|
|
||||||
# ax4 = plt.subplot(2, 2, 4, title='early_layout (undeskewed labels)')
|
|
||||||
# ax4.imshow(early_layout)
|
|
||||||
# plt.legend()
|
|
||||||
# plt.show()
|
|
||||||
|
|
||||||
# rs: rotate back (into undeskewed/original shape as early_layout input):
|
# plt.figure()
|
||||||
main_mask = rotate_image(main_mask_d, -slope_deskew)
|
# ax1 = plt.subplot(2, 2, 1)
|
||||||
|
# ax1.title.set_text('text_mask_d (deskewed text+table mask)')
|
||||||
min_area_text = 0.00001
|
# ax1.imshow(text_mask_d)
|
||||||
main_contour = return_contours_of_interested_region(main_mask, 1, min_area_text)[0]
|
# ax1.vlines(peaks_left, 0, height, label='peaks_left', colors='b')
|
||||||
text_contours = return_contours_of_interested_region(early_layout, label_text, min_area_text)
|
# ax1.vlines(peaks_right, 0, height, label='peaks_right', colors='b')
|
||||||
cx_text, cy_text = find_center_of_contours(text_contours)
|
# ax1.vlines([first_nonzero], 0, height, label='first_nonzero', colors='g')
|
||||||
|
# ax1.vlines([last_nonzero], 0, height, label='last_nonzero', colors='g')
|
||||||
|
# ax1.vlines([point_left], 0, height, label='point_left', colors='r')
|
||||||
|
# ax1.vlines([point_right], 0, height, label='point_right', colors='r')
|
||||||
|
# ax2 = plt.subplot(2, 2, 2, title='main_mask_d (deskewed main mask)', sharey=ax1)
|
||||||
|
# ax2.imshow(main_mask_d)
|
||||||
|
# ax3 = plt.subplot(2, 2, 3, title='text_mask_d_y (projection for minima)', sharex=ax1)
|
||||||
|
# ax3.plot(list(range(width)), text_mask_d_y)
|
||||||
|
# ax3.set_aspect('auto')
|
||||||
|
# ax4 = plt.subplot(2, 2, 4, title='early_layout (undeskewed labels)')
|
||||||
|
# ax4.imshow(early_layout)
|
||||||
|
# plt.legend()
|
||||||
|
# plt.show()
|
||||||
|
|
||||||
marg_contours = []
|
# rs: rotate back (into undeskewed/original shape as early_layout input):
|
||||||
for i, contour in enumerate(text_contours):
|
main_mask = rotate_image(main_mask_d, -slope_deskew)
|
||||||
if -1 == cv2.pointPolygonTest(main_contour,
|
|
||||||
(cx_text[i],
|
|
||||||
cy_text[i]),
|
|
||||||
False):
|
|
||||||
marg_contours.append(contour)
|
|
||||||
|
|
||||||
# early_layout_orig = np.copy(early_layout)
|
min_area_text = 0.00001
|
||||||
early_layout = cv2.fillPoly(early_layout, pts=marg_contours, color=label_marg)
|
main_contour = return_contours_of_interested_region(main_mask, 1, min_area_text)[0]
|
||||||
|
text_contours = return_contours_of_interested_region(early_layout, label_text, min_area_text)
|
||||||
|
cx_text, cy_text = find_center_of_contours(text_contours)
|
||||||
|
|
||||||
# plt.figure()
|
marg_contours = []
|
||||||
# ax1 = plt.subplot(2, 2, 1, title='main_mask_d (deskewed main mask)')
|
for i, contour in enumerate(text_contours):
|
||||||
# plt.imshow(main_mask_d)
|
if -1 == cv2.pointPolygonTest(main_contour,
|
||||||
# ax2 = plt.subplot(2, 2, 2, title='main_mask (undeskewed main mask)')
|
(cx_text[i],
|
||||||
# plt.imshow(main_mask)
|
cy_text[i]),
|
||||||
# ax3 = plt.subplot(2, 2, 3, title='early_layout (undeskewed labels original)')
|
False):
|
||||||
# plt.imshow(early_layout_orig)
|
marg_contours.append(contour)
|
||||||
# ax4 = plt.subplot(2, 2, 4, title='early_layout (undeskewed labels split)')
|
|
||||||
# plt.imshow(early_layout)
|
|
||||||
# plt.show()
|
|
||||||
|
|
||||||
|
# early_layout_orig = np.copy(early_layout)
|
||||||
|
early_layout = cv2.fillPoly(early_layout, pts=marg_contours, color=label_marg)
|
||||||
|
|
||||||
|
# plt.figure()
|
||||||
|
# ax1 = plt.subplot(2, 2, 1, title='main_mask_d (deskewed main mask)')
|
||||||
|
# plt.imshow(main_mask_d)
|
||||||
|
# ax2 = plt.subplot(2, 2, 2, title='main_mask (undeskewed main mask)')
|
||||||
|
# plt.imshow(main_mask)
|
||||||
|
# ax3 = plt.subplot(2, 2, 3, title='early_layout (undeskewed labels original)')
|
||||||
|
# plt.imshow(early_layout_orig)
|
||||||
|
# ax4 = plt.subplot(2, 2, 4, title='early_layout (undeskewed labels split)')
|
||||||
|
# plt.imshow(early_layout)
|
||||||
|
# plt.show()
|
||||||
|
|
||||||
|
# if there was no main text, then relabel marginalia as main
|
||||||
|
if not np.any(early_layout == label_text):
|
||||||
|
early_layout[early_layout == label_marg] = label_text
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue