如何使用自定义代码执行滑动邻域操作?

时间:2018-08-02 22:50:09

标签: arrays filtering nearest-neighbor

我需要使用自己的代码在3x3内核上执行滑动邻域操作。但是,我找不到排除边框像素的方法。另外,我得到零矩阵的奇怪结果。请参阅下面有关我到目前为止尝试过的内容:

P = magic(5);

P1 = P(:);
for ii = 1: length(P1)

% find row and col of ii in the image
[row, col] = find(P1(ii));

% find index of i in the image
idx = sub2ind(size(P1), row, col);

[ny, nx] = size(P);

% compute neighbors (3x3 kernel)
A(ii,:) = [ii-ny-1, ii-ny, ii-ny+1, ii-1, ii, ii+1, ii+ny-1, ii+ny, ii+ny+1];
if any(A(ii,:) < 0)
A(ii,:) = [];
continue
else
% compute median of A
P2(ii) = median(A(idx));
end

end
B = reshape(P2,[ny,nx]);

我很确定自己在代码的最后一部分中做错了很多事情。我很感谢任何建议/建议/帮助。谢谢。

0 个答案:

没有答案
相关问题