-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathboundbox.py
More file actions
67 lines (55 loc) · 2.03 KB
/
Copy pathboundbox.py
File metadata and controls
67 lines (55 loc) · 2.03 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
import os
import shutil
from time import time
import numpy as np
import SimpleITK as sitk
import scipy.ndimage as ndimage
import math
import random
from torch.autograd import Variable
import torch
import torch.nn.functional as F
start_time = time()
f = open('/home/qinwang/ZWB-KiTS19-Base/yuankun/test_bbox_yk_single.txt')
for line in f.readlines():
ori_line = line.strip().split(',')
ct_path = ori_line[0]
file = "/data/weihao/KiTS2019-3mm-test-clip/%s.nii" % ct_path.split('.')[0]
bbox = np.array(list(map(int, ori_line[1:])))
ct = sitk.ReadImage(file, sitk.sitkInt16)
ct_array = sitk.GetArrayFromImage(ct)
x = (bbox[0] + bbox[3])//2
y = (bbox[1] + bbox[4])//2
z = (bbox[2] + bbox[5])//2
m_x, m_y, m_z = x-192, y-120, z-40
w_x, w_y, w_z = x+192, y+120, z+40
if m_x < 0:
w_x += -m_x
m_x = 0
if m_y < 0:
w_y += -m_y
m_y = 0
if m_z < 0:
w_z += -m_z
m_z = 0
new_ct_array = ct_array[m_x:w_x, m_y:w_y, m_z:w_z]
if w_z > ct_array.shape[-1]:
pad = w_z - ct_array.shape[-1]
new_ct_array = np.pad(new_ct_array, ((0,0),(0,0),(pad, 0)), constant_values=0, mode='constant')
if w_y > ct_array.shape[-2]:
pad = w_y - ct_array.shape[-2]
new_ct_array = np.pad(new_ct_array, ((0,0),(pad, 0),(0,0)), constant_values=0, mode='constant')
if w_x > ct_array.shape[-3]:
pad = w_x - ct_array.shape[-3]
new_ct_array = np.pad(new_ct_array, ((pad, 0),(0,0),(0,0)), constant_values=0, mode='constant')
print(new_ct_array.shape)
if new_ct_array.shape != (384,240,80):
print('fuck')
exit(0)
new_ct = sitk.GetImageFromArray(new_ct_array)
new_ct.SetDirection(ct.GetDirection())
new_ct.SetOrigin(ct.GetOrigin())
new_ct.SetSpacing((ct.GetSpacing()))
index = ct_path.split('.')[0].split('_')[-1]
new_ct_name = 'img-' + index + '.nii'
sitk.WriteImage(new_ct, os.path.join("/data/weihao/testset2/", new_ct_name))