-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsegment_mouse.py
84 lines (68 loc) · 2.43 KB
/
segment_mouse.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import numpy
import skimage.segmentation.morphsnakes as sg
import scipy.ndimage.morphology as morph
import skimage.measure as measure
import nibabel as nib
import sys
import numpy as np
import scipy.ndimage.measurements as measurements
import Mouse_C
def cv_model(image,init_mask, num_divisions,num_iterations,start_end):
dt = (start_end[1] - start_end[0])/num_divisions
res = []
for i in range(num_divisions):
mares = Mouse_C.morph_cv(image, iterations=num_iterations, init_level_set=init_mask > 0,lambda1=start_end[0] + i*dt,lambda2=start_end[1]- i*dt)
res.append(mares)
return res
def combine_image(images):
res = None
for i in images:
if res is None:
res = i.astype(int)
else:
res = res + i.astype(int)
res = res / len(images)
return res
if __name__ == "__main__":
im_file = nib.load(sys.argv[1])
im_seg = nib.load(sys.argv[2])
res_path = sys.argv[3]
img = im_file.get_fdata()
treshold = float(sys.argv[4])
iterations = int(sys.argv[5])
cent = measurements.center_of_mass(img)
cent = [round(x) for x in cent]
moms = measure.moments(img,3)
imares = np.zeros(img.shape)
xm = cent[0]
dx = round(img.shape[0]/20)
if dx < 1:
dx = 2
ym = cent[1]
dy = round(img.shape[1]/20)
if dy < 1:
dy = 2
zm = cent[2]
dz = round(img.shape[2]/20)
if dz < 1:
dz = 2
imares[:, :, :] = 0
# imares[xm - 20:xm + 20, ym - 20:ym + 20, zm - 2:zm + 2] = 1
a = numpy.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
#Mouse_C.test_method()
imares[xm - dx:xm + dx, ym - dy:ym + dy, zm - dz:zm + dz] = 1
imares = Mouse_C.morph_cv(img, iterations=25, init_level_set=imares > 0, lambda2=2)
images = cv_model(img,init_mask=imares,num_divisions=10,num_iterations=100,start_end=[0.5,4])
imares = combine_image(images)
nif = nib.Nifti1Image(imares, im_file.affine)
nib.save(nif, res_path + '/combined.nii.gz')
imares = imares > treshold
imares = Mouse_C.morph_cv(img, iterations=2, init_level_set=imares > 0)
imares = morph.binary_fill_holes(imares)
imares = morph.binary_dilation(imares,iterations=iterations)
imares = morph.binary_fill_holes(imares)
imares = morph.binary_erosion(imares,iterations=iterations)
imares = imares.astype(np.int8)
nif = nib.Nifti1Image(imares, im_file.affine)
nib.save(nif, res_path + '/output.nii.gz')
print(1)