forked from ndwork/dworkLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfspecial3d.m
63 lines (54 loc) · 1.22 KB
/
fspecial3d.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
57
58
59
60
61
62
function h = fspecial3d( type, varargin )
% out = fspecial3d( type, parameters )
%
% Creates a 3D filter h of the specified type. The different types are
% listed below.
%
% h = fspecial3d( 'average', hSize )
% hSize can be a 3 element array or a scalar
%
% Written by Nicholas Dwork
switch type
case 'average'
h = fspecial3d_average( varargin{:} );
case 'gaussian'
h = fspecial3d_gaussian( varargin{:} );
end
end
function h = fspecial3d_average( hSize )
if numel(hSize)==1
ny=hSize; nx=hSize; nz=hSize;
else
ny=hSize(1); nx=hSize(2); nz=hSize(3);
end
N = ny * nx * nz;
h = 1/N * ones( ny, nx, nz );
end
function h = fspecial3d_gaussian( hSize, sigma )
if numel(hSize)==1
ny=hSize; nx=hSize; nz=hSize;
else
ny=hSize(1); nx=hSize(2); nz=hSize(3);
end
halfY = floor(ny/2);
if mod(ny,2)==0
y = -halfY:halfY-1;
else
y = -halfY:halfY;
end
halfX = floor(nx/2);
if mod(nx,2)==0
x = -halfX:halfX-1;
else
x = -halfX:halfX;
end
halfZ = floor(nz/2);
if mod(nz,2)==0
z = -halfZ:halfZ-1;
else
z = -halfZ:halfZ;
end
[x,y,z] = meshgrid(x,y,z);
h = exp( -0.5/(sigma*sigma) * ( x.*x + y.*y + z.*z ) );
h = h / sum(h(:));
end