我有一项任务我必须使用带有a(p) = 2
的a-trimmed过滤器,而不是3x3,所以这是我在octave上的代码:
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;
这是我的盐胡椒pic(含6%盐和6%胡椒)
这是我的过滤器的输出
我做错了什么或者是那样的??? Thx提前。
答案 0 :(得分: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]);
祝你好运!