about poly2mask function

4 views (last 30 days)
Reema Alhassan
Reema Alhassan on 4 Jul 2018
Edited: Walter Roberson on 4 Oct 2024
Could anyone please explain me this : I'm using poly2mask function to creat a mask of a polygon with an image the documentation of this image says "the function sets pixels that are inside the polygon to 1 and sets pixels outside the polygon to 0" if you could see here : https://ww2.mathworks.cn/help/images/ref/poly2mask.html#f6-465457 but here when I use it to create a mask of a polygon with my image
rx = [shape(i).X];
ry = [shape(i).Y];
cond1 = isnan(rx) | isnan(ry);
rx(cond1)=[];
ry(cond1)=[];
[X,Y] = meshgrid(x,y);
row=size(X,1);
col=size(X,2);
mask=poly2mask(rx,ry,row,col);
I did this to to test how many ones that I have sum(mask(:)) and the result is very small =9 which means that only 9 points inside the polygon which is impossible because row=1069 col=1363 X is 1069x1363 Y is 1069x1363 rx is 1x42708 ry is 1x42708
  1 Comment
DGM
DGM on 4 Oct 2024
This could easily be caused by the majority of vertices being outside the frame, or it could be due to a bunch of points being colinear or nearly colinear. Why would vertices be out of frame? It could also be caused by x and y components (of either the vertices or grid pairs) being transposed at some point, or it could be because the named x and y vectors are not in units of pixels, but are in some data units instead. It could also just be that the data points are simply not in range for some unforeseeable other reason.
It's not impossible to have the described result. Without the data or the code that defines the units of rx, ry, x, and y, answering the question is all just guesswork.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!