Skip to main content

Statistical Functions

sum

Calculates the sum of array elements.
InputArray
Input array (1 to 4 channels)
Scalar
Sum of all array elements for each channel
Example:

mean

Calculates the mean value of array elements.
InputArray
Input array (1 to 4 channels)
InputArray
Optional operation mask (8-bit single channel)
Scalar
Mean value for each channel
Example:

meanStdDev

Calculates mean and standard deviation of array elements.
InputArray
Input array (1 to 4 channels)
OutputArray
Output parameter: calculated mean value
OutputArray
Output parameter: calculated standard deviation
InputArray
Optional operation mask
Example:

minMaxLoc

Finds the global minimum and maximum in an array.
InputArray
Input single-channel array
double*
Pointer to returned minimum value (can be NULL)
double*
Pointer to returned maximum value (can be NULL)
Point*
Pointer to returned minimum location (can be NULL)
Point*
Pointer to returned maximum location (can be NULL)
InputArray
Optional mask to select a sub-array
Example:

norm

Calculates an absolute array norm, absolute difference norm, or relative difference norm.
InputArray
First input array
InputArray
Second input array (for difference norms)
int
Type of norm: NORM_INF, NORM_L1, NORM_L2, NORM_L2SQR, NORM_HAMMING, NORM_HAMMING2
InputArray
Optional operation mask
double
Calculated norm value
Norm Types:
  • NORM_INF: max(|x_i|)
  • NORM_L1: Σ|x_i|
  • NORM_L2: √(Σx_i²)
  • NORM_L2SQR: Σx_i²
Example:

normalize

Normalizes the norm or value range of an array.
InputArray
Input array
InputOutputArray
Output array (same size as src)
double
Norm value to normalize to or lower range boundary in range normalization
double
Upper range boundary in range normalization (not used for norm normalization)
int
Normalization type: NORM_INF, NORM_L1, NORM_L2, or NORM_MINMAX
int
Optional depth of output array
InputArray
Optional operation mask
Example:

countNonZero

Counts non-zero array elements.
InputArray
Single-channel array
int
Number of non-zero elements
Example:

hasNonZero

Checks if there are any non-zero elements in array.
bool
True if at least one non-zero element exists
Example:

findNonZero

Returns the list of locations of non-zero pixels.
InputArray
Single-channel array (8-bit or floating-point)
OutputArray
Output array of Point locations (N×1 or 1×N)
Example:

Reduction Operations

reduce

Reduces a matrix to a vector by applying an operation along a specified dimension.
InputArray
Input array
OutputArray
Output vector
int
Dimension to reduce: 0 (reduce to single row), 1 (reduce to single column)
int
Reduction operation: REDUCE_SUM, REDUCE_AVG, REDUCE_MAX, REDUCE_MIN, REDUCE_SUM2
int
Optional depth of output array
Operations:
  • REDUCE_SUM: Sum of all rows/columns
  • REDUCE_AVG: Mean of all rows/columns
  • REDUCE_MAX: Maximum of all rows/columns
  • REDUCE_MIN: Minimum of all rows/columns
  • REDUCE_SUM2: Sum of squared values
Example:

reduceArgMin

Finds indices of minimum elements along specified axis.
InputArray
Input array
OutputArray
Output array of indices
int
Dimension to reduce along
bool
Whether to return last index in case of multiple minimum values
Example:

reduceArgMax

Finds indices of maximum elements along specified axis. Example:

Sorting

sort

Sorts each matrix row or column in ascending or descending order.
InputArray
Input single-channel array
OutputArray
Output array (same size and type as src)
int
Operation flags: SORT_EVERY_ROW, SORT_EVERY_COLUMN, SORT_ASCENDING, SORT_DESCENDING
Example:

sortIdx

Sorts each matrix row or column and returns sorted indices instead of values.
OutputArray
Output integer array of sorted indices
Example:

Linear Algebra

determinant

Returns the determinant of a square matrix.
InputArray
Input matrix (must be square)
double
Determinant value
Example:

trace

Returns the trace (sum of diagonal elements) of a matrix.
InputArray
Input matrix
Scalar
Trace of the matrix
Formula:
Example:

invert

Finds the inverse or pseudo-inverse of a matrix.
InputArray
Input floating-point matrix
OutputArray
Output matrix of the same size and type as src
int
Inversion method: DECOMP_LU, DECOMP_SVD, DECOMP_CHOLESKY
double
Reciprocal condition number (for SVD) or 0 if singular
Decomposition Methods:
  • DECOMP_LU: LU decomposition (fastest for well-conditioned matrices)
  • DECOMP_SVD: Singular value decomposition (works for singular matrices)
  • DECOMP_CHOLESKY: Cholesky decomposition (for symmetric positive-definite matrices)
Example:

solve

Solves one or more linear systems or least-squares problems.
InputArray
Coefficient matrix (A in Ax=b)
InputArray
Right-hand side matrix (b in Ax=b)
OutputArray
Output solution (x in Ax=b)
int
Solution method: DECOMP_LU, DECOMP_SVD, DECOMP_CHOLESKY, DECOMP_QR, DECOMP_NORMAL
bool
True if solution exists
Example:

eigen

Calculates eigenvalues and eigenvectors of a symmetric matrix.
InputArray
Input symmetric square matrix
OutputArray
Output vector of eigenvalues (in descending order)
OutputArray
Output matrix of eigenvectors (one per row)
bool
True if successful
Example:
This function is optimized for symmetric matrices. For general matrices, use eigenNonSymmetric().

calcCovarMatrix

Calculates covariance matrix of a set of vectors.
InputArray
Input samples (each row or column is a sample)
OutputArray
Output covariance matrix
InputOutputArray
Input or output mean vector
int
Operation flags: COVAR_SCRAMBLED, COVAR_NORMAL, COVAR_USE_AVG, COVAR_SCALE, COVAR_ROWS, COVAR_COLS
int
Type of output matrices (CV_32F or CV_64F)
Example:

Timing and Profiling

getTickCount

Returns the number of ticks since a certain event (e.g., machine startup).
int64
Current tick count
Example:

getTickFrequency

Returns the number of ticks per second.
double
Tick frequency in Hz

TickMeter

A class to measure passing time and calculate performance metrics. Example:

System Information

getNumberOfCPUs

Returns the number of logical CPUs available for the process.

setNumThreads

Sets the number of threads used by OpenCV for parallel regions.
int
Number of threads. Use 0 or negative values to reset to default

getNumThreads

Returns the number of threads used by OpenCV for parallel regions.

getBuildInformation

Returns full configuration time cmake output including version, compiler, enabled modules, etc. Example:

getVersionString

Returns library version string (e.g., “4.8.0”).

getCPUFeaturesLine

Returns a string containing CPU features enabled during compilation. Example output:
  • No marker: baseline features
  • *: features enabled in dispatcher
  • ?: features enabled but not available in hardware

Utility Functions

setUseOptimized

Enables or disables optimized code (SSE, AVX, etc.).
bool
True to enable optimizations, false to disable

useOptimized

Returns the status of optimized code usage.

checkRange

Checks every element of an input array for invalid values.
InputArray
Input array
bool
If true, doesn’t throw exceptions on invalid values
Point*
Optional output parameter for position of first invalid value
double
Minimum valid value (inclusive)
double
Maximum valid value (inclusive)
bool
True if all elements are within range and not NaN/Inf
Example:

patchNaNs

Replaces all NaN values in an array with specified value.
InputOutputArray
Input/output floating-point array
double
Value to replace NaNs with
Example:

LUT

Performs a look-up table transform of an array.
InputArray
Input array (8-bit elements)
InputArray
Look-up table (256 elements)
OutputArray
Output array (same size as src)
Example:

convertScaleAbs

Scales, calculates absolute values, and converts to 8-bit unsigned type.
InputArray
Input array
OutputArray
Output array (CV_8U type)
double
Scale factor
double
Delta added to scaled values
Formula:
Example:

PSNR

Computes Peak Signal-to-Noise Ratio (PSNR) between two images.
InputArray
First input array
InputArray
Second input array (same size and type as src1)
double
Maximum pixel value (255.0 for 8-bit images)
double
PSNR value in decibels (dB)
Example:
Higher PSNR values indicate better quality. Typical values range from 20 to 50 dB, with 30-50 dB being good quality.