Replace NaN values in a matrix with values of x positions after it
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
2 Kommentare
Walter Roberson
am 10 Mai 2020
What do you want to have happen if there are more than x NaNs in a row, or if one of the values x later is itself nan?
Guillaume
am 10 Mai 2020
Note that the isub2 matrix attached cannot contain NaNs as it's of type uint8. Only matrices of type single or double can contain NaN.
Antworten (3)
Guillaume
am 10 Mai 2020
Bearbeitet: Guillaume
am 10 Mai 2020
regionfill seems to be the function you're after. So:
newimage = regionfill(yourimage, isnan(yourimage));
2 Kommentare
Walter Roberson
am 10 Mai 2020
No, the user wishes to fill any given NaN value with a copy of the column x further on, and regionfill() is not going to do that.
Guillaume
am 10 Mai 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
am 10 Mai 2020
Bearbeitet: Walter Roberson
am 10 Mai 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.
0 Kommentare
Image Analyst
am 10 Mai 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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Computer Vision with Simulink finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!