File size: 5,168 Bytes
a3290d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
@author: louisblankemeier
"""

import os

import numpy as np
from scipy.ndimage import zoom

from comp2comp.visualization.detectron_visualizer import Visualizer
from comp2comp.visualization.linear_planar_reformation import (
    linear_planar_reformation,
)


def method_visualizer(
    sagittal_image,
    axial_image,
    axial_slice,
    sagittal_slice,
    center_sagittal,
    radius_sagittal,
    center_axial,
    radius_axial,
    output_dir,
    anatomy,
):
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    axial_image = np.clip(axial_image, -300, 1800)
    axial_image = normalize_img(axial_image) * 255.0

    sagittal_image = np.clip(sagittal_image, -300, 1800)
    sagittal_image = normalize_img(sagittal_image) * 255.0

    sagittal_image = sagittal_image.reshape(
        (sagittal_image.shape[0], sagittal_image.shape[1], 1)
    )
    img_rgb = np.tile(sagittal_image, (1, 1, 3))
    vis = Visualizer(img_rgb)
    vis.draw_circle(
        circle_coord=center_sagittal, color=[0, 1, 0], radius=radius_sagittal
    )
    vis.draw_binary_mask(sagittal_slice)

    vis_obj = vis.get_output()
    vis_obj.save(os.path.join(output_dir, f"{anatomy}_sagittal_method.png"))

    axial_image = axial_image.reshape((axial_image.shape[0], axial_image.shape[1], 1))
    img_rgb = np.tile(axial_image, (1, 1, 3))
    vis = Visualizer(img_rgb)
    vis.draw_circle(circle_coord=center_axial, color=[0, 1, 0], radius=radius_axial)
    vis.draw_binary_mask(axial_slice)

    vis_obj = vis.get_output()
    vis_obj.save(os.path.join(output_dir, f"{anatomy}_axial_method.png"))


def hip_roi_visualizer(
    medical_volume,
    roi,
    centroid,
    hu,
    output_dir,
    anatomy,
):
    zooms = medical_volume.header.get_zooms()
    zoom_factor = zooms[2] / zooms[1]

    sagittal_image = medical_volume.get_fdata()[centroid[0], :, :]
    sagittal_roi = roi[centroid[0], :, :]

    sagittal_image = zoom(sagittal_image, (1, zoom_factor), order=1).round()
    sagittal_roi = zoom(sagittal_roi, (1, zoom_factor), order=3).round()
    sagittal_image = np.flip(sagittal_image.T)
    sagittal_roi = np.flip(sagittal_roi.T)

    axial_image = medical_volume.get_fdata()[:, :, round(centroid[2])]
    axial_roi = roi[:, :, round(centroid[2])]

    axial_image = np.flip(axial_image.T)
    axial_roi = np.flip(axial_roi.T)

    _ROI_COLOR = np.array([1.000, 0.340, 0.200])

    sagittal_image = np.clip(sagittal_image, -300, 1800)
    sagittal_image = normalize_img(sagittal_image) * 255.0
    sagittal_image = sagittal_image.reshape(
        (sagittal_image.shape[0], sagittal_image.shape[1], 1)
    )
    img_rgb = np.tile(sagittal_image, (1, 1, 3))
    vis = Visualizer(img_rgb)
    vis.draw_binary_mask(
        sagittal_roi,
        color=_ROI_COLOR,
        edge_color=_ROI_COLOR,
        alpha=0.0,
        area_threshold=0,
    )
    vis.draw_text(
        text=f"Mean HU: {round(hu)}",
        position=(412, 10),
        color=_ROI_COLOR,
        font_size=9,
        horizontal_alignment="left",
    )
    vis_obj = vis.get_output()
    vis_obj.save(os.path.join(output_dir, f"{anatomy}_hip_roi_sagittal.png"))

    """
    axial_image = np.clip(axial_image, -300, 1800)
    axial_image = normalize_img(axial_image) * 255.0
    axial_image = axial_image.reshape((axial_image.shape[0], axial_image.shape[1], 1))
    img_rgb = np.tile(axial_image, (1, 1, 3))
    vis = Visualizer(img_rgb)
    vis.draw_binary_mask(
        axial_roi, color=_ROI_COLOR, edge_color=_ROI_COLOR, alpha=0.0, area_threshold=0
    )
    vis.draw_text(
        text=f"Mean HU: {round(hu)}",
        position=(412, 10),
        color=_ROI_COLOR,
        font_size=9,
        horizontal_alignment="left",
    )
    vis_obj = vis.get_output()
    vis_obj.save(os.path.join(output_dir, f"{anatomy}_hip_roi_axial.png"))
    """


def hip_report_visualizer(medical_volume, roi, centroids, output_dir, anatomy, labels):
    _ROI_COLOR = np.array([1.000, 0.340, 0.200])
    image, mask = linear_planar_reformation(
        medical_volume, roi, centroids, dimension="axial"
    )
    # add 3rd dim to image
    image = np.flip(image.T)
    mask = np.flip(mask.T)
    mask[mask > 1] = 1
    # mask = np.expand_dims(mask, axis=2)
    image = np.expand_dims(image, axis=2)
    image = np.clip(image, -300, 1800)
    image = normalize_img(image) * 255.0
    img_rgb = np.tile(image, (1, 1, 3))
    vis = Visualizer(img_rgb)
    vis.draw_binary_mask(
        mask, color=_ROI_COLOR, edge_color=_ROI_COLOR, alpha=0.0, area_threshold=0
    )
    pos_idx = 0
    for key, value in labels.items():
        vis.draw_text(
            text=f"{key}: {value}",
            position=(310, 10 + pos_idx * 17),
            color=_ROI_COLOR,
            font_size=9,
            horizontal_alignment="left",
        )
        pos_idx += 1
    vis_obj = vis.get_output()
    vis_obj.save(os.path.join(output_dir, f"{anatomy}_report_axial.png"))


def normalize_img(img: np.ndarray) -> np.ndarray:
    """Normalize the image.
    Args:
        img (np.ndarray): Input image.
    Returns:
        np.ndarray: Normalized image.
    """
    return (img - img.min()) / (img.max() - img.min())