当前位置:网站首页>详解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]的范围。
边栏推荐
- Are you ready to automate continuous deployment in ci/cd?
- 准备好在CI/CD中自动化持续部署了吗?
- C9 colleges and universities, doctoral students make a statement of nature!
- Leecode brush questions record sword finger offer 44 A digit in a sequence of numbers
- 深度学习之环境配置 jupyter notebook
- 英雄联盟|王者|穿越火线 bgm AI配乐大赛分享
- Leetcode(547)——省份数量
- How to set encoding in idea
- [force buckle]41 Missing first positive number
- 基于GO语言实现的X.509证书
猜你喜欢
Mujoco Jacobi - inverse motion - sensor
ZYNQ移植uCOSIII
学习使用代码生成美观的接口文档!!!
Are you ready to automate continuous deployment in ci/cd?
. Bytecode structure of class file
509 certificat basé sur Go
.class文件的字节码结构
Interface master v3.9, API low code development tool, build your interface service platform immediately
Data sharing of the 835 postgraduate entrance examination of software engineering in Hainan University in 23
C9高校,博士生一作发Nature!
随机推荐
Leecode brushes questions to record interview questions 17.16 massagist
Leecode brush questions record sword finger offer 43 The number of occurrences of 1 in integers 1 to n
Leetcode(547)——省份数量
Deep learning environment configuration jupyter notebook
Mujoco Jacobi - inverse motion - sensor
Leecode brush questions record sword finger offer 11 Rotate the minimum number of the array
Data sharing of the 835 postgraduate entrance examination of software engineering in Hainan University in 23
Set (generic & list & Set & custom sort)
学习使用代码生成美观的接口文档!!!
Deep understanding of distributed cache design
Advanced learning of MySQL -- Fundamentals -- four characteristics of transactions
一图看懂对程序员的误解:西方程序员眼中的中国程序员
什么是时间
Five different code similarity detection and the development trend of code similarity detection
用tkinter做一个简单图形界面
@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
Interface (interface related meaning, different abstract classes, interface callback)
Mujoco finite state machine and trajectory tracking
How to judge whether an element in an array contains all attribute values of an object
深度学习之环境配置 jupyter notebook