-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gauss.m
84 lines (71 loc) · 2.67 KB
/
Gauss.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function [PSF, center] = Gauss( sigma, N)
%
% [PSF, center] = Gauss( sigma, N)
%
% This function constructs the Gaussian blur PSF, and the center of the PSF
% for 1, 2, and 3D blurs.
%
% Inputs:
% sigma - parameters defining the spread of the blur
% N - size of the PSF, determines the dimension of the PSF
%
% Output:
% PSF - normalized Gaussian point spread function
% For 1D: exp(- .5 x^2 / sigma^2)
% For 2D:
% exp(-x^2/2sigma^2 - y^2/2sigma^2), if sigma is a number
% exp(-x^2/2sigma(1)^2 - y^2/2sigma(2)^2), if sigma is
% length 2 vector
% elliptical Gaussian is sigma is length 3 vector
% For 3D:
% exp(-x^2/2sigma(1)^2 - y^2/2sigma(2)^2- z^2/2sigma(3)^2),
% if sigma is length 3 vector
% center - center of the PSF
%
% J. Chung 8/2015
%
dim = length(N);
switch dim
case 1 % 1D PSF
x = -fix(N/2):ceil(N/2)-1;
PSF = exp( -.5*(x.^2)/(sigma^2));
PSF = PSF/sum(PSF(:));
center = N/2+1;
case 2 % 2D PSF
m = N(1); n = N(2);
% Set up grid points to evaluate the Gaussian function.
x = -fix(n/2):ceil(n/2)-1;
y = -fix(m/2):ceil(m/2)-1;
[X,Y] = meshgrid(x,y);
% Compute the Gaussian PSF
if length(sigma) == 1
PSF = exp( -(X.^2)/(2*sigma^2) - (Y.^2)/(2*sigma^2) );
PSFsum = sum(PSF(:));
elseif length(sigma) == 2
s1 = sigma(1); s2 = sigma(2);
PSF = exp( -(X.^2)/(2*s1^2) - (Y.^2)/(2*s2^2) );
PSFsum = sum(PSF(:));
elseif length(sigma)==3 % Elliptical Gaussian function
s1 = sigma(1); s2 = sigma(2); s3 = sigma(3);
num = -((X.^2)*(s1^2) + (Y.^2)*(s2^2) - 2*(s3^2)*(X.*Y));
den = 2*(s1^2 * s2^2 - s3^4);
PSF = exp( num / den );
PSFsum = sum(PSF(:));
end
PSF = PSF/PSFsum; % normalize the PSf
center = [m/2+1, n/2+1];
case 3 % 3D PSf
% N is a vector with [m,n,k]
m = N(1); n = N(2); k = N(3);
% Set up grid points to evaluate the Gaussian function.
x = -fix(n/2):ceil(n/2)-1;
y = -fix(m/2):ceil(m/2)-1;
z = -fix(k/2):ceil(k/2)-1;
[X,Y,Z] = ndgrid(x,y,z);
% Compute the Gaussian PSF
s1 = sigma(1); s2 = sigma(2); s3 = sigma(3);
PSF = exp( -(X.^2)/(2*s1^2) - (Y.^2)/(2*s2^2) - (Z.^2)/(2*s3^2) );
PSFsum = sum(PSF(:));
PSF = PSF/PSFsum;
center = [m/2+1, n/2+1, k/2+1];
end