Switch to unified view

a b/.ipynb_checkpoints/lung_segmentation-Copy1-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
   "outputs": [],
54
   "source": [
55
    "data_path = \"resource/\"\n",
56
    "img_name = \"s1.mhd\"\n",
57
    "# img_name = \"lola11-01.mhd\"\n",
58
    "data = LoadData(data_path, img_name)\n",
59
    "data.loaddata()"
60
   ]
61
  },
62
  {
63
   "cell_type": "code",
64
   "execution_count": null,
65
   "metadata": {},
66
   "outputs": [],
67
   "source": [
68
    "data_path = \"resource/\"\n",
69
    "img_name = \"s1map.mhd\"\n",
70
    "labelmap = LoadData(data_path, img_name)\n",
71
    "labelmap.loaddata()"
72
   ]
73
  },
74
  {
75
   "cell_type": "markdown",
76
   "metadata": {},
77
   "source": [
78
    "## Lung Lobes Segmentation\n",
79
    "0. Data Preprocesing"
80
   ]
81
  },
82
  {
83
   "cell_type": "code",
84
   "execution_count": null,
85
   "metadata": {},
86
   "outputs": [],
87
   "source": [
88
    "vs = VesselSegment(original=data.image, closing=labelmap.image)"
89
   ]
90
  },
91
  {
92
   "cell_type": "code",
93
   "execution_count": null,
94
   "metadata": {},
95
   "outputs": [],
96
   "source": [
97
    "print \"   Shrik the region of lung...\"\n",
98
    "vs.erosion(lunglabel=[201, 202])\n",
99
    "\n",
100
    "print \"   Pricessing Generate lung mask...\"\n",
101
    "vs.generate_lung_mask(offset = 1024)\n",
102
    "\n",
103
    "# Write image...\n",
104
    "Lung_mask = sitk.GetImageFromArray(vs.img)\n",
105
    "sitk.WriteImage(Lung_mask, \"Lung_mask.mhd\")\n",
106
    "\n",
107
    "print \"   Processing Downsampling...\"\n",
108
    "vs.downsampling()\n",
109
    "\n",
110
    "print \"   Processing Thresholding...\"\n",
111
    "vs.thresholding(thval=180)\n",
112
    "\n",
113
    "print \"   Processing Region Growing...\"\n",
114
    "vs.max_filter(filter_size=5)\n",
115
    "\n",
116
    "# print \"   Processing Filtering...\"\n",
117
    "# vs.filtering(min_size=500, max_size=1000)"
118
   ]
119
  },
120
  {
121
   "cell_type": "code",
122
   "execution_count": null,
123
   "metadata": {},
124
   "outputs": [],
125
   "source": [
126
    "filtered = sitk.GetImageFromArray(vs.temp_img)\n",
127
    "sitk.WriteImage(filtered, \"filtered.mhd\")"
128
   ]
129
  },
130
  {
131
   "cell_type": "code",
132
   "execution_count": null,
133
   "metadata": {},
134
   "outputs": [],
135
   "source": [
136
    "filtered = sitk.ReadImage(\"filtered.mhd\")\n",
137
    "filtered = sitk.GetArrayFromImage(filtered)\n",
138
    "filtered[filtered > 0] = 1\n",
139
    "binary_filtered = sitk.GetImageFromArray(filtered)\n",
140
    "sitk.WriteImage(binary_filtered, \"binary_filtered.mhd\")"
141
   ]
142
  },
143
  {
144
   "cell_type": "markdown",
145
   "metadata": {
146
    "collapsed": true
147
   },
148
   "source": [
149
    "### Postprocessing for fissure enhancement"
150
   ]
151
  },
152
  {
153
   "cell_type": "code",
154
   "execution_count": null,
155
   "metadata": {},
156
   "outputs": [],
157
   "source": [
158
    "import SimpleITK as sitk\n",
159
    "from read_data import LoadData\n",
160
    "import numpy as np\n",
161
    "import collections\n",
162
    "\n",
163
    "# data = LoadData(path=\"fissure_enhancement_cxx/\", name=\"voxel_val_region_growing_3rd.mhd\")\n",
164
    "data = LoadData(path=\"\", name=\"filtered_rg.mhd\")\n",
165
    "data.loaddata()\n",
166
    "image = sitk.GetArrayFromImage(data.image)"
167
   ]
168
  },
169
  {
170
   "cell_type": "code",
171
   "execution_count": null,
172
   "metadata": {},
173
   "outputs": [],
174
   "source": [
175
    "nonzeros = image[image > 0]\n",
176
    "d = collections.Counter( nonzeros )\n",
177
    "val_key = []\n",
178
    "keys = set([])\n",
179
    "for key, val in d.items():\n",
180
    "    # if val > 1000:\n",
181
    "    if val > 5000:\n",
182
    "        keys.add(key)"
183
   ]
184
  },
185
  {
186
   "cell_type": "code",
187
   "execution_count": null,
188
   "metadata": {},
189
   "outputs": [],
190
   "source": [
191
    "len(keys)"
192
   ]
193
  },
194
  {
195
   "cell_type": "code",
196
   "execution_count": null,
197
   "metadata": {},
198
   "outputs": [],
199
   "source": [
200
    "image[image == 0] = 1\n",
201
    "\n",
202
    "# for p in np.nditer(image, op_flags=['readwrite']):\n",
203
    "#     if p.tolist() in keys:\n",
204
    "#         p[...] = 0\n",
205
    "\n",
206
    "for key in keys:\n",
207
    "    image[image == key] = 0\n",
208
    "\n",
209
    "image[image > 0] = 1\n",
210
    "image[image == 0] = 255\n",
211
    "image[image == 1] = 0"
212
   ]
213
  },
214
  {
215
   "cell_type": "code",
216
   "execution_count": null,
217
   "metadata": {},
218
   "outputs": [],
219
   "source": [
220
    "img = sitk.GetImageFromArray(image.astype(np.uint8))"
221
   ]
222
  },
223
  {
224
   "cell_type": "code",
225
   "execution_count": null,
226
   "metadata": {
227
    "collapsed": true
228
   },
229
   "outputs": [],
230
   "source": [
231
    "sitk.WriteImage(img, \"filtered.mhd\")"
232
   ]
233
  },
234
  {
235
   "cell_type": "code",
236
   "execution_count": null,
237
   "metadata": {
238
    "collapsed": true
239
   },
240
   "outputs": [],
241
   "source": [
242
    "size = 7\n",
243
    "closing = sitk.BinaryMorphologicalClosingImageFilter()\n",
244
    "closing.SetForegroundValue(255)\n",
245
    "closing.SetKernelRadius(size)\n",
246
    "img = closing.Execute(img)"
247
   ]
248
  },
249
  {
250
   "cell_type": "code",
251
   "execution_count": null,
252
   "metadata": {
253
    "collapsed": true
254
   },
255
   "outputs": [],
256
   "source": [
257
    "sitk.WriteImage(img, \"fissure_enhancement_cxx/voxel_val_region_growing_closing.mhd\")"
258
   ]
259
  },
260
  {
261
   "cell_type": "code",
262
   "execution_count": null,
263
   "metadata": {
264
    "collapsed": true
265
   },
266
   "outputs": [],
267
   "source": [
268
    "data = LoadData(path=\"\", name=\"binary_filtered.mhd\")\n",
269
    "data.loaddata()\n",
270
    "vessel = sitk.GetArrayFromImage(data.image)"
271
   ]
272
  },
273
  {
274
   "cell_type": "code",
275
   "execution_count": null,
276
   "metadata": {
277
    "collapsed": true
278
   },
279
   "outputs": [],
280
   "source": [
281
    "fissure = sitk.GetArrayFromImage(img)"
282
   ]
283
  },
284
  {
285
   "cell_type": "code",
286
   "execution_count": null,
287
   "metadata": {
288
    "collapsed": true
289
   },
290
   "outputs": [],
291
   "source": [
292
    "import copy\n",
293
    "fissure_vessel = copy.deepcopy(fissure)\n",
294
    "fissure_vessel[fissure_vessel != 0] = 1\n",
295
    "fissure_vessel[vessel != 0] = 2"
296
   ]
297
  },
298
  {
299
   "cell_type": "code",
300
   "execution_count": null,
301
   "metadata": {
302
    "collapsed": true
303
   },
304
   "outputs": [],
305
   "source": [
306
    "fissure_vessel_itk = sitk.GetImageFromArray(fissure_vessel)"
307
   ]
308
  },
309
  {
310
   "cell_type": "code",
311
   "execution_count": null,
312
   "metadata": {},
313
   "outputs": [],
314
   "source": [
315
    "sitk.WriteImage(fissure_vessel_itk, \"fissure_enhancement_cxx/fissure_vessel.mhd\")"
316
   ]
317
  },
318
  {
319
   "cell_type": "markdown",
320
   "metadata": {},
321
   "source": [
322
    "### Label map"
323
   ]
324
  },
325
  {
326
   "cell_type": "code",
327
   "execution_count": null,
328
   "metadata": {
329
    "collapsed": true
330
   },
331
   "outputs": [],
332
   "source": [
333
    "lung_mask = LoadData(path=\"\", name=\"Lung_mask.mhd\")\n",
334
    "lung_mask.loaddata()\n",
335
    "fissure = LoadData(path=\"fissure_enhancement_cxx/\", name=\"voxel_val_region_growing_closing.mhd\")\n",
336
    "fissure.loaddata()\n",
337
    "vessel = LoadData(path=\"\", name=\"binary_filtered.mhd\")\n",
338
    "vessel.loaddata()"
339
   ]
340
  },
341
  {
342
   "cell_type": "code",
343
   "execution_count": null,
344
   "metadata": {
345
    "collapsed": true
346
   },
347
   "outputs": [],
348
   "source": [
349
    "lung_mask = sitk.GetArrayFromImage(lung_mask.image)\n",
350
    "fissure = sitk.GetArrayFromImage(fissure.image)\n",
351
    "vessel = sitk.GetArrayFromImage(vessel.image)"
352
   ]
353
  },
354
  {
355
   "cell_type": "code",
356
   "execution_count": null,
357
   "metadata": {
358
    "collapsed": true
359
   },
360
   "outputs": [],
361
   "source": [
362
    "lung_mask[lung_mask != 0] = 3\n",
363
    "lung_mask[vessel > 0] = 1\n",
364
    "lung_mask[fissure > 0] = 2"
365
   ]
366
  },
367
  {
368
   "cell_type": "code",
369
   "execution_count": null,
370
   "metadata": {
371
    "collapsed": true
372
   },
373
   "outputs": [],
374
   "source": [
375
    "lung_mask = sitk.GetImageFromArray(lung_mask)\n",
376
    "sitk.WriteImage(lung_mask, \"label_map.mhd\")"
377
   ]
378
  },
379
  {
380
   "cell_type": "markdown",
381
   "metadata": {
382
    "collapsed": true
383
   },
384
   "source": [
385
    "### Fissure Evaluation"
386
   ]
387
  },
388
  {
389
   "cell_type": "code",
390
   "execution_count": null,
391
   "metadata": {
392
    "collapsed": true
393
   },
394
   "outputs": [],
395
   "source": [
396
    "result_dismap = LoadData(path=\"fissure_enhancement_cxx/\", name=\"distmap_voxel_val_rg.mhd\")\n",
397
    "result_dismap.loaddata()\n",
398
    "result_dismap_nda = sitk.GetArrayFromImage(result_dismap.image)"
399
   ]
400
  },
401
  {
402
   "cell_type": "code",
403
   "execution_count": null,
404
   "metadata": {
405
    "collapsed": true
406
   },
407
   "outputs": [],
408
   "source": [
409
    "gt_dismap = LoadData(path=\"fissure_enhancement_cxx/\", name=\"distance_map_gt_fissure.mhd\")\n",
410
    "gt_dismap.loaddata()\n",
411
    "gt_dismap_nda = sitk.GetArrayFromImage(gt_dismap.image)"
412
   ]
413
  },
414
  {
415
   "cell_type": "code",
416
   "execution_count": null,
417
   "metadata": {
418
    "collapsed": true
419
   },
420
   "outputs": [],
421
   "source": [
422
    "import copy"
423
   ]
424
  },
425
  {
426
   "cell_type": "code",
427
   "execution_count": null,
428
   "metadata": {
429
    "collapsed": true
430
   },
431
   "outputs": [],
432
   "source": [
433
    "gt_vals = copy.deepcopy(gt_dismap_nda)\n",
434
    "gt_vals[result_dismap_nda == 0] = 0"
435
   ]
436
  },
437
  {
438
   "cell_type": "code",
439
   "execution_count": null,
440
   "metadata": {
441
    "collapsed": true
442
   },
443
   "outputs": [],
444
   "source": [
445
    "result_vals = copy.deepcopy(result_dismap_nda)\n",
446
    "result_vals[gt_dismap_nda == 0] = 0"
447
   ]
448
  },
449
  {
450
   "cell_type": "code",
451
   "execution_count": null,
452
   "metadata": {
453
    "collapsed": true
454
   },
455
   "outputs": [],
456
   "source": [
457
    "num_total = float(np.count_nonzero(gt_vals) + np.count_nonzero(result_vals))\n",
458
    "mean = float(np.sum(gt_vals) + np.sum(result_vals)) / num_total"
459
   ]
460
  },
461
  {
462
   "cell_type": "code",
463
   "execution_count": null,
464
   "metadata": {},
465
   "outputs": [],
466
   "source": [
467
    "mean * 0.73"
468
   ]
469
  },
470
  {
471
   "cell_type": "code",
472
   "execution_count": null,
473
   "metadata": {
474
    "collapsed": true
475
   },
476
   "outputs": [],
477
   "source": []
478
  }
479
 ],
480
 "metadata": {
481
  "kernelspec": {
482
   "display_name": "Python 2",
483
   "language": "python",
484
   "name": "python2"
485
  },
486
  "language_info": {
487
   "codemirror_mode": {
488
    "name": "ipython",
489
    "version": 2
490
   },
491
   "file_extension": ".py",
492
   "mimetype": "text/x-python",
493
   "name": "python",
494
   "nbconvert_exporter": "python",
495
   "pygments_lexer": "ipython2",
496
   "version": "2.7.13"
497
  }
498
 },
499
 "nbformat": 4,
500
 "nbformat_minor": 1
501
}