当前位置:网站首页>详解OpenCV的矩阵规范化函数normalize()【范围化矩阵的范数或值范围(归一化处理)】,并附NORM_MINMAX情况下的示例代码
详解OpenCV的矩阵规范化函数normalize()【范围化矩阵的范数或值范围(归一化处理)】,并附NORM_MINMAX情况下的示例代码
2022-07-06 17:06:00 【昊虹图像算法】
函数normalize()有两个原型:
原型一:
void cv::normalize( InputArray src,
InputOutputArray dst,
double alpha = 1,
double beta = 0,
int norm_type = NORM_L2,
int dtype = -1,
InputArray mask = noArray()
)
原型二:
void cv::normalize( const SparseMat & src,
SparseMat & dst,
double alpha,
int normType
)
原型一的适用对象是密集矩阵,通常我们的矩阵都是密集矩阵。
原型二的适用对象是稀疏矩阵,稀疏矩阵的情况不是很常见,所以原型二在这篇博文中暂不作介绍。
在介绍各参数的意义前,先说下函数normalize()的作用。
函数normalize()有两个作用:
第一个作用是用于规范化矩阵的范数;
第二个作用是用于规范化矩阵的值范围,规范化矩阵的值范围通常就是我们说的归一化处理。
第二个作用是我们经常用到的,所以这篇博文重点介绍其第二个作用,第一个作用暂不作详细介绍。
当它用于规范化矩阵的范数时,即norm_type = NORM_MINMAX时,它通过线性缩放和平移操作实现如下目标:
∥ dst ∥ L p = alpha \| \texttt{dst} \| _{L_p}= \texttt{alpha} ∥dst∥Lp=alpha
上式中的dst代表目标矩阵。 ∥ dst ∥ L p \| \texttt{dst} \| _{L_p} ∥dst∥Lp代表目标矩阵的范数值。
上式中p的取值可以是Inf、1、2,对应于三种不同的范数。
当p=Inf时,对应的参数normType取值为NORM_INF;
当p=1时,对应的参数normType取值为NORM_L1;
当p=2时,对应的参数normType取值为NORM_L2。
当它用于规范化矩阵的值范围时,它通过线性缩放和平移操作实现如下目标:
即此时函数normalize()会把原矩阵中的值范围从[min(src), max(src)]按比例线性变换到[alpha, beta]的范围。
根据上面的这个目标,可知实现的具体数学表达式如下:
d s t ( i , j ) − a l p h a b e t a − a l p h a = s r c ( i , j ) − m i n ( s r c ) m a x ( s r c ) − m i n ( s r c ) \frac{dst(i,j)-alpha}{beta-alpha}=\frac{src(i,j)-min(src)}{max(src)-min(src)} beta−alphadst(i,j)−alpha=max(src)−min(src)src(i,j)−min(src)
根据上式,可以得到dst(i,j)的表达式,如下:
d s t ( i , j ) = ( b e t a − a l p h a ) ∗ s r c ( i , j ) − m i n ( s r c ) m a x ( s r c ) − m i n ( s r c ) + a l p h a dst(i,j)=(beta-alpha)*\frac{src(i,j)-min(src)}{max(src)-min(src)}+alpha dst(i,j)=(beta−alpha)∗max(src)−min(src)src(i,j)−min(src)+alpha
接下来,开始介绍其原型一各参数的意义,这里再把原型一复制过来。
void cv::normalize( InputArray src,
InputOutputArray dst,
double alpha = 1,
double beta = 0,
int norm_type = NORM_L2,
int dtype = -1,
InputArray mask = noArray()
)
- src—输入矩阵
- dst—输出矩阵。
- alpha—在这个函数第一个作用的情况中,它表示norm value;在第二个作用的情况中,函数normalize()会把原矩阵中的值范围从[min(src), max(src)]按比例线性变换到[alpha, beta]的范围。
- beta—这个函数第一个作用的情况中,没有作用,是一个无效参数。在第二个作用的情况中,函数normalize()会把原矩阵中的值范围从[min(src), max(src)]按比例线性变换到[alpha, beta]的范围。
- norm_type—这个参数决定了函数normalize()具体作何种操作。其可取值如下表所示:

最常用的是最后一个枚举值,即NORM_MINMAX,此时它实现的目标和具体的原理在介绍这个函数的参数意义前已经讲得很清楚了。
最后,附一个当norm_type = NORM_MINMAX时的Python示例代码。
# 博主微信/QQ 2487872782
# 有问题可以联系博主交流
# 有图像处理需求也请联系博主
# 图像处理技术交流QQ群 271891601
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# OpenCV的版本为4.4.0
import cv2 as cv
import numpy as np
A = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]], dtype='uint8')
B = A.copy()
cv.normalize(A, B, alpha=100, beta=200, norm_type=cv.NORM_MINMAX)
运行结果如下:
可见,通过函数normalize()的操作,将矩阵A中元素的值范围从[1,25]线性按比例变换到了[100,200]的范围。
边栏推荐
- Linear algebra of deep learning
- [user defined type] structure, union, enumeration
- Value Function Approximation
- 509 certificat basé sur Go
- Learn to use code to generate beautiful interface documents!!!
- Rails 4 asset pipeline vendor asset images are not precompiled
- PXE server configuration
- [Niuke classic question 01] bit operation
- 智能运维应用之道,告别企业数字化转型危机
- Attention SLAM:一种从人类注意中学习的视觉单目SLAM
猜你喜欢

Win10 startup error, press F9 to enter how to repair?
![[software reverse - solve flag] memory acquisition, inverse transformation operation, linear transformation, constraint solving](/img/16/71692f4cf89b7dc0fe62946e59ecd1.png)
[software reverse - solve flag] memory acquisition, inverse transformation operation, linear transformation, constraint solving

Testers, how to prepare test data

@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.

Configuring the stub area of OSPF for Huawei devices

Article management system based on SSM framework

Mujoco finite state machine and trajectory tracking
![[Niuke classic question 01] bit operation](/img/f7/e3a482c379ec9bbdb453a05e5e08cb.jpg)
[Niuke classic question 01] bit operation

Slam d'attention: un slam visuel monoculaire appris de l'attention humaine

深度学习之线性代数
随机推荐
37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme
【批处理DOS-CMD命令-汇总和小结】-字符串搜索、查找、筛选命令(find、findstr),Find和findstr的区别和辨析
Policy Gradient Methods
The programmer resigned and was sentenced to 10 months for deleting the code. Jingdong came home and said that it took 30000 to restore the database. Netizen: This is really a revenge
alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
[daily problem insight] prefix and -- count the number of fertile pyramids in the farm
建立自己的网站(17)
C Primer Plus Chapter 14 (structure and other data forms)
Lombok 同时使⽤ @Data 和 @Builder 的坑,你中招没?
Markov decision process
Trace tool for MySQL further implementation plan
37 page overall planning and construction plan for digital Village revitalization of smart agriculture
Deep understanding of distributed cache design
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
stm32F407-------SPI通信
ActiveReportsJS 3.1中文版|||ActiveReportsJS 3.1英文版
How to judge whether an element in an array contains all attribute values of an object
Data analysis course notes (V) common statistical methods, data and spelling, index and composite index
什么是时间