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

これがあなたの問題に対する私の解決策です。
マスクの中心がfloor(size(mask、1)/ 2)にあると誤解していたので、左上隅(1,1)のように計算しました。

したがって、パディングは、画像の下端と右端だけでなく、画像全体で循環する必要があります。

于 2012-12-25T16:10:04.113 に答える
0

パディングが間違っています-画像の最後(両方の軸)にのみパディングを追加しましたが、最初には追加しませんでした。

ウィンドウの原点は、各ステップでフィルターの出力を配置する場所を意味します。ウィンドウの中央にない場合、出力イメージ全体がシフトされます。この場合、それほど重要ではありませんが、より複雑な方法では、異なる出力が生成される可能性があります。

于 2012-12-23T09:09:58.417 に答える