3
%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)?

4

2 回答 2

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)。

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

于 2012-12-25T16:10:04.113 回答
0

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

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

于 2012-12-23T09:09:58.417 回答