1. 程式人生 > >opencv中歸一化函式normalize()的原理講解

opencv中歸一化函式normalize()的原理講解

1. 歸一化

歸一化就是要把需要處理的資料經過處理後(通過某種演算法)限制在你需要的一定範圍內。

首先歸一化是為了後面資料處理的方便,其次是保證程式執行時收斂加快。歸一化的具體作用是歸納統一樣本的統計分佈性。歸一化在0-1之間是統計的概率分佈,歸一化在某個區間上是統計的座標分佈。歸一化有同一、統一和合一的意思。

歸一化的目的,是使得沒有可比性的資料變得具有可比性,同時又保持相比較的兩個資料之間的相對關係,如大小關係;或是為了作圖,原來很難在一張圖上作出來,歸一化後就可以很方便的給出圖上的相對位置等。


2. opencv中的歸一化函式normalize()

opencv文件中的介紹如下:

C++:void normalize
(InputArray src, InputOutputArray dst, double alpha=1, double beta=0, int norm_type=NORM_L2, int dtype=-1, InputArray mask=noArray() )
C++:void normalize(const SparseMat& src, SparseMat& dst, double alpha, int normType)
Python:cv2.normalize(src[, dst[, alpha[, beta[, norm_type[, dtype[, mask]]]
]]]) → dst
Parameters:
  • src – input array.
  • dst – 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.
  • normType – normalization type (see the details below).
  • 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.

The functions normalize scale and shift the input array elements so that

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

(where p=Inf, 1 or 2) when normType=NORM_INFNORM_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.

從上面可以看成,opencv提供了四種不同的歸一化方式,分別為NORM_INFNORM_MINMAX,NORM_L1NORM_L2。下面分別解釋一下各自代表的含義及歸一化公式。

NORM_MINMAX:陣列的數值被平移或縮放到一個指定的範圍,線性歸一化。

比如歸一化到(min,max)範圍內:


NORM_INF: 歸一化陣列的(切比雪夫距離)L範數(絕對值的最大值)


NORM_L1 :歸一化陣列的(曼哈頓距離)L1-範數(絕對值的和)


NORM_L2:歸一化陣列的(歐幾里德距離)L2-範數


而其中的dtype為負數時,輸出陣列的type與輸入陣列的type相同;

否則,輸出陣列與輸入陣列只是通道數相同,而tpye=CV_MAT_DEPTH(dtype).