-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils.py
More file actions
69 lines (59 loc) · 2.27 KB
/
utils.py
File metadata and controls
69 lines (59 loc) · 2.27 KB
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
# ----------------------------------------------#
# Pro : cbct
# File : dataset.py
# Date : 2023/2/22
# Author : Qing Wu
# Email : wuqing@shanghaitech.edu.cn
# ----------------------------------------------#
import numpy as np
import SimpleITK as sitk
from tqdm import tqdm
from skimage.metrics import structural_similarity
from skimage.metrics import peak_signal_noise_ratio
def psnr(image, ground_truth):
data_range = np.max(ground_truth) - np.min(ground_truth)
return peak_signal_noise_ratio(ground_truth, image, data_range=data_range)
def ssim(image, ground_truth):
data_range = np.max(ground_truth) - np.min(ground_truth)
return structural_similarity(image, ground_truth, data_range=data_range)
def fan_beam_ray(proj_pos, SOD):
origin_x = 0
origin_y = -1
y = np.linspace(-1, 1, int(2*SOD)).reshape(-1, 1) # (2*SOD, ) -> (2*SOD, 1)
x = np.zeros_like(y) # (2*SOD, 1)
xy_temp = np.concatenate((x, y), axis=-1) # (2*SOD, 2)
xy_temp = np.concatenate((xy_temp, np.ones_like(x)), axis=-1) # (2*SOD, 3)
num_det = len(proj_pos)
xy = np.zeros(shape=(num_det, int(2*SOD), 2)) # (L, 2*SOD, 2)
for i in range(num_det):
fan_angle_rad = np.deg2rad(proj_pos[num_det-i-1])
M = np.array(
[
[np.cos(fan_angle_rad), -np.sin(fan_angle_rad),
-1*origin_x*np.cos(fan_angle_rad)+origin_y*np.sin(fan_angle_rad)+origin_x],
[np.sin(fan_angle_rad), np.cos(fan_angle_rad),
-1*origin_x*np.sin(fan_angle_rad)-origin_y*np.cos(fan_angle_rad)+origin_y],
[0, 0, 1]
]
)
temp = xy_temp @ M.T # (2*SOD, 3) @ (3, 3) -> (2*SOD, 3)
xy[i, :, :] = temp[:, :2] # (2*SOD, 2)
return xy
def grid_coordinate(h, w):
x = np.linspace(-1, 1, h)
y = np.linspace(-1, 1, w)
x, y = np.meshgrid(x, y, indexing='ij') # (h, w), (h, w)
xy = np.stack([x, y], -1).reshape(-1, 2) # (h*w, 2)
return xy
def rotate_ray(xy, angle):
xy_shape = xy.shape
angle_rad = np.deg2rad(angle)
trans_mat = np.array(
[
[np.cos(angle_rad), -np.sin(angle_rad)],
[np.sin(angle_rad), np.cos(angle_rad)],
]
)
xy = xy.reshape(-1, 2)
xy = (np.dot(xy, trans_mat.T)).reshape(xy_shape)
return xy