how to set nan to areas outsid a polygon on the image

5 views (last 30 days)
i have the vertices and facec of a figure i want to set all area outsid this boudaries to nan and save the image matrix. image matrix contain the polygon defined by vertices and faces and nan outside it.plz some help

Answers (7)

Kevin Moerman
Kevin Moerman on 31 Jan 2012
Then its more complicated. When you say vertices and faces do you mean a tesselation? e.g. a Delaunay tesselation? Of the head volume or just the outer surface?
For volume (e.g. tetrahedrons) tesselations you could use:
t = tsearchn(X,TES,XI);
which returns NaN for points outside of the tesselation (actually outside of the convex hull in case of a Delaunay tesselation). I've tried this for some non-Delaunay derived tesselations but I'm not sure if that is allowed/100% correct so you should check. (you could also look at using [k,d]= dsearchn(X,TES,XI) which allows you to check for distance to your vertices for each point and then formulate a treshold).
If you only have vertices and faces for the boundary of the head (e.g. surface triangulation) then you could add a known inner point to which all triangles can form a valid tetrahedron and then create your own tesselation. But this may not work for all boundary triangles (I imagine it gets complicated at the ears).
Alternatively if you work with image/voxel coordinates only and you have the indices for the voxels in the head. Lets say these are IND and M is your 3D image matrix then you could create your image matrix containing nan's as follows:
M_nan=nan(size(M)); %Matrix filled with nan's the size of M M_nan(IND)=M(IND); %Replace entries at IND with M(IND)
Or if L is a logic array describing the head entries then simply M(~L)=nan;
Kevin

Image Analyst
Image Analyst on 31 Jan 2012
If you know the coordinates for each slice, then you can use poly2mask() to get a binary image of your "in polygon" area. Then say
sliceImage(~binaryMask) = nan;
Do that for every slice.
  1 Comment
samiy goller
samiy goller on 9 Sep 2012
hi dear, if i want separate spine from back,in MRI image. what are we do?

Sign in to comment.


Kevin Moerman
Kevin Moerman on 31 Jan 2012
If its a 2D image you can use the inpolygon command.
Your polygon (which needs to form the boundary of your patch data) is defined by all vertices that have edges that are only part of 1 face.

zawaiter
zawaiter on 31 Jan 2012
thanks kevin for care actually i have 3d array contain a scanned MRI data (head).some data belong to the head and some belong to the film surrounding the head, i want to set all data not belong to the head (outside the head vertices)to nan.so it is a 3d image.can u explain more.

zawaiter
zawaiter on 31 Jan 2012
kevin thanks again,i will need sometime to read and understand ur answer,alittle complicated for me, but i just want tell you for now that i have got the vertices and faces simply by isosurface(D,70).

zawaiter
zawaiter on 2 Feb 2012
thank you very much kevin and image analyst your comments was of great help to me.

zawaiter
zawaiter on 8 Feb 2012
hi friends i am still having a problem with this issue,i have wrote two codes one depending on what kevin explaind and a second one depending on image analyst explanation.in the first code i noticed some ones in one side outside the head area and some zeros in the other side inside the head area,as if the mask is shifted to one side.i guess the solution may be in the part you mentioned about getting the distance and make a thresholding but i dont know how to do it.the second code recommended by image analyst,fit the head boundaries very well although there is alot of zeros inside the head area.i believe i might made alittle mistake here or there but i am not able to loctate it and fix it.i wish you allow me to send the codes and data to you and take look at them,i will be so greatfull.thanks again

Community Treasure Hunt

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

Start Hunting!