Skip to content

Commit

Permalink
Fix line merging with quadtrees. Deduplicate recognized lines.
Browse files Browse the repository at this point in the history
  • Loading branch information
redur committed May 2, 2024
1 parent 1068bb1 commit 03393da
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/stratigraphy/line_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from stratigraphy.util.dataclasses import Line
from stratigraphy.util.geometric_line_utilities import (
deduplicate_lines,
drop_vertical_lines,
merge_parallel_lines_approximately,
merge_parallel_lines_quadtree,
Expand Down Expand Up @@ -53,7 +54,9 @@ def detect_lines_lsd(page: fitz.Page, scale_factor=2, lsd_params=None) -> ArrayL

# Detect lines in the image
lines = lsd.detect(gray)[0]
return [line_from_array(line, scale_factor) for line in lines]
converted_lines = [line_from_array(line, scale_factor) for line in lines]
deduplicated_lines = deduplicate_lines(converted_lines)
return deduplicated_lines


def extract_lines(page: fitz.Page, line_detection_params: dict) -> list[Line]:
Expand Down
13 changes: 3 additions & 10 deletions src/stratigraphy/util/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,6 @@ def remove(self, line_index: str):
del self.hashmap[line_index]

def add(self, line: Line) -> str:
if not self._check_if_present(line):
key = uuid.uuid4().hex
self.hashmap[key] = line
return key
else:
logger.warning("Line already present in IndexedLines.")
return None

def _check_if_present(self, line: Line) -> bool:
return any(value.start == line.start and value.end == line.end for _key, value in self.hashmap.items())
key = uuid.uuid4().hex
self.hashmap[key] = line
return key
21 changes: 20 additions & 1 deletion src/stratigraphy/util/geometric_line_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@
sys.setrecursionlimit(10000) # required for the quadtree


def deduplicate_lines(lines: list[Line]) -> list[Line]:
"""Deduplicate lines by merging lines that are close to each other.
Args:
lines (list[Line]): The lines to deduplicate.
Returns:
list[Line]: The deduplicated lines.
"""
deduplicated_lines = []
for line in lines:
if not _check_if_present(deduplicated_lines, line):
deduplicated_lines.append(line)
return deduplicated_lines


def _check_if_present(lines, line: Line) -> bool:
return any(value.start == line.start and value.end == line.end for value in lines)


def drop_vertical_lines(lines: list[Line], threshold: float = 0.1) -> ArrayLike:
"""Given a list of lines, remove the lines that are close to vertical.
Expand Down Expand Up @@ -456,7 +476,6 @@ def merge_parallel_lines_quadtree(lines: list[Line], tol: int, angle_threshold:
merged_any = True
continue
if merged_any:
print("Starting recursion.")
return merge_parallel_lines_quadtree(
list(indexed_lines.hashmap.values()), tol=tol, angle_threshold=angle_threshold
)
Expand Down

0 comments on commit 03393da

Please sign in to comment.