From: DitzaN on
Hi,

I have the same problem, but my matrix is huge (960*1280) and I need filter of 5*5.
The code crashed in this line (because of memory problems):
idx = repmat(iA(:).',numel(is),1) + repmat(is(:)-1,1,numel(iA));
Could it be possible to limit the code to work only with points which are problematic with medfilt2:
points which have neighboring NaNs but aren't totally surrounded by them.

I would appreciate any suggestions!
Ditza
From: Bruno Luong on
Ditza, you lingt give this version a try, the memory requirement is less:

function M = mediannan(A, sz)

if nargin<2
sz = 5;
end
if length(sz)==1
sz = [sz sz];
end

if any(mod(sz,2)==0)
error('kernel size SZ must be odd)')
end
margin=(sz-1)/2;
AA = nan(size(A)+2*margin);
AA(1+margin(1):end-margin(1),1+margin(2):end-margin(2))=A;
[iB jB]=ndgrid(1:sz(1),1:sz(2));
is=sub2ind(size(AA),iB,jB);
[iA jA] = ndgrid(1:size(A,1),1:size(A,2));
iA = sub2ind(size(AA),iA,jA);

iA = uint32(iA(:).');
iS = uint32(is(:)-1);
idx = bsxfun(@plus,iA,iS);
% idx = repmat(iA(:).',numel(is),1) + repmat(is(:)-1,1,numel(iA));

B = sort(AA(idx),1);
clear idx
j = any(isnan(B),1);
last = zeros(1,size(B,2))+size(B,1);
[trash last(j)]=max(isnan(B(:,j)),[],1);
last(j)=last(j)-1;

M = nan(1,size(B,2));
valid = find(~isnan(A(:).')); % <- Simple check on A
mid = (last(valid)+1)/2;
i1 = sub2ind(size(B),floor(mid),valid);
i2 = sub2ind(size(B),ceil(mid),valid);
M(valid) = 0.5*(B(i1) + B(i2));
M = reshape(M,size(A));

end % medianna

% Bruno
From: DitzaN on
Hello Bruno,
Sorry it took me time to respond
It does exactly what I wanted
Thank you,
Ditza