annotate contours.py @ 8:ca362a9bfa20 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
author imgteam
date Mon, 23 Sep 2024 10:26:26 +0000
parents b74693340624
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
1 """
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
2 Copyright (c) 2017-2023 Leonid Kostrykin, Biomedical Computer Vision Group, Heidelberg University.
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
3
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
4 Distributed under the MIT license.
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
6 """
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
7
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
8 import numpy as np
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
9 import skimage.morphology as morph
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
10
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
11
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
12 class ContourPaint:
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
13 """Yields masks corresponding to contours of objects.
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
14
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
15 :param fg_mask: Binary mask of the image foreground. Any contour never overlaps the image foreground except of those image regions corresponding to the contoured object itself.
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
16 :param thickness: The thickness of the contour (width, in pixels).
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
17 :param where: The position of the contour (``inner``, ``center``, or ``outer``).
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
18 """
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
19
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
20 def __init__(self, fg_mask, thickness, where='center'):
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
21 assert where in ('inner', 'center', 'outer')
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
22 self.fg_mask = fg_mask
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
23 self.where = where
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
24 self.thickness = thickness
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
25 if where == 'inner':
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
26 self.selem_inner = morph.disk(self.thickness)
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
27 self.selem_outer = None
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
28 elif where == 'center':
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
29 self.selem_inner = morph.disk(self.thickness - self.thickness // 2)
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
30 self.selem_outer = morph.disk(self.thickness // 2)
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
31 elif where == 'outer':
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
32 self.selem_inner = None
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
33 self.selem_outer = morph.disk(self.thickness)
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
34
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
35 def get_contour_mask(self, mask):
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
36 """Returns the binary mask of the contour of an object.
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
37
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
38 :param mask: Binary mask of an object.
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
39 :return: Binary mask of the contour
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
40 """
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
41 if self.selem_inner is not None:
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
42 inner_contour = np.logical_xor(mask, morph.binary_erosion(mask, self.selem_inner))
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
43 else:
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
44 inner_contour = np.zeros(mask.shape, bool)
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
45 if self.selem_outer is not None:
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
46 outer_contour = np.logical_and(np.logical_not(self.fg_mask), morph.binary_dilation(mask, self.selem_outer))
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
47 else:
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
48 outer_contour = np.zeros(mask.shape, bool)
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
diff changeset
49 return np.logical_or(inner_contour, outer_contour)