2011-12-13 25 views
0
%% Adaptive Median Filtering - The Code 

ip1 = imread ('lena.gif');     %% Undistorted image 
ip = imnoise (ip1,'salt & pepper',0.25); %% Image corrupted with 'Salt and Pepper Noise' 

ip_median_filt1 = medfilt2(ip);    %% Apply median filter to noisy image with window dimensions of 3x3 pixels 
ip_median_filt2 = medfilt2(ip,[4,4]);  %% Apply median filter to noisy image with window dimensions of 4x4 pixels 
figure(1), clf; 
subplot (2, 1, 1), imshow (ip, []); 
subplot (2, 1, 2), imshow (ip_median_filt1, []); 

%% We now proceed with the adaptive median filtering of the noisy image and 
%% prove that the results are better than those of the standard median filter 
%% shown above 

%% Packing zeros around the edge pixels of the noisy input image so as to 
%% allow the facilitate the processing of edge-pixels of the image 

ip_edge = zeros (212,276); 

ip_convert = double (ip); 

%%%%%%%%%% there seems to be error on the following line 
ip_edge (11:202, 11:266) = ip_edge (11:202, 11:266) + ip_convert; 

smax=9; 

for i=11:202 
    for j=11:266 
     sx=3; 
     sy=3; 
     while ((sx<=smax) && (sy<=smax)) 
      ip_edge_min = ip_edge (i, j); 
      ip_edge_max = ip_edge (i, j); 
      ip_edge_median = median(median(ip_edge((i-floor(sx/2)):(i+floor(sx/2)),(j-floor(sy/2)):(j+floor(sy/2))))); 
      for k= (i-floor (sx/2)) :(i+floor (sx/2)) 
       for l= (j-floor (sy/2)) :(j+floor (sy/2)) 
        if ip_edge (k, l) < ip_edge_min 
         ip_edge_min = ip_edge (k, l); 
        end 
        if ip_edge (k, l) > ip_edge_max 
         ip_edge_max = ip_edge (k, l); 
        end 
       End 
      end 
      A = ip_edge_median - ip_edge_min; 
      B = ip_edge_median - ip_edge_max; 
      if (A>0) && (B<0) 
       C = ip_edge (i, j) - ip_edge_min; 
       D = ip_edge (I) - ip_edge_max; 
       if (C>0) && (D<0) 
        pledge (i, j) = ip_edge (i, j); 
        break 
       else 
        ip_edge (i, j) = ip_edge_median; 
        break 
       end 
      else 
       sx=sx+2; 
       sy=sy+2; 
       if (sx>smax) && (sy>smax) 
        ip_edge(i,j) = ip_edge(i,j); 
       end 
      end 
     end 
    end 
    end 
end 


figure(2), clf; 
imshow(ip_edge,[]); 

我得到一個錯誤在與%%%%%%%%%%行:一些錯誤與自適應中值濾波代碼

???錯誤使用==>加矩陣尺寸必須一致。 ==>自適應於22 ip_edge(11:202,11:266)= ip_edge(11:202,11:266)+ ip_convert;

+0

簡單:你不能加兩個不同大小的矩陣。 – cyborg

回答

3

您的錯誤與自適應濾波無關。只是矩陣尺寸不匹配!

提示:您不應該明確指定圖像的尺寸。 使用類似:

ip_edge = zeros(size(ip1) + 20); 
ip_edge(11:end-10,11:end-10) = double(ip); 

另外,您可以使用內置的功能padarray

ip_edge = padarray(double(ip), [10 10]) 

順便說你的代碼是非常低效的。 Matlab的規則#1是:永不循環!好吧,這並不總是可行的,但它是你應該瞄準的。 這裏是滑動中值濾波一個「瘦」代碼:

A = imread('lena.gif'); 
fun = @(x) median(x(:)); 
B = nlfilter(A,[3 3],fun); 
imshow(A), figure, imshow(B) 

是你的「適應性」是什麼意思? 祝你好運在你學習Matlab :-)

+0

自適應中值濾波是一種新的濾波方式,可以選擇噪聲候選值,並將中值應用於它們。 – vini

+0

我是新的MATLAB,所以代碼就是這樣的 – vini

+0

非常感謝我得到了我所做的錯誤! – vini

3

Matlab的告訴你問題是什麼,要能夠加入區域(11:202,11:266)的ip_edgeip_convert,他們需要有相同的尺寸。

ip_edge區域的大小是192 x 256,我猜測你ip_convert矩陣,如果在那一刻,不同尺寸的(不能確切知道,因爲莉娜GIF加載不是一個標準的Matlab的圖像)。