countEachLabel

Count number of pixel labels for each class of bigimageDatastore

Description

example

counts = countEachLabel(bigds) returns the number of each pixel label for all big images in big image datastore bigds.

Examples

collapse all

Load pixel label data.

load('buildingPixelLabeled.mat');

Specify the classes and pixel label IDs of the pixel label data.

pixelLabelID = [1 2 3 4];
classNames = ["sky" "grass" "building" "sidewalk"];

Create a bigimage to manage the pixel label data.

bigLabeledImage = bigimage(uint8(label),'Classes',classNames,'PixelLabelIDs',pixelLabelID);
bigimageshow(bigLabeledImage)

Create a bigimageDatastore that reads blocks of size 200-by-150 pixels at the finest resolution level from bigLabeledImage.

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

Input Arguments

collapse all

Big image datastore, specified as a bigimageDatastore object.

Output Arguments

collapse all

Label information, returned as a table that contains three variables.

Pixel Count VariablesDescription
NamePixel label class name
PixelCountNumber of pixels in class
ImagePixelCountTotal number of pixels in images that have an instance of the 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:

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

You can pass the calculated class weights to a pixelClassificationLayer.

Introduced in R2020a