超分辨率评价标准PSNR
程序员文章站
2023-12-31 13:16:10
...
以下内容参考于百度百科(PSNR):
PSRN; peck signal to Nosie Ratio,峰值信噪比
为了衡量经过处理后的影响品质,通常会参考PSNR值来衡量某个程序能否令人满意。它是原图与被处理图像之间的均方误差相对于(2n-1)2的对数,单位db。
PSNR = 10 * log10((2n-1)2/MSE)
MSE:原图像与处理图像之间的均方误差。
PSNR越大,代表失真越少。
将对数里的平方拿出来,则前面乘以20
代码展示:
#include <iostream>
#include <opencv2/opencv.hpp>
float PSNR_8uc1(const cv::Mat& pred, const cv::Mat& gt)
{
cv::Mat pred_32f, gt_32f;
pred.convertTo(pred_32f, CV_32F);
gt.convertTo(gt_32f, CV_32F);
cv::Mat diff = pred_32f - gt_32f;
diff = diff.mul(diff);
cv::Scalar mean = cv::mean(diff);
float rmse = std::sqrt(mean[0]);
if (!rmse) return 100;
return 20 * std::log10(255.0 / rmse);
}
int main()
{
cv::Mat pred = cv::imread("bird_GT_scale_2.bmp",0);
cv::Mat gt = cv::imread("bird_GT.bmp",0);
float psnr = PSNR_8uc1(pred, gt);
float psnr2 = cv::PSNR(pred, gt);
printf("psnr = %.3f, opencv psnr: %.3f\n", psnr, psnr2);
system("pause");
return 0;
}
opencv由PSNR接口,打印两个结果,是相同的
有一个奇怪的现象,就是用python求出来的结果不一样,暂时没有找到原因,如果有人知道,请告知于我,多谢。
import math
import numpy as np
import cv2
def PSNR(pred, gt):
imdff = pred - gt
rmse = math.sqrt(np.mean(imdff ** 2))
if rmse == 0:
return 100
return 20 * math.log10(255.0 / rmse)
def main():
pred = cv2.imread('bird_GT_scale_2.bmp', 0)
gt = cv2.imread('bird_GT.bmp', 0)
psnr = PSNR(pred, gt)
print('psnr: ', psnr)
if __name__ == '__main__':
main()