Skip to content

normalize

Core and matricesfunctionOpenCV 5.0.0
import { normalize } from '@banou/opencv-wasm'

Use after await initOpenCV(). See the initialization and named imports guide.

ARGUMENTSsrc, dst, alpha, beta
FUNCTIONnormalize
RETURN TYPEvoid
Call structure. A void return can still write to destination arguments. The parameter descriptions define inputs, outputs and ownership.

Normalizes the norm or value range of an array.

The function cv::normalize normalizes scale and shift the input array elements so that

\| \texttt{dst} \| _{L_p}= \texttt{alpha}

(where p=Inf, 1 or 2) when normType=NORM_INF, NORM_L1, or NORM_L2, respectively; or so that

\min _I  \texttt{dst} (I)= \texttt{alpha} , \, \, \max _I  \texttt{dst} (I)= \texttt{beta}

when normType=NORM_MINMAX (for dense arrays only). The optional mask specifies a sub-array to be normalized. This means that the norm or min-n-max are calculated over the sub-array, and then this sub-array is modified to be normalized. If you want to only use the mask to calculate the norm or min-max but modify the whole array, you can use norm and Mat::convertTo.

In case of sparse matrices, only the non-zero values are analyzed and transformed. Because of this, the range transformation for sparse matrices is not allowed since it can shift the zero level.

Possible usage with some positive example data:

    vector<double> positiveData = { 2.0, 8.0, 10.0 };
    vector<double> normalizedData_l1, normalizedData_l2, normalizedData_inf, normalizedData_minmax;

    // Norm to probability (total count)
    // sum(numbers) = 20.0
    // 2.0      0.1     (2.0/20.0)
    // 8.0      0.4     (8.0/20.0)
    // 10.0     0.5     (10.0/20.0)
    normalize(positiveData, normalizedData_l1, 1.0, 0.0, NORM_L1);

    // Norm to unit vector: ||positiveData|| = 1.0
    // 2.0      0.15
    // 8.0      0.62
    // 10.0     0.77
    normalize(positiveData, normalizedData_l2, 1.0, 0.0, NORM_L2);

    // Norm to max element
    // 2.0      0.2     (2.0/10.0)
    // 8.0      0.8     (8.0/10.0)
    // 10.0     1.0     (10.0/10.0)
    normalize(positiveData, normalizedData_inf, 1.0, 0.0, NORM_INF);

    // Norm to range [0.0;1.0]
    // 2.0      0.0     (shift to left border)
    // 8.0      0.75    (6.0/8.0)
    // 10.0     1.0     (shift to right border)
    normalize(positiveData, normalizedData_minmax, 1.0, 0.0, NORM_MINMAX);

Note: Due to rounding issues, min-max normalization can result in values outside provided boundaries. If exact range conformity is needed, following workarounds can be used:

  • use double floating point precision (dtype = CV_64F)
  • manually clip values (cv::max(res, left_bound, res), cv::min(res, right_bound, res) or np.clip)

See: norm, Mat::convertTo, SparseMat::convertTo

normalize(src: Mat, dst: Mat, alpha: number, beta: number, norm_type: number, dtype: number, mask: Mat): void;
6 available overloads
normalize(src: Mat, dst: Mat): void;
normalize(src: Mat, dst: Mat, alpha: number): void;
normalize(src: Mat, dst: Mat, alpha: number, beta: number): void;
normalize(src: Mat, dst: Mat, alpha: number, beta: number, norm_type: number): void;
normalize(src: Mat, dst: Mat, alpha: number, beta: number, norm_type: number, dtype: number): void;
normalize(src: Mat, dst: Mat, alpha: number, beta: number, norm_type: number, dtype: number, mask: Mat): void;
src

input array.

dst

Input/output value, modified by the native operation. output array of the same size as src .

alpha

norm value to normalize to or the lower range boundary in case of the range normalization.

beta

upper range boundary in case of the range normalization; it is not used for the norm normalization.

norm_type

normalization type (see cv::NormTypes).

dtype

when negative, the output array has the same type as src; otherwise, it has the same number of channels as src and the depth =CV_MAT_DEPTH(dtype).

mask

optional operation mask of type CV_8U, CV_8S or CV_Bool.

These signatures describe this package. Upstream documentation can mention optional backends that are absent from this build. Check runtime compatibility before choosing a backend or file format.