以下均为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