Main Content


Count occurrence of pixel or box labels



counts = countEachLabel(ds) returns a table containing information about the pixel or box labels and count for the input datastore, ds.


collapse all

Load a table that contains bounding boxes with labels for vehicles.


Load a table that contains bounding boxes with labels for stop signs and cars.


Combine ground truth boxes and labels, excluding the image filenames in the first column.

vehiclesTbl  = vehicleTrainingData(:,2:end);
stopSignsTbl = stopSignsAndCars(:,2:end);

Create a boxLabelDatastore using 2 tables, one with vehicle label data and the other with stop signs label data.

blds = boxLabelDatastore(vehiclesTbl,stopSignsTbl);
tbl = countEachLabel(blds)
tbl=4×3 table
     Label      Count    ImageCount
    ________    _____    __________

    vehicle      336        295    
    stopSign      42         41    
    carRear       10          9    
    carFront       9          8    

Create a histogram plot using the labels and the respective label counts.


Create another histogram overlaying the respective image counts.

hold on;

Input Arguments

collapse all

Datastore with labeled data for training a semantic segmentation network or an object detection network, specified as a pixelLabelDatastore or boxLabelDatastore object.

Output Arguments

collapse all

Label information, returned as a table. The labeled data table contain three variables.

For pixelLabelDatastore inputs, the counts output contains:

Pixel Count VariablesDescription
NamePixel label class name
PixelCountNumber of pixels in class
ImagePixelCountTotal number of pixels in images that had an instance of the class

For boxLabelDatastore inputs, the counts output table contains:

Box Count VariablesDescription
LabelBox label class name
CountTotal number of labels of the class across all images
ImageCountTotal number of images that contain one or more instances of the class


The output of countEachLabel can be used to calculate class weights for class balancing. For example, for labeled pixel data information in tbl:

  • 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. The weight for each class is defined as median(imageFreq)/imageFreq(c), where imageFreq(c) represents the number of pixels of the class divided by the total number of pixels in images that had an instance of the class (c):

    imageFreq = tbl.PixelCount ./ tbl.ImagePixelCount
    classWeights = median(imageFreq) ./ imageFreq

When training the network using trainnet (Deep Learning Toolbox), you can pass the calculated class weights to the loss function. For an example, see Semantic Segmentation Using Deep Learning.

Version History

Introduced in R2017b

See Also