4 views (last 30 days)

Suppose I have a matrix (actually grayscale images with think streaks of NaNs):

[ 3 4 4 2 4 6 8 6 3 2

4 5 NaN NaN 2 3 4 2 1 2

2 NaN NaN NaN 3 2 4 3 1 2

3 NaN NaN NaN 3 4 2 5 3 2

4 NaN NaN NaN 2 4 1 3 2 5

4 5 NaN NaN 2 3 4 2 1 2

2 NaN NaN NaN 3 2 4 3 1 2

3 2 NaN NaN NaN 4 2 5 3 2

4 3 2 NaN NaN NaN 4 1 3 2

4 3 2 3 NaN NaN 2 1 3 2

]

I want to replace all NaNs with 'x' values after it, for example for x=3 the second and third row woud be like:

4 5 3 4 2 3 4 2 1 2,

2 3 2 4 3 2 4 3 1 2,

I actually want to fill the NaNs with the background color smoothly so that the NaN lines are not visible clearly.

I have tried using the Matlab functions like fillmissing and fillgaps, and a function inpaint_nans() from matlab file_exchange, but they all leave a clear line that is prominent over the background.

Attached is the real image, where nans have been replaced by zeros.

Any ideas would be appreciated.

Thanks.

Guillaume
on 10 May 2020

Edited: Guillaume
on 10 May 2020

regionfill seems to be the function you're after. So:

newimage = regionfill(yourimage, isnan(yourimage));

Walter Roberson
on 10 May 2020

Guillaume
on 10 May 2020

"I actually want to fill the NaNs with the background color smoothly so that the NaN lines are not visible clearly."

Sounds to me like the user wants to fill the background so that it's continuous with the surrounding region, which is exactly what regionfill is going to do. The actual filling algorithm doesn't matter much.

If regionfill doesn't do the deeds, then I would try inpaintCoherent. Both of these are designed to fill missing pixels with a background so that the filled region doesn't stand out.

Walter Roberson
on 10 May 2020

Edited: Walter Roberson
on 10 May 2020

for c = size(isub2,2) - x: -1 : 1

mask = isnan(isub2(:,c));

isub2(mask,c) = isub2(mask,c+x);

end

This code can handle streams of nans that are longer than x, and can handle the possibility that the position x further on might also be nan: it starts at the right hand side and moves leftward, so those nans would already have been filled in.

The one thing it cannot handle is the possibility of nan in the last x columns, as there is nothing x further columns on to copy. I can think of an easy and not unreasonable modification that would handle nan in the last x columns except in the last column; the last column just doesn't have anything further on to copy from.

Image Analyst
on 10 May 2020

You could try to anti-alias it by doing a weighted sum of the original plus a blurred version of the nan image:

m = [ 3 4 4 2 4 6 8 6 3 2

4 5 NaN NaN 2 3 4 2 1 2

2 NaN NaN NaN 3 2 4 3 1 2

3 NaN NaN NaN 3 4 2 5 3 2

4 NaN NaN NaN 2 4 1 3 2 5

4 5 NaN NaN 2 3 4 2 1 2

2 NaN NaN NaN 3 2 4 3 1 2

3 2 NaN NaN NaN 4 2 5 3 2

4 3 2 NaN NaN NaN 4 1 3 2

4 3 2 3 NaN NaN 2 1 3 2

]

subplot(2, 2, 1);

imshow(m, [], 'InitialMagnification', 800);

title('Original Matrix');

impixelinfo

% Define x image.

x = 2;

xImage = x * ones(size(m));

nanLocations = 2 * isnan(m)

subplot(2, 2, 2);

imshow(nanLocations, []);

title('Nan Locations');

impixelinfo

% Blur nanLocations to get percentages.

windowWidth = 3;

kernel = ones(windowWidth) / windowWidth^2;

blurred = conv2(nanLocations, kernel, 'same')

% Normalize

blurred = blurred / max(blurred(:));

subplot(2, 2, 3);

imshow(blurred, [], 'InitialMagnification', 800);

title('Blurred');

impixelinfo

% Make nans zero so we can make weighted sum

m(isnan(m)) = 0;

% Do weighted sum

output = m .* (1 - blurred) + xImage .* blurred;

subplot(2, 2, 4);

imshow(output, [], 'InitialMagnification', 800);

title('Output');

impixelinfo

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/524394-replace-nan-values-in-a-matrix-with-values-of-x-positions-after-it#comment_844752

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/524394-replace-nan-values-in-a-matrix-with-values-of-x-positions-after-it#comment_844752

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/524394-replace-nan-values-in-a-matrix-with-values-of-x-positions-after-it#comment_844843

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/524394-replace-nan-values-in-a-matrix-with-values-of-x-positions-after-it#comment_844843

Sign in to comment.