# 正文

## 图像相似度测量

### 直方图方法

<span style="font-size:18px;"><span style="font-size:18px;">double getHistSimilarity(const Mat& I1, const Mat& I2)
{
int histSize = 256;
float range[] = {0,256};
const float* histRange = {range};
bool uniform = true;
bool accumulate = false;

Mat hist1,hist2;

calcHist(&I1,1,0,Mat(),hist1,1,&histSize,&histRange,uniform,accumulate);
normalize(hist1,hist1,0,1,NORM_MINMAX,-1,Mat());

calcHist(&I2,1,0,Mat(),hist2,1,&histSize,&histRange,uniform,accumulate);
normalize(hist2,hist2,0,1,NORM_MINMAX,-1,Mat());

return compareHist(hist1, hist2, CV_COMP_CORREL);

}</span></span>

/* Histogram comparison methods */
enum
{
CV_COMP_CORREL        =0,
CV_COMP_CHISQR        =1,
CV_COMP_INTERSECT     =2,
CV_COMP_BHATTACHARYYA =3,
CV_COMP_HELLINGER     =CV_COMP_BHATTACHARYYA
};

### 基于峰值信噪比（PSNR）的方法

<span style="font-size:18px;">double getPSNR(const Mat& I1, const Mat& I2)
{
Mat s1;
absdiff(I1, I2, s1);       // |I1 - I2|
s1.convertTo(s1, CV_32F);  // cannot make a square on 8 bits
s1 = s1.mul(s1);           // |I1 - I2|^2

Scalar s = sum(s1);         // sum elements per channel

double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels

if( sse <= 1e-10) // for small values return zero
return 0;
else
{
double  mse =sse /(double)(I1.channels() * I1.total());
double psnr = 10.0*log10((255*255)/mse);
return psnr;
}
}</span>

### 基于结构相似性（SSIM,structural similarity (SSIM) index measurement）的方法

<span style="font-size:18px;">Scalar getMSSIM( const Mat& i1, const Mat& i2)
{
const double C1 = 6.5025, C2 = 58.5225;
/***************************** INITS **********************************/
int d     = CV_32F;

Mat I1, I2;
i1.convertTo(I1, d);           // cannot calculate on one byte large values
i2.convertTo(I2, d);

Mat I2_2   = I2.mul(I2);        // I2^2
Mat I1_2   = I1.mul(I1);        // I1^2
Mat I1_I2  = I1.mul(I2);        // I1 * I2

/*************************** END INITS **********************************/

Mat mu1, mu2;   // PRELIMINARY COMPUTING
GaussianBlur(I1, mu1, Size(11, 11), 1.5);
GaussianBlur(I2, mu2, Size(11, 11), 1.5);

Mat mu1_2   =   mu1.mul(mu1);
Mat mu2_2   =   mu2.mul(mu2);
Mat mu1_mu2 =   mu1.mul(mu2);

Mat sigma1_2, sigma2_2, sigma12;

GaussianBlur(I1_2, sigma1_2, Size(11, 11), 1.5);
sigma1_2 -= mu1_2;

GaussianBlur(I2_2, sigma2_2, Size(11, 11), 1.5);
sigma2_2 -= mu2_2;

GaussianBlur(I1_I2, sigma12, Size(11, 11), 1.5);
sigma12 -= mu1_mu2;

///////////////////////////////// FORMULA ////////////////////////////////
Mat t1, t2, t3;

t1 = 2 * mu1_mu2 + C1;
t2 = 2 * sigma12 + C2;
t3 = t1.mul(t2);              // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))

t1 = mu1_2 + mu2_2 + C1;
t2 = sigma1_2 + sigma2_2 + C2;
t1 = t1.mul(t2);               // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))

Mat ssim_map;
divide(t3, t1, ssim_map);      // ssim_map =  t3./t1;

Scalar mssim = mean( ssim_map ); // mssim = average of ssim map
return mssim;
}
</span>

## 图像模板匹配

CV_TM_SQDIFF计算公式：

CV_TM_CCORR计算公式：

EMD is defined as the minimal cost that must be paid to transform one histograminto the other, where there is a “ground distance” between the basic featuresthat are aggregated into the histogram。

*******************************************************************************************************************************************************************************************************

2015-7-24