当前位置:   article > 正文

OpenCV实战之一 | 使用OpenCV进行图像超分辨率_opencv提升分辨率

opencv提升分辨率

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

一、OpenCV安装

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

二、模型下载

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

EDSR

ESPCN

FSRCNN

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

四、超分算法效果评估

通过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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

五、相关超分辨率算法

WDSR

2018 NTIRE超分辨率冠军

RCAN

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

SAN

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

ESRT(CVPR 2022)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/755554
推荐阅读
相关标签
  

闽ICP备14008679号