Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kostrykin committed Mar 12, 2024
1 parent 5572a64 commit 16f5ea7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 65 deletions.
94 changes: 29 additions & 65 deletions tools/colorize_labels/colorize_labels.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,39 @@
"""
Copyright (c) 2017-2024 Leonid Kostrykin, Biomedical Computer Vision Group, Heidelberg University
import argparse

Distributed under the MIT license.
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
import superdsm.io
import superdsm.render

Originally implemented in:
https://github.com/BMCV/SuperDSM/blob/master/superdsm/render.py
"""

import numpy as np
def _parse_hex_part(hex_part):
return int(f'0x{hex_part}')


def shuffle_labels(labels, bg_label=None, seed=None):
"""
Randomly shuffles the values of an integer-valued image.
def color_hex_to_rgb_tuple(hex):
if hex.startswith('#'):
hex = hex[1:]
return (
_parse_hex_part(hex[0:2]),
_parse_hex_part(hex[2:4]),
_parse_hex_part(hex[4:6]),
)

:param labels:
An object of type ``numpy.ndarray`` corresponding to labeled segmentation masks.

:param bg_label:
If not ``None``, then this label stays fixed.
if __name__ == '__main__':

:param seed:
The seed used for randomization.
parser = argparse.ArgumentParser()
parser.add_argument('input', type=str)
parser.add_argument('bg_label', type=int)
parser.add_argument('bg_color', type=str)
parser.add_argument('cmap', type=str, default='gist_rainbow')
parser.add_argument('seed', type=int)
args = parser.parse_args()

:return:
An object of type ``numpy.ndarray`` corresponding to ``labels`` with shuffled values (labels).
"""
label_values0 = frozenset(labels.flatten())
if bg_label is not None: label_values0 -= {bg_label}
label_values0 = list(label_values0)
if seed is not None: np.random.seed(seed)
label_values1 = np.asarray(label_values0).copy()
np.random.shuffle(label_values1)
label_map = dict(zip(label_values0, label_values1))
result = np.zeros_like(labels)
for l in label_map.keys():
cc = (labels == l)
result[cc] = label_map[l]
return result
im = superdsm.io.imread(args.input)


def colorize_labels(labels, bg_label=0, cmap='gist_rainbow', bg_color=(0,0,0), shuffle=None):
"""
Returns a colorized representation of an integer-valued image.
:param labels:
An object of type ``numpy.ndarray`` corresponding to labeled segmentation masks.
:param bg_label:
Image areas with this label are forced to the color ``bg_color``.
:param cmap:
The colormap used to colorize the remaining labels (see `the list <https://matplotlib.org/stable/tutorials/colors/colormaps.html>`_).
:param bg_color:
The color used to represent the image regions with label ``bg_label`` (RGB).
:param shuffle:
If not ``None``, then used as ``seed`` to shuffle the labels before colorization, and not used otherwise.
:return:
An object of type ``numpy.ndarray`` corresponding to an RGB image.
"""
if shuffle is not None:
labels = shuffle_labels(labels, bg_label=bg_label, seed=shuffle)
if isinstance(cmap, str):
cmap = plt.get_cmap(cmap)
img = cmap((labels - labels.min()) / float(labels.max() - labels.min()))
if img.shape[2] > 3: img = img[:,:,:3]
if bg_label is not None:
bg = (labels == bg_label)
img[bg] = np.asarray(bg_color)[None, None, :]
return img
superdsm.render.colorize_labels(
labels=im,
bg_label=args.bg_label,
cmap=args.cmap,
bg_color=color_hex_to_rgb_tuple(args.bg_color),
shuffle=args.seed,
)
39 changes: 39 additions & 0 deletions tools/colorize_labels/colorize_labels.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<tool id="colorize_labels" name="Colorize label map" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="20.05">
<description>with SuperDSM</description>
<macros>
<token name="@TOOL_VERSION@">0.2.0</token>
<token name="@VERSION_SUFFIX@">0</token>
</macros>
<edam_operations>
<edam_operation>operation_3443</edam_operation>
</edam_operations>
<xrefs>
<xref type="bio.tools">superdsm</xref>
<xref type="biii">superdsm</xref>
</xrefs>
<requirements>
<requirement type="package" version="0.2.0">superdsm</requirement>
</requirements>
<inputs>
<param name="input" type="data" format="tiff,png" label="Input image (label map)" />
<param name="method_id" type="select" label="Thresholding method">
<option value="manual">Manual</option>
<option value="otsu" selected="True">Globally adaptive / Otsu</option>
<option value="li">Globally adaptive / Li's Minimum Cross Entropy</option>
<option value="isodata">Globally adaptive / Isodata</option>
<option value="yen">Globally adaptive / Yen</option>
<option value="loc_gaussian">Locally adaptive / Gaussian</option>
<option value="loc_median">Locally adaptive / Median</option>
<option value="loc_mean">Locally adaptive / Mean</option>
</param>
</inputs>
<outputs>
<data format="png" name="output" from_work_dir="out.png" />
</outputs>
<help>
Colorize label map for visualization.
</help>
<citations>
<citation type="doi">10.1109/TPAMI.2022.3185583</citation>
</citations>
</tool>

0 comments on commit 16f5ea7

Please sign in to comment.