2
我有這個代碼將執行圖像上的高斯濾波器(低通濾波器)。但是,此濾鏡僅適用於灰度圖像。我該如何改進它,以便它可以在彩色圖像上工作?我知道有很多內置函數,但我是圖像處理的新手,我正在嘗試學習基礎知識。用於RGB圖像的Matlab低通濾波器
%Read an Image
Img = imread('peppers.png');
Im = rgb2gray(Img);
I = double(Im);
%Design the Gaussian Kernel
%Standard Deviation
sigma = 1.76;
%Window size
sz = 4;
[x,y]=meshgrid(-sz:sz,-sz:sz);
M = size(x,1)-1;
N = size(y,1)-1;
Exp_comp = -(x.^2+y.^2)/(2*sigma*sigma);
Kernel= exp(Exp_comp)/(2*pi*sigma*sigma);
%Initialize
Output=zeros(size(I));
%Pad the vector with zeros
I = padarray(I,[sz sz]);
%Convolution
for i = 1:size(I,1)-M
for j =1:size(I,2)-N
Temp = I(i:i+M,j:j+M).*Kernel;
Output(i,j)=sum(Temp(:));
end
end
%Image without Noise after Gaussian blur
Output = uint8(Output);
figure,imshow(Output);
您需要修改代碼的最後兩行。您正在覆蓋輸出的第一個通道,而不是寫入其他兩個通道。我還建議在所有語句的末尾添加分號,否則你會在命令提示符中得到很多回響。 – rayryeng