# countEachLabel

Counts number of pixel labels for each class

## Description

specifies additional parameters.`counts`

= countEachLabel(___,`Name,Value`

)

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

## Input Arguments

## Output Arguments

## 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

`blockedImage`

| `blockedImageDatastore`

| `pixelClassificationLayer`

(Computer Vision Toolbox)