如何在Matlab中制作高斯滤波器(How to make a Gaussian filter in Matlab)

编程入门 行业动态 更新时间:2024-10-27 04:35:24
如何在Matlab中制作高斯滤波器(How to make a Gaussian filter in Matlab)

我试图在Matlab中制作高斯滤波器,而不使用imfilter()和fspecial() 。 我试过这个,但结果并不像我用imfilter和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

对于2D高斯,

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()是有问题的...

谢谢。

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.

最满意答案

这里有一个选择:

创建2D高斯:

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

在这里输入图像描述

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

enter image description here

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

enter image description here

更多推荐

本文发布于:2023-07-22 02:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1216017.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:滤波器   高斯   如何在   filter   Matlab

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!