一些错误与自适应中值滤波器代码


%% 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,[]);

我在%%%%%%%%%%:

一行得到一个错误

? ?错误使用==>加矩阵尺寸必须一致。错误==> adaptive at 22 ip_edge (11:202, 11:266) = ip_edge (11:202, 11:266) + ip_convert;

您的错误与自适应过滤无关。只是矩阵尺寸不匹配!

提示:您不应该明确指定图像的尺寸。例如:

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的第一条规则是:永远不要循环!好吧,这并不总是可行的,但这是你应该瞄准的目标。下面是滑动中值滤波的"精简"代码:
A = imread('lena.gif');
fun = @(x) median(x(:));
B = nlfilter(A,[3 3],fun);
imshow(A), figure, imshow(B)

这就是你所说的"适应性"吗?祝你学习Matlab顺利:-)

Matlab告诉你问题是什么,为了能够添加ip_edgeip_convert的区域(11:202,11:266),它们需要具有相同的维度。

ip_edge区域的大小是192 x 256,我猜你的ip_convert矩阵如果在那一刻不同的大小(不能确定,因为你加载的lena gif不是标准的Matlab图像)。

最新更新