当前位置:网站首页>详解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]的范围。
边栏推荐
- Mujoco produces analog video
- Dr selection of OSPF configuration for Huawei devices
- Data analysis course notes (III) array shape and calculation, numpy storage / reading data, indexing, slicing and splicing
- 基於GO語言實現的X.509證書
- Memory optimization of Amazon memorydb for redis and Amazon elasticache for redis
- 【JokerのZYNQ7020】AXI_ EMC。
- Leecode brushes questions to record interview questions 17.16 massagist
- 接口(接口相关含义,区别抽象类,接口回调)
- Advanced learning of MySQL -- basics -- basic operation of transactions
- Attention slam: a visual monocular slam that learns from human attention
猜你喜欢
stm32F407-------SPI通信
【软件逆向-自动化】逆向工具大全
Telerik UI 2022 R2 SP1 Retail-Not Crack
Idea automatically imports and deletes package settings
Attention slam: a visual monocular slam that learns from human attention
C9 colleges and universities, doctoral students make a statement of nature!
【YoloV5 6.0|6.1 部署 TensorRT到torchserve】环境搭建|模型转换|engine模型部署(详细的packet文件编写方法)
Amazon MemoryDB for Redis 和 Amazon ElastiCache for Redis 的内存优化
Jenkins' user credentials plug-in installation
Zynq transplant ucosiii
随机推荐
threejs图片变形放大全屏动画js特效
Distributed cache
Policy Gradient Methods
509 certificat basé sur Go
Leecode brush questions record sword finger offer 43 The number of occurrences of 1 in integers 1 to n
String comparison in batch file - string comparison in batch file
ActiveReportsJS 3.1中文版|||ActiveReportsJS 3.1英文版
Linear algebra of deep learning
代码克隆的优缺点
Leecode brushes questions and records interview questions 01.02 Determine whether it is character rearrangement for each other
Core knowledge of distributed cache
Leecode brush questions record sword finger offer 44 A digit in a sequence of numbers
Mujoco second order simple pendulum modeling and control
Idea automatically imports and deletes package settings
Interface (interface related meaning, different abstract classes, interface callback)
@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
equals()与hashCode()
Notes of training courses selected by Massey school
Understand the misunderstanding of programmers: Chinese programmers in the eyes of Western programmers
Rails 4 asset pipeline vendor asset images are not precompiled