How to make a Gaussian filter in Matlab
I have tried to make a Gaussian filter in Matlab without using imfilter()
and fspecial()
. I have tried this but result is not like the one I have with imfilter and fspecial.
Here is my codes.
function Gaussian_filtered = Gauss(image_x, sigma) % for single axis % http://en.wikipedia.org/wiki/Gaussian_filter Gaussian_filtered = exp(-image_x^2/(2*sigma^2)) / (sigma*sqrt(2*pi)); end
for 2D Gaussian,
function h = Gaussian2D(hsize, sigma) n1 = hsize; n2 = hsize; for i = 1 : n2 for j = 1 : n1 % size is 10; % -5<center<5 area is covered. c = [j-(n1+1)/2 i-(n2+1)/2]'; % A product of both axes is 2D Gaussian filtering h(i,j) = Gauss(c(1), sigma)*Gauss(c(2), sigma); end end end
and the final one is
function Filtered = GaussianFilter(ImageData, hsize, sigma) %Get the result of Gaussian filter_ = Gaussian2D(hsize, sigma); %check image [r, c] = size(ImageData); Filtered = zeros(r, c); for i=1:r for j=1:c for k=1:hsize for m=1:hsize Filtered = Filtered + ImageData(i,j).*filter_(k,m); end end end end end
I have tried to make a Gaussian filter in Matlab without using imfilter()
and fspecial()
. I have tried this but result is not like the one I have with imfilter and fspecial.
Here is my codes.
function Gaussian_filtered = Gauss(image_x, sigma)
% for single axis
% http://en.wikipedia.org/wiki/Gaussian_filter
Gaussian_filtered = exp(-image_x^2/(2*sigma^2)) / (sigma*sqrt(2*pi));
end
for 2D Gaussian,
function h = Gaussian2D(hsize, sigma)
n1 = hsize;
n2 = hsize;
for i = 1 : n2
for j = 1 : n1
% size is 10;
% -5<center<5 area is covered.
c = [j-(n1+1)/2 i-(n2+1)/2]';
% A product of both axes is 2D Gaussian filtering
h(i,j) = Gauss(c(1), sigma)*Gauss(c(2), sigma);
end
end
end
and the final one is
function Filtered = GaussianFilter(ImageData, hsize, sigma)
%Get the result of Gaussian
filter_ = Gaussian2D(hsize, sigma);
%check image
[r, c] = size(ImageData);
Filtered = zeros(r, c);
for i=1:r
for j=1:c
for k=1:hsize
for m=1:hsize
Filtered = Filtered + ImageData(i,j).*filter_(k,m);
end
end
end
end
end
But the processed image is almost same as the input image. I wonder the last function GaussianFiltered()
is problematic...
Thanks.
NOTE:-
Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help , Finance Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.Answers:
here's an alternative:
Create the 2D-Gaussian:
function f=gaussian2d(N,sigma) % N is grid size, sigma speaks for itself [x y]=meshgrid(round(-N/2):round(N/2), round(-N/2):round(N/2)); f=exp(-x.^2/(2*sigma^2)-y.^2/(2*sigma^2)); f=f./sum(f(:));SEE COMPLETE ANSWER CLICK THE LINKhttps://matlabhelpers.com/questions/how-to-make-a-gaussian-filter-in-matlab.php
Comments
Post a Comment