Strange interp3 behavior

1 visualización (últimos 30 días)
Cyrano Chatziantoniou
Cyrano Chatziantoniou el 8 de Abr. de 2021
Respondida: Abhimenyu el 4 de Abr. de 2024
I'm working on copying masks between medical images and was trying to implement with interp3. In order to test the code, I tried to copy the mask to the original image, which should yield the exact same mask. The resulting mask was missing some parts though. This is the smallest example I could make that reproduces the result
%%Create mask
empty = zeros(100, 100);
empty(50,50) = 1;
empty(20,20) = 1;
empty(70,30) = 1;
r = bwdist(empty);
t = r <= 30;
mask = zeros(100,100,100);
mask(:,:,50) = t;
%% Get indices & interpolate
idx = find(mask);
[xq,yq,zq] = ind2sub(size(mask), idx);
samples = interp3(mask, xq, yq, zq);
%% compare
newMask = zeros(size(mask));
newMask(find(mask)) = samples;
imshowpair(mask(:,:,50), newMask(:,:,50))
This is the outcome: some part of the mask isn't sampled correctly. For some reason it's always the same area at the bottom left.
I tried to shuffle the indices where the image should be sampled, that results in this:
Why don't all points get interpolated correctly? I'm guessing it's an error in my implementation and not interp3, but I really don't understand what I'm not doing right.

Respuestas (1)

Abhimenyu
Abhimenyu el 4 de Abr. de 2024
Hi Cyrano,
From the information shared, I have inferred that the issue you're encountering with "interp3" MATLAB function and the mask copying process in MATLAB stems from a misunderstanding of how "interp3" works. The "interp3" function is designed for interpolating values at non-integer grid points within a 3D volume. When it is used to interpolate at the exact locations of the original grid points (which are all integers), you're essentially asking for the original values at those points. However, due to floating-point inaccuracies and the nature of interpolation methods, the original values might not get back, especially if the interpolation method assumes some form of smoothing or if the points are considered to be outside the interpolation grid.
Interpolation, by its very nature, introduces a level of approximation that is unnecessary and counterproductive for copying data that is inherently discrete and exact as in this case of binary data.
If issues arise with copying due to transformations or other operations that necessitate interpolation, the nearest-neighbor interpolation option(method='nearest') with "interp3" for binary masks should be used, which would avoid the introduction of non-binary values. However, for the use case of copying, direct assignment is the correct approach as given by the MATLAB code given below:
%%Create mask
empty = zeros(100, 100);
empty(50,50) = 1;
empty(20,20) = 1;
empty(70,30) = 1;
r = bwdist(empty);
t = r <= 30;
mask = zeros(100,100,100);
mask(:,:,50) = t;
%% compare
newMask = mask;
imshowpair(mask(:,:,50), newMask(:,:,50))
This will ensure that "newMask" is an exact copy of "mask", without any of the issues introduced by interpolation.
For more information on the "interp3" MATLAB function follow this MATLAB R2024A documentation link: https://www.mathworks.com/help/matlab/ref/interp3.html
I hope this helps!

Categorías

Más información sobre Interpolation en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by