前言
圖像超分辨率(Image Super-Resolution,簡(jiǎn)稱ISR)是一種圖像處理技術(shù),將低分辨率的圖像通過算法轉(zhuǎn)換成高分辨率圖像,從而增加圖像的細(xì)節(jié)和清晰度。
ISR技術(shù)對(duì)于許多計(jì)算機(jī)視覺和圖像處理任務(wù)都是至關(guān)重要的,如圖像重建、監(jiān)視、醫(yī)學(xué)圖像處理等。
一、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
函數(shù),而此函數(shù)存在于OpenCV4.4以上以及。
OpenCV代碼庫(kù)目前僅支持4種不同的超分辨率模型:
EDSR
-
EDSR(2017 CVPR NTIRE2017超分辨率挑戰(zhàn)賽冠軍)
- 優(yōu)點(diǎn):高精度
- 缺點(diǎn):模型文件大且運(yùn)行速度慢
- 模型參數(shù):提供x2,x3,x4訓(xùn)練模型
- 模型下載:EDSR_Tensorflow
- 論文:Enhanced Deep Residual Networks for Single Image Super-Resolution
- Pytorch Code:EDSR-PyTorch
ESPCN
-
ESPCN(2016 CVPR):
- 優(yōu)點(diǎn):體積小,速度快,并且仍然表現(xiàn)良好;它可以進(jìn)行對(duì)視頻進(jìn)行實(shí)時(shí)處理(取決于圖像大?。?/li>
- 缺點(diǎn):與更新的、更健壯的模型相比,在視覺上表現(xiàn)更差
- 模型參數(shù):提供x2,x3,x4訓(xùn)練模型
- 模型下載:TF-ESPCN
- 論文:Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network
FSRCNN
-
FSRCNN(2016 ECCV)
- 優(yōu)點(diǎn):快速,小巧;可以進(jìn)行實(shí)時(shí)視頻升頻
- 缺點(diǎn):不夠準(zhǔn)確
- 模型參數(shù):提供x2,x3,x4訓(xùn)練模型和small訓(xùn)練模型
- 模型下載:FSRCNN_Tensorflow
- 論文:Accelerating the Super-Resolution Convolutional Neural Network
LapSRN
-
LapSRN(2017 CVPR)
- 優(yōu)點(diǎn):該模型可以通過一次向前傳遞進(jìn)行多尺度超分辨率,可以支持2x,4x,8x和[2x,4x]和[2x,4x,8x]超分辨率
- 缺點(diǎn):它比ESPCN和FSRCNN慢,并且精度比EDSR差
- 模型參數(shù):提供x2,x4,x8訓(xùn)練模型
- 模型下載:TF-LAPSRN
- 論文:Deep laplacian pyramid networks for fast and accurate super-resolution
- Pytorch Code:pytorch-LapSRN
? 總結(jié):實(shí)踐應(yīng)用最廣泛的是EDSR模型,其精度高,但推理速度太慢,所以2倍放大和4倍放大可以考慮使用ESPCN代替,4倍和8倍放大可以考慮使用LapSRN。當(dāng)然超分放大需要高性能運(yùn)算,還是用高性能顯卡運(yùn)算較為合適。注意的是OpenCV的dnn_superres
模塊不適用于移動(dòng)端設(shè)備或嵌入式設(shè)備,因?yàn)镺penCV對(duì)設(shè)備性能有一定要求。所以移動(dòng)端可以參考ncnn的超分放大實(shí)現(xiàn)。
三、代碼實(shí)現(xiàn)
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)
四、超分算法效果評(píng)估
通過PSNR(峰值信噪比)和SSIM(結(jié)構(gòu)相似性)來評(píng)估圖像放大后的效果,PSNR越大,圖像失真越小。SSIM也是越大,圖像失真越小。PSNR和SSIM介紹見博客:【圖像評(píng)價(jià)指標(biāo)】PSNR和SSIM
- OpenCV官方文檔給了基礎(chǔ)測(cè)試結(jié)果:
-
2倍超分放大
-
3倍超分放大
-
4倍超分放大
-
2倍超分放大
Python代碼
算法評(píng)估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"
# 放大系數(shù)
scale = 4
# 模型路徑,根據(jù)算法確定
model = f"./model/{algorithm}_x{scale}.pb"
# 裁剪圖像,使圖像對(duì)齊
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
# 縮小圖像,以實(shí)現(xiàn)基準(zhǔn)質(zhì)量測(cè)試
img_downscaled = cv2.resize(cropped, None, fx=1.0 / scale, fy=1.0 / scale)
img_new = upscale(img_downscaled, algorithm, scale)
# 獲得模型質(zhì)量評(píng)估值
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)
五、相關(guān)超分辨率算法
WDSR
2018 NTIRE超分辨率冠軍
- 論文解析
- Pytorch Code:wdsr_ntire2018
RCAN
ECCV2018超分冠軍方案,EDSR的改進(jìn),加入通道注意力文章來源:http://www.zghlxwxcb.cn/news/detail-788660.html
- 論文:Image Super-Resolution Using Very Deep Residual Channel Attention Networks
- Pytorch Code:RCAN
SAN
CVPR2019,RCAN的改進(jìn),使用二階注意力文章來源地址http://www.zghlxwxcb.cn/news/detail-788660.html
- 論文:Second-Order Attention Network for Single Image Super-Resolution
- Pytorch Code:SAN
ESRT(CVPR 2022)
- 論文:Transformer for Single Image Super-Resolution
- Pytorch Code:ESRT
到了這里,關(guān)于OpenCV實(shí)戰(zhàn)之一 | 使用OpenCV進(jìn)行圖像超分辨率的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!