Skip to content

Commit

Permalink
Improve detection of the line to split pages
Browse files Browse the repository at this point in the history
  • Loading branch information
bansan85 committed Oct 24, 2020
1 parent c92fc8b commit cad8600
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 143 deletions.
27 changes: 18 additions & 9 deletions compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ def get_right_point_from_alpha_posy(


def keep_angle_pos_closed_to_target(
data: Tuple[float, Optional[int]],
data: Tuple[int, int, int, int],
limit_angle: float,
target_angle: float,
target_pos: int,
limit_pos: int,
) -> bool:
ang, pos = data
ang, pos = get_angle_0_180_posx((data[0], data[1]), (data[2], data[3]))

if pos is None:
return False
angle_ok = (
Expand Down Expand Up @@ -200,9 +201,7 @@ def find_dpi(
raise Exception("dpi", "non détecté")


def find_closed_value(
histogram: Dict[int, Tuple[Tuple[int, int], Tuple[int, int]]], i: int
) -> Tuple[Tuple[int, int], Tuple[int, int]]:
def find_closed_value(histogram: Dict[int, _T], i: int) -> _T:
ibis = 0
while True:
if i + ibis in histogram:
Expand Down Expand Up @@ -230,10 +229,8 @@ def get_timestamp_ns() -> int:
return time.time_ns() # pylint: disable=no-member,useless-suppression


def get_top_histogram(
smooth: Any, histogram: Dict[int, Tuple[Tuple[int, int], Tuple[int, int]]]
) -> List[Tuple[Tuple[int, int], Tuple[int, int]]]:
retval: List[Tuple[Tuple[int, int], Tuple[int, int]]] = []
def get_top_histogram(smooth: Any, histogram: Dict[int, _T]) -> List[_T]:
retval: List[_T] = []
if smooth[0] > smooth[1]:
retval.append(find_closed_value(histogram, 0))
for i in range(1, len(smooth) - 1):
Expand All @@ -242,3 +239,15 @@ def get_top_histogram(
if smooth[len(smooth) - 1] > smooth[len(smooth) - 2]:
retval.append(find_closed_value(histogram, len(smooth) - 1))
return retval


def get_tops_indices_histogram(smooth: Any) -> List[int]:
retval: List[int] = []
if smooth[0] > smooth[1]:
retval.append(0)
for i in range(1, len(smooth) - 1):
if smooth[i] > smooth[i - 1] and smooth[i] > smooth[i + 1]:
retval.append(i)
if smooth[len(smooth) - 1] > smooth[len(smooth) - 2]:
retval.append(len(smooth) - 1)
return retval
16 changes: 16 additions & 0 deletions cv2ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,19 @@ def remove_black_border_in_image(
gray_bordered2 = cv2.bitwise_not(res)
write_image_if(gray_bordered2, enable_debug, "_2e.png")
return gray_bordered2


def erode_and_dilate(
image: Any, size: Tuple[int, int], iterations: int
) -> Any:
eroded = cv2.erode(
image,
cv2.getStructuringElement(cv2.MORPH_ELLIPSE, size),
iterations=iterations,
)
dilate = cv2.dilate(
eroded,
cv2.getStructuringElement(cv2.MORPH_ELLIPSE, size),
iterations=iterations,
)
return dilate
6 changes: 6 additions & 0 deletions page/crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,12 @@ def crop_around_page(

if rect is None:
raise Exception("Failed to found contour of the page.")

if enable_debug is not None:
image_cnt = cv2ext.convertion_en_couleur(image)
cv2.drawContours(image_cnt, [rect], 0, (0, 0, 255), 3)
cv2.imwrite(enable_debug + "_" + str(n_page) + "_1_6.png", image_cnt)

x_crop1 = [rect[0, 0, 0], rect[1, 0, 0], rect[2, 0, 0], rect[3, 0, 0]]
y_crop1 = [rect[0, 0, 1], rect[1, 0, 1], rect[2, 0, 1], rect[3, 0, 1]]
x_crop1.sort()
Expand Down
Loading

0 comments on commit cad8600

Please sign in to comment.