Filter image that contains NaNs in Matlab?

前端 未结 4 2011
被撕碎了的回忆
被撕碎了的回忆 2021-02-14 13:09

I have a 2d array (doubles) representing some data, and it has a bunch of NaNs in it. The contour plot of the data looks like this:

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-14 13:35

    One approach would be to replace the NaN values with nearest-neighbor interpolates using scatteredInterpolant (or TriScatteredInterp in older MATLAB versions) before performing the filtering, then replacing those points again with NaN values afterward. This would be akin to filtering a full 2-D array using the 'replicate' argument as opposed to the 'symmetric' argument as a boundary option for imfilter (i.e. you're replicating as opposed to reflecting values at the jagged NaN boundary).

    Here's what the code would look like:

    % Make your filter:
    filtWidth = 7;
    imageFilter = fspecial('gaussian', filtWidth, filtWidth);
    
    % Interpolate new values for Nans:
    nanMask = isnan(rfVals);
    [r, c] = find(~nanMask);
    [rNan, cNan] = find(nanMask);
    F = scatteredInterpolant(c, r, rfVals(~nanMask), 'nearest');
    interpVals = F(cNan, rNan);
    data = rfVals;
    data(nanMask) = interpVals;
    
    % Filter the data, replacing Nans afterward:
    dataFiltered = imfilter(data, imageFilter, 'replicate', 'conv');
    dataFiltered(nanMask) = nan;
    

提交回复
热议问题