-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPaaS_utils.py
195 lines (149 loc) Β· 5.64 KB
/
IPaaS_utils.py
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
184
185
186
187
188
189
190
191
192
193
194
195
import shutil
import os
import cv2
import numpy as np
import dicom
from dicom import dataelem
import pickle
have_PIL = True
try:
import PIL.Image
import PIL.ImageOps as ops
except ImportError:
have_PIL = False
have_numpy = True
try:
import numpy as np
except ImportError:
have_numpy = False
def image_computing_edges(filename):
filename = './images/' + filename
img = cv2.imread(filename, 0)
edges = cv2.Canny(img, 50, 25)
cv2.imwrite(filename[:-4] + '_cv.png', edges)
def image_computing_watershed(filename, k=30, dist=0.1):
#filename = '/home/hugo/PycharmProjects/IPaaS/images/' + filename
print(filename)
#img = cv2.imread(filename, 0)
gray = cv2.imread(filename, 0)
#gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#ret, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
ret, thresh = cv2.threshold(gray, 230, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# noise removal
kernel = np.ones((k, k), np.uint8)
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)
# sure background area
sure_bg = cv2.dilate(opening, kernel, iterations=3)
# Finding sure foreground area
dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5)
ret, sure_fg = cv2.threshold(dist_transform, dist * dist_transform.max(), 255, 0)
# Finding unknown region
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg, sure_fg)
# Marker labelling
ret, markers = cv2.connectedComponents(sure_fg)
# Add one to all labels so that sure background is not 0, but 1
markers = markers + 1
# Now, mark the region of unknown with zero
markers[unknown == 255] = 0
#####attention here
height, width = gray.shape
img = np.zeros((height,width,3), np.uint8)
cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB, img)
#invert img
#img = cv2.bitwise_not(img)
markers = cv2.watershed(img, markers)
img[markers == -1] = [255, 0, 0]
nf = filename[:-4] + '_cv.png'
cv2.imwrite(nf, img)
return nf
def get_LUT_value(data, window, level):
"""Apply the RGB Look-Up Table for the given
data and window/level value."""
if not have_numpy:
raise ImportError("Numpy is not available."
"See http://numpy.scipy.org/"
"to download and install")
return np.piecewise(data,
[data <= (level - 0.5 - (window - 1) / 2),
data > (level - 0.5 + (window - 1) / 2)],
[0, 255, lambda data: ((data - (level - 0.5)) /
(window - 1) + 0.5) * (255 - 0)])
def get_PIL_image(dataset):
"""Get Image object from Python Imaging Library(PIL)"""
if not have_PIL:
raise ImportError("Python Imaging Library is not available. "
"See http://www.pythonware.com/products/pil/ "
"to download and install")
if ('PixelData' not in dataset):
raise TypeError("Cannot show image -- DICOM dataset does not have "
"pixel data")
# can only apply LUT if these window info exists
if ('WindowWidth' not in dataset) or ('WindowCenter' not in dataset):
bits = dataset.BitsAllocated
samples = dataset.SamplesPerPixel
if bits == 8 and samples == 1:
mode = "L"
elif bits == 8 and samples == 3:
mode = "RGB"
elif bits == 16:
# not sure about this -- PIL source says is 'experimental'
# and no documentation. Also, should bytes swap depending
# on endian of file and system??
mode = "I;16"
else:
raise TypeError("Don't know PIL mode for %d BitsAllocated "
"and %d SamplesPerPixel" % (bits, samples))
# PIL size = (width, height)
size = (dataset.Columns, dataset.Rows)
# Recommended to specify all details
# by http://www.pythonware.com/library/pil/handbook/image.htm
im = PIL.Image.frombuffer(mode, size, dataset.PixelData,
"raw", mode, 0, 1)
else:
image = get_LUT_value(dataset.pixel_array, dataset.WindowWidth,
dataset.WindowCenter)
# Convert mode to L since LUT has only 256 values:
# http://www.pythonware.com/library/pil/handbook/image.htm
im = PIL.Image.fromarray(image).convert('L')
return im
def show_PIL(dataset):
"""Display an image using the Python Imaging Library (PIL)"""
im = get_PIL_image(dataset)
im.show()
def save_PIL(filename, dataset):
nf = filename[:-8]+'.png'
im = get_PIL_image(dataset)
im = im.convert(mode='I')
im.save(nf)
return nf
def save_segmentation(dcm_filename, seg_img_filename):
seg_dcm_fl = dcm_filename[:-8] + '_seg.dcm'
img = cv2.imread(seg_img_filename)
i = pickle.dumps(img)
ds = dicom.read_file(dcm_filename)
de = dataelem.DataElement(0x00690069, 'UN', i)
ds.add(de)
dicom.write_file(seg_dcm_fl, ds)
return seg_dcm_fl
def read_seg(dcm_filename):
ds = dicom.read_file(dcm_filename)
img = ds[0x00690069].value
i = pickle.loads(img)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', i)
cv2.waitKey(0)
cv2.destroyAllWindows()
newf = dcm_filename+ '_convert.png'
cv2.imwrite(newf, i)
def read_dicom(f):
return dicom.read_file(f, force=True)
#f = "./images/mammo_new.dcm"
#ds = dicom.read_file(f, force=True)
#show_PIL(ds)
#nf=save_PIL(f, ds)
#nf='./images/mammo_pil_tmp.png'
#print(nf)
#nnf = image_computing_watershed(nf)
#seg_dcm = save_segmentation(f, nnf)
#read_seg(seg_dcm)