How to fill surface with interpolated values?

Hi,
I'm working on a mesh, resulting from an stl file . I extracted the mesh with gridtrimesh.m
This is the mesh plotted over the stl object:
As you cas see, there are some holes (for instance, the one in the red rectangle).
How can I fill the holes?
I tried with interp2, but it seems that it can't "go" beyond the surface borders.
Do you have any suggestion?
Thank you in advance for any replies.

More Answers (1)

the cyclist
the cyclist on 19 Jun 2022
Edited: the cyclist on 19 Jun 2022
Your code does not seem to have a call to interp2, so I'm not sure what might have gone wrong there.
If you did not specify an extrapolation method explicitly, then the implicit method that MATLAB uses may have filled in NaN, depending on the interpolation method. For example, linear interpolation will fill NaN in the extrapolation region, if you do not specify the extrapolation method.

8 Comments

Thank you for your answer.
This is the code I used to obtain the mesh:
%open the file
[file,path]=uigetfile('*.stl','Load');
Tissue=stlread(strcat(path,file));
% V= vertices list
% F=connectivity list
V=Tissue.Points;
F=Tissue.ConnectivityList;
%rotation matrix
t=-90;
Rx=[1 0 0; 0 cosd(t) -sind(t) ; 0 sind(t) cosd(t)];
Vrot=Rx*V';
[X,Y,Z]=demonstration_fun(Vrot', F); %attached below
titolo=file; titolo=strrep(titolo,'_ML_cleaned_pre.stl', ''); titolo=strrep(titolo, '_', ' ');
%plot
figure,
l=patch('vertices',V','faces',F,'edgecolor','none', 'facecolor',[0.8 0.8 0.8],'facelighting','gouraud');
hold on
ll=surf(X,Y,Z);
light('Position',[0 -200 0],'Style','local'), light('Position',[-200 0 0],'Style','local')
light('Position',[200 0 0],'Style','local'), light('Position',[0 0 200],'Style','local')
hold off
%select the query points on the patch and save them in a struct
%for using interp2, extrapolate Xq and Yq
for i=1:size(struct,2)
Xq(i)=struct(i).Position(1); %query point coordinates X
Yq(i)=struct(i).Position(2); %query point coordinates Y
ZZ(i)=struct(i).Position(3); %query point coordinates Z
end
Zq = interp2(X,Y,Z,Xq,Yq, 'cubic', 0);
%plot Zq points
figure,
l=patch('vertices',V','faces',F,'edgecolor','none', 'facecolor',[0.8 0.8 0.8],'facelighting','gouraud');
hold on
ll=surf(X,Y,Z);
light('Position',[0 -200 0],'Style','local'), light('Position',[-200 0 0],'Style','local')
light('Position',[200 0 0],'Style','local'),light('Position',[0 0 200],'Style','local')
plot3(Xq,Yq,Zq,'o','Color','b','MarkerSize',10)
hold off
the problem is that I cannot add point to the mesh I already have. Is there a method to do it?
thank you
the file is in this folder:
https://drive.google.com/drive/folders/16g9HA60FFAFwgP8ww0LdHRozsdm8jSQy?usp=sharing
I couldn't get your code to run. I put all the files you uploaded in a directory (including the file you uploaded to google instead of here). Then I ran the code you pasted above that "obtains the mesh". I get the error:
Error using patch
Value must be a 1x2 or 1x3 vector of numeric type.
Error in answerTest (line 22) % <--------- This is my own file, where I put your code, so may not correspond to your line numbering
l=patch('vertices',V','faces',F,'edgecolor','none', 'facecolor',[0.8 0.8 0.8],'facelighting','gouraud');
This does not seem to be related to your actual question, so I'm not sure what the issue is.
That being said, your interpolation
Zq = interp2(X,Y,Z,Xq,Yq, 'cubic', 0);
will not do any extrapolation outside the range of points you have. The cubic method will fill in NaN unless you specify the extrapolation method, as described in the documentation for interp2.
I'm sorry, there was a typo:
l=patch('vertices',V,'faces',F,'edgecolor','none', 'facecolor',[0.8 0.8 0.8],'facelighting','gouraud');
I wrote V' instead of V.
However, do you know if there is a method to do it outside the range of the mesh points?
Fixed that, and now I get this error:
Error using struct
Conversion to struct from double is not possible.
Error in answerTest (line 34)% <--- Again, you'll have a different line number
Xq(i)=struct(i).Position(1); %query point coordinates X
When you say you want a method "outside the range of the mesh points", which operation are you talking about? Do you mean the interpolation (which I have already explained what to try), the patch command, or something else?
I need some functions that help me to obtain a mesh that is "complete", as the patch plotted below (the grey one).
For the error, sorry, it was my fault: it was meant to manually choose the points you want to interpolate. For instance:
if you export them with the "export cursor data to workspace" command and save them as a struct called (fancifully) "struct", you can extract the vectors with Xq and Yq points. If you give it in input to interp2, as written in the code above (maybe wrongly), I obtain this:
If you note, the points are on the cheek and not on the jaw, as previously taken.
Do you have any advice?
Thank you so much for all the time it took to help me so far
I have a hypothesis about what is going on here, but I'm not certain.
It seems from your image that the colored mesh is only covering the top surface of the gray image, and not "curling" under, to cover the parts of the face that curve underneath.
You can see that nearer the jaw, the mesh lines drop straight down to the next surface, but do not capture that inward-curving part of the surface. (I am imagining the color as if it were all dropped straight down like rain, rather than a net that would grip all sides of the face.)
This makes sense to me, because meshgrid will only yield one value of Z for each (X,Y) pair.
[Maybe you realized all this already.]
I don't have enough experience to suggest a way to fix this. @Image Analyst's idea of using scatteredInterpolant seems like a good one, though.
scatteredInterpolant will only get you the top surface. It will not curl under and give you the bottom surface. However, if that's okay, you can use it but you should find those spurious spikes and filter them out so you just have valid data.

Sign in to comment.

Products

Release

R2022a

Community Treasure Hunt

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

Start Hunting!