marcomusy / vedo

A python module for scientific analysis of 3D data based on VTK and Numpy
https://vedo.embl.es
MIT License
2.05k stars 266 forks source link

Optimize Loading and Visualization of Multiple .nii.gz Segments in Vedo #1145

Closed ivishalanand closed 4 months ago

ivishalanand commented 4 months ago

Description:

I am loading and visualizing several segmented body parts from a segmentation job. The segmentation resulted in approximately 120 .nii.gz files, each representing a different segment of the body. I need to visualize all these segments simultaneously in their respective locations in 3D space.

Current Issue:

When I load all the segments using Vedo, the application becomes slow and laggy. The lag significantly affects the interactivity and usability of the visualization tool. I need a way to optimize the loading and rendering process without merging the segments into a single file. Maintaining the ability to toggle the visibility of individual segments is crucial for my analysis and visualization tasks.

Code Example:

Below is the current code I am using to load and visualize the segments:

from pathlib import Path
import random
from vedo.applications import *
from vedo import *

subject = "s0024"
SEGMENTATION_PATH = "/segmentations"
directory = Path(SEGMENTATION_PATH)

file_paths_with_sizes = [(path, path.stat().st_size) for path in directory.rglob('*') if path.is_file()]

# Sort the files by size in descending order
file_paths_with_sizes.sort(key=lambda x: x[1], reverse=True)

sorted_file_paths = [str(path) for path, size in file_paths_with_sizes]

def _generate_colors(count):
    """Generate random colormap names."""
    available_colormaps = ['Accent', 'Blues', 'BrBG', 'BuGn', 'BuPu', 'CMRmap', 'Dark2', 'GnBu', 'Greens',
                            'Greys', 'OrRd', 'Oranges', 'PRGn', 'Paired', 'Pastel1', 'Pastel2', 'PiYG', 'PuBu',
                            'PuBuGn', 'PuOr', 'PuRd', 'Purples', 'RdBu', 'RdGy', 'RdPu', 'RdYlBu', 'RdYlGn', 
                            'Reds', 'Set1', 'Set2', 'Set3', 'Spectral', 'Wistia', 'YlGn', 'YlGnBu', 'YlOrBr',
                            'YlOrRd', 'afmhot', 'autumn', 'binary', 'bone', 'brg', 'bwr', 'cividis', 'cool',
                            'coolwarm', 'copper', 'cubehelix', 'flag', 'gist_earth', 'gist_gray', 'gist_heat',
                            'gist_ncar', 'gist_rainbow', 'gist_stern', 'gist_yarg', 'gnuplot', 'gnuplot2', 
                            'gray', 'hot', 'hsv', 'inferno', 'jet', 'magma', 'nipy_spectral', 'ocean', 
                            'pink', 'plasma', 'prism', 'rainbow', 'seismic', 'spring', 'summer', 'tab10', 
                            'tab20', 'tab20b', 'tab20c', 'terrain', 'turbo', 'twilight', 'twilight_shifted', 
                            'viridis', 'winter']

    return random.sample(available_colormaps, count)

# Load segmented volumes
volumes = []
SEGMENTS = 70
for path, color in zip(sorted_file_paths[:SEGMENTS], _generate_colors(SEGMENTS)):
    vol = Volume(path).cmap(color)
    volumes.append(vol)

show(*volumes, axes=1, bg='white', viewup='z', interactive=True)

Requirements:

Request:

Thank you for your assistance!

marcomusy commented 4 months ago

was this already answered elsewhere (?) Reopen if not.