python-OpenCV之normalize(归一化 )函数详解

归一化就是要把需要处理的数据经过处理后(通过某种算法)限制在你需要的一定范围内。

归一化的目的,是使得没有可比性的数据变得具有可比性,同时又保持相比较的两个数据之间的相对关系,如大小关系;或是为了作图,原来很难在一张图上作出来,归一化后就可以很方便的给出图上的相对位置等。

接下来主要讲述python_opencv中的normalize()函数,其原型为:

def normalize(src, dst, alpha=None, beta=None, norm_type=None, dtype=None, mask=None): # real signature unknown; restored from __doc__
    """
    normalize(src, dst[, alpha[, beta[, norm_type[, dtype[, mask]]]]]) -> dst
    .   @brief Normalizes the norm or value range of an array.
    .   
    .   The function cv::normalize normalizes scale and shift the input array elements so that
    .   \f[\| \texttt{dst} \| _{L_p}= \texttt{alpha}\f]
    .   (where p=Inf, 1 or 2) when normType=NORM_INF, NORM_L1, or NORM_L2, respectively; or so that
    .   \f[\min _I  \texttt{dst} (I)= \texttt{alpha} , \, \, \max _I  \texttt{dst} (I)= \texttt{beta}\f]
    .   
    .   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:
    .   @code{.cpp}
    .   vector positiveData = { 2.0, 8.0, 10.0 };
    .   vector 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);
    .   @endcode
    .   
    .   @param src input array.
    .   @param dst output array of the same size as src .
    .   @param alpha norm value to normalize to or the lower range boundary in case of the range
    .   normalization.
    .   @param beta upper range boundary in case of the range normalization; it is not used for the norm
    .   normalization.
    .   @param norm_type normalization type (see cv::NormTypes).
    .   @param 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).
    .   @param mask optional operation mask.
    .   @sa norm, Mat::convertTo, SparseMat::convertTo
    """
    pass
可能有些同学看不懂注释,没关系,我们通过几个例子来详细阐述该函数是如何实现归一化的。

在做图像处理的过程中,我们有时会对图像增强,增加其对比度,在增强之后就会利用该函数进行取值范围归一化。

cv2.normalize(img,img,0,255,cv2.NORM_MINMAX)

注:把所有的值缩放到【0,255】范围内,具体的操作方式可参考https://blog.csdn.net/lanmeng_smile/article/details/49903865

你可能感兴趣的:(python-OpenCV之normalize(归一化 )函数详解)