File size: 6,278 Bytes
f8be18d
 
4a7553f
f8be18d
4a7553f
35315d1
 
 
 
 
4a7553f
c4f6f4c
4a7553f
f8be18d
35315d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a7553f
f8be18d
 
 
 
4a7553f
 
1ede165
 
 
 
 
4a7553f
 
 
 
 
 
 
 
 
 
 
1ede165
4a7553f
 
 
 
 
 
 
 
 
f8be18d
4a7553f
 
f8be18d
 
 
 
4a7553f
f8be18d
4a7553f
f8be18d
 
4a7553f
f8be18d
4a7553f
 
 
f8be18d
4a7553f
 
 
f8be18d
 
 
 
35315d1
 
f8be18d
 
 
 
 
 
 
56661e2
f8be18d
 
4a7553f
f8be18d
4a7553f
 
 
 
 
 
 
f8be18d
 
 
 
 
 
 
 
40c1e35
f8be18d
 
 
c4f6f4c
f8be18d
35315d1
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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'''

    # Create the spatial reference
    grid = pv.ImageData()
    values = np.transpose(values, (1,2,0))
    # Set the grid dimensions: shape + 1 because we want to inject our values on
    # the CELL data
    grid.dimensions = np.array(values.shape) + 1

    # Edit the spatial reference
    # The bottom left corner of the data set
    origin = np.array(resolution[0]) * np.array(values.shape) * 0.5
    grid.origin = origin
    #print(f'Scan size in meter: {origin * 2}')
    grid.spacing = resolution[0]  # These are the cell sizes along each axis

    # Add the data values to the cell data
    grid.cell_data["values"] = values.flatten(order="F")  # Flatten the array!

    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):
    # get data
    mri = np.asarray(sample['mri_seg'])
    resolution = np.asarray(sample['resolution'])
    # set plotter
    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()
    # store screenshot
    img = p.screenshot("./extras/img.png", return_img=True)
    # read screenshot
    img = Image.fromarray(img)
    # set plotter lateral
    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)
    # resize
    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:
    """
    # get point-cloud from sample
    pc = np.asarray(sample['body_cont_pc'])
    # get mesh and mesh-free-verts from sample
    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'])

    # create point-cloud
    pcd = o3d.geometry.PointCloud()
    pcd.points = o3d.utility.Vector3dVector(pc)
    pcd.paint_uniform_color([0.6509803922, 0.2901960784, 0.2823529412])
    pcd_front = pcd.__copy__()

    # create mesh
    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])

    # create mesh-free-verts
    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])

    # visualize sample
    vis = o3d.visualization.Visualizer()
    vis.create_window()
    # side-view
    xyz = (-np.pi / 2, 0, 0)
    R1 = o3d.geometry.get_rotation_matrix_from_xyz(xyz)
    # vis mesh with pointcloud
    vis.add_geometry(mesh.rotate(R1, center=(0, 0, 0)))
    vis.add_geometry(pcd.rotate(R1, center=(0, 0, 0)))
    # vis mesh-free-verts with pointcloud
    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)))
    # render
    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')

    # get args
    args = parser.parse_args()
    assert args.gender in ['male', 'female']
    assert args.split in ['train', 'validation', 'test']

    # load split
    hit_dataset = load_dataset("varora/hit", name=args.gender, split=args.split)

    # to load specific split, use:
    # male splits
    #male_val = load_dataset("varora/hit", "male", split="validation")
    #male_val = load_dataset("varora/hit", "male", split="validation")
    #male_test = load_dataset("varora/hit", "male", split="test")
    # female splits
    #female_train = load_dataset("varora/hit", "female", split="train")
    #female_val = load_dataset("varora/hit", "female", split="validation")
    #female_test = load_dataset("varora/hit", "female", split="test")

    # len of split
    N_dataset = hit_dataset.__len__()

    # get idx for sample
    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}."

    # get sample
    hit_sample = hit_dataset[idx]
    # visualize the sample
    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)