double psnr(Mat &I1, Mat &I2){ Mat s1; absdiff(I1, I2, s1); s1.convertTo(s1, CV_32F);//转换为32位的float类型,8位不能计算平方 s1 = s1.mul(s1); Scalar s = sum(s1); //计算每个通道的和 double sse = s.val[0] + s.val[1] + s.val[2]; if( sse <= 1e-10) // for small values return zero return 0; else { double mse = sse / (double)(I1.channels() * I1.total()); // sse/(w*h*3) double psnr = 10.0 * log10((255*255)/mse); return psnr; } } double ssim(Mat &i1, Mat & i2){ const double C1 = 6.5025, C2 = 58.5225; int d = CV_32F; Mat I1, I2; i1.convertTo(I1, d); i2.convertTo(I2, d); Mat I1_2 = I1.mul(I1); Mat I2_2 = I2.mul(I2); Mat I1_I2 = I1.mul(I2); Mat mu1, mu2; 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, sigam2_2, sigam12; GaussianBlur(I1_2, sigma1_2, Size(11, 11), 1.5); sigma1_2 -= mu1_2; GaussianBlur(I2_2, sigam2_2, Size(11, 11), 1.5); sigam2_2 -= mu2_2; GaussianBlur(I1_I2, sigam12, Size(11, 11), 1.5); sigam12 -= mu1_mu2; Mat t1, t2, t3; t1 = 2 * mu1_mu2 + C1; t2 = 2 * sigam12 + C2; t3 = t1.mul(t2); t1 = mu1_2 + mu2_2 + C1; t2 = sigma1_2 + sigam2_2 + C2; t1 = t1.mul(t2); Mat ssim_map; divide(t3, t1, ssim_map); Scalar mssim = mean(ssim_map); double ssim = (mssim.val[0] + mssim.val[1] + mssim.val[2]) /3; return ssim; }