U Ph&@sddlmZddlmZmZddlZddlZddlm Z m Z ddl m Z ddl mZddlmZddlmZdd lmZmZerdd lmZned d d \ZZddgZdddddddddddejdejfdddddddddddddddddd d!dZd(ddd%ddddd&d'dZdS))) annotations) TYPE_CHECKINGAnyN) DtypeLikeNdarrayOrTensor) SpatialPad) rescale_array)repeat)optional_import)convert_data_typeconvert_to_dst_type)pyplot matplotlibr )name matshow3d blend_images) rnoneFrrz str | Noneztuple[int, int]z int | Noneintz float | Nonestrboolrztuple[Any, np.ndarray])volumefigtitlefigsizeframes_per_row frame_dim channel_dimvminvmaxevery_n interpolationshow fill_valuemargindtypekwargsreturnc st|tjdd|dk r8|dks0j|dkr8tdtttfrtdtjs^tdtjt dd Ddd }t |d dtj fd d Ddd nt jd krt dq|dk rt|ddjdjdjdfn*t|ddjdjdf|dkrBtn|}|dkrZtn|}ddt| d |stttt }ttt |d ttt }d|t gg}|dk r|ddgg7}|| | ggd7}tjj|dd|d| dtfdd t|D}|dk rLt|dd}t|tjr`|}n2|dkrvtjdd}|js|d|jd}|j|f||| d|| d|dk r|!||dk rt"|dr|#|| rt$||fS)a8 Create a 3D volume figure as a grid of images. Args: volume: 3D volume to display. data shape can be `BCHWD`, `CHWD` or `HWD`. Higher dimensional arrays will be reshaped into (-1, H, W, [C]), `C` depends on `channel_dim` arg. A list of channel-first (C, H[, W, D]) arrays can also be passed in, in which case they will be displayed as a padded and stacked volume. fig: matplotlib figure or Axes to use. If None, a new figure will be created. title: title of the figure. figsize: size of the figure. frames_per_row: number of frames to display in each row. If None, sqrt(firstdim) will be used. frame_dim: for higher dimensional arrays, which dimension from (`-1`, `-2`, `-3`) is moved to the `-3` dimension. dim and reshape to (-1, H, W) shape to construct frames, default to `-3`. channel_dim: if not None, explicitly specify the channel dimension to be transposed to the last dimensionas shape (-1, H, W, C). this can be used to plot RGB color image. if None, the channel dimension will be flattened with `frame_dim` and `batch_dim` as shape (-1, H, W). note that it can only support 3D input image. default is None. vmin: `vmin` for the matplotlib `imshow`. vmax: `vmax` for the matplotlib `imshow`. every_n: factor to subsample the frames so that only every n-th frame is displayed. interpolation: interpolation to use for the matplotlib `matshow`. show: if True, show the figure. fill_value: value to use for the empty part of the grid. margin: margin to use for the grid. dtype: data type of the output stacked frames. kwargs: additional keyword arguments to matplotlib `matshow` and `imshow`. See Also: - https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.imshow.html - https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.matshow.html Example: >>> import numpy as np >>> import matplotlib.pyplot as plt >>> from monai.visualize import matshow3d # create a figure of a 3D volume >>> volume = np.random.rand(10, 10, 10) >>> fig = plt.figure() >>> matshow3d(volume, fig=fig, title="3D Volume") >>> plt.show() # create a figure of a list of channel-first 3D volumes >>> volumes = [np.random.rand(1, 10, 10, 10), np.random.rand(1, 10, 10, 10)] >>> fig = plt.figure() >>> matshow3d(volumes, fig=fig, title="List of Volumes") >>> plt.show() )data output_typerN)rr)rzKchannel_dim must be: None, 0 or 1, and channels of image must be 1, 3 or 4.z volume must be a list of arrays.cSsg|] }|jqS)shape.0vr.r.J/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/visualize/utils.py nszmatshow3d..axisrcsg|] }|qSr.r.r0)padr.r3r4psr,rF)copyconstant)modeconstant_valuescs&g|]fddtDqS)csg|]}|qSr.r.)r1j)colsivolr.r3r4sz(matshow3d...)range)r1)rArC)rBr3r4sT) tight_layouto)r r!r#offset_size_inches)%r npndarrayr/ ValueError isinstancelisttuplemaxasarrayr concatenatelen expand_dimsmoveaxisreshapenanminnanmaxrceilsqrtminr7astypeblockrDpltAxesfigureaxes add_subplotmatshowr6 set_titlehasattrrHr$)rrrrrrrr r!r"r#r$r%r&r'r(pad_sizerowswidthimaxr.)rAr7rCr3r"s`C &          ?hsvTzfloat | NdarrayOrTensor)imagelabelalphacmaprescale_arraystransparent_backgroundr)c Cs<|jddkrtd|jddkr,td|jdd|jddkrPtdt|tjtjfr|jdd|jddkrtd|rt|}t|}|jddkrt|d dd }d d d d dd}|||}t|tjtjfr|}n(t|tjrt ||}n t ||}|r$d||dk<d|} | |||S)a 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. alpha: this specifies the weighting given to the label, where 0 is completely transparent and 1 is completely opaque. This can be given as either a single value or an array/tensor that is the same size as the input image. cmap: specify colormap in the matplotlib, default to `hsv`, for more details, please refer to: https://matplotlib.org/2.0.2/users/colormaps.html. rescale_arrays: whether to rescale the array to [0, 1] first, default to `True`. transparent_background: if true, any zeros in the label field will not be colored. .. image:: ../../docs/images/blend_images.png rrzLabel should have 1 channel.)rr,z"Image should have 1 or 3 channels.Nz3image and label should have matching spatial sizes.z;if alpha is image, size should match input image and label.r,r5rr)rormr)cSsPtj|}t|tj^}}||d}t|dddd}t||^}}|S)Nrr9r,)r] colormapsget_cmapr rIrJrTr )rorm_cmapZlabel_np_Z label_rgb_np label_rgbr.r.r3 get_label_rgbs   z#blend_images..get_label_rgb) r/rKrLrIrJtorchTensorrr full_like) rlrmrnrorprqrwrvZw_labelZw_imager.r.r3rs2   )rjrkTT) __future__rtypingrrnumpyrIrxmonai.config.type_definitionsrrZmonai.transforms.croppad.arrayrmonai.transforms.utilsr0monai.transforms.utils_pytorch_numpy_unificationr monai.utils.moduler monai.utils.type_conversionr r rr r]ru__all__nanfloat32rrr.r.r.r3 sD     0