-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathliver_segm.py
66 lines (53 loc) · 2.2 KB
/
liver_segm.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
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 21 16:19:07 2021
@author: Katarina Milicevic, School of Electrical Engineering
Belgrade, Serbia
Segmentation of liver and spleen
"""
import SimpleITK as sitk
import numpy as np
import os
import scipy.ndimage as ndi
def show_hist(main_dir):
img = sitk.ReadImage(main_dir + "/data/vein_phase_preprocessed.mha")
img_array = sitk.GetArrayFromImage(img)
volume_hist = ndi.histogram(img_array, min=0, max=255, bins=256)
return volume_hist
def main(main_dir, thres_min = 120, thres_max = 126):
print("Segmentacija zapocela")
img = sitk.ReadImage(main_dir + "/data/vein_phase_preprocessed.mha")
img_array = sitk.GetArrayFromImage(img)
z_top = 0
z_bottom = 3*img_array.shape[0]//5 #img_array.shape[0]-1
try:
heart_sitk = sitk.ReadImage(main_dir + "/segmentation results/heart.mhd")
heart_array = sitk.GetArrayFromImage(heart_sitk)
bones_sitk = sitk.ReadImage(main_dir + "/segmentation results/bones.mhd")
bones_array = sitk.GetArrayFromImage(bones_sitk)
print("1")
heart_spine = heart_array | bones_array
print("2")
only_liver_and_spleen = np.where(heart_spine==0, img_array, 0)
print("3")
bin_im = (only_liver_and_spleen<thres_max)*(only_liver_and_spleen>thres_min)
print("4")
liver = np.zeros(only_liver_and_spleen.shape, dtype = 'uint8')
print("5")
for z in range(z_top, z_bottom+1):
liver[z,:,:] = bin_im[z,:,:]
print("6")
liver_sitk = sitk.GetImageFromArray(liver)
liver_sitk.SetSpacing(img.GetSpacing())
cleaned_thresh_img = sitk.BinaryOpeningByReconstruction(liver_sitk, [4, 4, 4])
filter = sitk.BinaryDilateImageFilter()
dilated = filter.Execute(cleaned_thresh_img)
liver_sitk = sitk.BinaryClosingByReconstruction(dilated, [3, 3, 3])
segm_dir = os.path.join(main_dir, "segmentation results")
if not os.path.exists(segm_dir):
os.makedirs(segm_dir)
sitk.WriteImage(liver_sitk, segm_dir + "/liver_and_spleen.mhd")
print("Segmentacija zavrsena")
return 1
except:
return 0