本文介绍了如何在Matlab中制作高斯滤波器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在不使用 imfilter()fspecial() 的情况下在 Matlab 中制作高斯滤波器.我已经尝试过这个,但结果与我使用 imfilter 和 fspecial 的结果不同.

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.

这是我的代码.

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

对于二维高斯,

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

最后一个是

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

但处理后的图像与输入图像几乎相同.我想知道最后一个函数 GaussianFiltered() 有问题...

But the processed image is almost same as the input image. I wonder the last function GaussianFiltered() is problematic...

谢谢.

推荐答案

这里有一个替代方案:

创建二维高斯:

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

过滤后的图像,假设您的图像名为 Im:

Filtered image, given your image is called Im:

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

这里有一些情节:

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'));

这篇关于如何在Matlab中制作高斯滤波器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-25 22:49