当前位置:网站首页>详解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 Jacobi - inverse motion - sensor
- Matlab learning notes
- 准备好在CI/CD中自动化持续部署了吗?
- .class文件的字节码结构
- OSPF configuration command of Huawei equipment
- Attention slam: a visual monocular slam that learns from human attention
- X.509 certificate based on go language
- Model-Free Control
- Equals() and hashcode()
- Zynq transplant ucosiii
猜你喜欢

【批處理DOS-CMD命令-匯總和小結】-字符串搜索、查找、篩選命令(find、findstr),Find和findstr的區別和辨析

Deep understanding of distributed cache design

准备好在CI/CD中自动化持续部署了吗?

St table

AI super clear repair resurfaces the light in Huang Jiaju's eyes, Lecun boss's "deep learning" course survival report, beautiful paintings only need one line of code, AI's latest paper | showmeai info

三维扫描体数据的VTK体绘制程序设计

英雄联盟|王者|穿越火线 bgm AI配乐大赛分享

Jenkins' user credentials plug-in installation

Are you ready to automate continuous deployment in ci/cd?

Understand the misunderstanding of programmers: Chinese programmers in the eyes of Western programmers
随机推荐
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
Basic information of mujoco
MySQL learning notes (mind map)
[user defined type] structure, union, enumeration
5种不同的代码相似性检测,以及代码相似性检测的发展趋势
Are you ready to automate continuous deployment in ci/cd?
C9 colleges and universities, doctoral students make a statement of nature!
C Primer Plus Chapter 14 (structure and other data forms)
【批處理DOS-CMD命令-匯總和小結】-字符串搜索、查找、篩選命令(find、findstr),Find和findstr的區別和辨析
【YoloV5 6.0|6.1 部署 TensorRT到torchserve】环境搭建|模型转换|engine模型部署(详细的packet文件编写方法)
Alexnet experiment encounters: loss Nan, train ACC 0.100, test ACC 0.100
[force buckle]41 Missing first positive number
Understand the misunderstanding of programmers: Chinese programmers in the eyes of Western programmers
Data processing of deep learning
How to set encoding in idea
Idea automatically imports and deletes package settings
ActiveReportsJS 3.1中文版|||ActiveReportsJS 3.1英文版
String comparison in batch file - string comparison in batch file
mongodb客户端操作(MongoRepository)
Interface (interface related meaning, different abstract classes, interface callback)