赞
踩
前言
图像超分辨率(Image Super-Resolution,简称ISR)是一种图像处理技术,将低分辨率的图像通过算法转换成高分辨率图像,从而增加图像的细节和清晰度。
ISR技术对于许多计算机视觉和图像处理任务都是至关重要的,如图像重建、监视、医学图像处理等。
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模型,其精度高,但推理速度太慢,所以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
算法评估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)
2018 NTIRE超分辨率冠军
ECCV2018超分冠军方案,EDSR的改进,加入通道注意力
CVPR2019,RCAN的改进,使用二阶注意力
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。