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!