作者:翟天保Steven
版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处
函数原型
void normalize( InputArray src, OutputArray dst, double alpha = 1, double beta = 0,
int norm_type = NORM_L2, int dtype = -1, InputArray mask = noArray());
参数说明
- InputArray类型的src,输入图像,如Mat类型。
- OutputArray类型的dst,输出图像。
- double类型的alpha,归一化相关的数值。
- double类型的beta,归一化相关的数值。
- int类型的norm_type,归一化类型。
- int类型的dtype,默认值-1,与输出矩阵的类型和通道相关。
- InputArray类型的mask,掩膜。
针对第三个参数alpha和第四个参数beta,在不同归一化类型时,作用不一样:
- NORM_MINMAX :alpha和beta的最大值是归一化的最大值,两者的最小值是归一化的最小值,alpha为1,beta为0,同alpha为0,beta为1,是一致的。
- NORM_INF:beta值无用;输出矩阵的数值为,原始矩阵数值除以矩阵最大值的结果,alpha可以控制倍数。
}*alpha)
- NORM_L1:beta值无用;输出矩阵的数值为,原始矩阵数值除以矩阵数据绝对值和的结果,alpha可以控制倍数。

- NORM_L2:beta值无用;输出矩阵的数值为,原始矩阵数值除以矩阵数据平方和再开根号的结果,alpha可以控制倍数。
^{2}}}*alpha)
测试代码
#include<iostream>
#include<opencv2/opencv.hpp>
#include<ctime>
using namespace std;
using namespace cv;
int main(void)
{
cv::Mat test = (Mat_<float>(4, 4) << 1, 2, 3, 4, 5, 6, 2, 4, 5, 6, 1, 4, 2, 7, 8, 6);
cout <<" "<< "T=" << endl << " " << test << endl << endl;
cv::Mat inf, L1, L2, MinMax,inf_,L1_,L2_,MinMax_;
cv::normalize(test, MinMax, 0, 1, NORM_MINMAX);
double min=0, max=0;
cv::minMaxLoc(test, &min, &max,0,0);
MinMax_ = (test - min) * 1 / (max - min);
cout << " " << "MinMax=" << endl << " " << MinMax << endl;
cout << " " << "MinMax_=" << endl << " " << MinMax_ << endl << endl;
cv::normalize(test, inf, 10, 0, NORM_INF);
double max1 = 0;
cv::minMaxLoc(test, 0, &max1, 0, 0);
inf_ = test * 10 / max;
cout << " " << "inf=" << endl << " " << inf << endl;
cout << " " << "inf_=" << endl << " " << inf_ << endl << endl;
cv::normalize(test, L1, 10, 0, NORM_L1);
cv::Mat abs_;
float sum_ = 0.0f;
abs_=cv::abs(test);
sum_ = cv::sum(abs_)[0];
L1_ = test *10 / sum_;
cout << " " << "L1=" << endl << " " << L1 << endl;
cout << " " << "L1_=" << endl << " " << L1_ << endl << endl;
cv::normalize(test, L2, 10, 0, NORM_L2);
cv::Mat pow_;
float sum1_ = 0.0f;
float sqrt_ = 0.0f;
cv::pow(test,2,pow_);
sum1_ = cv::sum(pow_)[0];
sqrt_ = sqrt(sum1_);
L2_ = test * 10 / sqrt_;
cout << " " << "L2=" << endl <<" " << L2 << endl;
cout << " " << "L2_=" << endl << " " << L2_ << endl << endl;
system("pause");
return 0;
}
测试效果
图1 验证结果对比图
从图中可以看出:直接调用normalize函数不同归一化模式结果,与按公式输出结果对比,数值完全一致。
如果该函数有什么我没发现的问题,欢迎评论区批评指正~
如果文章帮助到你了,可以点个赞让我知道,我会很快乐~加油!