cv2.normalize()
array = cv2.normalize(array,None,0,255,cv2.NORM_MINMAX)
-
cv2.NORM_MINMAX :使用的放缩方式是 min_max 的方式 -
其对应的原理是:
x
^
=
x
?
m
i
n
m
a
x
?
m
i
n
?
(
m
a
x
′
?
m
i
n
′
)
+
m
i
n
′
\hat{x} = \frac{x-min}{max-min} * (max^{'}-min^{'}) + min^{'}
x^=max?minx?min??(max′?min′)+min′
-
x
^
\hat{x}
x^ 表示矩阵中任意一点归一化之后的值
-
x
x
x 表示矩阵中的所有原始值
-
m
i
n
,
m
a
x
min,max
min,max 分别代表矩阵中的最大值和最小值
-
m
a
x
′
,
m
i
n
′
max^{'}, min^{'}
max′,min′ 分别表示放缩的目标范围中的最大和最小值
-
例如当我们想把 0-255 值放缩到 0-1 之间,那么
m
a
x
=
255
,
m
i
n
=
0
,
m
a
x
′
=
1
,
m
i
n
′
=
0
max=255, min=0, max^{'}=1,min^{'}=0
max=255,min=0,max′=1,min′=0 就可以得到我们平时用的归一化公式:
x
^
=
x
?
m
i
n
m
a
x
?
m
i
n
\hat{x} = \frac{x-min}{max-min}
x^=max?minx?min? -
而当我们想把值从 0-1 之间放缩到 0-255 那么
m
a
x
=
1
,
m
i
n
=
0
,
m
a
x
′
=
255
,
m
i
n
′
=
0
max=1, min=0, max^{'}=255,min^{'}=0
max=1,min=0,max′=255,min′=0
x
^
=
x
?
0
1
?
0
?
(
255
?
0
)
+
0
=
x
?
255
\hat{x} = \frac{x-0}{1-0} * (255-0) + 0=x*255
x^=1?0x?0??(255?0)+0=x?255 -
除此之外,还有其他的归一化和放缩的选项,例如:
cv2.NORM_INF :归一化数组的(切比雪夫距离)L∞范数(绝对值的最大值)cv2.NORM_L1 : 归一化数组的(曼哈顿距离)L1-范数(绝对值的和)cv2.NORM_L2 : 归一化数组的(欧几里德距离)L2-范数 -
想知道具体的公式解析可以参考文章: opencv中归一化函数cv2.normalize()的原理讲解
代码示例
import cv2
test_set = torchvision.datasets.CIFAR10(root='../datasets', train=False, download=False, transform=None)
x = train_set.data[0]
x = x / 255.
x
array([[[0.23137255, 0.24313725, 0.24705882],
[0.16862745, 0.18039216, 0.17647059],
[0.19607843, 0.18823529, 0.16862745],
...,
[0.61960784, 0.51764706, 0.42352941],
[0.59607843, 0.49019608, 0.4 ],
[0.58039216, 0.48627451, 0.40392157]],
[[0.0627451 , 0.07843137, 0.07843137],
[0. , 0. , 0. ],
[0.07058824, 0.03137255, 0. ],
...,
[0.48235294, 0.34509804, 0.21568627],
[0.46666667, 0.3254902 , 0.19607843],
[0.47843137, 0.34117647, 0.22352941]],
[[0.09803922, 0.09411765, 0.08235294],
[0.0627451 , 0.02745098, 0. ],
[0.19215686, 0.10588235, 0.03137255],
...,
[0.4627451 , 0.32941176, 0.19607843],
[0.47058824, 0.32941176, 0.19607843],
[0.42745098, 0.28627451, 0.16470588]],
...,
[[0.81568627, 0.66666667, 0.37647059],
[0.78823529, 0.6 , 0.13333333],
[0.77647059, 0.63137255, 0.10196078],
...,
[0.62745098, 0.52156863, 0.2745098 ],
[0.21960784, 0.12156863, 0.02745098],
[0.20784314, 0.13333333, 0.07843137]],
[[0.70588235, 0.54509804, 0.37647059],
[0.67843137, 0.48235294, 0.16470588],
[0.72941176, 0.56470588, 0.11764706],
...,
[0.72156863, 0.58039216, 0.36862745],
[0.38039216, 0.24313725, 0.13333333],
[0.3254902 , 0.20784314, 0.13333333]],
[[0.69411765, 0.56470588, 0.45490196],
[0.65882353, 0.50588235, 0.36862745],
[0.70196078, 0.55686275, 0.34117647],
...,
[0.84705882, 0.72156863, 0.54901961],
[0.59215686, 0.4627451 , 0.32941176],
[0.48235294, 0.36078431, 0.28235294]]])
plt.imshow(x)
x_ = cv2.normalize(x,None,0,255,cv2.NORM_MINMAX)
x_
array([[[ 59, 62, 63],
[ 43, 46, 45],
[ 50, 48, 43],
...,
[158, 132, 108],
[152, 125, 102],
[148, 124, 103]],
[[ 16, 20, 20],
[ 0, 0, 0],
[ 18, 8, 0],
...,
[123, 88, 55],
[119, 83, 50],
[122, 87, 57]],
[[ 25, 24, 21],
[ 16, 7, 0],
[ 49, 27, 8],
...,
[118, 84, 50],
[120, 84, 50],
[109, 73, 42]],
...,
[[208, 170, 96],
[201, 153, 34],
[198, 161, 26],
...,
[160, 133, 70],
[ 56, 31, 7],
[ 53, 34, 20]],
[[180, 139, 96],
[173, 123, 42],
[186, 144, 30],
...,
[184, 148, 94],
[ 97, 62, 34],
[ 83, 53, 34]],
[[177, 144, 116],
[168, 129, 94],
[179, 142, 87],
...,
[216, 184, 140],
[151, 118, 84],
[123, 92, 72]]], dtype=uint8)
plt.imshow(x_)
|