From 710783a2f82db6605fbdace3171ed4e4ad8bc938 Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Mon, 11 Apr 2022 09:28:00 +0200 Subject: [PATCH] merging algorithm explanation adjusted --- image_prediction/stitching/merging.py | 29 ++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/image_prediction/stitching/merging.py b/image_prediction/stitching/merging.py index 0892ba7..33e3541 100644 --- a/image_prediction/stitching/merging.py +++ b/image_prediction/stitching/merging.py @@ -1,6 +1,6 @@ from copy import deepcopy from functools import reduce -from typing import Iterable, Callable +from typing import Iterable, Callable, List from PIL import Image from funcy import juxt, first, rest, rcompose @@ -17,14 +17,29 @@ def no_new_merges(pairs1, pairs2): return len(pairs1) == len(pairs2) -def merge_along_both_axes(pairs: Iterable[ImageMetadataPair]): +def merge_along_both_axes(pairs: Iterable[ImageMetadataPair]) -> List[ImageMetadataPair]: pairs = merge_along_axis(pairs, "x") pairs = list(merge_along_axis(pairs, "y")) return pairs -def merge_along_axis(pairs: Iterable[ImageMetadataPair], axis): +def merge_along_axis(pairs: Iterable[ImageMetadataPair], axis) -> Iterable[ImageMetadataPair]: + """Partially merges image-metadata pairs of adjacent images along a given axis. Needs to be iterated with + alternating axes until no more merges happen to merge all adjacent images. + + Explanation: + + Merging algorithm works as follows: + A dot represents a pair, a bracket a group and a colon a merged pair. + 1) Start with pairs: (........) + 2) Align on lesser: ([....] [....]) + 3) Align on greater: ([[..] [..]] [[....]]) + 4) Flatten once: ([..] [..] [....]) + 5) Merge orthogonally: ([:] [..] [:..]) + 6) Flatten once: (:..:..) + """ + def group_pairs_within_groups_by_greater_coordinate(groups): return map(CoordGrouper(axis).group_pairs_by_greater_coordinate, groups) @@ -35,11 +50,11 @@ def merge_along_axis(pairs: Iterable[ImageMetadataPair], axis): return CoordGrouper(axis).group_pairs_by_lesser_coordinate(pairs) return rcompose( - group_pairs_by_lesser_coordinate, # pairs -> groups of pairs aligned on one edge - group_pairs_within_groups_by_greater_coordinate, # -> groups of pairs fully aligned on orthogonal axis - flatten_groups_once, # groups of groups of pairs -> groups of pairs + group_pairs_by_lesser_coordinate, + group_pairs_within_groups_by_greater_coordinate, + flatten_groups_once, merge_groups_along_orthogonal_axis, - flatten_groups_once, # groups of pairs -> pairs + flatten_groups_once, )(pairs)