From 5f1018dfb375d1fa613a2b7b1103634e40f097d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20H=2E=20Benedetti?= Date: Tue, 19 Nov 2024 00:25:28 +0100 Subject: [PATCH] Fixed overlap failure in tiles construction --- src/microglia_analyzer/_tests/workflow.py | 21 +---------------- src/microglia_analyzer/tiles/patch.py | 7 ++++-- src/microglia_analyzer/tiles/tiler.py | 28 ++++++++++++++++++++++- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/microglia_analyzer/_tests/workflow.py b/src/microglia_analyzer/_tests/workflow.py index 9dcd791..79c737c 100644 --- a/src/microglia_analyzer/_tests/workflow.py +++ b/src/microglia_analyzer/_tests/workflow.py @@ -21,24 +21,5 @@ def microglia_to_tiles(): tile_name = im_name.replace(".tif", f"_{str(i).zfill(2)}.tif") tifffile.imwrite(os.path.join(output_folder, tile_name), tile) -def classic_workflow_test(): - def logging(message): - print("[MA] " + message) - - img_target = "/home/benedetti/Documents/projects/2060-microglia/data/_testing_images/adulte 4.tif" - - mam = MicrogliaAnalyzer(logging) - mam.load_image(img_target) - mam.set_calibration(0.325, "µm") - mam.export_patches() - mam.set_segmentation_model("/home/benedetti/Documents/projects/2060-microglia/µnet/µnet-V004") - mam.segment_microglia() - return - mam.set_classification_model("µyolo-V005") - mam.classify_microglia() - mam.make_skeletons() - mam.extract_metrics() - - if __name__ == "__main__": - classic_workflow_test() \ No newline at end of file + microglia_to_tiles() \ No newline at end of file diff --git a/src/microglia_analyzer/tiles/patch.py b/src/microglia_analyzer/tiles/patch.py index aa5f854..cea7c2d 100644 --- a/src/microglia_analyzer/tiles/patch.py +++ b/src/microglia_analyzer/tiles/patch.py @@ -41,6 +41,9 @@ def __init__(self, patch_size, overlap, indices, shape, grid): self._check_neighbours() self._process_overlap() + def __str__(self): + return f"Patch2D({self.patch_size} > {self.ul_corner}, {self.lr_corner}, {self.overlaps})" + def _process_patch(self): """ From the indices on the grid, determines the upper-left and lower-right corners of the patch. @@ -86,9 +89,9 @@ def _process_overlap(self): if self.has_neighbour[0]: self.overlaps[0] = max(self.overlap, x - self.ul_corner[1]) if self.has_neighbour[1]: - self.overlaps[1] = self.overlap if self.lr_corner[0] + self.patch_size <= self.shape[0] else self.shape[0] - self.lr_corner[0] + self.overlaps[1] = self.overlap if (self.lr_corner[0] + self.patch_size - self.overlap <= self.shape[0]) else (self.shape[0] - self.lr_corner[0]) if self.has_neighbour[2]: - self.overlaps[2] = self.overlap if self.lr_corner[1] + self.patch_size <= self.shape[1] else self.shape[1] - self.lr_corner[1] + self.overlaps[2] = self.overlap if (self.lr_corner[1] + self.patch_size - self.overlap <= self.shape[1]) else (self.shape[1] - self.lr_corner[1]) if self.has_neighbour[3]: self.overlaps[3] = max(self.overlap, y - self.ul_corner[0]) diff --git a/src/microglia_analyzer/tiles/tiler.py b/src/microglia_analyzer/tiles/tiler.py index fb729a3..40aa9ba 100644 --- a/src/microglia_analyzer/tiles/tiler.py +++ b/src/microglia_analyzer/tiles/tiler.py @@ -228,4 +228,30 @@ def tiles_to_image(self, patches): coef = np.stack([self.blending_coefs[i]] * n_channels, axis=-1) if n_channels > 1 else self.blending_coefs[i] copies[i] *= coef canvas[p.ul_corner[0]:p.lr_corner[0], p.ul_corner[1]:p.lr_corner[1]] += copies[i] - return canvas.astype(patches[0].dtype) \ No newline at end of file + return canvas.astype(patches[0].dtype) + + +if __name__ == "__main__": + import tifffile + from microglia_analyzer.experimental.tiles import generate_checkerboard + + # checkerboard_img = np.squeeze(np.array(generate_checkerboard(2048, 2048, 16, 16))) + checkerboard_img = np.ones((2048, 2048), np.float32) + tifffile.imwrite("/tmp/exp/01-original.tif", checkerboard_img) + + tiles_manager = ImageTiler2D(512, 128, checkerboard_img.shape) + tiles = tiles_manager.image_to_tiles(checkerboard_img) + tifffile.imwrite("/tmp/exp/02-tiles.tif", tiles) + + merged = tiles_manager.tiles_to_image(tiles) + tifffile.imwrite("/tmp/exp/03-merged.tif", merged) + + tifffile.imwrite("/tmp/exp/04-coefs.tif", tiles_manager.blending_coefs) + + tifffile.imwrite( + "/tmp/exp/05-gradient.tif", + tiles_manager.tiles_to_image(tiles_manager.blending_coefs) + ) + + for p in tiles_manager.layout: + print(p)