generally working
@@ -2321,3 +2321,48 @@ def _resequence_labels(array):
_sequence(array, count)
return array.reshape(a_shape)
+
+
+def nl_means_layered(im, cores=None, patch_size=15, patch_distance=25,
+ h=4, sigma=6, axis=0):
+ import dask
+ from skimage.restoration import denoise_nl_means, estimate_sigma
+ from skimage.exposure import rescale_intensity, match_histograms
+ from dask.diagnostics import ProgressBar
+ dask.config.set(scheduler="threads")
+
+ # im = ps.generators.blobs(shape=[300, 300, 300])
+ # im = im - (im == 1)*(np.random.rand(*im.shape)*0.4)
+ # im = im + (im == 0)*(np.random.rand(*im.shape)*0.4)
+
+ @dask.delayed
+ def apply_func(func, **kwargs):
+ return func(**kwargs)
+
+ temp = np.copy(im)
+ for i in range(im.shape[2]):
+ temp[:, :, i] = match_histograms(temp[:, :, i], temp[:, :, 0],
+ multichannel=False)
+ p2, p98 = np.percentile(temp, (2, 98))
+ temp = rescale_intensity(temp, in_range=(p2, p98))
+ temp = temp / temp.max()
+ sigma_est = np.mean(estimate_sigma(temp[:, :, 0], multichannel=False))
+
+ kw = {'im': temp,
+ 'cores': None,
+ 'patch_size': patch_size,
+ 'patch_distance': patch_distance,
+ 'h': h * sigma_est,
+ 'multichannel': False,
+ 'fast_mode': True}
+
+ results = []
+ for i in range(im.shape[2]):
+ layer = im[i, ...]
+ kw["image"] = layer
+ t = apply_func(func=denoise_nl_means, **kw)
+ results.append(t)
+ with ProgressBar():
+ ims = dask.compute(results, num_workers=cores)[0]
+ result = np.array(ims)
+ return result
