在Matlab中使用循环填充的中值滤波器

时间:2012-12-22 10:24:10

标签: matlab image-processing

%This routine performs median filtering on an image.
% 
% Input:       im - a grayscale image (values in [0,255]) 
%    winSize - a 1x2 vector describing the size (height,width) of thefiltering window. 
% Output:     nim - a grayscale image (values in [0,255]) which is the median filtered im.   
%             Image nim is of the same size as im.
% 
% Method:  Performs Median Filtering on image im in windows of size winSize. Assume the 
%                     window origin is at floor(size(B)/2)+1. Assume cyclic-padding.

所以这里是我在图像处理中给出的练习的定义 这是我的解决方案:

function [ nim ] = medianFilt( im,winSize )
nim = im;
temp = cat(2,[im ;im(1:winSize(1)-1,:)],[ im(:,1:winSize(2)-1); im(1:winSize(1)-1,1:winSize(2)-1)])

for i = 1:size(im,1);
    for j = 1:size(im,2);
        winSizeMatrix = temp(i:i+winSize(1)-1,j:j+winSize(2)-1);
        winSizeVector = reshape(winSizeMatrix,[],1);
        medianOfVector = median(double(winSizeVector));
        nim(i,j) = medianOfVector;
    end
end

end

我得到一个平滑图片的结果 - 我正在使用盐和胡椒过滤器,但最后的像素看起来像是从第一个像素复制而不是固定过滤器。是他们的输出方式应该是什么?或者我错过了什么?

也有人可以解释为什么我需要假设窗口原点在楼层(大小(im / 2)+1)??

2 个答案:

答案 0 :(得分:2)

function [ nim ] = medianFilt( im,winSize )
%This routine performs median filtering on an image.
% 
% Input:       im - a grayscale image (values in [0,255]) 
%                       winSize - a 1x2 vector describing the size (height,width) of the filtering window. 
% Output:     nim - a grayscale image (values in [0,255]) which is the median filtered im. 
%                       Image nim is of the same size as im.
% 
% Method:  Performs Median Filtering on image im in windows of size winSize. Assume the 
%                     window origin is at floor(size(B)/2)+1. Assume cyclic-padding.
nim = im;
%temp = cat(2,[im ;im(1:winSize(1)-1,:)],[ im(:,1:winSize(2)-1);     im(1:winSize(1)-1,1:winSize(2)-1)]);
paddedIm = padarray(im, [floor(winSize(1)/2) floor(winSize(2)/2)],'circular');

for i = 1:size(im,1);
    for j = 1:size(im,2);
        startAi = i; % start point of window in padded array A to convolve with mask
        finishAi = i + winSize(1) - 1; % start point of  window in padded array A to convolve with mask
        startAj = j; %end point of window in padded array A to convolve with mask
        finishAj = j + winSize(2) - 1; %end point of window in padded array A to convolve with mask
        % create vector out of the window to multiply with vector of mask
        vectPaddedA = reshape(paddedIm(startAi :finishAi,startAj:finishAj)',1,[]);
        medianOfVector = median(double(vectPaddedA));
        nim(i,j) = medianOfVector;
    end
end

end

这是我的问题解决方案。
你误会了面具的中心在地板上(大小(面具,1)/ 2),你已经计算出它在左上角(1,1)。

因此,填充需要在图像周围循环,而不仅仅是在下边缘和右边缘。

答案 1 :(得分:0)

你的填充错误 - 你只在图像的末尾添加了填充(两个轴),但不是在开头。

窗口原点表示在每个步骤中放置过滤器输出的位置。如果它不在窗口中心,则整个输出图像将被移位。在这种情况下,它不是很重要,但在更复杂的方法中,它可以产生不同的输出。