How can i fill in the vectors gotten from boundary function?

6 views (last 30 days)
Hello. I have coordinates of 2D points and found the closed boundary of the clouds. And I need a binarized 2D array which is filled inside the boundary.
How can I use the vector(k) returned from boundary function (boundary(x,y))?
for example,
x = gallery('uniformdata',30,1,1);
y = gallery('uniformdata',30,1,10);
k = boundary(x,y);
I have tried to save the plotted image and to use "find" function. But it was not a good idea because there were missing pixels (not colored pixels) at the edge. Thus, I am finding the way to use the vector (k) directly.
Thanks for all.

Accepted Answer

Image Analyst
Image Analyst on 17 Oct 2018
Edited: Image Analyst on 17 Oct 2018
If you have the Image Processing Toolbox and want a digital image, you can specify how many rows and columns there should be in the array and use poly2mask:
xb = x(k);
yb = y(k);
binaryImage = poly2mask(xb, yb, rows, columns);
If you don't want a binary image and just want to keep it on the graph/plot, use patch():
patch(xb, yb, 'red');

More Answers (1)

Steven Lord
Steven Lord on 17 Oct 2018
Generate sample data and compute the boundary.
rng default
x = gallery('uniformdata',30,1,1);
y = gallery('uniformdata',30,1,10);
k = boundary(x,y);
Use the coordinates and the boundary information to create a polyshape.
P = polyshape(x(k), y(k));
Make a grid that spans the rectangle that covers the polyshape.
[minx, maxx] = bounds(x);
[miny, maxy] = bounds(y);
queryx = linspace(minx, maxx, 20);
queryy = linspace(miny, maxy, 20);
[xm, ym] = meshgrid(queryx, queryy);
xm = xm(:);
ym = ym(:);
Determine which points are inside the polyshape.
isin = isinterior(P, xm, ym);
Plot the polyshape and the points. Points in the grid that are inside the polyshape are green circles, points that are outside the polyshape are red X markers.
plot(P, 'FaceColor', 'w');
hold on;
plot(xm(isin), ym(isin), 'go')
plot(xm(~isin), ym(~isin), 'rx')
  3 Comments
HyunSang Park
HyunSang Park on 18 Oct 2018
Thank you for the detailed explanation. But my matlab doesn't take polyshape function. Mine is 2017a and do I need to update to use polyshape?
Image Analyst
Image Analyst on 18 Oct 2018
I added that release to the question (at the upper right, like you should have done when you originally posted). So now, what about my answer above, though poly2mask requires the Image Processing toolbox, but patch doesn't)?

Sign in to comment.

Categories

Find more on Elementary Polygons in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!