3
3
# @Author: oesteban
4
4
# @Date: 2014-09-01 10:33:35
5
5
# @Last Modified by: oesteban
6
- # @Last Modified time: 2014-09-02 16:00:36
6
+ # @Last Modified time: 2014-09-03 15:07:46
7
7
from nipype .interfaces .base import (traits , TraitedSpec , BaseInterface ,
8
8
File , isdefined )
9
9
from nipype .utils .filemanip import split_filename
@@ -95,6 +95,8 @@ class DenoiseInputSpec(TraitedSpec):
95
95
desc = ('noise distribution model' ))
96
96
noise_mask = File (desc = ('mask in which the standard deviation of noise '
97
97
'will be computed' ), exists = True )
98
+ patch_radius = traits .Int (1 , desc = 'patch radius' )
99
+ block_radius = traits .Int (5 , desc = 'block_radius' )
98
100
99
101
100
102
class DenoiseOutputSpec (TraitedSpec ):
@@ -127,19 +129,28 @@ class Denoise(BaseInterface):
127
129
def _run_interface (self , runtime ):
128
130
out_file = op .abspath (self ._gen_outfilename ())
129
131
130
- mask = None
132
+ settings = dict (mask = None ,
133
+ rician = (self .inputs .noise_model == 'rician' ))
134
+
131
135
if isdefined (self .inputs .in_mask ):
132
- mask = nb .load (self .inputs .in_mask ).get_data ()
136
+ settings ['mask' ] = nb .load (self .inputs .in_mask ).get_data ()
137
+
138
+ if isdefined (self .inputs .patch_radius ):
139
+ settings ['patch_radius' ] = self .inputs .patch_radius
140
+
141
+ if isdefined (self .inputs .block_radius ):
142
+ settings ['block_radius' ] = self .inputs .block_radius
133
143
134
144
noise_mask = None
135
145
if isdefined (self .inputs .in_mask ):
136
146
noise_mask = nb .load (self .inputs .noise_mask ).get_data ()
137
147
138
- nlmeans_proxy (self .inputs .in_file , in_mask = mask ,
139
- noise_mask = noise_mask ,
140
- rician = (self .inputs .noise_model == 'rician' ),
141
- out_file = out_file )
142
- iflogger .info ('Denoised image saved as {i}' .format (i = out_file ))
148
+ _ , s = nlmeans_proxy (self .inputs .in_file ,
149
+ settings ,
150
+ noise_mask = noise_mask ,
151
+ out_file = out_file )
152
+ iflogger .info (('Denoised image saved as {i}, estimated '
153
+ 'sigma={s}' ).format (i = out_file , s = s ))
143
154
return runtime
144
155
145
156
def _list_outputs (self ):
@@ -191,7 +202,7 @@ def resample_proxy(in_file, order=3, new_zooms=None, out_file=None):
191
202
return out_file , new_zooms
192
203
193
204
194
- def nlmeans_proxy (in_file , in_mask = None , rician = True ,
205
+ def nlmeans_proxy (in_file , settings ,
195
206
noise_mask = None , out_file = None ):
196
207
"""
197
208
Uses non-local means to denoise 4D datasets
@@ -209,18 +220,13 @@ def nlmeans_proxy(in_file, in_mask=None, rician=True,
209
220
data = img .get_data ()
210
221
aff = img .get_affine ()
211
222
212
- if in_mask is None :
213
- mask = data [..., 0 ] > 80
214
- else :
215
- mask = in_mask > 0
216
-
217
- nmask = mask
223
+ nmask = data [..., 0 ] > 80
218
224
if noise_mask is not None :
219
225
nmask = noise_mask > 0
220
226
221
- sigma = np .std (data [~ nmask ])
222
- den = nlmeans (data , sigma = sigma , mask = mask )
227
+ sigma = np .std (data [nmask == 1 ])
228
+ den = nlmeans (data , sigma , ** settings )
223
229
224
230
nb .Nifti1Image (den .astype (hdr .get_data_dtype ()), aff ,
225
231
hdr ).to_filename (out_file )
226
- return out_file
232
+ return out_file , sigma
0 commit comments