Difference between manually-created gaussian filter and fspecial

4 views (last 30 days)
I am wondering why there is difference between these two, and how could the max vaule in fspecial (0.4026) is larger than manually-created gaussian (0.3989) when x = 0?
manually-created gaussian filter code:
x = [-2 -1 0 1 2];
gauss_filter = 1/(sqrt(2*pi)*sigma)*exp(-x.^2/(2*sigma^2))
gauss_filter =
0.0540 0.2420 0.3989 0.2420 0.0540
while the fspecial create gaussian filter:
gauss_filter = 1/(sqrt(2*pi)*sigma)*exp(-x.^2/(2*sigma^2))'
gauss_filter =
0.0545 0.2442 0.4026 0.2442 0.0545

Answers (1)

DGM
DGM on 24 Oct 2024
Edited: DGM on 24 Oct 2024
I believe the original question was pasted incorrectly, since both examples appear to be identical (except a transpose). Neither shows the use of fspecial().
However, the difference between the two results is simple. For the filters to not alter the image mean, their sum must be 1.
sigma = 1;
x = [-2 -1 0 1 2];
fk1 = 1/(sqrt(2*pi)*sigma)*exp(-x.^2/(2*sigma^2)) % as calculated
fk1 = 1×5
0.0540 0.2420 0.3989 0.2420 0.0540
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
sum(fk1) % not sum-normalized
ans = 0.9909
fk1 = fk1/sum(fk1) % normalize it
fk1 = 1×5
0.0545 0.2442 0.4026 0.2442 0.0545
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
sum(fk1) % sum-normalized
ans = 1

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!