标题:
OpenCV进行图像相似度对比的几种办法(2)
[打印本页]
作者:
yuyang911220
时间:
2016-9-16 17:47
标题:
OpenCV进行图像相似度对比的几种办法(2)
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; }}double getPSNR_GPU_optimized(const Mat& I1, const Mat& I2, BufferPSNR& b){ b.gI1.upload(I1); b.gI2.upload(I2); b.gI1.convertTo(b.t1, CV_32F); b.gI2.convertTo(b.t2, CV_32F); gpu::absdiff(b.t1.reshape(1), b.t2.reshape(1), b.gs); gpu::multiply(b.gs, b.gs, b.gs); double sse = gpu::sum(b.gs, b.buf)[0]; 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; }}double getPSNR_GPU(const Mat& I1, const Mat& I2){ gpu::GpuMat gI1, gI2, gs, t1,t2; gI1.upload(I1); gI2.upload(I2); gI1.convertTo(t1, CV_32F); gI2.convertTo(t2, CV_32F); gpu::absdiff(t1.reshape(1), t2.reshape(1), gs); gpu::multiply(gs, gs, gs); Scalar s = gpu::sum(gs); 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)(gI1.channels() * I1.total()); double psnr = 10.0*log10((255*255)/mse); return psnr; }}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; }Scalar getMSSIM_GPU( const Mat& i1, const Mat& i2){ const float C1 = 6.5025f, C2 = 58.5225f; /***************************** INITS **********************************/ gpu::GpuMat gI1, gI2, gs1, t1,t2; gI1.upload(i1); gI2.upload(i2); gI1.convertTo(t1, CV_MAKE_TYPE(CV_32F, gI1.channels())); gI2.convertTo(t2, CV_MAKE_TYPE(CV_32F, gI2.channels())); vector<gpu::GpuMat> vI1, vI2; gpu::split(t1, vI1); gpu::split(t2, vI2); Scalar mssim; for( int i = 0; i < gI1.channels(); ++i ) { gpu::GpuMat I2_2, I1_2, I1_I2; gpu::multiply(vI2[i], vI2[i], I2_2); // I2^2 gpu::multiply(vI1[i], vI1[i], I1_2); // I1^2 gpu::multiply(vI1[i], vI2[i], I1_I2); // I1 * I2 /*************************** END INITS **********************************/ gpu::GpuMat mu1, mu2; // PRELIMINARY COMPUTING gpu::GaussianBlur(vI1[i], mu1, Size(11, 11), 1.5); gpu::GaussianBlur(vI2[i], mu2, Size(11, 11), 1.5); gpu::GpuMat mu1_2, mu2_2, mu1_mu2; gpu::multiply(mu1, mu1, mu1_2); gpu::multiply(mu2, mu2, mu2_2); gpu::multiply(mu1, mu2, mu1_mu2); gpu::GpuMat sigma1_2, sigma2_2, sigma12; gpu::GaussianBlur(I1_2, sigma1_2, Size(11, 11), 1.5); //sigma1_2 = sigma1_2 - mu1_2; gpu::subtract(sigma1_2,mu1_2,sigma1_2); gpu::GaussianBlur(I2_2, sigma2_2, Size(11, 11), 1.5); //sigma2_2 = sigma2_2 - mu2_2; gpu::GaussianBlur(I1_I2, sigma12, Size(11, 11), 1.5); (Mat)sigma12 =(Mat)sigma12 - (Mat)mu1_mu2; //sigma12 = sigma12 - mu1_mu2 ///////////////////////////////// FORMULA //////////////////////////////// gpu::GpuMat t1, t2, t3; // t1 = 2 * mu1_mu2 + C1; // t2 = 2 * sigma12 + C2; // gpu::multiply(t1, t2, t3); // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))// // t1 = mu1_2 + mu2_2 + C1; // t2 = sigma1_2 + sigma2_2 + C2; // gpu::multiply(t1, t2, t1); // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2)) gpu::GpuMat ssim_map; gpu::divide(t3, t1, ssim_map); // ssim_map = t3./t1; Scalar s = gpu::sum(ssim_map); mssim.val[i] = s.val[0] / (ssim_map.rows * ssim_map.cols); } return mssim; }
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0