Optional normalization
Install the package using pip
:
pip install medviz
Or install from the source:
pip install git+https://github.com/mohsenhariri/medviz
Import the package and use the layered_plot function
import medviz
medviz.layered_plot(image_path="dataset/1-1.nii", mask_paths=["dataset/small_bowel.nii", "dataset/1-1-label.nii"], mask_colors=["red", "yellow"], title="Layered Plot")
- make env
- source env_platform_ver/bin/activate
- make
- make check
- make pireq
- Numpy
- Matplotlib
- Pandas
- Nibabel
- Pydicom
- Tabulate
If your OS doesn't have a backend GUI to render Matplotlib, you need to install pyqt5 (via PIP), then set QT_QPA_PLATFORM=wayland
in your environment variables.
make piu pyqt5
export QT_QPA_PLATFORM=wayland
-
path_in: Converts a given path to an absolute path, optionally using the ROOT_IN environment variable as a base.
-
save_path_dir: Validates and ensures the existence of a directory path, creating it if necessary. It can also use the ROOT_OUT environment variable as a base.
-
save_path_file: Generates a unique save path for a file, appends a timestamp if the file already exists, and ensures its parent directory exists.
- Max
- Min
- Midian
- Plot layered images and masks
- Generate Gif
- Basic Usage: Plot a single image without any mask:
viz.plot3d('path/to/image.nii')
- Plotting Multiple Images: If you have multiple images to plot:
images = ['path/to/image1.nii', 'path/to/image2.nii']
viz.plot3d(images)
- Displaying Images with Masks: To visualize images along with their corresponding masks:
images = ['path/to/image1.nii', 'path/to/image2.nii']
masks = ['path/to/mask1.nii', 'path/to/mask2.nii']
viz.plot3d(images, masks=masks)
- Using Custom Colors: To specify custom colors for the masks:
images = ['path/to/image1.nii', 'path/to/image2.nii']
masks = ['path/to/mask1.nii', 'path/to/mask2.nii']
colors = ['red', 'green']
viz.plot3d(images, masks=masks, colors=colors)
- Displaying Titles: For adding titles to the plotted images:
images = ['path/to/image1.nii', 'path/to/image2.nii']
masks = ['path/to/mask1.nii', 'path/to/mask2.nii']
colors = ['red', 'green']
titles = ['Image 1', 'Image 2']
viz.plot3d(images, masks=masks, colors=colors, titles=titles)
- Specifying Display Plane: You can visualize the images in different planes:
viz.plot3d('path/to/image.nii', plane="sagittal")
- Saving the Plot: To save the plot to a specified location:
viz.plot3d('path/to/image.nii', save_path='path/to/save_directory/')
- Specifying Rows and Columns: To organize multiple images into a specific number of rows and columns:
Specify Rows:
images = ['path/to/image1.nii', 'path/to/image2.nii', 'path/to/image3.nii']
viz.plot3d(images, rows=2)
This will generate a 2x2 grid, with the third image in the first cell of the second row.
Specify Columns:
images = ['path/to/image1.nii', 'path/to/image2.nii', 'path/to/image3.nii']
viz.plot3d(images, columns=2)
This will also generate a 2x2 grid similar to the previous example.
Specifying Both Rows and Columns: For more control, you can specify both rows and columns:
images = ['path/to/image1.nii', 'path/to/image2.nii', 'path/to/image3.nii', 'path/to/image4.nii']
viz.plot3d(images, rows=2, columns=2)
This will arrange the images in a 2x2 grid with each cell having one image.
- Combining Multiple Options: You can also combine multiple options for a comprehensive view:
images = ['path/to/image1.nii', 'path/to/image2.nii']
masks = ['path/to/mask1.nii', 'path/to/mask2.nii']
titles = ["First Image with Mask", "Second Image with Mask"]
mask_colors = ["#FF0000", "#00FF00"]
viz.plot3d(images, masks=masks, mask_colors=mask_colors, titles=titles, save_path='path/to/save_directory/')
Method signature:
def gif(
image: Union[str, Path, np.ndarray],
masks: Optional[Union[str, Path, np.ndarray, List[Union[str, Path, np.ndarray]]]] = None,
mask_colors: Optional[List[str]] = None,
start_slice: Optional[int] = 0,
end_slice: Optional[int] = None,
slices: Optional[List[int]] = None,
cmap: str = "gray",
plane: str = "axial",
save_path: Union[str, Path] = "output.gif",
duration: int = 100,
segments_title: Optional[List[str]] = None
) -> None:
load image and mask(s) from file path:
image = 'path/to/image.nii' or 'path/to/image.nii.gz' or 'path/to/image.mha' or 'path/to/image.dcm'
mask = 'path/to/mask.nii' or 'path/to/mask.nii.gz' or 'path/to/mask.mha' or 'path/to/mask.dcm'
masks = ['path/to/mask1.nii', 'path/to/mask2.nii']
- Basic Usage:
Generate a GIF from a 3D image without any masks:
viz.gif(image)
This will save the GIF as output.gif in the current directory.
- Set save path:
viz.gif(image, save_path='path/to/save_directory/')
This will save the GIF as output.gif in the specified directory.
- Overlaying Mask
To overlay mask on the image:
viz.gif(image, mask=mask)
- Overlaying Multiple Masks
To overlay multiple masks on the image:
viz.gif(image, masks=masks)
This will overlay the masks in the order they are specified in the list.
- Using Custom Colors
To specify custom colors for the masks:
viz.gif(image, masks=masks, mask_colors=['red', 'green'])
- Displaying Titles
For adding titles to the plotted images:
viz.gif(image, masks=masks, mask_colors=['red', 'green'], segments_title=['Segment 1', 'Segment 2'])
- Specifying Display Plane
You can visualize the images in different planes:
viz.gif(image, plane="sagittal")
- Specifying Slices
To specify the slices to be included in the GIF:
viz.gif(image, slices=[0, 10, 20, 30, 40])
- Define a range of slices
To specify a range of slices to be included in the GIF:
viz.gif(image, start_slice=20, end_slice=40)
- Specifying Duration
To specify the duration of each frame in the GIF, the default is 100ms:
viz.gif(image, duration=200)
- Combining Multiple Options
You can also combine multiple options for a comprehensive view:
viz.gif(image, masks=masks, mask_colors=['red', 'green'], segments_title=['Segment 1', 'Segment 2'], slices=[0, 10, 20, 30, 40], save_path='path/to/save_directory/')
- 2D images.
inpute: - an array of images
viz.plot2d_images_array()
viz.plot2d_images_path()
- 2D image and masks- layered plot
inpute: - an image - an array of masks
viz.plot2d_layered_path()
viz.plot2d_layered_array()
- 3D images with slider
inpute: - an array of images
viz.plot3d.images()
- 3D images and masks- layered plot with slider
inpute: - an array of images - an array of masks
viz.plot3d.images_masks()