%% One-Hot Encode Image for Semantic Segmentation
% Use onehotencode to encode a matrix of class labels, such as a semantic segmentation of an image.
% Define a simple 15-by-15 pixel segmentation matrix of class labels.

A = "blue";
B = "green";
C = "black";

A = repmat(A,8,15);
B = repmat(B,7,5);
C = repmat(C,7,5);

seg = [A;B C B];

% Convert the segmentation matrix into a categorical array.

seg = categorical(seg);

% One-hot encode the segmentation matrix into an array of type single. Expand the encoded labels into the third dimension.

encSeg = onehotencode(seg,3,"single");

% Check the size of the encoded segmentation.

size(encSeg)

% The three possible classes of the pixels in the segmentation matrix are encoded as vectors in the third dimension.
% Copyright 2020 The MathWorks, Inc.