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

Function get_images

monai/transforms/utils_create_transform_ims.py:370–410  ·  view source on GitHub ↗

Get image. If is dictionary, extract key. If is list, stack. If both dictionary and list, do both. Also return the image size as string to be used im the imshow. If it's a list, return `N x (H,W,D)`.

(data, is_label=False)

Source from the content-addressed store, hash-verified

368
369
370def get_images(data, is_label=False):
371 """Get image. If is dictionary, extract key. If is list, stack. If both dictionary and list, do both.
372 Also return the image size as string to be used im the imshow. If it's a list, return `N x (H,W,D)`.
373 """
374 # If not a list, convert
375 if not isinstance(data, list):
376 data = [data]
377 key = CommonKeys.LABEL if is_label else CommonKeys.IMAGE
378 is_map = isinstance(data[0], dict)
379 # length of the list will be equal to number of samples produced. This will be 1 except for transforms that
380 # produce `num_samples`.
381 data = [d[key] if is_map else d for d in data]
382 data = [d[0] for d in data] # remove channel component
383
384 # for each sample, create a list of the orthogonal views. If image is 2d, length will be 1. If 3d, there
385 # will be three orthogonal views
386 num_samples = len(data)
387 num_orthog_views = 3 if data[0].ndim == 3 else 1
388 shape_str = (f"{num_samples} x " if num_samples > 1 else "") + str(data[0].shape)
389 for i in range(num_samples):
390 data[i] = [get_2d_slice(data[i], view, is_label) for view in range(num_orthog_views)]
391
392 out = []
393 if num_samples == 1:
394 out = data[0]
395 else:
396 # we might need to panel the images. this happens if a transform produces e.g. 4 output images.
397 # In this case, we create a 2-by-2 grid from them. Output will be a list containing n_orthog_views,
398 # each element being either the image (if num_samples is 1) or the panelled image.
399 nrows = int(np.floor(num_samples**0.5))
400 for view in range(num_orthog_views):
401 result = np.asarray([d[view] for d in data])
402 nindex, height, width = result.shape
403 ncols = nindex // nrows
404 # only implemented for square number of images (e.g. 4 images goes to a 2-by-2 panel)
405 if nindex != nrows * ncols:
406 raise NotImplementedError
407 # want result.shape = (height*nrows, width*ncols), have to be careful about striding
408 result = result.reshape(nrows, ncols, height, width).swapaxes(1, 2).reshape(height * nrows, width * ncols)
409 out.append(result)
410 return out, shape_str
411
412
413def create_transform_im(

Callers 1

create_transform_imFunction · 0.85

Calls 2

get_2d_sliceFunction · 0.85
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…