Opencv學習筆記——圖像像素的歸一化處理

以下均爲github上opencv的個人學習筆記,原路徑如下:

https://github.com/JimmyHHua/opencv_tutorials

源碼示例:

import cv2 as cv 
import numpy as np

src = cv.imread("C:/Users/Mark/Desktop/CV/opencv_tutorials-master/opencv_tutorials-master/python/code_011/test.png")
cv.namedWindow("input",cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)
gray = cv.cvtColor(src,cv.COLOR_BGR2GRAY)

#轉換爲浮點數類型數組
gray = np.float32(gray)
print(gray)

#scale and shift by NORM_MINMAX
dst = np.zeros(gray.shape,dtype = np.float32)
cv.normalize(gray, dst=dst, alpha=0, beta=1.0,norm_type=cv.NORM_MINMAX)
print(dst)
cv.imshow("NORM_MINMAX", np.uint8(dst*255))

#scale and shift by NORM_INF
dst1 = np.zeros(gray.shape,dtype=np.float32)
cv.normalize(gray,dst=dst1,alpha=1.0,beta=0,norm_type=cv.NORM_INF)
print(dst1)
cv.imshow("NORM_INF", np.uint8(dst1*255))

#scale and shift by NORM_L1
dst2 = np.zeros(gray.shape,dtype = np.float32)
cv.normalize(gray,dst=dst2,alpha=1.0,beta=0,norm_type=cv.NORM_L1)
print(dst2)
cv.imshow("NORM_L1", np.uint8(dst2*10000000))

#scale and shift by NORM_L2
dst3 = np.zeros(gray.shape,dtype = np.float32)
cv.normalize(gray,dst=dst3,alpha=1.0,beta=0,norm_type=cv.NORM_L2)
print(dst3)
cv.imshow("NORM_L2", np.uint8(dst3*10000))

cv.waitKey(0)
cv.destroyAllWindows()

這裏用到了cv.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

                                                                                       

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

                                                                    

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_INF, NORM_MINMAX,NORM_L1和NORM_L2。下面分別解釋一下各自代表的含義及歸一化公式。

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

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

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

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

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

而其中的dtype爲負數時,輸出數組的type與輸入數組的type相同;

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

原文鏈接:https://blog.csdn.net/kuweicai/article/details/78988886

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章