Image 如何在Matlab中制作高斯滤波器
我曾尝试在Matlab中制作高斯滤波器,但没有使用Image 如何在Matlab中制作高斯滤波器,image,matlab,gaussian,Image,Matlab,Gaussian,我曾尝试在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*s
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
对于二维高斯分布
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
但处理后的图像与输入图像几乎相同。我想知道最后一个函数gaussianfilted()
有问题
谢谢。这里有一个替代方案:
创建二维高斯曲线:
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))
由于for循环,此示例代码速度较慢。在matlab中,您可以更好地使用conv2,正如用户建议的那样:bla,或者只使用filter2
I = imread('peppers.png'); %load example data
I = I(:,:,1);
N=5; %must be odd
sigma=1;
figure(1);imagesc(I);colormap gray
x=1:N;
X=exp(-(x-((N+1)/2)).^2/(2*sigma^2));
h=X'*X;
h=h./sum(h(:));
%I=filter2(h,I); %this is faster
[is,js]=size(I);
Ib = NaN(is+N-1,js+N-1); %add borders
b=(N-1)/2 +1;
Ib(b:b+is-1,b:b+js-1)=I;
I=zeros(size(I));
for i = 1:is
for j = 1:js
I(i,j)=sum(sum(Ib(i:i+N-1,j:j+N-1).*h,'omitnan'));
end
end
figure(2);imagesc(I);colormap gray
什么不仅仅用于生成高斯核,然后可能用于应用它?您至少可以使用conv2(图像,高斯核,'same')?好的,非常感谢。但它也使用Matlab函数meshgrid..有没有办法不使用meshgrid()来生成高斯分布?无论如何谢谢你!!!我想做一个高斯滤波器的matlab代码,没有任何原始的matlab纯函数…网格矩阵很容易用任何语言创建。这就是为什么在许多语言中都有meshgrid(你可以在python、java等语言中找到它)。在没有conv2的情况下是否可以实现它?问题是关于高斯滤波器的。
conv2
只是一种使用它的方法,因此不需要它来创建二维高斯矩阵。那么你的问题是什么?
Im=rand(100);subplot(1,2,1);imagesc(Im)
subplot(1,2,2);imagesc(conv2(Im,gaussian2d(7,2.5),'same'));
I = imread('peppers.png'); %load example data
I = I(:,:,1);
N=5; %must be odd
sigma=1;
figure(1);imagesc(I);colormap gray
x=1:N;
X=exp(-(x-((N+1)/2)).^2/(2*sigma^2));
h=X'*X;
h=h./sum(h(:));
%I=filter2(h,I); %this is faster
[is,js]=size(I);
Ib = NaN(is+N-1,js+N-1); %add borders
b=(N-1)/2 +1;
Ib(b:b+is-1,b:b+js-1)=I;
I=zeros(size(I));
for i = 1:is
for j = 1:js
I(i,j)=sum(sum(Ib(i:i+N-1,j:j+N-1).*h,'omitnan'));
end
end
figure(2);imagesc(I);colormap gray