-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathvolshrink.m
51 lines (49 loc) · 1.4 KB
/
volshrink.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
function newvol = volshrink(vol, layer, mask)
%
% vol = volshrink(vol, layer, mask)
%
% thinning a binary image or volume by a given pixel width
% this is similar to bwmorph(vol,'thin',3) except
% this does it in both 2d and 3d
%
% author: Qianqian Fang, <q.fang at neu.edu>
%
% input:
% vol: a volumetric binary image
% layer: number of iterations for the thickenining
% mask: (optional) a 3D neighborhood mask
%
% output:
% vol: the volume image after the thinning operations
%
% -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
%
if (nargin < 2)
layer = 1;
end
if (nargin < 3 || isempty(mask))
if (ndims(vol) == 3)
mask = zeros(3, 3, 3);
mask(2, 2, :) = 1;
mask(:, 2, 2) = 1;
mask(2, :, 2) = 1;
else
mask = [0 1 0; 1 1 1; 0 1 0];
end
end
mask = rot90(mask, 2);
totalmask = sum(mask(:));
newvol = ones(size(vol) + 2);
if (ndims(vol) == 3)
newvol(2:end - 1, 2:end - 1, 2:end - 1) = (vol ~= 0);
for i = 1:layer
newvol(2:end - 1, 2:end - 1, 2:end - 1) = (convn(logical(newvol), logical(mask), 'valid') == totalmask);
end
newvol = double(newvol(2:end - 1, 2:end - 1, 2:end - 1));
else
newvol(2:end - 1, 2:end - 1) = (vol ~= 0);
for i = 1:layer
newvol(2:end - 1, 2:end - 1) = (convn(logical(newvol), logical(mask), 'valid') == totalmask);
end
newvol = double(newvol(2:end - 1, 2:end - 1));
end