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

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.

 Risposta accettata

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');

Più risposte (1)

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 Commenti

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?
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)?

Accedi per commentare.

Categorie

Prodotti

Release

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by