Switch to unified view

a b/.ipynb_checkpoints/lung_segmentation-checkpoint.ipynb
1
{
2
 "cells": [
3
  {
4
   "cell_type": "markdown",
5
   "metadata": {},
6
   "source": [
7
    "# Lung Lobes Segmentation"
8
   ]
9
  },
10
  {
11
   "cell_type": "markdown",
12
   "metadata": {},
13
   "source": [
14
    "## Imports"
15
   ]
16
  },
17
  {
18
   "cell_type": "code",
19
   "execution_count": null,
20
   "metadata": {},
21
   "outputs": [],
22
   "source": [
23
    "%matplotlib inline\n",
24
    "\n",
25
    "import SimpleITK as sitk\n",
26
    "import numpy as np\n",
27
    "import matplotlib.pyplot as plt\n",
28
    "import scipy as sp \n",
29
    "import gui\n",
30
    "import cv2\n",
31
    "import matplotlib.image as mpimg\n",
32
    "\n",
33
    "# from mayavi import mlab\n",
34
    "from scipy import signal\n",
35
    "from myshow import myshow, myshow3d\n",
36
    "from read_data import LoadData\n",
37
    "from lung_segment import LungSegment\n",
38
    "from vessel_segment import VesselSegment\n",
39
    "from mpl_toolkits.mplot3d import Axes3D"
40
   ]
41
  },
42
  {
43
   "cell_type": "markdown",
44
   "metadata": {},
45
   "source": [
46
    "## Read data"
47
   ]
48
  },
49
  {
50
   "cell_type": "code",
51
   "execution_count": null,
52
   "metadata": {
53
    "collapsed": true
54
   },
55
   "outputs": [],
56
   "source": [
57
    "# loading data\n",
58
    "data_path = \"resource/\"\n",
59
    "img_name = \"lola11-01.mhd\"\n",
60
    "data = LoadData(data_path, img_name)\n",
61
    "data.loaddata()"
62
   ]
63
  },
64
  {
65
   "cell_type": "code",
66
   "execution_count": null,
67
   "metadata": {
68
    "collapsed": true
69
   },
70
   "outputs": [],
71
   "source": [
72
    "print \"the shape of image is \", data.image.GetSize()"
73
   ]
74
  },
75
  {
76
   "cell_type": "markdown",
77
   "metadata": {},
78
   "source": [
79
    "## Lung Segmentation\n",
80
    "Rescale the intensities and map them to [0,255]"
81
   ]
82
  },
83
  {
84
   "cell_type": "code",
85
   "execution_count": null,
86
   "metadata": {},
87
   "outputs": [],
88
   "source": [
89
    "% matplotlib notebook\n",
90
    "\n",
91
    "WINDOW_LEVEL = (1050,500)\n",
92
    "ls = LungSegment(data.image)\n",
93
    "\n",
94
    "# Convert image to uint8 for showing \n",
95
    "ls.conv_2_uint8(WINDOW_LEVEL)\n",
96
    "\n",
97
    "# Set the seed point manually...\n",
98
    "seed_pts = [(125,237,200), (369,237,200)]\n",
99
    "\n",
100
    "# Compute region growing\n",
101
    "ls.regiongrowing(seed_pts)\n",
102
    "\n",
103
    "# showimg image\n",
104
    "ls.image_showing(\"Region Growing Result\")"
105
   ]
106
  },
107
  {
108
   "cell_type": "markdown",
109
   "metadata": {},
110
   "source": [
111
    "Write the region growing image"
112
   ]
113
  },
114
  {
115
   "cell_type": "code",
116
   "execution_count": null,
117
   "metadata": {
118
    "collapsed": true
119
   },
120
   "outputs": [],
121
   "source": [
122
    "sitk.WriteImage(ls.temp_img, \"seg_implicit_thresholds.mhd\")"
123
   ]
124
  },
125
  {
126
   "cell_type": "code",
127
   "execution_count": null,
128
   "metadata": {},
129
   "outputs": [],
130
   "source": [
131
    "# Morphological Operatinon (Closing)\n",
132
    "ls.image_closing(7)\n",
133
    "\n",
134
    "# write image\n",
135
    "sitk.WriteImage(ls.temp_img, \"img_closing.mhd\")"
136
   ]
137
  },
138
  {
139
   "cell_type": "code",
140
   "execution_count": null,
141
   "metadata": {},
142
   "outputs": [],
143
   "source": [
144
    "img_closing = sitk.ReadImage(\"img_closing.mhd\") # reading the existed closing image \n",
145
    "\n",
146
    "# get the numpy array of the 3D closing image for future using\n",
147
    "img_closing_ndarray = sitk.GetArrayFromImage(img_closing)"
148
   ]
149
  },
150
  {
151
   "cell_type": "markdown",
152
   "metadata": {},
153
   "source": [
154
    "## Vasculature Segmentation"
155
   ]
156
  },
157
  {
158
   "cell_type": "code",
159
   "execution_count": null,
160
   "metadata": {},
161
   "outputs": [],
162
   "source": [
163
    "# get the result of previous lung segmentation.\n",
164
    "img_closing_ndarray = sitk.GetImageFromArray(img_closing_ndarray)"
165
   ]
166
  },
167
  {
168
   "cell_type": "code",
169
   "execution_count": null,
170
   "metadata": {},
171
   "outputs": [],
172
   "source": [
173
    "vs = VesselSegment(original=data.image, closing=img_closing_ndarray)"
174
   ]
175
  },
176
  {
177
   "cell_type": "code",
178
   "execution_count": null,
179
   "metadata": {},
180
   "outputs": [],
181
   "source": [
182
    "print \"   Pricessing Generate lung mask...\"\n",
183
    "vs.generate_lung_mask(lunglabel=[1,-5000], offset = 0)\n",
184
    "\n",
185
    "# Write image...\n",
186
    "Lung_mask = sitk.GetImageFromArray(vs.img)\n",
187
    "sitk.WriteImage(Lung_mask, \"Lung_mask.mhd\")\n",
188
    "\n",
189
    "print \"   Processing Downsampling...\"\n",
190
    "vs.downsampling()\n",
191
    "\n",
192
    "print \"   Processing Thresholding...\"\n",
193
    "vs.thresholding(thval=180)\n",
194
    "down = sitk.GetImageFromArray(vs.temp_img)\n",
195
    "sitk.WriteImage(down, \"downsample.mhd\")\n",
196
    "\n",
197
    "print \"   Processing Region Growing...\"\n",
198
    "vs.max_filter(filter_size=5)"
199
   ]
200
  },
201
  {
202
   "cell_type": "code",
203
   "execution_count": null,
204
   "metadata": {},
205
   "outputs": [],
206
   "source": [
207
    "# save the vasculature-segmented image\n",
208
    "filtered = sitk.GetImageFromArray(vs.temp_img)\n",
209
    "sitk.WriteImage(filtered, \"filtered.mhd\")"
210
   ]
211
  },
212
  {
213
   "cell_type": "code",
214
   "execution_count": null,
215
   "metadata": {},
216
   "outputs": [],
217
   "source": [
218
    "# convert to binary image\n",
219
    "filtered = sitk.ReadImage(\"filtered.mhd\")\n",
220
    "filtered = sitk.GetArrayFromImage(filtered)\n",
221
    "filtered[filtered > 0] = 1\n",
222
    "binary_filtered = sitk.GetImageFromArray(filtered)\n",
223
    "sitk.WriteImage(binary_filtered, \"binary_filtered.mhd\")"
224
   ]
225
  },
226
  {
227
   "cell_type": "markdown",
228
   "metadata": {
229
    "collapsed": true
230
   },
231
   "source": [
232
    "## Postprocessing for fissure enhancement\n",
233
    "**Note:** the following steps need the result of fissure segmentation obtained by the C++ codes I provide. Since the SimpleITK package didn't provide enough functions for fissure segmentation (like computing 3D Hessian matrix), I used ITK C++ for this part, instead."
234
   ]
235
  },
236
  {
237
   "cell_type": "code",
238
   "execution_count": null,
239
   "metadata": {},
240
   "outputs": [],
241
   "source": [
242
    "import SimpleITK as sitk\n",
243
    "from read_data import LoadData\n",
244
    "import numpy as np\n",
245
    "import collections\n",
246
    "\n",
247
    "# Load the fissure image\n",
248
    "data = LoadData(path=\"fissure_enhancement_cxx/\", name=\"vessel_rg.mhd\")\n",
249
    "data.loaddata()\n",
250
    "image = sitk.GetArrayFromImage(data.image)"
251
   ]
252
  },
253
  {
254
   "cell_type": "code",
255
   "execution_count": null,
256
   "metadata": {},
257
   "outputs": [],
258
   "source": [
259
    "# count the volume for each label and remove the ones less than 5000.\n",
260
    "nonzeros = image[image > 0]\n",
261
    "d = collections.Counter( nonzeros )\n",
262
    "val_key = []\n",
263
    "keys = set([])\n",
264
    "for key, val in d.items():\n",
265
    "    if val > 5000:\n",
266
    "        keys.add(key)\n",
267
    "\n",
268
    "image[image == 0] = 1\n",
269
    "for key in keys:\n",
270
    "    image[image == key] = 0\n",
271
    "\n",
272
    "image[image > 0] = 2\n",
273
    "image[image == 0] = 1 # the regions left are set to 1\n",
274
    "image[image == 2] = 0 # rest is 0\n",
275
    "img = sitk.GetImageFromArray(image.astype(np.uint8))"
276
   ]
277
  },
278
  {
279
   "cell_type": "code",
280
   "execution_count": null,
281
   "metadata": {
282
    "collapsed": true
283
   },
284
   "outputs": [],
285
   "source": [
286
    "# Using closing to fill holes\n",
287
    "size = 7\n",
288
    "closing = sitk.BinaryMorphologicalClosingImageFilter()\n",
289
    "closing.SetForegroundValue(255)\n",
290
    "closing.SetKernelRadius(size)\n",
291
    "img = closing.Execute(img)\n",
292
    "# save results\n",
293
    "sitk.WriteImage(img, \"fissure_enhancement_cxx/voxel_val_region_growing_closing.mhd\")"
294
   ]
295
  },
296
  {
297
   "cell_type": "markdown",
298
   "metadata": {},
299
   "source": [
300
    "## Generate Label map for lung, vasculature and fissure regions"
301
   ]
302
  },
303
  {
304
   "cell_type": "code",
305
   "execution_count": null,
306
   "metadata": {
307
    "collapsed": true
308
   },
309
   "outputs": [],
310
   "source": [
311
    "lung_mask = LoadData(path=\"\", name=\"Lung_mask.mhd\")\n",
312
    "lung_mask.loaddata()\n",
313
    "fissure = LoadData(path=\"fissure_enhancement_cxx/\", name=\"voxel_val_region_growing_closing.mhd\")\n",
314
    "fissure.loaddata()\n",
315
    "vessel = LoadData(path=\"\", name=\"binary_filtered.mhd\")\n",
316
    "vessel.loaddata()"
317
   ]
318
  },
319
  {
320
   "cell_type": "code",
321
   "execution_count": null,
322
   "metadata": {
323
    "collapsed": true
324
   },
325
   "outputs": [],
326
   "source": [
327
    "lung_mask = sitk.GetArrayFromImage(lung_mask.image)\n",
328
    "fissure = sitk.GetArrayFromImage(fissure.image)\n",
329
    "vessel = sitk.GetArrayFromImage(vessel.image)"
330
   ]
331
  },
332
  {
333
   "cell_type": "code",
334
   "execution_count": null,
335
   "metadata": {
336
    "collapsed": true
337
   },
338
   "outputs": [],
339
   "source": [
340
    "lung_mask[lung_mask != 0] = 3\n",
341
    "lung_mask[vessel > 0] = 1\n",
342
    "lung_mask[fissure > 0] = 2"
343
   ]
344
  },
345
  {
346
   "cell_type": "code",
347
   "execution_count": null,
348
   "metadata": {
349
    "collapsed": true
350
   },
351
   "outputs": [],
352
   "source": [
353
    "lung_mask = sitk.GetImageFromArray(lung_mask)\n",
354
    "sitk.WriteImage(lung_mask, \"label_map.mhd\")"
355
   ]
356
  }
357
 ],
358
 "metadata": {
359
  "kernelspec": {
360
   "display_name": "Python 2",
361
   "language": "python",
362
   "name": "python2"
363
  },
364
  "language_info": {
365
   "codemirror_mode": {
366
    "name": "ipython",
367
    "version": 2
368
   },
369
   "file_extension": ".py",
370
   "mimetype": "text/x-python",
371
   "name": "python",
372
   "nbconvert_exporter": "python",
373
   "pygments_lexer": "ipython2",
374
   "version": "2.7.13"
375
  }
376
 },
377
 "nbformat": 4,
378
 "nbformat_minor": 1
379
}