人工智能 计算机视觉 OpenCV实战之一 | 使用OpenCV进行图像超分辨率 - 最近邻插值\nPSNT:{psnr}, SSIM:{ssim}&#34

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多助力网站平台 👁️ 419 次阅读

前言 图像超分辨率(Image Super-Resolution,简称ISR)是一种图像处理技术,将低分辨率的图像通过算法转换成高分辨率图像,从而增加图像的细节和清晰度。 ISR技术对于许多计算机视觉和图像处理任务都是至关重要的,如图像重建、监视、医学图像处理等。

目录

一、OpenCV安装二、模型下载EDSRESPCNFSRCNNLapSRN

三、代码实现四、超分算法效果评估Python代码

五、相关超分辨率算法WDSRRCANSANESRT(CVPR 2022)

一、OpenCV安装

pip install opencv-python -i https://mirror.baidu.com/pypi/simple

pip install opencv-contrib-python -i https://mirror.baidu.com/pypi/simple

二、模型下载

⭐ 注意的是模型的加载需要使用到cv2.dnn_superres函数,而此函数存在于OpenCV4.4以上以及。 OpenCV代码库目前仅支持4种不同的超分辨率模型:

EDSR

EDSR(2017 CVPR NTIRE2017超分辨率挑战赛冠军)

优点:高精度缺点:模型文件大且运行速度慢模型参数:提供x2,x3,x4训练模型模型下载:EDSR_Tensorflow论文:Enhanced Deep Residual Networks for Single Image Super-ResolutionPytorch Code:EDSR-PyTorch

ESPCN

ESPCN(2016 CVPR):

优点:体积小,速度快,并且仍然表现良好;它可以进行对视频进行实时处理(取决于图像大小)缺点:与更新的、更健壮的模型相比,在视觉上表现更差模型参数:提供x2,x3,x4训练模型模型下载:TF-ESPCN论文:Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network

FSRCNN

FSRCNN(2016 ECCV)

优点:快速,小巧;可以进行实时视频升频缺点:不够准确模型参数:提供x2,x3,x4训练模型和small训练模型模型下载:FSRCNN_Tensorflow论文:Accelerating the Super-Resolution Convolutional Neural Network

LapSRN

LapSRN(2017 CVPR)

优点:该模型可以通过一次向前传递进行多尺度超分辨率,可以支持2x,4x,8x和[2x,4x]和[2x,4x,8x]超分辨率缺点:它比ESPCN和FSRCNN慢,并且精度比EDSR差模型参数:提供x2,x4,x8训练模型模型下载:TF-LAPSRN论文:Deep laplacian pyramid networks for fast and accurate super-resolutionPytorch Code:pytorch-LapSRN

⭐ 总结:实践应用最广泛的是EDSR模型,其精度高,但推理速度太慢,所以2倍放大和4倍放大可以考虑使用ESPCN代替,4倍和8倍放大可以考虑使用LapSRN。当然超分放大需要高性能运算,还是用高性能显卡运算较为合适。注意的是OpenCV的dnn_superres模块不适用于移动端设备或嵌入式设备,因为OpenCV对设备性能有一定要求。所以移动端可以参考ncnn的超分放大实现。

三、代码实现

import cv2

from cv2 import dnn_superres

def upscale(img, alg_name, scale):

# Create an SR object

sr = cv2.dnn_superres.DnnSuperResImpl_create()

# Read the desired model

path = f"./model/{alg_name}_x{scale}.pb"

sr.readModel(path)

# Set the desired model and scale to get correct pre- and post-processing

sr.setModel(alg_name,scale)

# Upscale the image

result = sr.upsample(img)

return result

if __name__ == '__main__':

img = cv2.imread(path_to_image)

# 使用LapSRN x4模型

res = upscale(img=img, alg_name='lapsrn', scale=4)

cv2.imshow('result', res)

cv2.waitKey(0)

四、超分算法效果评估

通过PSNR(峰值信噪比)和SSIM(结构相似性)来评估图像放大后的效果,PSNR越大,图像失真越小。SSIM也是越大,图像失真越小。PSNR和SSIM介绍见博客:【图像评价指标】PSNR和SSIM

OpenCV官方文档给了基础测试结果:

2倍超分放大 3倍超分放大 4倍超分放大

Python代码 拼多多帮助力入口

算法评估Python代码如下:

import cv2

def upscale(img, alg_name, scale):

# Create an SR object

sr = cv2.dnn_superres.DnnSuperResImpl_create()

# Read the desired model

path = f"./models/{alg_name}_x{scale}.pb"

sr.readModel(path)

# Set the desired model and scale to get correct pre- and post-processing

sr.setModel(alg_name, scale)

# Upscale the image

result = sr.upsample(img)

return result

def getQualityValues(upsampled, orig):

psnr = cv2.PSNR(upsampled, orig)

q, _ = cv2.quality.QualitySSIM_compute(upsampled, orig)

ssim = (q[0] + q[1] + q[2]) / 3

return round(psnr, 3), round(ssim, 3)

if __name__ == "__main__":

# 图片路径

img_path = "./data/images/1.jpg"

# 算法名称 edsr, espcn, fsrcnn or lapsrn

algorithm = "lapsrn"

# 放大系数

scale = 4

# 模型路径,根据算法确定

model = f"./model/{algorithm}_x{scale}.pb"

# 裁剪图像,使图像对齐

img = cv2.imread(img_path)

width = img.shape[0] - (img.shape[0] % scale)

height = img.shape[1] - (img.shape[1] % scale)

cropped = img[0:width, 0:height]

# Downscale the image for benchmarking

# 缩小图像,以实现基准质量测试

img_downscaled = cv2.resize(cropped, None, fx=1.0 / scale, fy=1.0 / scale)

img_new = upscale(img_downscaled, algorithm, scale)

# 获得模型质量评估值

psnr, ssim = getQualityValues(cropped, img_new)

print("=" * 30)

print(f"{algorithm}_x{scale}\nPSNT:{psnr}, SSIM:{ssim}")

print("=" * 30)

# INTER_CUBIC - 三次样条插值放大图像

bicubic = cv2.resize(img_downscaled, None, fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)

psnr, ssim = getQualityValues(cropped, bicubic)

print(f"三次样条插值\nPSNT:{psnr}, SSIM:{ssim}")

print("=" * 30)

# INTER_NEAREST - 最近邻插值

nearest = cv2.resize(img_downscaled, None, fx=scale, fy=scale, interpolation=cv2.INTER_NEAREST)

psnr, ssim = getQualityValues(cropped, nearest)

print(f"最近邻插值\nPSNT:{psnr}, SSIM:{ssim}")

print("=" * 30)

# Lanczos插值

lanczos = cv2.resize(img_downscaled, None, fx=scale, fy=scale, interpolation=cv2.INTER_LANCZOS4);

psnr, ssim = getQualityValues(cropped, lanczos)

print(f"Lanczos插值\nPSNT:{psnr}, SSIM:{ssim}")

print("=" * 30)

五、相关超分辨率算法

WDSR

2018 NTIRE超分辨率冠军

论文解析Pytorch Code:wdsr_ntire2018

RCAN

ECCV2018超分冠军方案,EDSR的改进,加入通道注意力

论文:Image Super-Resolution Using Very Deep Residual Channel Attention NetworksPytorch Code:RCAN

SAN

CVPR2019,RCAN的改进,使用二阶注意力

论文:Second-Order Attention Network for Single Image Super-ResolutionPytorch Code:SAN

ESRT(CVPR 2022)

论文:Transformer for Single Image Super-ResolutionPytorch Code:ESRT

推荐文章,人工智能

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧