-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathsmoothbinvol.m
56 lines (52 loc) · 1.69 KB
/
smoothbinvol.m
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
function vol = smoothbinvol(vol, layer)
%
% vol=smoothbinvol(vol,layer)
%
% perform a memory-limited 3D image smoothing
%
% author: Qianqian Fang <q.fang at neu.edu>
%
% input:
% vol: a 3D volumetric image to be smoothed
% layer: number of iterations for the smoothing
%
% output:
% vol: the volumetric image after smoothing
%
% -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
%
dim = size(vol);
dxy = dim(1) * dim(2);
fulllen = prod(dim);
weight = 1 ./ 6.;
step = 4000;
% in case vol is a logical
vol = double(vol);
offs = [1, -1, dim(1), -dim(1), dxy, -dxy];
for i = 1:layer
% find all non-zero values
idx = find(vol);
% get the neighbors of all the non-zero values
% this may cause wrapping -- TODO
val = vol(idx);
for k = 1:6
nextidx = idx + offs(k);
% find all 1-valued voxels that are located within the domain
goodidx = find(nextidx > 0 & nextidx < fulllen);
% for all neighboring voxels, add a fraction from the non-0 voxels
% problematic when running in parallel (racing)
len = length(goodidx);
% control granualarity with step
if (len > step)
for j = 1:step:len - step
vol(nextidx(goodidx(j:j + step - 1))) = vol(nextidx(goodidx(j:j + step - 1))) + weight * val(goodidx(j:j + step - 1));
end
vol(nextidx(goodidx(j + step:end))) = vol(nextidx(goodidx(j + step:end))) + weight * val(goodidx(j + step:end));
else
vol(nextidx(goodidx)) = vol(nextidx(goodidx)) + weight * val(goodidx);
end
% the above line may change the values of the non-zero voxels, recover
% them
end
vol(idx) = val;
end