Helpful functions for plotting and working with image arrays
Helpful functions for plotting and working with image arrays.
convert_to_rgb(im, channel_colors=['white'], channel_ranges=[[0.01, 0.99]])
Convert a (channels, y, x) array to an RGB array of shape (y, x, 3).
This function will return a copy of im
in order not to affect the input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im |
array or ndarray
|
Input image of shape (channels, y, x). |
required |
channel_colors |
list[str]
|
A list with python color strings (e.g. 'red')
corresponding with the color for each channel in |
['white']
|
channel_ranges |
list[list[float]]
|
A list of 2-element lists (e.g. [0.01, 0.99]) giving the value ranges that should be mapped to colors for each channel. If the given numerical values are less or equal to 1.0, they are interpreted as quantiles and the corresponding intensity values are calculated on the channel non-zero values, otherwise they are directly used as intensities. Values outside of this range will be clipped. |
[[0.01, 0.99]]
|
Returns:
Type | Description |
---|---|
ndarray
|
An RGB image of shape (y, x, 3), where the third axis corresponds to
red, green and blue channels. The image is suitable for plotting with
|
Examples:
Convert an image img
from (2, y, x) to (y, x, 3), using the
channels as yellow and blue intensities:
Source code in src/ez_zarr/plotting.py
get_shuffled_cmap(cmap_name='hsv', seed=42)
Create shuffled color map.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cmap_name |
str
|
The name of the color map (passed to |
'hsv'
|
seed |
int
|
Used to seed the random number generator with numpy.random.seed. |
42
|
Examples:
Obtain a matplotlib.colors.ListedColormap with randomized color ordering:
Source code in src/ez_zarr/plotting.py
pad_image(im, output_shape, constant_value=0)
Pad an image by adding pixels of constant_value
symmetrically around it
to make it shape output_shape
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im |
array or ndarray
|
Input image. |
required |
output_shape |
tuple[int]
|
Desired output shape after padding (must be greater or equal to im.shape). |
required |
constant_value |
int
|
Value for added pixels. |
0
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray with padded image of shape |
Examples:
Make the image im
(100, 100) in shape:
Source code in src/ez_zarr/plotting.py
plot_image(im, msk=None, msk_alpha=0.3, show_label_values=False, restrict_to_label_values=[], label_text_colour='white', label_fontsize='xx-large', channels=[0], channel_colors=['white'], channel_ranges=[[0.01, 0.95]], z_projection_method='maximum', pad_to_yx=[0, 0], pad_value=0, image_transform=None, axis_style='none', spacing_yx=None, title=None, title_fontsize='large', scalebar_pixel=0, scalebar_color='white', scalebar_position='bottomright', scalebar_label=None, scalebar_fontsize='large', call_show=True, fig_width_inch=24.0, fig_height_inch=16.0, fig_dpi=150, fig_style='dark_background')
Plot an image array, optionally overlaid with a segmentation mask.
Plot an image (provided as an array with shape (ch,z,y,x), (ch,y,x) or (y,x))
by summarizing multiple z planes using z_projection_method
(if needed),
mapping channels values (in the range of channel_ranges
) to colors
(given by channel_colors
), converting it to an RGB array of shape
(x,y,3) and plotting it using matplotlib.pyplot.imshow
.
If msk
is provided, it is assumed to contain a compatible segmentation
mask and will be plotted transparently (controlled by msk_alpha
)
on top of the image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im |
ndarray
|
An array with four (ch,z,y,x), three (ch,y,x) or two (y,x) dimensions with image intensities. |
required |
msk |
ndarray
|
An optional cooresponding mask array with
a shape compatible to |
None
|
msk_alpha |
float
|
A scalar value between 0 (fully transparent) and 1 (solid) defining the transparency of the mask. |
0.3
|
show_label_values |
bool
|
Whether to show the label values at the centroid of each label. |
False
|
restrict_to_label_values |
list or scalar
|
A scalar or a (possibly
empty) list of label values. Only has an effect if |
[]
|
label_text_colour |
str
|
The colour of the label text, if
|
'white'
|
label_fontsize |
float or str
|
The font size of the label text, if
|
'xx-large'
|
channels |
list[int]
|
The image channel(s) to be plotted. For example,
to plot the first and third channel of a 4-channel image with
shape (4,1,500,600), you can use |
[0]
|
channel_colors |
list[str]
|
A list with python color strings
(e.g. 'red') defining the color for each channel in |
['white']
|
channel_ranges |
list[list[float]]
|
A list of 2-element lists (e.g. [0.01, 0.95]) giving the the value ranges that should be mapped to colors for each channel. If the given numerical values are less or equal to 1.0, they are interpreted as quantiles and the corresponding intensity values are calculated on the channel non-zero values, otherwise they are directly used as intensities. Values outside of this range will be clipped. |
[[0.01, 0.95]]
|
z_projection_method |
str
|
Method for combining multiple z planes. For available methods, see ez_zarr.plotting.zproject (default: 'maximum'). |
'maximum'
|
pad_to_yx |
list[int]
|
If the image or label mask are smaller, pad
them by |
[0, 0]
|
pad_value |
int
|
Value to use for constant-value image padding. |
0
|
image_transform |
Callable
|
A function to transform the image values
before conversion to RGB and plotting. If |
None
|
axis_style |
str
|
A string scalar defining how to draw the axis. Should
be one of 'none' (no axis, the default), 'pixel' (show pixel labels),
'frame' (show just a frame around the plot without ticks)
or 'micrometer' (show micrometer labels). If |
'none'
|
spacing_yx |
list[float]
|
The spacing of pixels in y and x direction,
in micrometer, used to convert pixels to micrometer when
|
None
|
title |
str
|
String to add as a title on top of the image. If |
None
|
title_fontsize |
float or str
|
Font size of the title. Values accepted
by |
'large'
|
scalebar_pixel |
int
|
If non-zero, draw a scalebar of size |
0
|
scalebar_color |
str
|
Scalebar color. |
'white'
|
scalebar_position |
str
|
position of the scale bar, one of 'topleft', 'topright', 'bottomleft' or 'bottomright' |
'bottomright'
|
scalebar_label |
str
|
If not |
None
|
scalebar_fontsize |
float or str
|
Font size of the scalebar label. Values
accepted by |
'large'
|
call_show |
bool
|
If |
True
|
fig_width_inch |
float
|
Figure width (in inches). |
24.0
|
fig_height_inch |
float
|
Figure height (in inches). |
16.0
|
fig_dpi |
int
|
Figure resolution (dots per inch). |
150
|
fig_style |
str
|
Style for the figure. Supported are 'brightfield', which
is a special mode for single-channel brightfield microscopic images
(it will automatically set |
'dark_background'
|
Examples:
Plot the first channel of img
in gray-scale.
Source code in src/ez_zarr/plotting.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 |
|
zproject(im, axis=1, method='maximum', keepdims=True, img_bit=16)
Project a 4D or 3D image along z-axis according to desired projection method.
Helper function to conveniently project multi-dimensional images along z-axis according to desired projection method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im |
array or ndarray
|
The array to be projected. |
required |
axis |
int
|
The axis on which to perform the projection (typically
the z-axis). Unless otherwise specified, it defaults to axis 1 (by convention Fractal
outputs have shape ch,z,y,x). Note: if masks are projected, Fractal output have usually
shape z,y,x, so use |
1
|
method |
str
|
The projection method of choice. Available options are 'maximum' (default), 'minimum' (typically used for brightfield images), 'sum', 'average'. |
'maximum'
|
keepdims |
bool
|
Should the output array keep the same dimensions
as the input array ( |
True
|
img_bit |
int
|
Is the image 8- or 16-bit? Relevant for 'sum' projection, where clipping might be necessary. |
16
|
Returns:
Type | Description |
---|---|
Union[Array, ndarray]
|
The input array where the z-axis has been projected according to the method of choice. |
Examples:
Project np.array along z-axis:
>>> zproject(im=np.array([[[1,2,4],[1,3,5],[1,6,7]],[[1,2,4],[1,3,5],[1,6,7]]]), method='maximum', axis=1)