# mpoly2mask - convert multiple polygons to a mask

バージョン 1.2.0.0 (6.17 KB) 作成者:
Converts an object with multiple boundary contours to a logical mask
ダウンロード: 1.7K

ライセンスの表示

MPOLY2MASK Convert multiple region-of-interest polygons to a mask.
BW = mpoly2mask(XY, BWSIZE) computes a binary region-of-interest mask,
BW, from multiple region-of-interest polygons represented by XY. The size
of BW (in rows, columns) is given in the 2-element BWSIZE. The class of
BW is logical with 1 inside the set of XY polygons and 0 outside.
XY is a cell array with separate polygon (xy) coordinates given as an
N-by-2 array in each cell element. Alternatively, XY can be an N-by-2
array containing all polygon coordinates, with each successive polygon
separated by a pair of NaN elements. By default, the output mask is the
union (mask1 & mask2 & ...) of all separate polygons.

BW = mpoly2mask(..., 'style','ij') will interpret the input given in XY
as being IJ-style contours rather than XY-style. This is consistent with
MATLAB's bwboundaries command which returns contours in ij format (row
coordinates in the first column, column coordinates in the second).

BW = mpoly2mask(XY, XVEC, YVEC) where XVEC and YVEC are vectors,
specifies the locations of the pixel centers of BW

BW = mpoly2mask(XY, ..., A) allows complex relationships between the
contours in XY. A is a square logical matrix with side length equal to
the number of contours in XY, whose rows and columns correspond to each
separate contour in XY. The boundaries *enclosed* by the (i)th contour,
or the boundary *enclosing* the i(th) contour can both be found using A
as follows:

enclosing_boundary = find(A(i,:));
enclosed_boundaries = find(A(:,i));

For example, a donut shape can be represented by two circular contours in
XY (XY{1} being the larger contour, X{2} being the smaller, and A having
contents:

A = [0 0
1 0]

Here, A(2,1) indicates that the second circle in XY is enclosed by the
first. A may be sparse, as created by the command sparse(2,1,true,2,2).
The logical matrix A can be ommited, in which case it defaults to a fully
false matrix indicating no enclosing contours, and the contents of BW
will be the union of all contours in XY.

Example 1:
[B,~,~,A] = bwboundaries(BW);
figure
subplot(1,2,1), imshow(BW), title('Original')
subplot(1,2,2), imshow(BW2), title('mpoly2mask recreation')

Example 2:
% Define polygon vertices
xyPts = {
[-90 -60; -10 40; 50 -50] ... A triangle (object 1)
[-80 -80; -80 20; 30 20; 30 -80]}; ... A square (object 2)};
% Define mask pixel locations
xVec = -150:150;
yVec = -100:50;
% Define different connectivity styles between polygons
[A,B] = deal(false(2));
A(1,2) = 1; % The first object is removed from the second
B(2,1) = 1; % The second object is removed from the first
BW = mpoly2mask(xyPts, xVec, yVec); % Union all objects
BW_A = mpoly2mask(xyPts, xVec, yVec, A);
BW_B = mpoly2mask(xyPts, xVec, yVec, B);
% Diisplay
figure
subplot(1,3,1), imagesc(xVec,yVec,BW), axis image, title 'Union'
subplot(1,3,2), imagesc(xVec,yVec,BW_A), axis image, title 'Connectivity A'
subplot(1,3,3), imagesc(xVec,yVec,BW_B), axis image, title 'Connectivity B'

Note that masks with contours extracted using bwboundaries that are then
directly passed to poly2mask (as in the first example) may have 1-pixel
border regions that do not match. This issue, including a simple
work-around is discussed at:

### 引用

Sven (2024). mpoly2mask - convert multiple polygons to a mask (https://www.mathworks.com/matlabcentral/fileexchange/46428-mpoly2mask-convert-multiple-polygons-to-a-mask), MATLAB Central File Exchange. 取得済み .

すべてのリリースと互換性あり
##### プラットフォームの互換性
Windows macOS Linux

### Community Treasure Hunt

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

Start Hunting!
バージョン 公開済み リリース ノート
1.2.0.0

More descriptive title text

1.1.0.0

Made a little more robust to zero/one connectivity matrix instead of logical mask. Added extra help example.

1.0.0.0