2016-10-30 49 views
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); 

答えて

0

RGBイメージは赤、緑、青のカラーチャネルで構成されています。 RGB画像

  1. に対して画像処理を実行するには、
  2. が修正R、Gから画像をreconstuct一旦画像

  3. プロセスの3つの成分の各成分(R、G、B)を分離しなければなりません、詳細はB

    img = imread('peppers.png'); 
    R = img(:,:,1); %get the Red part 
    G = img(:,:,2); %get the Blue part 
    B = img(:,:,3); %get the Green part 
    R_gaussian = gaussianFilter(R); %write your own function name 
    G_gaussian = gaussianFilter(G); %or repeat the code by REPLACING I as 
    B_gaussian = gaussianFilter(B); %Red Green Blue components 
    RGB_gaussian = cat(3,R_gaussian, G_gaussian, B_gaussian); %merging the components 
    %since you are learning you can do this for better unedrstanding 
    RGB_gaussian = zeros(size(img)); %make a matrix of size of original image 
    RGB_gaussian(:,:,1)=R_gaussian; % Replace the values 
    RGB_gaussian(:,:,2)=G_gaussian; 
    RGB_gaussian(:,:,3)=B_gaussian; 
    

はこれが役立つことができます:http://www.bogotobogo.com/Matlab/Matlab_Tutorial_Digital_Image_Processing_6_Filter_Smoothing_Low_Pass_fspecial_filter2.php

+0

コードの最後の2行を変更する必要があります。他の2つのチャンネルに書き込む代わりに、出力の最初のチャンネルを上書きしています。また、すべてのステートメントの最後にセミコロンを追加することをお勧めします。そうしないと、コマンドプロンプトに多くのエコーが表示されます。 – rayryeng

関連する問題