Skip to main content

Statistical Functions

sum

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

mean

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

meanStdDev

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

minMaxLoc

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

norm

Calculates an absolute array norm, absolute difference norm, or relative difference norm.
src1
InputArray
First input array
src2
InputArray
Second input array (for difference norms)
normType
int
Type of norm: NORM_INF, NORM_L1, NORM_L2, NORM_L2SQR, NORM_HAMMING, NORM_HAMMING2
mask
InputArray
Optional operation mask
return
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.
src
InputArray
Input array
dst
InputOutputArray
Output array (same size as src)
alpha
double
Norm value to normalize to or lower range boundary in range normalization
beta
double
Upper range boundary in range normalization (not used for norm normalization)
norm_type
int
Normalization type: NORM_INF, NORM_L1, NORM_L2, or NORM_MINMAX
dtype
int
Optional depth of output array
mask
InputArray
Optional operation mask
Example:

countNonZero

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

hasNonZero

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

findNonZero

Returns the list of locations of non-zero pixels.
src
InputArray
Single-channel array (8-bit or floating-point)
idx
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.
src
InputArray
Input array
dst
OutputArray
Output vector
dim
int
Dimension to reduce: 0 (reduce to single row), 1 (reduce to single column)
rtype
int
Reduction operation: REDUCE_SUM, REDUCE_AVG, REDUCE_MAX, REDUCE_MIN, REDUCE_SUM2
dtype
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.
src
InputArray
Input array
dst
OutputArray
Output array of indices
axis
int
Dimension to reduce along
lastIndex
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.
src
InputArray
Input single-channel array
dst
OutputArray
Output array (same size and type as src)
flags
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.
dst
OutputArray
Output integer array of sorted indices
Example:

Linear Algebra

determinant

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

trace

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

invert

Finds the inverse or pseudo-inverse of a matrix.
src
InputArray
Input floating-point matrix
dst
OutputArray
Output matrix of the same size and type as src
flags
int
Inversion method: DECOMP_LU, DECOMP_SVD, DECOMP_CHOLESKY
return
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.
src1
InputArray
Coefficient matrix (A in Ax=b)
src2
InputArray
Right-hand side matrix (b in Ax=b)
dst
OutputArray
Output solution (x in Ax=b)
flags
int
Solution method: DECOMP_LU, DECOMP_SVD, DECOMP_CHOLESKY, DECOMP_QR, DECOMP_NORMAL
return
bool
True if solution exists
Example:

eigen

Calculates eigenvalues and eigenvectors of a symmetric matrix.
src
InputArray
Input symmetric square matrix
eigenvalues
OutputArray
Output vector of eigenvalues (in descending order)
eigenvectors
OutputArray
Output matrix of eigenvectors (one per row)
return
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.
samples
InputArray
Input samples (each row or column is a sample)
covar
OutputArray
Output covariance matrix
mean
InputOutputArray
Input or output mean vector
flags
int
Operation flags: COVAR_SCRAMBLED, COVAR_NORMAL, COVAR_USE_AVG, COVAR_SCALE, COVAR_ROWS, COVAR_COLS
ctype
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).
return
int64
Current tick count
Example:

getTickFrequency

Returns the number of ticks per second.
return
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.
nthreads
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.).
onoff
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.
a
InputArray
Input array
quiet
bool
If true, doesn’t throw exceptions on invalid values
pos
Point*
Optional output parameter for position of first invalid value
minVal
double
Minimum valid value (inclusive)
maxVal
double
Maximum valid value (inclusive)
return
bool
True if all elements are within range and not NaN/Inf
Example:

patchNaNs

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

LUT

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

convertScaleAbs

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

PSNR

Computes Peak Signal-to-Noise Ratio (PSNR) between two images.
src1
InputArray
First input array
src2
InputArray
Second input array (same size and type as src1)
R
double
Maximum pixel value (255.0 for 8-bit images)
return
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.