# how to find indices on a boundary?

조회 수: 37(최근 30일)
Itzik Ben Shabat 16 May 2013
Hi, I have a polygon defined by its vertices in 2D (i.e a nX2 matrix). I have a pixel image of size MxM. i wish to label all the pixels that are on the boundary f the polygon. is there a function that does this? i know inpolygon, but it labels all vertices that are inside the polygon, all i need is the boundary. any thoughts? thanks

댓글을 달려면 로그인하십시오.

### 답변(4개)

Thomas 16 May 2013
편집: Thomas 16 May 2013

#### 댓글 수: 0

댓글을 달려면 로그인하십시오.

Sean de Wolski 16 May 2013
You could use poly2mask() to create a mask of your polygon and then use bwboundaries or bwtraceboundaries() to draw the edges.
This will then give you a logical mask of the edges of your polygon. You can use this to do whatever you want to do, e.g. change the color in the original image etc.

#### 댓글 수: 2

Itzik Ben Shabat 16 May 2013
this helps but bwtraceboundaries return indices of boundary. i wish to create a mask of the boundary. is there a simple way to do this ?
Image Analyst 16 May 2013

댓글을 달려면 로그인하십시오.

Matt Kindig 16 May 2013
You can use the inpoly() function on the file exchange ( http://www.mathworks.com/matlabcentral/fileexchange/10391-fast-points-in-polygon-test). It can return points on the boundary separately from those inside. It's also a lot faster than the built-in inpolygon().

#### 댓글 수: 0

댓글을 달려면 로그인하십시오.

Image Analyst 16 May 2013
편집: Image Analyst 16 May 2013
Like Sean said, first use poly2mask() to create a binary image of a solid polygon from your list of perimeter coordinates. This solid polygon can be used as a mask.
Then, from the binary image and want the (x,y) coordinates of all of the pixels in the boundary, use bwboundaries() in the Image Processing Toolbox. See my Image Segmentation Tutorial for an example: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
If you want just the convex hull points (like where the rubber band would contact your object if you wrapped a rubber band around it) use convhull().
If you want an image of the perimeter pixels, use bwperim() in the Image Processing Toolbox. You can use this as a mask also, if you want.

#### 댓글 수: 4

표시 이전 댓글 수: 1
Itzik Ben Shabat 17 May 2013
Ok, this is exactly what i was looking for. however a new problem arises. some of my polygons extend outside the image. when i use poly2msk() and bwperim() i get that one of those boundaries is the edge of the image. is there a way around that? thansk a lot! you have been very helpful.
Image Analyst 17 May 2013
Where would you like the boundary to be in that case? It can't guess at what it might be outside the image.
Sean de Wolski 17 May 2013
Here's how I would approach this Itzik:
Find the minimum x and y values of your coordinates. Add the absolute values of these points + 1 to your x and y coordinates. Essentially this is shifting your points to be strictly greater than or equal to 1.
Then use poly2mask and IA/my steps from above. Then subtract off the values you added.

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by