%% 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,[]);
我在使用%%%%%%%%%%:
的行中收到错误???使用==&gt;时出错加上Matrix尺寸必须达成一致。 ==&gt;中的错误自适应22 ip_edge(11:202,11:266)= ip_edge(11:202,11:266)+ ip_convert;
答案 0 :(得分:3)
Matlab告诉你问题是什么,为了能够添加ip_edge
和ip_convert
的区域(11:202,11:266),它们需要具有相同的尺寸。
ip_edge
区域的大小为192 x 256
,我猜你的ip_convert矩阵,如果当时大小不同(因为你加载的lena gif不是肯定的)一个标准的Matlab图像)。
答案 1 :(得分: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好运: - )