(Not recommended) Count number of pixel labels for each class of
countEachLabel function of the
bigimageDatastore object is not recommended. Use the
countEachLabel function associated with the
blockedImageDatastore object instead. For more information, see Compatibility Considerations.
Calculate Class Weights of Labeled Big Images
Load pixel label data.
Specify the classes and pixel label IDs of the pixel label data.
pixelLabelID = [1 2 3 4]; classNames = ["sky" "grass" "building" "sidewalk"];
bigimage to manage the pixel label data.
bigLabeledImage = bigimage(uint8(label),'Classes',classNames,'PixelLabelIDs',pixelLabelID); bigimageshow(bigLabeledImage)
bigimageDatastore that reads blocks of size 200-by-150 pixels at the finest resolution level from
level = 1; blockSize = [200 150]; biglabelds = bigimageDatastore(bigLabeledImage,level,'BlockSize',blockSize);
Count the number of pixel labels for each class.
tbl = countEachLabel(biglabelds)
tbl=4×3 table Name PixelCount BlockPixelCount __________ __________ _______________ "sky" 81525 1.58e+05 "grass" 32983 51200 "building" 1.8036e+05 3.072e+05 "sidewalk" 10491 51200
Balance the classes by using uniform prior weighting.
prior = 1/numel(classNames); uniformClassWeights = prior ./ tbl.PixelCount
uniformClassWeights = 4×1 10-4 × 0.0307 0.0758 0.0139 0.2383
Balance the classes by using inverse frequency weighting.
totalNumberOfPixels = sum(tbl.PixelCount); freq = tbl.PixelCount / totalNumberOfPixels; invFreqClassWeights = 1./freq
invFreqClassWeights = 4×1 3.7456 9.2580 1.6931 29.1067
Balance the classes by using median frequency weighting.
freq = tbl.PixelCount ./ tbl.BlockPixelCount; medFreqClassWeights = median(freq) ./ freq
medFreqClassWeights = 4×1 1.0689 0.8562 0.9394 2.6917
counts — Label information
Label information, returned as a table that contains three variables.
|Pixel Count Variables||Description|
|Pixel label class name|
|Number of pixels in class|
|Total number of pixels in images that have an instance of the class|
You can use the label information returned by
calculate class weights for class balancing. For example, for labeled pixel data
Uniform class balancing weights each class such that each contains a uniform prior probability:
numClasses = height(tbl) prior = 1/numClasses; classWeights = prior./tbl.PixelCount
Inverse frequency balancing weights each class such that underrepresented classes are given higher weight:
totalNumberOfPixels = sum(tbl.PixelCount) frequency = tbl.PixelCount / totalNumberOfPixels; classWeights = 1./frequency
Median frequency balancing weights each class using the median frequency:
imageFreq = tbl.PixelCount ./ tbl.ImagePixelCount classWeights = median(imageFreq) ./ imageFreq
You can pass the calculated class weights to a
pixelClassificationLayer (Computer Vision Toolbox).
Version HistoryIntroduced in R2020a
countEachLabel function is not recommended
Not recommended starting in R2021a
Starting in R2021a, the
bigimageDatastore object and its object
functions, which operate on data from
bigimage objects, are no
longer recommended. Instead, use the
blockedImageDatastore object and its object functions, which operate
on data from
Although there are no plans to remove the
object and its
countEachLabel function at this time, switch to
blockedImageDatastore object and its
countEachLabel function to take advantage of the additional
capabilities and flexibility.