MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / blend_images

Function blend_images

monai/visualize/utils.py:164–230  ·  view source on GitHub ↗

Blend an image and a label. Both should have the shape CHW[D]. The image may have C==1 or 3 channels (greyscale or RGB). The label is expected to have C==1. Args: image: the input image to blend with label data. label: the input label to blend with image data.

(
    image: NdarrayOrTensor,
    label: NdarrayOrTensor,
    alpha: float | NdarrayOrTensor = 0.5,
    cmap: str = "hsv",
    rescale_arrays: bool = True,
    transparent_background: bool = True,
)

Source from the content-addressed store, hash-verified

162
163
164def blend_images(
165 image: NdarrayOrTensor,
166 label: NdarrayOrTensor,
167 alpha: float | NdarrayOrTensor = 0.5,
168 cmap: str = "hsv",
169 rescale_arrays: bool = True,
170 transparent_background: bool = True,
171) -> NdarrayOrTensor:
172 """
173 Blend an image and a label. Both should have the shape CHW[D].
174 The image may have C==1 or 3 channels (greyscale or RGB).
175 The label is expected to have C==1.
176
177 Args:
178 image: the input image to blend with label data.
179 label: the input label to blend with image data.
180 alpha: this specifies the weighting given to the label, where 0 is completely
181 transparent and 1 is completely opaque. This can be given as either a
182 single value or an array/tensor that is the same size as the input image.
183 cmap: specify colormap in the matplotlib, default to `hsv`, for more details, please refer to:
184 https://matplotlib.org/2.0.2/users/colormaps.html.
185 rescale_arrays: whether to rescale the array to [0, 1] first, default to `True`.
186 transparent_background: if true, any zeros in the label field will not be colored.
187
188 .. image:: ../../docs/images/blend_images.png
189
190 """
191
192 if label.shape[0] != 1:
193 raise ValueError("Label should have 1 channel.")
194 if image.shape[0] not in (1, 3):
195 raise ValueError("Image should have 1 or 3 channels.")
196 if image.shape[1:] != label.shape[1:]:
197 raise ValueError("image and label should have matching spatial sizes.")
198 if isinstance(alpha, (np.ndarray, torch.Tensor)):
199 if image.shape[1:] != alpha.shape[1:]: # pytype: disable=attribute-error,invalid-directive
200 raise ValueError("if alpha is image, size should match input image and label.")
201
202 # rescale arrays to [0, 1] if desired
203 if rescale_arrays:
204 image = rescale_array(image)
205 label = rescale_array(label)
206 # convert image to rgb (if necessary) and then rgba
207 if image.shape[0] == 1:
208 image = repeat(image, 3, axis=0)
209
210 def get_label_rgb(cmap: str, label: NdarrayOrTensor) -> NdarrayOrTensor:
211 _cmap = plt.colormaps.get_cmap(cmap)
212 label_np, *_ = convert_data_type(label, np.ndarray)
213 label_rgb_np = _cmap(label_np[0])
214 label_rgb_np = np.moveaxis(label_rgb_np, -1, 0)[:3]
215 label_rgb, *_ = convert_to_dst_type(label_rgb_np, label)
216 return label_rgb
217
218 label_rgb = get_label_rgb(cmap, label)
219 if isinstance(alpha, (torch.Tensor, np.ndarray)):
220 w_label = alpha
221 elif isinstance(label, torch.Tensor):

Callers 1

test_blendMethod · 0.90

Calls 3

rescale_arrayFunction · 0.90
repeatFunction · 0.90
get_label_rgbFunction · 0.85

Tested by 1

test_blendMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…