# cvNormalize 根据某种范数或者数值范围归一化数组.

Normalize

void cvNormalize( const CvArr* src, CvArr* dst,
double a=1, double b=0, int norm_type=CV_L2,
src

dst

a

b

norm_type

CV_C - 归一化数组的C-范数(绝对值的最大值)
CV_L1 - 归一化数组的L1-范数(绝对值的和)
CV_L2 - 归一化数组的(欧几里德)L2-范数
CV_MINMAX - 数组的数值被平移或缩放到一个指定的范围

norm_type=CV_C时, src 被重新"缩放"(rescale)到dst, 使得dst的值是线性映射到[0,1]区间.(a,b其实无作用)
norm_type=CV_L1,或者 CV_L2时, 得到L1,L2规范化的dst.(a,b其实无作用)
norm_type=CV_MINMAX时, src会被缩放(rescale)和移动(translation)到dst,使得dst的值是线性映射到[b,a]区间.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

void displayMat(const CvMat* mat){
int col=mat->width;
int row=mat->height;
double* data=mat->data.db;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
std::cout<<data[i*col+j]<<", ";
}
}
std::cout<<std::endl;
}
int main (int argc, char * const argv[]) {
// insert code here...
std::cout << "normalization\n";

double data[]={1,4,5,6,7,10};

CvMat src=cvMat(6,1,CV_64FC1,data);
CvMat dst=cvMat(6,1,CV_64FC1,data);

std::cout<<"a=5,b=0: ";cvNormalize(&src,&dst,5,0,CV_C,NULL);
displayMat(&dst);std::cout<<"---------"<<std::endl;

std::cout<<"a=5,b=0: ";cvNormalize(&src,&dst,5,0,CV_L1,NULL);
displayMat(&dst);std::cout<<"---------"<<std::endl;

std::cout<<"a=5,b=0: ";cvNormalize(&src,&dst,5,0,CV_L2,NULL);
displayMat(&dst);std::cout<<"---------"<<std::endl;

std::cout<<"a=5,b=0: ";cvNormalize(&src,&dst,5,0,CV_MINMAX,NULL);
displayMat(&dst);std::cout<<"---------"<<std::endl;

return 0;
}

a=5,b=0: CV_C: 0.1, 0.4, 0.5, 0.6, 0.7, 1,
---------
a=5,b=0: CV_L1: 0.030303, 0.121212, 0.151515, 0.181818, 0.212121, 0.30303,
---------
a=5,b=0: CV_L2: 0.0663723, 0.265489, 0.331862, 0.398234, 0.464606, 0.663723,
---------
a=5,b=0: CV_MINMAX: 0, 1.66667, 2.22222, 2.77778, 3.33333, 5,
---------
L1_norm: 每個元素乘上1/sqrt(1+4+5+6+7+10)
L2_norm: 每個元素乘上1/sqrt(1+16+25+36+49+100)
CV_MINMAX:使每個元素限制在[a=5,b=0]之間算法如下:dst(i)=(src(i)-min(src))*(5-0)/(max(src)-min(src))
1-->0
4-->3*5/9=1.6666
5-->4*5/9=2.2222