Main Content

poly2label

Create label matrix from set of ROIs

Since R2020b

Description

example

L= poly2label(roiPositions,roiLabelIDs,imageSize)creates a numeric label matrixLfrom the regions of interest (ROIs) defined inroiPositions.roiLabelIDsspecifies the numeric ID for each ROI inroiPositions.imageSizespecifies the size of the output label matrix.

L= poly2label(roiPositions,roiLabelIDs,R)creates a numeric label matrix where the spatial referencing objectRspecifies the coordinate system used by the ROI positions inroiPositions. The function assumes that the ROI positions are in world limits defined byR. TheImageSizeproperty ofRspecifies the size of the label matrixL.

Examples

collapse all

Read an image into the workspace and display it.

figure I = imread('baby.jpg'); imshow(I)

Initialize the ROI position cell array and image size variables. If you passpoly2labela size value containing three dimensions, it only uses the first two,m-by-n.

numPolygon = 3; roiPositions = cell(numPolygon,1); imSize = size(I);

Specify the coordinates of three ROIs in theroiPositionscell array. In this example, the first ROI is a triangle, requiring coordinates for three corners. The other two ROIs are quadrilaterals, requiring coordinates for four corners.

roiPositions{1} = [500 500; 250 1300; 1000 500]; roiPositions{2} = [1500 1100; 1500 1400; 2000 1400; 2000 700]; roiPositions{3} = [80 2600; 480 2700; 470 3000; 100 3000];

Create an array for label IDs the same size as theroiPositionscell array.

roilabelID = zeros(numPolygon,1,'uint8');

Specify label ID values that correspond to the order in which you listed the ROIs inroiPositions. The first ROI is a triangle so give it the label1. The next two ROIs are both quadrilaterals so give them the label2.

roilabelID(1) = 1; roilabelID(2) = 2; roilabelID(3) = 2;

Draw the three ROIs on the figure.

forid = 1:numPolygon drawpolygon('Position',roiPositions{id});end

Figure contains an axes object. The axes object contains 4 objects of type image, images.roi.polygon.

Create a label matrix from the ROIs. The label matrix is the same size,m-by-n, as the original image.

L = poly2label(roiPositions,roilabelID,imSize);

Display the label matrix overlaid on the original image.

figure; B = labeloverlay(I,L); imshow(B);

Figure contains an axes object. The axes object contains an object of type image.

Input Arguments

collapse all

Coordinate vectors, specified as a 1-by-Pcell array of numeric vectors, wherePis the total number of ROIs. Each cell array element is ans-by-2 coordinate vector of the form[x1 y1; …; xs ys], wheresis the total number of vertices for that ROI. Eachx,ypair defines a vertex of the ROI. If the ROI shape is not already closed, thepoly2label函数自动关闭的形状。你可以年代pecify any number of ROIs.

Data Types:double|cell

Labels for each ROI, specified as a numeric vector of the same length as theroiPositionsargument. Each label in the vector corresponds to the ROI in the associated position in theroiPositionscell array.

poly2labelassigns the value0to all background pixels in the output image.

Data Types:single|double|int8|int16|int32|uint8|uint16|uint32

Size of the output label matrix, specified as a 2- or 3-element numeric vector. If you specify a 3-element vector, thepoly2labelfunction uses only the first two dimensions,m-by-n.

Data Types:double

Spatial referencing information, specified as animref2dobject.

Output Arguments

collapse all

Label matrix, returned as anm-by-nmatrix of nonnegative values of the same data type asroiLabelIDs. Pixels labeled0are the background.

Tips

  • Thepoly2labelfunction sets pixels that are inside an ROI to a label value. For more information about classifying pixels on the ROI boundary, seeClassify Pixels That Are Partially Enclosed by ROI.

  • When the positions of several ROIs overlap each other, the ROI label with the lowest index number in theroiPositionscell array overwrites the other ROIs.

Version History

Introduced in R2020b