Main Content

countEachLabel

Counts number of pixel labels for each class

Since R2021a

    Description

    example

    counts = countEachLabel(bimds) counts the occurrence of each pixel label in all the blocks represented by the blocked image datastore bimds.

    counts = countEachLabel(___,Name,Value) specifies additional parameters.

    If bimds contains categorical data, countEachLabel obtains the class names from the categories specified in the InitialValue property of the first blocked image. In this case, do not specify values for the 'Classes' and 'PixelLabelIDs' parameters. If bimds contains numeric data, you must provide values for the 'Classes' and 'PixelLabelIDs' parameters.

    Examples

    collapse all

    Create a blocked image from a sample label image.

    label_bim = blockedImage('yellowlily-segmented.png', 'BlockSize', [512 512]);

    Create a blocked image datastore from the blocked image.

    lbimds = blockedImageDatastore(label_bim);

    Count the labels in the blocked image datastore. Labels 0 and 3 both map to 'Background'.

    countEachLabel(lbimds, ...
          "Classes", ["Background", "Flower", "Leaf", "Background"],...
          "PixelLabelIDs", [0, 1, 2, 3])
    ans=3×3 table
            Name        PixelCount    BlockPixelCount
        ____________    __________    _______________
    
        "Background"    2.3706e+06      3.1457e+06   
        "Flower"        4.3349e+05      1.5729e+06   
        "Leaf"          3.4159e+05      2.0972e+06   
    
    

    Input Arguments

    collapse all

    Blocked image datastore, specified as a blockedImageDatastore object.

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

    Example: countEachLabel(lbimds, ... "Classes",["Background","Flower","Leaf","Background"],... "PixelLabelIDs",[0,1,2,3])

    Class names, specified as a string array or a cell array of char vectors.

    Example: "Classes",["Background","Flower","Leaf"]

    Data Types: char | string | cell

    Values for each label, specified as a numeric array of values with the same length as 'Classes'. This parameter provides the mapping from numeric values to the label class.

    Example: "PixelLabelIDs",[0,1,2,3]

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

    Use new or existing parallel pool, specified as a logical scalar true or false. If no parallel pool is active, countEachLabel opens a new pool based on the default parallel settings. This syntax requires Parallel Computing Toolbox™.

    Data Types: logical

    Output Arguments

    collapse all

    Counts the occurrence of each pixel label in all blocks represented by the blocked image datastore, returned as a table that contains three variables.

    Pixel Count VariablesDescription
    NamePixel label class name
    PixelCountNumber of pixels of a given class in all blocks
    ImagePixelCountTotal number of pixels in blocks that have an instance of the given class

    Tips

    You can use the label information returned by countEachLabel 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 c is defined as median(imageFreq)/imageBlockFreq(c) where imageBlockFreq(c) is the number of pixels of a given class divided by the total number of pixels in image blocks that had an instance of the given class c.

      imageBlockFreq = tbl.PixelCount ./ tbl.BlockPixelCount
      classWeights = median(imageBlockFreq) ./ imageBlockFreq
      

    You can pass the calculated class weights to a pixelClassificationLayer (Computer Vision Toolbox).

    Version History

    Introduced in R2021a

    See Also

    | | (Computer Vision Toolbox)