How to mask each region in image within boundary

I would like to compute some factor inside the a boundary. I have the coordinates of this boundary. I must use a rectangle which each iteration is moving though the image. However, I cannot apply boundary for this rectangle. how to set the inside the of the boundary to 1 and 0 for the rest for each rectangle in MATLAB.

Upvotes: 0

Views: 268

Answers (1)

Jablonski
Jablonski

Reputation: 23

Turn your coordinates into integers. Then, function poly2mask can convert your boundary to a binary mask, with this syntax:

BW = poly2mask(x, y, m, n)

where x and y are vectors with a list of coordinates of boundary points; m and n are the width and height for the new binary image BW. Example (adapted from link above):

x = [63 186 190 54 63];
y = [60 60 204 209 60];
bw = poly2mask(x,y,256,256);
imshow(bw)

Link to resulting figure

Then, for a rectangle in coordinates (i,j), with width w and height h, you would get the information you want as

i = 128; j = 128;
w = 100; h = 100;

aux = bw(i:i + h - 1, j:j + w - 1);

figure, imshow(aux)

Upvotes: 2

Related Questions