Skip to content

Commit

Permalink
Fixed overlap failure in tiles construction
Browse files Browse the repository at this point in the history
  • Loading branch information
c-h-benedetti committed Nov 18, 2024
1 parent 9b6fdb5 commit 5f1018d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
21 changes: 1 addition & 20 deletions src/microglia_analyzer/_tests/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
microglia_to_tiles()
7 changes: 5 additions & 2 deletions src/microglia_analyzer/tiles/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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])

Expand Down
28 changes: 27 additions & 1 deletion src/microglia_analyzer/tiles/tiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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)

0 comments on commit 5f1018d

Please sign in to comment.