# Math and statistics » Error measures module#include "diplib/statistics.h"

Quantifying the difference between images.

• Reference

## Classes

struct dip::SpatialOverlapMetrics
Holds return values for the function dip::SpatialOverlap.

## Functions

auto dip::MeanError(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {}) -> dip::dfloat
Calculates the mean error difference between corresponding sample values of in and reference.
auto dip::MeanSquareError(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {}) -> dip::dfloat
Calculates the mean square error difference between corresponding sample values of in and reference.
dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {}) -> dip::dfloat
Calculates the root mean square (RMS) error difference between corresponding sample values of in and reference.
dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {}) -> dip::dfloat
Calculates the mean absolute error difference between corresponding sample values of in and reference.
dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {}) -> dip::dfloat
Calculates the maximum absolute error difference between corresponding sample values of in and reference.
auto dip::IDivergence(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {}) -> dip::dfloat
Calculates the I-divergence between corresponding sample values of in and reference.
auto dip::InProduct(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {}) -> dip::dfloat
Calculates the sum of the product of corresponding sample values of in and reference.
auto dip::LnNormError(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {}, dip::dfloat order = 2.0) -> dip::dfloat
Calculates the order norm difference between corresponding sample values of in and reference.
auto dip::PSNR(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {}, dip::dfloat peakSignal = 0.0) -> dip::dfloat
Calculates the peak signal-to-noise ratio, in dB.
auto dip::SSIM(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {}, dip::dfloat sigma = 1.5, dip::dfloat K1 = 0.01, dip::dfloat K2 = 0.03) -> dip::dfloat
Calculates the structural similarity index (a visual similarity measure)
dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {}, dip::uint nBins = 256) -> dip::dfloat
Calculates the mutual information, in bits, using a histogram with nBins-by-nBins bins.
auto dip::SpatialOverlap(dip::Image const& in, dip::Image const& reference) -> dip::SpatialOverlapMetrics
Compares a segmentation result in to the ground truth reference.
auto dip::DiceCoefficient(dip::Image const& in, dip::Image const& reference) -> dip::dfloat
Compares a segmentation result in to the ground truth reference, determining the Dice coefficient.
auto dip::JaccardIndex(dip::Image const& in, dip::Image const& reference) -> dip::dfloat
Compares a segmentation result in to the ground truth reference, determining the Jaccard index.
auto dip::Specificity(dip::Image const& in, dip::Image const& reference) -> dip::dfloat
Compares a segmentation result in to the ground truth reference, determining the specificity of the segmentation.
auto dip::Sensitivity(dip::Image const& in, dip::Image const& reference) -> dip::dfloat
Compares a segmentation result in to the ground truth reference, determining the sensitivity of the segmentation.
auto dip::Accuracy(dip::Image const& in, dip::Image const& reference) -> dip::dfloat
Compares a segmentation result in to the ground truth reference, determining the accuracy of the segmentation.
auto dip::Precision(dip::Image const& in, dip::Image const& reference) -> dip::dfloat
Compares a segmentation result in to the ground truth reference, determining the precision of the segmentation.
dip::Image const& in, dip::Image const& reference) -> dip::dfloat
Computes the Hausdorff distance between two binary images.
dip::Image const& in, dip::Image const& reference) -> dip::dfloat
Computes the modified Hausdorff distance between two binary images.
dip::Image const& in, dip::Image const& reference) -> dip::dfloat
Computes the sum of minimal distances (SMD) between two binary images.
dip::Image const& in, dip::Image const& reference) -> dip::dfloat
Computes the complement weighted sum of minimal distances (CWSMD) between two binary images.
auto dip::Entropy(dip::Image const& in, dip::Image const& mask = {}, dip::uint nBins = 256) -> dip::dfloat
Calculates the entropy, in bits, using a histogram with nBins bins.
dip::Image const& in, dip::Image const& mask = {}) -> dip::dfloat
Estimates the variance of white Gaussian noise in an image.

## Class documentation

### struct dip::SpatialOverlapMetrics

Holds return values for the function dip::SpatialOverlap.

Variables
dip::dfloat truePositives Number of true positives
dip::dfloat trueNegatives Number of true negatives
dip::dfloat falsePositives Number of false positives
dip::dfloat falseNegatives Number of false negatives
dip::dfloat diceCoefficient The F1-measure, harmonic mean between precision (PPV) and sensitivity (recall, TPR).
dip::dfloat jaccardIndex The ratio of the intersection to the union.
dip::dfloat sensitivity Also called recall, true positive rate (TPR).
dip::dfloat specificity Also called true negative rate (TNR).
dip::dfloat fallout Also called false positive rate (FPR), equal to 1-specificity.
dip::dfloat accuracy Fraction of correctly segmented pixels.
dip::dfloat precision Also called positive predictive value (PPV).

## Function documentation

### dip::dfloat dip::MeanError(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {})

Calculates the mean error difference between corresponding sample values of in and reference.

Optionally the mask image can be used to exclude pixels from the calculation by setting the value of these pixels in mask to zero.

Singleton expansion is applied if the image sizes don’t match. Complex input is not allowed, use dip::MeanAbsoluteError instead.

### dip::dfloat dip::MeanSquareError(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {})

Calculates the mean square error difference between corresponding sample values of in and reference.

Optionally the mask image can be used to exclude pixels from the calculation by setting the value of these pixels in mask to zero.

Singleton expansion is applied if the image sizes don’t match. For complex input, uses the modulus of the differences.

### dip::dfloat dip::RootMeanSquareError(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {})

Calculates the root mean square (RMS) error difference between corresponding sample values of in and reference.

Optionally the mask image can be used to exclude pixels from the calculation by setting the value of these pixels in mask to zero.

Singleton expansion is applied if the image sizes don’t match. For complex input, uses the modulus of the differences.

### dip::dfloat dip::MeanAbsoluteError(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {})

Calculates the mean absolute error difference between corresponding sample values of in and reference.

Optionally the mask image can be used to exclude pixels from the calculation by setting the value of these pixels in mask to zero.

Singleton expansion is applied if the image sizes don’t match.

### dip::dfloat dip::MaximumAbsoluteError(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {})

Calculates the maximum absolute error difference between corresponding sample values of in and reference.

Optionally the mask image can be used to exclude pixels from the calculation by setting the value of these pixels in mask to zero.

Singleton expansion is applied if the image sizes don’t match.

### dip::dfloat dip::IDivergence(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {})

Calculates the I-divergence between corresponding sample values of in and reference.

The I-Divergence is defined as and is divided by the number of pixels. It is the -log of a Poisson distribution with the stirling approximation for . For , the stirling approximation would fail, is returned.

Optionally the mask image can be used to exclude pixels from the calculation by setting the value of these pixels in mask to zero.

Singleton expansion is applied if the image sizes don’t match. Complex input is not allowed.

### dip::dfloat dip::InProduct(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {})

Calculates the sum of the product of corresponding sample values of in and reference.

The sum of the product of in and reference corresponds to the value of the cross-correlation function at zero displacement (see dip::CrossCorrelationFT) and is a measure of correlation between the two images.

Optionally the mask image can be used to exclude pixels from the calculation by setting the value of these pixels in mask to zero.

Singleton expansion is applied if the image sizes don’t match. Complex input is not allowed.

### dip::dfloat dip::LnNormError(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {}, dip::dfloat order = 2.0)

Calculates the order norm difference between corresponding sample values of in and reference.

Optionally the mask image can be used to exclude pixels from the calculation by setting the value of these pixels in mask to zero.

Singleton expansion is applied if the image sizes don’t match. For complex input, uses the modulus of the differences.

### dip::dfloat dip::PSNR(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {}, dip::dfloat peakSignal = 0.0)

Calculates the peak signal-to-noise ratio, in dB.

If peakSignal<=0, computes the peak signal as the difference between maximum and minimum in reference. PSNR is defined as 20 * log10( peakSignal / RootMeanSquareError( in, reference, mask )).

Optionally the mask image can be used to exclude pixels from the calculation by setting the value of these pixels in mask to zero.

Singleton expansion is applied if the image sizes don’t match.

### dip::dfloat dip::SSIM(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {}, dip::dfloat sigma = 1.5, dip::dfloat K1 = 0.01, dip::dfloat K2 = 0.03)

Calculates the structural similarity index (a visual similarity measure)

Returns the average SSIM, computed locally in a Gaussian window of size sigma, using constants K1 and K2. These two constants should be small (<<1) positive values and serve to avoid instabilities.

Optionally the mask image can be used to exclude pixels from the calculation by setting the value of these pixels in mask to zero.

The two input images must be real-valued. Singleton expansion is applied if the image sizes don’t match.

### dip::dfloat dip::MutualInformation(dip::Image const& in, dip::Image const& reference, dip::Image const& mask = {}, dip::uint nBins = 256)

Calculates the mutual information, in bits, using a histogram with nBins-by-nBins bins.

Optionally the mask image can be used to exclude pixels from the calculation by setting the value of these pixels in mask to zero.

The two input images must be real-valued and scalar. Singleton expansion is applied if the image sizes don’t match.

### dip::SpatialOverlapMetrics dip::SpatialOverlap(dip::Image const& in, dip::Image const& reference)

Compares a segmentation result in to the ground truth reference.

Both in and reference are expected to be either binary images, or real-valued images in the range [0,1] indicating likelihoods (i.e. a fuzzy segmentation). If images contain values outside that range, the results will be meaningless. Both images must be scalar and of the same sizes.

If only one measure is of interest, it will be more efficient to use one of the specialized functions: dip::DiceCoefficient, dip::JaccardIndex, dip::Specificity, dip::Sensitivity, dip::Accuracy, or dip::Precision.

### dip::dfloat dip::DiceCoefficient(dip::Image const& in, dip::Image const& reference)

Compares a segmentation result in to the ground truth reference, determining the Dice coefficient.

The Dice coefficient (also known as Sørensen–Dice coefficient) is defined as twice the area of the intersection of in and reference divided by the sum of their areas,

The Dice coefficient is equivalent to the harmonic mean between precision and sensitivity or recall (i.e. the F1 score):

dfloat dice = dip::DiceCoefficient( a, b );
dfloat alsoDice = 2.0 / ( 1.0 / dip::Precision( a, b ) + 1.0 / dip::Sensitivity( a, b ));


Note that this measure is symmetric, that is, it yields the same result if one switches the two images.

The two input images must have the same sizes, be scalar, and either binary or real-valued. Real-valued inputs will be considered as fuzzy segmentations, and expected to be in the range [0,1].

### dip::dfloat dip::JaccardIndex(dip::Image const& in, dip::Image const& reference)

Compares a segmentation result in to the ground truth reference, determining the Jaccard index.

The Jaccard index is defined as the area of the intersection of in and reference divided by their union,

Note that this measure is symmetric, that is, it yields the same result if one switches the two images.

The two input images must have the same sizes, be scalar, and either binary or real-valued. Real-valued inputs will be considered as fuzzy segmentations, and expected to be in the range [0,1].

### dip::dfloat dip::Specificity(dip::Image const& in, dip::Image const& reference)

Compares a segmentation result in to the ground truth reference, determining the specificity of the segmentation.

Specificity is also referred to as True Negative Rate, and is computed as the ratio of true negatives to the total amount of negatives in the reference image,

The two input images must have the same sizes, be scalar, and either binary or real-valued. Real-valued inputs will be considered as fuzzy segmentations, and expected to be in the range [0,1].

### dip::dfloat dip::Sensitivity(dip::Image const& in, dip::Image const& reference)

Compares a segmentation result in to the ground truth reference, determining the sensitivity of the segmentation.

Sensitivity, also referred to as recall or True Positive Rate, is computed as the ratio of the true positives to the total amount of positives in the reference image,

Note that precision and sensitivity are each others mirror, that is, precision yields the same result as sensitivity with switched input images.

The two input images must have the same sizes, be scalar, and either binary or real-valued. Real-valued inputs will be considered as fuzzy segmentations, and expected to be in the range [0,1].

### dip::dfloat dip::Accuracy(dip::Image const& in, dip::Image const& reference)

Compares a segmentation result in to the ground truth reference, determining the accuracy of the segmentation.

Accuracy is defined as the ratio of correctly classified pixels to the total number of pixels,

Note that this measure is symmetric, that is, it yields the same result if one switches the two images.

The two input images must have the same sizes, be scalar, and either binary or real-valued. Real-valued inputs will be considered as fuzzy segmentations, and expected to be in the range [0,1].

### dip::dfloat dip::Precision(dip::Image const& in, dip::Image const& reference)

Compares a segmentation result in to the ground truth reference, determining the precision of the segmentation.

Precision, or Positive Predictive Value, is defined as the ratio of the true positives to the total amount of positives in the in image,

Note that precision and sensitivity are each others mirror, that is, precision yields the same result as sensitivity with switched input images.

The two input images must have the same sizes, be scalar, and either binary or real-valued. Real-valued inputs will be considered as fuzzy segmentations, and expected to be in the range [0,1].

### dip::dfloat dip::HausdorffDistance(dip::Image const& in, dip::Image const& reference)

Computes the Hausdorff distance between two binary images.

The Hausdorff distance is the largest distance one can find between a point in one set and the nearest point in the other set.

Note that this measure is symmetric, that is, it yields the same result if one switches the two images.

The two input images must have the same sizes, be scalar, and binary.

### dip::dfloat dip::ModifiedHausdorffDistance(dip::Image const& in, dip::Image const& reference)

Computes the modified Hausdorff distance between two binary images.

The modified Hausdorff distance is the average distance between a point in one set and the nearest point in the other set. The measure is made symmetric by swapping the two sets and using the largest obtained result.

Note that this measure is symmetric, that is, it yields the same result if one switches the two images.

The two input images must have the same sizes, be scalar, and binary.

### dip::dfloat dip::SumOfMinimalDistances(dip::Image const& in, dip::Image const& reference)

Computes the sum of minimal distances (SMD) between two binary images.

The sum of minimal distances is the sum of distances between a point in one set and the nearest point in the other set. The measure is made symmetric by swapping the two sets and averaging the results.

Note that this measure is symmetric, that is, it yields the same result if one switches the two images.

The two input images must have the same sizes, be scalar, and binary.

### dip::dfloat dip::ComplementWeightedSumOfMinimalDistances(dip::Image const& in, dip::Image const& reference)

Computes the complement weighted sum of minimal distances (CWSMD) between two binary images.

The complement weighted sum of minimal distances is the weighted sum of distances between a point in the first set and the nearest point in the second set. The weights are given by the distance of the point in the first set to its boundary. The measure is made symmetric by swapping the two sets and summing the results.

Note that this measure is symmetric, that is, it yields the same result if one switches the two images.

The two input images must have the same sizes, be scalar, and binary.

### dip::dfloat dip::Entropy(dip::Image const& in, dip::Image const& mask = {}, dip::uint nBins = 256)

Calculates the entropy, in bits, using a histogram with nBins bins.

Optionally the mask image can be used to exclude pixels from the calculation by setting the value of these pixels in mask to zero.

The input image must be real-valued and scalar.

### dip::dfloat dip::EstimateNoiseVariance(dip::Image const& in, dip::Image const& mask = {})

Estimates the variance of white Gaussian noise in an image.

The method assumes white (uncorrelated) noise, with a Gaussian distribution and zero mean. It may fail if the image contains complex or fine-grained texture.

If mask is not given, creates a mask that avoids edge regions.