|
a |
|
b/functions.py |
|
|
1 |
import cv2 |
|
|
2 |
import numpy as np |
|
|
3 |
import os |
|
|
4 |
import dicom |
|
|
5 |
from pylab import * |
|
|
6 |
from mpl_toolkits.mplot3d import Axes3D |
|
|
7 |
slices=[] |
|
|
8 |
def dicomRead(path): |
|
|
9 |
|
|
|
10 |
for dirName, subdirList, fileList in os.walk(path): |
|
|
11 |
for filename in fileList: |
|
|
12 |
if ".dcm" in filename.lower(): |
|
|
13 |
slices.append(dicom.read_file(os.path.join(dirName, filename))) |
|
|
14 |
slices.sort(key=lambda x: int(x.ImagePositionPatient[2])) |
|
|
15 |
try: |
|
|
16 |
slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2]) |
|
|
17 |
except: |
|
|
18 |
slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation) |
|
|
19 |
|
|
|
20 |
for s in slices: |
|
|
21 |
s.SliceThickness = slice_thickness |
|
|
22 |
image = np.stack([s.pixel_array for s in slices]) |
|
|
23 |
return image[0:255,:,:] |
|
|
24 |
def floodfill(image, start_point, value): |
|
|
25 |
height, width = image.shape[:2] |
|
|
26 |
points = [start_point] |
|
|
27 |
flag = [[0 for j in range(width)] for i in range(height)] |
|
|
28 |
flag[start_point[0]][start_point[1]] = 1 |
|
|
29 |
origin_value = image[start_point[0]][start_point[1]] |
|
|
30 |
while len(points) > 0: |
|
|
31 |
pt = points.pop(0) |
|
|
32 |
dx = [0, 1, 0, -1] |
|
|
33 |
dy = [1, 0, -1, 0] |
|
|
34 |
for x, y in zip(dx, dy): |
|
|
35 |
if (0 <= pt[0] + x < height and 0 <= pt[1] + y < width and |
|
|
36 |
origin_value == image[pt[0] + x][pt[1] + y] and |
|
|
37 |
flag[pt[0] + x][pt[1] + y] == 0): |
|
|
38 |
flag[pt[0] + x][pt[1] + y] = 1 |
|
|
39 |
points.append((pt[0] + x, pt[1] + y)) |
|
|
40 |
image[pt[0]][pt[1]] = value |
|
|
41 |
return image |
|
|
42 |
|
|
|
43 |
|
|
|
44 |
def switch_pixels(image, origin_value, value): |
|
|
45 |
for i in range(image.shape[0]): |
|
|
46 |
for j in range(image.shape[1]): |
|
|
47 |
if image[i][j] == origin_value: |
|
|
48 |
image[i][j] = value |
|
|
49 |
return image |
|
|
50 |
|
|
|
51 |
def morphology_open(image): |
|
|
52 |
# morphology open operation |
|
|
53 |
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) |
|
|
54 |
image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel) |
|
|
55 |
return image |
|
|
56 |
|
|
|
57 |
def segment(image): |
|
|
58 |
threashold=625 |
|
|
59 |
mask = np.copy(image) |
|
|
60 |
|
|
|
61 |
mask = morphology_open(mask) |
|
|
62 |
ret, mask = cv2.threshold(mask, threashold, 255, cv2.THRESH_BINARY_INV) |
|
|
63 |
|
|
|
64 |
# set margin to black |
|
|
65 |
h, w = mask.shape[:2] |
|
|
66 |
for i in range(h): |
|
|
67 |
for j in range(w): |
|
|
68 |
if ((i == 0 or j == 0 or i == h - 1 or j == w - 1) and |
|
|
69 |
mask[i][j] != 0): |
|
|
70 |
mask = floodfill(mask, (i, j), 0) |
|
|
71 |
|
|
|
72 |
# fill holes in middle |
|
|
73 |
mask = floodfill(mask, (0, 0), -1) |
|
|
74 |
mask = switch_pixels(mask, 0, 255) |
|
|
75 |
mask = switch_pixels(mask, -1, 0) |
|
|
76 |
return mask |
|
|
77 |
|
|
|
78 |
|
|
|
79 |
def test3D(ConstPixelDims = None): |
|
|
80 |
xx, yy = np.meshgrid(np.linspace(0, 1, 512), np.linspace(0, 1, 512)) |
|
|
81 |
X = xx |
|
|
82 |
Y = yy |
|
|
83 |
#Z = i |
|
|
84 |
ax2 = gca(projection='3d') |
|
|
85 |
off = 4000 / (ConstPixelDims[2] - 1) |
|
|
86 |
for i in range(ConstPixelDims[2]): # ConstPixelDims[2] |
|
|
87 |
tempImage = ArrayDicom[:, :, i] |
|
|
88 |
tempImage = np.ma.masked_where(tempImage < 1200, tempImage) |
|
|
89 |
print("i : " + str(i)) |
|
|
90 |
print("tempImage.shape[0] " + str(tempImage.shape[0])) |
|
|
91 |
print("tempImage.shape[1] " + str(tempImage.shape[1])) |
|
|
92 |
x, y = ogrid[0:tempImage.shape[0], 0:tempImage.shape[1]] |
|
|
93 |
##print("x : " + str(x) + " :: y : " + str(y)) |
|
|
94 |
Z = 10 * np.ones(X.shape) |
|
|
95 |
ax = gca(projection='3d') |
|
|
96 |
|
|
|
97 |
# ax.plot_surface(x, y, Z, rstride=5, cstride=5, facecolors=tempImage,cmap='gray' ) |
|
|
98 |
|
|
|
99 |
ax2.contourf(X, Y, tempImage, zdir='z', offset=i * off, antialiased=True) |
|
|
100 |
|
|
|
101 |
# ax2.set_zlim((0.,1.)) |
|
|
102 |
|
|
|
103 |
show() |