[5d12a0]: / ants / segmentation / prior_based_segmentation.py

Download this file

69 lines (48 with data), 2.1 kB

 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
__all__ = ['prior_based_segmentation']
import ants
def prior_based_segmentation(image, priors, mask, priorweight=0.25, mrf=0.1, iterations=25):
"""
Spatial prior-based image segmentation.
Markov random field regularized, prior-based image segmentation that is a
wrapper around atropos (see ANTs and related publications).
ANTsR function: `priorBasedSegmentation`
Arguments
---------
image : ANTsImage or list/tuple of ANTsImage types
input image or image list for multivariate segmentation
priors : list/tuple of ANTsImage types
list of priors that cover the number of classes
mask : ANTsImage
segment inside this mask
prior_weight : scalar
usually 0 (priors used for initialization only), 0.25 or 0.5.
mrf : scalar
regularization, higher is smoother, a numerical value in range 0.0 to 0.2
iterations : integer
maximum number of iterations. could be a large value eg 25.
Returns
-------
dictionary with the following key/value pairs:
`segmentation`: ANTsImage
actually segmented image
`probabilityimages` : list of ANTsImage types
one image for each segmentation class
Example
-------
>>> import ants
>>> fi = ants.image_read(ants.get_ants_data('r16'))
>>> seg = ants.kmeans_segmentation(fi,3)
>>> mask = ants.threshold_image(seg['segmentation'], 1, 1e15)
>>> priorseg = ants.prior_based_segmentation(fi, seg['probabilityimages'], mask, 0.25, 0.1, 3)
"""
if ants.is_image(image):
dim = image.dimension
elif isinstance(image, (tuple,list)) and (ants.is_image(image[0])):
dim = image[0].dimension
else:
raise ValueError('image argument must be ANTsImage or list/tuple of ANTsImage types')
nhood = 'x'.join(['1']*dim)
mrf = '[%s,%s]' % (str(mrf), nhood)
conv = '[%s,0]' % (str(iterations))
pseg = ants.atropos(a=image, m=mrf, c=conv, i=priors, x=mask, priorweight=priorweight)
return pseg