2014-07-03 86 views
0

我有一個任務,我必須在一個3x3使用一個微調濾波器a(p) = 2所以這是我上八度代碼:圖像處理出了錯

for(i=2:511), 
    for(j=2:511), 
    D=NoiseImg(i-1:i+1,j-1:j+1);m=1; 
     for k=1:3,for l=1:3,E(m)=D(k,l);m=m+1;end;end; 
     El=sort(E);sum=0; 
     for n=3:7, 
     sum=sum+El(n); 
     end; 
     P2(i,j)=sum/5; 
    end; 
end; 

這是我的N胡椒鹽PIC(含6%的食鹽N 6%胡椒粉)enter image description here

,這裏是我的過濾器 enter image description here

什麼,我做錯了還是確定這樣的輸出??? Thx提前。

+0

你能解釋一下你想要過濾器做什麼嗎?此外,你可以顯示代碼來顯示圖像? – Royi

回答

1

這裏是代碼相當於你的意圖,其工作原理:

mInputImage = imread('1.png'); 
mInputImage = double(mInputImage)/255; 
mInputImage = mean(mInputImage, 3); 

numRows = size(mInputImage, 1); 
numCols = size(mInputImage, 2); 
numPixels = numRows * numCols; 

vSaltPixelsIdx  = randi([1, (numPixels)], [round(0.05 * numPixels), 1]); 
vPepperPixelsIdx = randi([1, (numPixels)], [round(0.05 * numPixels), 1]); 

mNoisyImage = mInputImage; 

mNoisyImage(vSaltPixelsIdx)  = 1; 
mNoisyImage(vPepperPixelsIdx) = 0; 

mOutputImage = zeros(numRows, numCols); 


for ii = [2:(numRows - 1)] 
    for jj = [2:(numCols - 1)] 
     mCurrPatch = mNoisyImage((ii - 1):(ii + 1), (jj - 1):(jj + 1)); 
     vCurrPatch = mCurrPatch(:); 
     vCurrPatch = sort(vCurrPatch); 

     mOutputImage(ii, jj) = mean(vCurrPatch(3:7)); 
    end 
end 


figure(); 
imshow(mInputImage, [0, 1]); 

figure(); 
imshow(mNoisyImage, [0, 1]); 

figure(); 
imshow(mOutputImage, [0, 1]); 

祝您好運!