Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

How to make a Gaussian filter in Matlab

Date: 2022-08-03 11:06:43

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. 

 

Expert Answer:

s: 

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(:)); 

Filtered image, given your image is called Im

 

filtered_signal=conv2(Im,gaussian2d(N,sig),'same');

Here's some plots: 

 

imagesc(gaussian2d(7,2.5))

 

Im=rand(100);subplot(1,2,1);imagesc(Im)
 subplot(1,2,2);imagesc(conv2(Im,gaussian2d(7,2.5),'same'));

 

Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

Our Comprehensive Matlab Assignment Help Services

Personalized Tutoring:Our team of MATLAB experts offers one-on-one tutoring sessions tailored to your specific needs. Whether you’re struggling with basic concepts or advanced algorithms, we provide clear, step-by-step guidance to help you understand and master MATLAB.

Assignment Assistance:Facing tight deadlines or complex assignments? We’re here to help! From initial problem analysis to code development and debugging, we offer full-spectrum support to ensure your assignments meet the highest standards.

Project Development: Need help with a research project? Our specialists can assist you in designing and implementing robust MATLAB solutions. We cover everything from project planning and data collection to coding, simulation, and result analysis.

Coursework Support: We provide comprehensive support for your coursework, helping you understand lectures, complete lab exercises, and prepare for exams. Our goal is to ensure you grasp the core principles and practical applications of MATLAB.

Thesis and Dissertation Guidance:Writing a thesis or dissertation? Our experts can assist you in incorporating MATLAB for data analysis, modeling, and simulation. We help you develop a strong methodological framework and ensure your research stands out.

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com