MATLAB Answers

How to convert a closed curve into a binary mask

10 views (last 30 days)
Maura Monville
Maura Monville on 9 Jul 2019
Edited: KSSV on 9 Jul 2019
I have a set of 2D closed curves represented by their X and Y ordered coordinates in a text file.
I have attached one of the curves as an example.
My goal is to convert the enclosed area into a binary image.
Builtin dunction "poly2mask" generates a completely black square whereas I would like the region inside the closed curve to be black
and the region outside the curve to be white.
Thank you in advance for any suggestion and help.
Regards,
Maura

  0 Comments

Sign in to comment.

Accepted Answer

KSSV
KSSV on 9 Jul 2019
Edited: KSSV on 9 Jul 2019
data = importdata('data.txt') ;
x = data(:,1) ;
y = data(:,2) ;
N = 500 ;
xi = linspace(min(x),max(x),N) ;
yi = linspace(min(y),max(y),N) ;
[X,Y] = meshgrid(xi,yi) ;
idx = inpolygon(X,Y,x,y) ;
Z = ones(size(X)) ;
Z(idx) = 0 ;
imshow(Z)
untitled.bmp

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products


Release

R2019a

Translated by