Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Creating a 3D mask from two 3d surfaces

Subject: Creating a 3D mask from two 3d surfaces

From: Luca

Date: 15 Jun, 2012 13:34:07

Message: 1 of 11

Hello everyone!

Here's my problem. I'm quite positive that in matlab there is a SIMPLE way to do this, but it's not my field and I do not know where to start exactly.
I have (as an input) two sets of points. They represent the inner and outer surface of something shapes approximatively like an ellipsoidal annulus.
I need in output a 3d mask that is "true" for points between the inner and the outer surface and "false" for all the other points.
something like "poly2mask" but in 3d. The points are already ordered.
I think that I should probably start using delaunay triangulation to obtain surfaces from the points I have. And then??

thank you very much,

Luca

Subject: Creating a 3D mask from two 3d surfaces

From: Matt J

Date: 15 Jun, 2012 13:45:06

Message: 2 of 11

"Luca " <l.presottoRE@MOVE.campus.unimib.NOTit> wrote in message <jrfdkf$nlk$1@newscl01ah.mathworks.com>...
> Hello everyone!
>
> Here's my problem. I'm quite positive that in matlab there is a SIMPLE way to do this, but it's not my field and I do not know where to start exactly.
> I have (as an input) two sets of points. They represent the inner and outer surface of something shapes approximatively like an ellipsoidal annulus.
> I need in output a 3d mask that is "true" for points between the inner and the outer surface and "false" for all the other points.
> something like "poly2mask" but in 3d. The points are already ordered.
> I think that I should probably start using delaunay triangulation to obtain surfaces from the points I have. And then??
================

If the outer ellipse is given by the inequalities

(x-xc)'*Qo*(x-xc)<=1

and the inner ellipse is given by

(x-xc)'*Qi*(x-xc)<=1

Then you can do

[I,J,K]=ndgrid(1:N,1:N,1:N); %assume image is NxNxN

dx=[I(:)-xc(1),J(:)-xc(2),K(:)-xc(3)].';

quadform=@(Q) sum((Q*dx).*dx);

mask= quadform(Qo)<=1 & quadform(Qi)>=1;

 

Subject: Creating a 3D mask from two 3d surfaces

From: Luca

Date: 15 Jun, 2012 14:00:08

Message: 3 of 11

"Matt J" wrote in message <jrfe92$qs2$1@newscl01ah.mathworks.com>...

> If the outer ellipse is given by the inequalities
>
[cut]
> mask= quadform(Qo)<=1 & quadform(Qi)>=1;

I don't have an ellipse defined by its equation. Otherwise it would be too easy!!!
(actually I already do something like you suggested to extract a first approximation of my object out of the matrix).
I also don't want to fit an ellipse to this points. I just told that I was working with ellipsoidal shapes just to give the idea of what I was dealing with.

I repeat that I have two sets of points P1 and P2 (size (P)= (300,3) ) that describe two surfaces of my solid (like an ellipsoidal annulus but not exactly, and I need to mantain this difference).

How can I get a 3D mask from them?

Subject: Creating a 3D mask from two 3d surfaces

From: Matt J

Date: 15 Jun, 2012 15:22:09

Message: 4 of 11

"Luca " <l.presottoRE@MOVE.campus.unimib.NOTit> wrote in message <jrff58$1nq$1@newscl01ah.mathworks.com>...
>
> I repeat that I have two sets of points P1 and P2 (size (P)= (300,3) ) that describe two surfaces of my solid (like an ellipsoidal annulus but not exactly, and I need to mantain this difference).
>
> How can I get a 3D mask from them?
==============

If you're just looking for a mask that approximately covers the shape, you can use imfill and imdilate to create the mask. Round your P1, P2 to voxel locations, fill a mask with 1s at those locations, use imdilate to seal the gaps between the P1,P2, and finally use imfill to fill in the mask.

If you need to be more exact than that, then you need to fit some sort of surface to the points (if not an ideal ellipsoid then something else) and then use the analytical form of that surface to derive the mask. The points alone can't define the region indicated by your mask. There are infinitely many surfaces that could be run through them. You need a model of the surface to go with the points.

Subject: Creating a 3D mask from two 3d surfaces

From: Luca

Date: 15 Jun, 2012 15:44:07

Message: 5 of 11

"Matt J" wrote in message <jrfjv1$nr7$1@newscl01ah.mathworks.com>...
-------
> If you're just looking for a mask that approximately covers the shape, you can use imfill and imdilate to create the mask. Round your P1, P2 to voxel locations, fill a mask with 1s at those locations, use imdilate to seal the gaps between the P1,P2, and finally use imfill to fill in the mask.
=======
Well that's a clever idea. I might try to give a look at it and see how it works.
I think its effectiveness depends on how far my points that define the surface are.

------
> If you need to be more exact than that, then you need to fit some sort of surface to the points (if not an ideal ellipsoid then something else) and then use the analytical form of that surface to derive the mask. The points alone can't define the region indicated by your mask. There are infinitely many surfaces that could be run through them. You need a model of the surface to go with the points.
======

That goes back to my initial question. I can't fit an analytical form to my points because they represent the shape of something that's naturally irregular. And I need to preserve this.
Let's suppose a Delaunay triangulation of my points creates a surface that approximates correctly the surface I want.
Can I get a mask out of it?
Naively I suppose I should be able in some way to evaluate if each point in an ndgrid is inside or outside the surface.... But I never used triangulation commands in matlab until a week ago...and just to try to find a solution to this problem!
Am I right? Can I do this?
If yes... can you point me to some instructions/relevant documentations?

Subject: Creating a 3D mask from two 3d surfaces

From: Matt J

Date: 15 Jun, 2012 16:11:09

Message: 6 of 11

"Luca " <l.presottoRE@MOVE.campus.unimib.NOTit> wrote in message <jrfl87$t6p$1@newscl01ah.mathworks.com>...
>
> That goes back to my initial question. I can't fit an analytical form to my points because they represent the shape of something that's naturally irregular. And I need to preserve this.
> Let's suppose a Delaunay triangulation of my points creates a surface that approximates correctly the surface I want.
> Can I get a mask out of it?
> Naively I suppose I should be able in some way to evaluate if each point in an ndgrid is inside or outside the surface.... But I never used triangulation commands in matlab until a week ago...and just to try to find a solution to this problem!
> Am I right? Can I do this?
> If yes... can you point me to some instructions/relevant documentations?
======================

Looks like you could do it with TriScatterdInterp. Convert P1, P2 to spherical coordinates (relative to an origin at the centroid of the object). Use TriScatterdInterp to obtain your surface as a function surfdist(phi, theta) where surfdist is the distance of the surface from the origin at phi, theta.

Then proceed using ndgrid as before. Evaluate the distance of all voxels from the origin and compare this distance to surfdist(phi, theta) where phi, theta is a spherical voxel coordinate. surfdist<=truedistance means inside and surfdist>=truedistance means outside.

interpolate the surface at all the phi, theta

Subject: Creating a 3D mask from two 3d surfaces

From: Luca

Date: 15 Jun, 2012 16:29:08

Message: 7 of 11

"Matt J" wrote in message <jrfmqt$7g7$1@newscl01ah.mathworks.com>...
-------------------
> Looks like you could do it with TriScatterdInterp. Convert P1, P2 to spherical coordinates (relative to an origin at the centroid of the object). Use TriScatterdInterp to obtain your surface as a function surfdist(phi, theta) where surfdist is the distance of the surface from the origin at phi, theta.
>
> Then proceed using ndgrid as before. Evaluate the distance of all voxels from the origin and compare this distance to surfdist(phi, theta) where phi, theta is a spherical voxel coordinate. surfdist<=truedistance means inside and surfdist>=truedistance means outside.
>
> interpolate the surface at all the phi, theta
==============
Well... Actually I already have the points in spherical coordinates. And I had a post some days ago about how to convert a whole mask from spherical to cartesian coordinates just to do this. (and I did it with TriScatteredInterp in the end)
Too bad... I tought there was a simpler method with surface triangulation or something like that. (and this method with threescattered interpolant does not always work fine near phi = -pi/2 where too many points with different theta meets)

Coming to the good news... The idea of setting to 1 the points of a matrix near my coordinate and then doing an imclose... Works just fine! Much better than I could have dreamed of!
I would just need to take care of a few situations where maybe my points could find themselves too far or too close to each other compared to my STREL... But that's fine, no problem for now.
Just a last question. Which is the matlab-efficient (and maybe coding compact) way set to one the points indicated by my vector of coordinates?
After a few trials I went with:
mask(sub2ind(size(mask),round(points(:,1)),round(points(:,2)),round(points(:,3))) = 1;
but I cannot but think it's somehow "odd".
Is it correct/the best way?

Many thanks for your help.
And... Very good idea you came up with!

Subject: Creating a 3D mask from two 3d surfaces

From: Matt J

Date: 15 Jun, 2012 19:17:07

Message: 8 of 11

"Luca " <l.presottoRE@MOVE.campus.unimib.NOTit> wrote in message <jrfnsk$bnp$1@newscl01ah.mathworks.com>...
>
> Well... Actually I already have the points in spherical coordinates. And I had a post some days ago about how to convert a whole mask from spherical to cartesian coordinates just to do this. (and I did it with TriScatteredInterp in the end)
> Too bad... I tought there was a simpler method with surface triangulation or something like that. (and this method with threescattered interpolant does not always work fine near phi = -pi/2 where too many points with different theta meets)
>
> Coming to the good news... The idea of setting to 1 the points of a matrix near my coordinate and then doing an imclose... Works just fine! Much better than I could have dreamed of!
> I would just need to take care of a few situations where maybe my points could find themselves too far or too close to each other compared to my STREL... But that's fine, no problem for now.
> Just a last question. Which is the matlab-efficient (and maybe coding compact) way set to one the points indicated by my vector of coordinates?
> After a few trials I went with:
> mask(sub2ind(size(mask),round(points(:,1)),round(points(:,2)),round(points(:,3))) = 1;
> but I cannot but think it's somehow "odd".
> Is it correct/the best way?
=========

It doesn't look odd to me, although if class(mask)='logical', which presumably it would be, it is minutely more optimal to do

mask(sub2ind(size(mask),round(points(:,1)),round(points(:,2)),round(points(:,3))) = true; %Use logical true instead of '1'

That way, MATLAB doesn't have to do any casting.


One other thing to consider. Once you've found an initial guess of the mask morphologically, it is now possible to go back and use the TriScatteredInterp approach, but this time only apply it to voxel coordinates in your imdilated boundary (and then update your mask). This is a much smaller number of points and should make the interpolation task go much faster. All points not in the imdilated boundary you already know are safely inside or outside the shape, assuming you have a decent sized STREL.

The result should be the same as if you applied scattered interpolation to all voxels in the volume, but with much less computation time.

Subject: Creating a 3D mask from two 3d surfaces

From: Andrew

Date: 17 Sep, 2012 18:22:07

Message: 9 of 11

would this help? http://www.mathworks.com/matlabcentral/fileexchange/37863-blended-3d-poly2mask


"Luca " <l.presottoRE@MOVE.campus.unimib.NOTit> wrote in message <jrfdkf$nlk$1@newscl01ah.mathworks.com>...
> Hello everyone!
>
> Here's my problem. I'm quite positive that in matlab there is a SIMPLE way to do this, but it's not my field and I do not know where to start exactly.
> I have (as an input) two sets of points. They represent the inner and outer surface of something shapes approximatively like an ellipsoidal annulus.
> I need in output a 3d mask that is "true" for points between the inner and the outer surface and "false" for all the other points.
> something like "poly2mask" but in 3d. The points are already ordered.
> I think that I should probably start using delaunay triangulation to obtain surfaces from the points I have. And then??
>
> thank you very much,
>
> Luca

Subject: Creating a 3D mask from two 3d surfaces

From: Anusha

Date: 20 Sep, 2012 02:51:13

Message: 10 of 11

"Andrew " <adiamondcsi@gmail.com> wrote in message <k37pof$nc0$1@newscl01ah.mathworks.com>...
> would this help? http://www.mathworks.com/matlabcentral/fileexchange/37863-blended-3d-poly2mask
>
>
> "Luca " <l.presottoRE@MOVE.campus.unimib.NOTit> wrote in message <jrfdkf$nlk$1@newscl01ah.mathworks.com>...
> > Hello everyone!
> >
> > Here's my problem. I'm quite positive that in matlab there is a SIMPLE way to do this, but it's not my field and I do not know where to start exactly.
> > I have (as an input) two sets of points. They represent the inner and outer surface of something shapes approximatively like an ellipsoidal annulus.
> > I need in output a 3d mask that is "true" for points between the inner and the outer surface and "false" for all the other points.
> > something like "poly2mask" but in 3d. The points are already ordered.
> > I think that I should probably start using delaunay triangulation to obtain surfaces from the points I have. And then??
> >
> > thank you very much,
> >
> > Luca

Hai,

I have quite similar problem here. I want to generate a mask from one set of 3D point in x,y,z coordinates. Pls tell me how to do this, since i am not very clear on the discussed methodology since I am very new to this. Thanks.

Regards.

Subject: Creating a 3D mask from two 3d surfaces

From: Anusha

Date: 27 Sep, 2012 07:03:07

Message: 11 of 11

"Matt J" wrote in message <jrfjv1$nr7$1@newscl01ah.mathworks.com>...
> "Luca " <l.presottoRE@MOVE.campus.unimib.NOTit> wrote in message <jrff58$1nq$1@newscl01ah.mathworks.com>...
> >
> > I repeat that I have two sets of points P1 and P2 (size (P)= (300,3) ) that describe two surfaces of my solid (like an ellipsoidal annulus but not exactly, and I need to mantain this difference).
> >
> > How can I get a 3D mask from them?
> ==============
>
> If you're just looking for a mask that approximately covers the shape, you can use imfill and imdilate to create the mask. Round your P1, P2 to voxel locations, fill a mask with 1s at those locations, use imdilate to seal the gaps between the P1,P2, and finally use imfill to fill in the mask.
>

Can I get more specific on how to perform this. I need to get an approximate 3D ellipsoid mask from a set of 3D points.

Anusha

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us