Skip to content

Commit

Permalink
Improve deduplication of lines; intermediate commit for debugging.
Browse files Browse the repository at this point in the history
  • Loading branch information
redur committed May 3, 2024
1 parent 03393da commit 48717ec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/stratigraphy/line_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ def detect_lines_lsd(page: fitz.Page, scale_factor=2, lsd_params=None) -> ArrayL

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


def extract_lines(page: fitz.Page, line_detection_params: dict) -> list[Line]:
Expand All @@ -75,6 +73,7 @@ def extract_lines(page: fitz.Page, line_detection_params: dict) -> list[Line]:
scale_factor=line_detection_params["pdf_scale_factor"],
)
lines = drop_vertical_lines(lines, threshold=line_detection_params["vertical_lines_threshold"])
lines = deduplicate_lines(lines)
merging_params = line_detection_params["line_merging_params"]
if merging_params["use_clustering"]:
lines = merge_parallel_lines_approximately(
Expand Down
16 changes: 13 additions & 3 deletions src/stratigraphy/util/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ def remove(self, line_index: str):
del self.hashmap[line_index]

def add(self, line: Line) -> str:
key = uuid.uuid4().hex
self.hashmap[key] = line
return key
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.distance_to(line.start) < 0.1 and value.end.distance_to(line.end) < 0.1
for _key, value in self.hashmap.items()
)
7 changes: 5 additions & 2 deletions src/stratigraphy/util/geometric_line_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ def deduplicate_lines(lines: list[Line]) -> list[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 _check_if_present(lines: list[Line], line: Line) -> bool:
return any(
value.start.distance_to(line.start) < 0.1 and value.end.distance_to(line.end) < 0.1 for value in lines
) # we are on a pixel grid and 0.1 is a reasonable threshold


def drop_vertical_lines(lines: list[Line], threshold: float = 0.1) -> ArrayLike:
Expand Down Expand Up @@ -476,6 +478,7 @@ 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 48717ec

Please sign in to comment.