502 lines (501 with data), 10.9 kB
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lung Lobes Segmentation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Imports"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"\n",
"import SimpleITK as sitk\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import scipy as sp \n",
"import gui\n",
"import cv2\n",
"import matplotlib.image as mpimg\n",
"\n",
"# from mayavi import mlab\n",
"from scipy import signal\n",
"from myshow import myshow, myshow3d\n",
"from read_data import LoadData\n",
"from lung_segment import LungSegment\n",
"from vessel_segment import VesselSegment\n",
"from mpl_toolkits.mplot3d import Axes3D"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Read data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data_path = \"resource/\"\n",
"img_name = \"s1.mhd\"\n",
"# img_name = \"lola11-01.mhd\"\n",
"data = LoadData(data_path, img_name)\n",
"data.loaddata()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data_path = \"resource/\"\n",
"img_name = \"s1map.mhd\"\n",
"labelmap = LoadData(data_path, img_name)\n",
"labelmap.loaddata()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lung Lobes Segmentation\n",
"0. Data Preprocesing"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vs = VesselSegment(original=data.image, closing=labelmap.image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print \" Shrik the region of lung...\"\n",
"vs.erosion(lunglabel=[201, 202])\n",
"\n",
"print \" Pricessing Generate lung mask...\"\n",
"vs.generate_lung_mask(offset = 1024)\n",
"\n",
"# Write image...\n",
"Lung_mask = sitk.GetImageFromArray(vs.img)\n",
"sitk.WriteImage(Lung_mask, \"Lung_mask.mhd\")\n",
"\n",
"print \" Processing Downsampling...\"\n",
"vs.downsampling()\n",
"\n",
"print \" Processing Thresholding...\"\n",
"vs.thresholding(thval=180)\n",
"\n",
"print \" Processing Region Growing...\"\n",
"vs.max_filter(filter_size=5)\n",
"\n",
"# print \" Processing Filtering...\"\n",
"# vs.filtering(min_size=500, max_size=1000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"filtered = sitk.GetImageFromArray(vs.temp_img)\n",
"sitk.WriteImage(filtered, \"filtered.mhd\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"filtered = sitk.ReadImage(\"filtered.mhd\")\n",
"filtered = sitk.GetArrayFromImage(filtered)\n",
"filtered[filtered > 0] = 1\n",
"binary_filtered = sitk.GetImageFromArray(filtered)\n",
"sitk.WriteImage(binary_filtered, \"binary_filtered.mhd\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"### Postprocessing for fissure enhancement"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import SimpleITK as sitk\n",
"from read_data import LoadData\n",
"import numpy as np\n",
"import collections\n",
"\n",
"# data = LoadData(path=\"fissure_enhancement_cxx/\", name=\"voxel_val_region_growing_3rd.mhd\")\n",
"data = LoadData(path=\"\", name=\"filtered_rg.mhd\")\n",
"data.loaddata()\n",
"image = sitk.GetArrayFromImage(data.image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"nonzeros = image[image > 0]\n",
"d = collections.Counter( nonzeros )\n",
"val_key = []\n",
"keys = set([])\n",
"for key, val in d.items():\n",
" # if val > 1000:\n",
" if val > 5000:\n",
" keys.add(key)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(keys)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"image[image == 0] = 1\n",
"\n",
"# for p in np.nditer(image, op_flags=['readwrite']):\n",
"# if p.tolist() in keys:\n",
"# p[...] = 0\n",
"\n",
"for key in keys:\n",
" image[image == key] = 0\n",
"\n",
"image[image > 0] = 1\n",
"image[image == 0] = 255\n",
"image[image == 1] = 0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"img = sitk.GetImageFromArray(image.astype(np.uint8))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"sitk.WriteImage(img, \"filtered.mhd\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"size = 7\n",
"closing = sitk.BinaryMorphologicalClosingImageFilter()\n",
"closing.SetForegroundValue(255)\n",
"closing.SetKernelRadius(size)\n",
"img = closing.Execute(img)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"sitk.WriteImage(img, \"fissure_enhancement_cxx/voxel_val_region_growing_closing.mhd\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data = LoadData(path=\"\", name=\"binary_filtered.mhd\")\n",
"data.loaddata()\n",
"vessel = sitk.GetArrayFromImage(data.image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"fissure = sitk.GetArrayFromImage(img)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import copy\n",
"fissure_vessel = copy.deepcopy(fissure)\n",
"fissure_vessel[fissure_vessel != 0] = 1\n",
"fissure_vessel[vessel != 0] = 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"fissure_vessel_itk = sitk.GetImageFromArray(fissure_vessel)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sitk.WriteImage(fissure_vessel_itk, \"fissure_enhancement_cxx/fissure_vessel.mhd\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Label map"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"lung_mask = LoadData(path=\"\", name=\"Lung_mask.mhd\")\n",
"lung_mask.loaddata()\n",
"fissure = LoadData(path=\"fissure_enhancement_cxx/\", name=\"voxel_val_region_growing_closing.mhd\")\n",
"fissure.loaddata()\n",
"vessel = LoadData(path=\"\", name=\"binary_filtered.mhd\")\n",
"vessel.loaddata()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"lung_mask = sitk.GetArrayFromImage(lung_mask.image)\n",
"fissure = sitk.GetArrayFromImage(fissure.image)\n",
"vessel = sitk.GetArrayFromImage(vessel.image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"lung_mask[lung_mask != 0] = 3\n",
"lung_mask[vessel > 0] = 1\n",
"lung_mask[fissure > 0] = 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"lung_mask = sitk.GetImageFromArray(lung_mask)\n",
"sitk.WriteImage(lung_mask, \"label_map.mhd\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"### Fissure Evaluation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"result_dismap = LoadData(path=\"fissure_enhancement_cxx/\", name=\"distmap_voxel_val_rg.mhd\")\n",
"result_dismap.loaddata()\n",
"result_dismap_nda = sitk.GetArrayFromImage(result_dismap.image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"gt_dismap = LoadData(path=\"fissure_enhancement_cxx/\", name=\"distance_map_gt_fissure.mhd\")\n",
"gt_dismap.loaddata()\n",
"gt_dismap_nda = sitk.GetArrayFromImage(gt_dismap.image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import copy"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"gt_vals = copy.deepcopy(gt_dismap_nda)\n",
"gt_vals[result_dismap_nda == 0] = 0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"result_vals = copy.deepcopy(result_dismap_nda)\n",
"result_vals[gt_dismap_nda == 0] = 0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"num_total = float(np.count_nonzero(gt_vals) + np.count_nonzero(result_vals))\n",
"mean = float(np.sum(gt_vals) + np.sum(result_vals)) / num_total"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mean * 0.73"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 1
}