|
import argparse |
|
|
|
from datasets import load_dataset |
|
|
|
import open3d as o3d |
|
import pyvista as pv |
|
from PIL import Image |
|
import matplotlib |
|
import matplotlib.pyplot as plt |
|
|
|
import numpy as np |
|
import random |
|
|
|
|
|
def plot_3D_image(values, resolution, p=None, interactive_slice=False, orthogonal_slices=True): |
|
''' Interactive plot of the 3D volume''' |
|
|
|
|
|
grid = pv.ImageData() |
|
values = np.transpose(values, (1,2,0)) |
|
|
|
|
|
grid.dimensions = np.array(values.shape) + 1 |
|
|
|
|
|
|
|
origin = np.array(resolution[0]) * np.array(values.shape) * 0.5 |
|
grid.origin = origin |
|
|
|
grid.spacing = resolution[0] |
|
|
|
|
|
grid.cell_data["values"] = values.flatten(order="F") |
|
|
|
if p is None: |
|
p = pv.Plotter() |
|
|
|
if orthogonal_slices: |
|
slices = grid.slice_orthogonal() |
|
cmap = matplotlib.colors.ListedColormap(['black', 'indianred', 'goldenrod', 'steelblue', 'ghostwhite']) |
|
p.add_mesh(slices, cmap=cmap) |
|
|
|
if interactive_slice: |
|
p.add_mesh_clip_plane(grid) |
|
|
|
return p |
|
|
|
|
|
def get_sliced_mri_png(sample): |
|
|
|
mri = np.asarray(sample['mri_seg']) |
|
resolution = np.asarray(sample['resolution']) |
|
|
|
p = pv.Plotter(shape=(1, 1), off_screen=True) |
|
p.subplot(0, 0) |
|
plotter = plot_3D_image(mri, resolution, p, interactive_slice=False, orthogonal_slices=True) |
|
plotter.view_yz() |
|
plotter.remove_scalar_bar() |
|
|
|
img = p.screenshot("./extras/img.png", return_img=True) |
|
|
|
img = Image.fromarray(img) |
|
|
|
p = pv.Plotter(shape=(1, 1), off_screen=True) |
|
p.subplot(0, 0) |
|
plotter = plot_3D_image(mri, resolution, p, interactive_slice=False, orthogonal_slices=True) |
|
plotter.remove_scalar_bar() |
|
plotter.view_xz() |
|
img_lateral = p.screenshot("./extras/img_lateral.png", return_img=True) |
|
img_lateral = Image.fromarray(img_lateral) |
|
|
|
img = img.resize((512+128, 372+128)) |
|
img_lateral = img_lateral.resize((512+128, 372+128)) |
|
return img, img_lateral |
|
|
|
|
|
def vis_hit_sample(sample): |
|
""" |
|
:param sample: HIT dataset sample |
|
:return: |
|
""" |
|
|
|
pc = np.asarray(sample['body_cont_pc']) |
|
|
|
mesh_verts = np.asarray(sample['smpl_dict']['verts']) |
|
mesh_verts_free = np.asarray(sample['smpl_dict']['verts_free']) |
|
mesh_faces = np.asarray(sample['smpl_dict']['faces']) |
|
|
|
|
|
pcd = o3d.geometry.PointCloud() |
|
pcd.points = o3d.utility.Vector3dVector(pc) |
|
pcd.paint_uniform_color([0.6509803922, 0.2901960784, 0.2823529412]) |
|
pcd_front = pcd.__copy__() |
|
|
|
|
|
mesh = o3d.geometry.TriangleMesh() |
|
mesh.vertices = o3d.utility.Vector3dVector(mesh_verts) |
|
mesh.triangles = o3d.utility.Vector3iVector(mesh_faces) |
|
mesh.paint_uniform_color([0.737254902, 0.7960784314, 0.8196078431]) |
|
|
|
|
|
mesh_free = o3d.geometry.TriangleMesh() |
|
mesh_free.vertices = o3d.utility.Vector3dVector(mesh_verts_free) |
|
mesh_free.triangles = o3d.utility.Vector3iVector(mesh_faces) |
|
mesh_free.paint_uniform_color([0.737254902, 0.7960784314, 0.8196078431]) |
|
|
|
|
|
vis = o3d.visualization.Visualizer() |
|
vis.create_window() |
|
|
|
xyz = (-np.pi / 2, 0, 0) |
|
R1 = o3d.geometry.get_rotation_matrix_from_xyz(xyz) |
|
|
|
vis.add_geometry(mesh.rotate(R1, center=(0, 0, 0))) |
|
vis.add_geometry(pcd.rotate(R1, center=(0, 0, 0))) |
|
|
|
vis.add_geometry(mesh_free.translate((1.2, 0, 0))) |
|
vis.add_geometry(mesh_free.rotate(R1, center=(0, 0, 0))) |
|
vis.add_geometry(pcd_front.translate((1.2, 0, 0))) |
|
vis.add_geometry(pcd_front.rotate(R1, center=(0, 0, 0))) |
|
|
|
vis.get_render_option().mesh_show_wireframe = True |
|
vis.get_render_option().point_size = 2 |
|
vis.poll_events() |
|
vis.update_renderer() |
|
vis.run() |
|
return 0 |
|
|
|
|
|
if __name__ == '__main__': |
|
parser = argparse.ArgumentParser(description='HIT dataset visualization') |
|
parser.add_argument('--gender', type=str, default='male') |
|
parser.add_argument('--split', type=str, default='train') |
|
parser.add_argument('--idx', type=int, default=None) |
|
parser.add_argument('--show_skin', action='store_true') |
|
parser.add_argument('--show_tissue', action='store_true') |
|
|
|
|
|
args = parser.parse_args() |
|
assert args.gender in ['male', 'female'] |
|
assert args.split in ['train', 'validation', 'test'] |
|
|
|
|
|
hit_dataset = load_dataset("varora/hit", name=args.gender, split=args.split) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
N_dataset = hit_dataset.__len__() |
|
|
|
|
|
if not args.idx: |
|
idx = random.randint(0, N_dataset) |
|
else: |
|
idx = args.idx |
|
assert idx < N_dataset, f"{idx} in {args.gender}:{args.split} is out of range for dataset of length {N_dataset}." |
|
|
|
|
|
hit_sample = hit_dataset[idx] |
|
|
|
print(f"Visualizing sample no. {idx} in {args.gender}:{args.split}.") |
|
if args.show_tissue: |
|
img, img_lateral = get_sliced_mri_png(hit_sample) |
|
img.show() |
|
img_lateral.show() |
|
elif args.show_skin: |
|
vis_hit_sample(hit_sample) |
|
else: |
|
img, img_lateral = get_sliced_mri_png(hit_sample) |
|
img.show() |
|
img_lateral.show() |
|
vis_hit_sample(hit_sample) |
|
|
|
|
|
|