位置: IT常识 - 正文
推荐整理分享Opencv学习之:将图片的值进行范围调整 cv2.normalize()(opencv语句),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:opencv jni,opencv convexhull,opencv gui,opencv convertto,opencv转pil,opencv convexhull,opencv converto,opencv convertto,内容如对您有帮助,希望把文章链接给更多的朋友!
cv2.NORM_MINMAX :使用的放缩方式是 min_max 的方式
其对应的原理是: x^=x−minmax−min∗(max′−min′)+min′\hat{x} = \frac{x-min}{max-min} * (max^{'}-min^{'}) + min^{'}x=max−minx−min∗(max′−min′)+min′
x^\hat{x}x 表示矩阵中任意一点归一化之后的值xxx 表示矩阵中的所有原始值min,maxmin,maxmin,max 分别代表矩阵中的最大值和最小值max′,min′max^{'}, min^{'}max′,min′ 分别表示放缩的目标范围中的最大和最小值例如当我们想把 0-255 值放缩到 0-1 之间,那么 max=255,min=,max′=1,min′=max=255, min=0, max^{'}=1,min^{'}=0max=255,min=0,max′=1,min′=0 就可以得到我们平时用的归一化公式: x^=x−minmax−min\hat{x} = \frac{x-min}{max-min}x=max−minx−min
而当我们想把值从 0-1 之间放缩到 0-255 那么 max=1,min=,max′=255,min′=max=1, min=0, max^{'}=255,min^{'}=0max=1,min=0,max′=255,min′=0 x^=x−1−∗(255−)+=x∗255\hat{x} = \frac{x-0}{1-0} * (255-0) + 0=x*255x=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.xarray([[[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_)上一篇:毕业设计-基于深度学习的医学影像分割(毕业设计基于51单片机多功能电子钟的设计)
下一篇:万字长文:Stable Diffusion 保姆级教程(万字长文护国安是谁写的)
友情链接: 武汉网站建设