-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
118 lines (94 loc) · 3.16 KB
/
Copy pathutils.py
File metadata and controls
118 lines (94 loc) · 3.16 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
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
import os
import cv2
import numpy as np
import pydicom as dicomio
import SimpleITK as sitk
from PIL import Image
def loadlist(path):
list = os.listdir(path)
list.sort()
return list
def loadFile(filename):
ds = sitk.ReadImage(filename)
img_array = sitk.GetArrayFromImage(ds)
if len(img_array.shape) == 3:
frame_num, width, height = img_array.shape
ch = 1
return img_array, frame_num, width, height, ch
elif len(img_array.shape) == 4:
frame_num, width, height, ch = img_array.shape
return img_array, frame_num, width, height, ch
def loadFileInformation(filename):
information = {}
ds = dicomio.read_file(filename, force=True)
information['dicom_num'] = ds.SOPInstanceUID
# information['PatientID'] = ds.PatientID
# information['PatientName'] = ds.PatientName
# information['PatientBirthDate'] = ds.PatientBirthDate
# information['PatientSex'] = ds.PatientSex
# information['StudyID'] = ds.StudyID
# information['StudyDate'] = ds.StudyDate
# information['StudyTime'] = ds.StudyTime
# information['InstitutionName'] = ds.InstitutionName
# information['Manufacturer'] = ds.Manufacturer
# information['NumberOfFrames'] = ds.NumberOfFrames
return information
def showImage(img_array):
img_array = img_array
img_bitmap = Image.fromarray(img_array)
# img_bitmap.show()
return img_bitmap
def MatrixToImage(data, ch):
# data = (data+1024)*0.125
# new_im = Image.fromarray(data.astype(np.uint8))
# new_im.show()
if ch == 3:
img_rgb = cv2.cvtColor(data, cv2.COLOR_BGR2RGB)
if ch == 1:
data = (data + 1024) * 0.125
img_rgb = data.astype(np.uint8)
return img_rgb
def PETToImage(img_array, color_reversed=True):
info = np.finfo(img_array.dtype)
data = img_array.astype(np.float64) / np.max(img_array)
if color_reversed is True:
data = 255 - 255 * data
elif color_reversed is False:
data = 255 * data
# data = (data + 1024) * 0.125
img = data.astype(np.uint8)
img = np.transpose(img, (1, 2, 0))
# cv2.imshow('test', img)
return img
def dfs_showdir(path, depth):
if depth == 0:
print("root:[" + path + "]")
for item in os.listdir(path):
if '.git' not in item:
print("| " * depth + "+--" + item)
newitem = path +'/'+ item
if os.path.isdir(newitem):
dfs_showdir(newitem, depth +1)
# print(path_list)
def isdir(x):
return os.path.isdir(x) and x != '.svn'
def mkfloders(src,tar):
paths = os.listdir(src)
paths = map(lambda name:os.path.join(src, name), paths)
paths = list(filter(isdir, paths))
if(len(paths)<=0):
return
for i in paths:
(filepath, filename)=os.path.split(i)
targetpath = os.path.join(tar,filename)
not os.path.isdir(targetpath) and os.mkdir(targetpath)
mkfloders(i,targetpath)
def mkdir(path):
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path)
print(path + ' successfully be made!')
return True
else:
print(path + ' the folder already existed!')
return False