Untitled

 avatar
unknown
python
a year ago
1.6 kB
2
Indexable
def show_interactive_3d(brain_array, mask_array):
    # Use marching cubes to obtain the surface mesh of the brain and the mask
    verts_brain, faces_brain, _, _ = measure.marching_cubes(brain_array, level=0.1)
    verts_mask, faces_mask, _, _ = measure.marching_cubes(mask_array, level=0.1)

    # Create the brain mesh
    x_brain, y_brain, z_brain = zip(*verts_brain)
    brain_mesh = go.Mesh3d(
        x=x_brain, y=y_brain, z=z_brain,
        i=faces_brain[:, 0], j=faces_brain[:, 1], k=faces_brain[:, 2],
        color='orange', opacity=0.5, name='Brain'
    )

    # Create the mask mesh
    x_mask, y_mask, z_mask = zip(*verts_mask)
    mask_mesh = go.Mesh3d(
        x=x_mask, y=y_mask, z=z_mask,
        i=faces_mask[:, 0], j=faces_mask[:, 1], k=faces_mask[:, 2],
        color='blue', opacity=0.5, name='Mask'
    )

    # Create the figure and add both meshes
    fig = go.Figure(data=[brain_mesh, mask_mesh])
    fig.update_layout(
        scene=dict(aspectmode='data'),
        title='3D Visualization of Brain with Mask',
        width=1000,  # Set the desired width here
        height=800  # Set the desired height here
    )
    fig.show()




def plot_original_brain_with_ground_truth(folder_path, idx):

    img_path = test_files[idx]+'/'+test_files[idx]+'_flair.nii'
    img = nib.load(folder_path+img_path)
    img_data = np.asarray(img.get_fdata())

    mask_path = test_files[idx]+'/'+test_files[idx]+'_seg.nii'
    mask = nib.load(folder_path+mask_path)
    mask_data = np.asarray(mask.get_fdata())

    show_interactive_3d(img_data, mask_data)
Editor is loading...
Leave a Comment