图像质量评估——PSNR:峰值信噪比和SSIM:结构相似性(纯手撸代码)

本文主要是介绍图像质量评估——PSNR:峰值信噪比和SSIM:结构相似性(纯手撸代码),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

  • PSNR
    • 原理
    • 代码
    • 运行测试结果
  • SSIM
    • 原理
    • 代码
    • 运行测试结果
  • 总结

PSNR

原理

PSNR 是一种衡量图像质量的指标,它是通过比较原始图像和失真图像之间的差异来计算的。具体来说,PSNR 是通过比较两幅图像的每个像素值来计算的。给定一个大小为 m×n 的干净图像 I 和噪声图像 K,均方误差 (MSE) 定义为:

在这里插入图片描述
然后 PSNR (dB) 就定义为:
在这里插入图片描述
其中 MAX_I^2
为图片可能的最大像素值。如果每个像素都由 8 位二进制来表示,那么就为 255。通常,如果像素值由 B 位二进制来表示,那么 MAX_I = 2^B-1。

PSNR 主要比较的是两幅图像的每个像素值的差异,这种差异被称为 “噪声”。如果两幅图像完全相同,那么噪声就为零,PSNR 就为无穷大。如果两幅图像有很大的差异,那么噪声就会很大,PSNR 就会相应地减小。因此,PSNR 越大,表示图像质量越好。

代码

import numpy as np
import cv2def reorder_image(img, input_order='HWC', output_order='HWC'):''' reorder_image '''if input_order not in ['HWC', 'CHW']:raise ValueError(f'Wrong input_order {input_order}. Supported input_orders are ' "'HWC' and 'CHW'")if output_order not in ['HWC', 'CHW']:raise ValueError(f'Wrong output_order {output_order}. Supported output_orders are ' "'HWC' and 'CHW'")if len(img.shape) == 2:img, input_order = img[..., None], 'CHW'if input_order == 'CHW' and output_order == 'HWC':img = img.transpose(1, 2, 0)elif input_order == 'HWC' and output_order == 'CHW':img = img.transpose(2, 0, 1)return imgdef _convert_input_type_range(img):''' convert input to [0, 1] '''img_type = img.dtypeimg = img.astype(np.float32)if img_type == np.float32:passelif img_type == np.uint8:img /= 255.else:raise TypeError(f'The img type should be np.float32 or np.uint8, but got {img_type}')return imgdef _convert_output_type_range(img, dst_type):''' convert output to dst_type '''if dst_type not in (np.uint8, np.float32):raise TypeError(f'The dst_type should be np.float32 or np.uint8, but got {dst_type}')if dst_type == np.uint8:img = img.round()else:img /= 255.return img.astype(dst_type)def bgr2ycbcr(img, y_only=False):''' bgr space to ycbcr space '''img_type = img.dtypeimg = _convert_input_type_range(img)if y_only:out_img = np.dot(img, [24.966, 128.553, 65.481]) + 16.0else:out_img = np.matmul(img, [[24.966, 112.0, -18.214], [128.553, -74.203, -93.786], [65.481, -37.797, 112.0]]) + [16, 128, 128]out_img = _convert_output_type_range(out_img, img_type)return out_imgdef calculate_psnr(img, img2, crop_border, input_order='HWC', test_y_channel=False, **_kwargs):''' calculate_psnr '''assert img.shape == img2.shape, (f'Image shapes are different: {img.shape}, {img2.shape}.')if input_order not in ['HWC', 'CHW']:raise ValueError(f'Wrong input_order {input_order}. Supported input_orders are ' '"HWC" and "CHW"')if not isinstance(crop_border, (list, tuple)):crop_border = (crop_border, crop_border)img = reorder_image(img, input_order=input_order).astype(np.float64)img2 = reorder_image(img2, input_order=input_order).astype(np.float64)if crop_border[0] != 0:img = img[crop_border[0]:-crop_border[0], ...]img2 = img2[crop_border[0]:-crop_border[0], ...]if crop_border[1] != 0:img = img[:, crop_border[1]:-crop_border[1], ...]img2 = img2[:, crop_border[1]:-crop_border[1], ...]if test_y_channel:img = bgr2ycbcr(img.astype(np.float32) / 255., y_only=True) * 255img2 = bgr2ycbcr(img2.astype(np.float32) / 255., y_only=True) * 255mse = np.mean((img - img2)**2)                 # MSE均方误差if mse == 0:return float('inf')PSNR_result = 20. * np.log10(255. / np.sqrt(mse))return PSNR_resultif __name__ == '__main__':img1 = cv2.imread("datasets/Set5/GTmod12/hh74.png")  # 读入图片1img2 = cv2.imread("visualization/Set5/hh74_ETDS_M4C32_x4.png")  # 读入图片2PSNR_result = calculate_psnr(img1, img2, 4)print("PSNR_result = ",PSNR_result)

运行测试结果

在这里插入图片描述

SSIM

原理

SSIM(结构相似性)是一种衡量两幅图像相似度的指标。相对 PSNR 而言,SSIM 在评价图像质量上更能符合人类的视觉特性。SSIM 使用的两张图像中,一张为未经压缩的无失真图像,另一张为失真后的图像。SSIM 公式基于样本 x 和 y 之间的三个比较衡量:亮度 (luminance)、对比度 (contrast) 和结构 (structure)。具体的计算公式如下所示:

在这里插入图片描述
SSIM 主要比较的是两幅图像的亮度、对比度和结构。这三个因素都是人类视觉系统在评价图像质量时的重要因素。因此,SSIM 能够更好地反映人类视觉系统对图像质量的感知。

代码


import numpy as np
import cv2def reorder_image(img, input_order='HWC', output_order='HWC'):''' reorder_image '''if input_order not in ['HWC', 'CHW']:raise ValueError(f'Wrong input_order {input_order}. Supported input_orders are ' "'HWC' and 'CHW'")if output_order not in ['HWC', 'CHW']:raise ValueError(f'Wrong output_order {output_order}. Supported output_orders are ' "'HWC' and 'CHW'")if len(img.shape) == 2:img, input_order = img[..., None], 'CHW'if input_order == 'CHW' and output_order == 'HWC':img = img.transpose(1, 2, 0)elif input_order == 'HWC' and output_order == 'CHW':img = img.transpose(2, 0, 1)return imgdef _convert_input_type_range(img):''' convert input to [0, 1] '''img_type = img.dtypeimg = img.astype(np.float32)if img_type == np.float32:passelif img_type == np.uint8:img /= 255.else:raise TypeError(f'The img type should be np.float32 or np.uint8, but got {img_type}')return imgdef _convert_output_type_range(img, dst_type):''' convert output to dst_type '''if dst_type not in (np.uint8, np.float32):raise TypeError(f'The dst_type should be np.float32 or np.uint8, but got {dst_type}')if dst_type == np.uint8:img = img.round()else:img /= 255.return img.astype(dst_type)def bgr2ycbcr(img, y_only=False):''' bgr space to ycbcr space '''img_type = img.dtypeimg = _convert_input_type_range(img)if y_only:out_img = np.dot(img, [24.966, 128.553, 65.481]) + 16.0else:out_img = np.matmul(img, [[24.966, 112.0, -18.214], [128.553, -74.203, -93.786], [65.481, -37.797, 112.0]]) + [16, 128, 128]out_img = _convert_output_type_range(out_img, img_type)return out_imgdef _ssim(img, img2):''' ssim '''c1, c2 = (0.01 * 255)**2, (0.03 * 255)**2img = img.astype(np.float64)img2 = img2.astype(np.float64)kernel = cv2.getGaussianKernel(11, 1.5)window = np.outer(kernel, kernel.transpose())mu1 = cv2.filter2D(img, -1, window)[5:-5, 5:-5]mu2 = cv2.filter2D(img2, -1, window)[5:-5, 5:-5]mu1_sq, mu2_sq = mu1**2, mu2**2mu1_mu2 = mu1 * mu2sigma1_sq = cv2.filter2D(img**2, -1, window)[5:-5, 5:-5] - mu1_sqsigma2_sq = cv2.filter2D(img2**2, -1, window)[5:-5, 5:-5] - mu2_sqsigma12 = cv2.filter2D(img * img2, -1, window)[5:-5, 5:-5] - mu1_mu2ssim_map = ((2 * mu1_mu2 + c1) * (2 * sigma12 + c2)) / ((mu1_sq + mu2_sq + c1) * (sigma1_sq + sigma2_sq + c2))return ssim_map.mean()def calculate_ssim(img, img2, crop_border, input_order='HWC', test_y_channel=False, **_kwargs):''' calculate_ssim '''assert img.shape == img2.shape, (f'Image shapes are different: {img.shape}, {img2.shape}.')if input_order not in ['HWC', 'CHW']:raise ValueError(f'Wrong input_order {input_order}. Supported input_orders are ' '"HWC" and "CHW"')if not isinstance(crop_border, (list, tuple)):crop_border = (crop_border, crop_border)img = reorder_image(img, input_order=input_order)img2 = reorder_image(img2, input_order=input_order)img = img.astype(np.float64)img2 = img2.astype(np.float64)if crop_border[0] != 0:img = img[crop_border[0]:-crop_border[0], ...]img2 = img2[crop_border[0]:-crop_border[0], ...]if crop_border[1] != 0:img = img[:, crop_border[1]:-crop_border[1], ...]img2 = img2[:, crop_border[1]:-crop_border[1], ...]if test_y_channel:img = bgr2ycbcr(img.astype(np.float32) / 255., y_only=True)[..., None] * 255img2 = bgr2ycbcr(img2.astype(np.float32) / 255., y_only=True)[..., None] * 255ssims = []for i in range(img.shape[2]):ssims.append(_ssim(img[..., i], img2[..., i]))ssims_result = np.array(ssims).mean()return ssims_resultif __name__ == '__main__':img1 = cv2.imread("datasets/Set5/GTmod12/hh74.png")  # 读入图片1img2 = cv2.imread("visualization/Set5/hh74_ETDS_M4C32_x4.png")  # 读入图片2ssims_result = calculate_ssim(img1, img2, 4)print("PSNR_result = ", ssims_result)

运行测试结果

在这里插入图片描述

总结

以上就是图像质量评估——PSNR:峰值信噪比和SSIM:结构相似性的原理及详细代码,希望能帮到你,总结不易,撸码不易,三连多多支持,谢谢!

这篇关于图像质量评估——PSNR:峰值信噪比和SSIM:结构相似性(纯手撸代码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/372408

相关文章

JAVA项目swing转javafx语法规则以及示例代码

《JAVA项目swing转javafx语法规则以及示例代码》:本文主要介绍JAVA项目swing转javafx语法规则以及示例代码的相关资料,文中详细讲解了主类继承、窗口创建、布局管理、控件替换、... 目录最常用的“一行换一行”速查表(直接全局替换)实际转换示例(JFramejs → JavaFX)迁移建

MySQL快速复制一张表的四种核心方法(包括表结构和数据)

《MySQL快速复制一张表的四种核心方法(包括表结构和数据)》本文详细介绍了四种复制MySQL表(结构+数据)的方法,并对每种方法进行了对比分析,适用于不同场景和数据量的复制需求,特别是针对超大表(1... 目录一、mysql 复制表(结构+数据)的 4 种核心方法(面试结构化回答)方法 1:CREATE

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

MyBatis中的两种参数传递类型详解(示例代码)

《MyBatis中的两种参数传递类型详解(示例代码)》文章介绍了MyBatis中传递多个参数的两种方式,使用Map和使用@Param注解或封装POJO,Map方式适用于动态、不固定的参数,但可读性和安... 目录✅ android方式一:使用Map<String, Object>✅ 方式二:使用@Param

SpringBoot实现图形验证码的示例代码

《SpringBoot实现图形验证码的示例代码》验证码的实现方式有很多,可以由前端实现,也可以由后端进行实现,也有很多的插件和工具包可以使用,在这里,我们使用Hutool提供的小工具实现,本文介绍Sp... 目录项目创建前端代码实现约定前后端交互接口需求分析接口定义Hutool工具实现服务器端代码引入依赖获

利用Python在万圣节实现比心弹窗告白代码

《利用Python在万圣节实现比心弹窗告白代码》:本文主要介绍关于利用Python在万圣节实现比心弹窗告白代码的相关资料,每个弹窗会显示一条温馨提示,程序通过参数方程绘制爱心形状,并使用多线程技术... 目录前言效果预览要点1. 爱心曲线方程2. 显示温馨弹窗函数(详细拆解)2.1 函数定义和延迟机制2.2

Springmvc常用的注解代码示例

《Springmvc常用的注解代码示例》本文介绍了SpringMVC中常用的控制器和请求映射注解,包括@Controller、@RequestMapping等,以及请求参数绑定注解,如@Request... 目录一、控制器与请求映射注解二、请求参数绑定注解三、其他常用注解(扩展)四、注解使用注意事项一、控制

C++简单日志系统实现代码示例

《C++简单日志系统实现代码示例》日志系统是成熟软件中的一个重要组成部分,其记录软件的使用和运行行为,方便事后进行故障分析、数据统计等,:本文主要介绍C++简单日志系统实现的相关资料,文中通过代码... 目录前言Util.hppLevel.hppLogMsg.hppFormat.hppSink.hppBuf

VS Code中的Python代码格式化插件示例讲解

《VSCode中的Python代码格式化插件示例讲解》在Java开发过程中,代码的规范性和可读性至关重要,一个团队中如果每个开发者的代码风格各异,会给代码的维护、审查和协作带来极大的困难,这篇文章主... 目录前言如何安装与配置使用建议与技巧如何选择总结前言在 VS Code 中,有几款非常出色的 pyt

利用Python将PDF文件转换为PNG图片的代码示例

《利用Python将PDF文件转换为PNG图片的代码示例》在日常工作和开发中,我们经常需要处理各种文档格式,PDF作为一种通用且跨平台的文档格式,被广泛应用于合同、报告、电子书等场景,然而,有时我们需... 目录引言为什么选择 python 进行 PDF 转 PNG?Spire.PDF for Python