double calPSNR(VideoCapture capture2, VideoCapture capture3) { Mat frame1, frame2; double mse = 0; int frameCount = 0;//初始化 while (1) { // 从两个视频中读取一帧 capture2 >> frame1; capture3 >> frame2; if (frame1.empty() || frame2.empty()) { break; } // 计算两帧之间的MSE Mat diff; absdiff(frame1, frame2, diff); diff = diff.mul(diff); Scalar mseScalar = mean(diff); double frameMse = mseScalar.val[0] + mseScalar.val[1] + mseScalar.val[2]; // 累计MSE mse += frameMse; frameCount++; } // 计算平均MSE mse /= frameCount; cout << "MSE: " << mse << endl; //计算峰值信噪比PSNR double psnr = 0;//初始化 psnr = 10 * log10((255 * 255) / mse); return psnr; }又错误吗
时间: 2023-06-20 18:07:41 浏览: 136
在计算PSNR时,可能会出现除以0的情况,需要在代码中添加判断。另外,计算PSNR值时需要注意,MSE值越小,PSNR值越大。建议将代码中计算平均MSE的语句放到计算PSNR之前。以下是修改后的代码:
```
double calPSNR(VideoCapture capture2, VideoCapture capture3) {
Mat frame1, frame2;
double mse = 0;
int frameCount = 0;//初始化
while (1) {
// 从两个视频中读取一帧
capture2 >> frame1;
capture3 >> frame2;
if (frame1.empty() || frame2.empty()) {
break;
}
// 计算两帧之间的MSE
Mat diff;
absdiff(frame1, frame2, diff);
diff = diff.mul(diff);
Scalar mseScalar = mean(diff);
double frameMse = mseScalar.val[0] + mseScalar.val[1] + mseScalar.val[2];
// 累计MSE
mse += frameMse;
frameCount++;
}
if (mse == 0) {
return 100; // 避免除以0
}
// 计算平均MSE
mse /= frameCount;
cout << "MSE: " << mse << endl;
// 计算峰值信噪比PSNR
double psnr = 0;//初始化
psnr = 10 * log10((255 * 255) / mse);
return psnr;
}
```
阅读全文