利用OpenCV实现基于深度学习的超分辨率处理
重磅干货,第一时间送达

OpenCV是一个非常强大的计算机视觉处理的工具库。很多小伙伴在入门图像处理时都需要学习OpenCV的使用。但是随着计算机视觉技术的发展,越来越多的算法涌现出来,人们逐渐觉得OpenCV比较落后而放弃了使用OpenCV。
但是,实际上OpenCV时一个与时俱进的开源代码库。正在逐渐的吸收和接纳最新的算法。本文我们来介绍如何使用OpenCV实现基于深度学习的图像超分辨率(SR)。使用OpenCV的好处就是,我们不需要知道任何图像超分辨率的相关知识,就可以使用这个代码,并实现图像超分辨率。
具体操作步骤:
1. 安装OpenCV contrib模块
OpenCV中的超分辨率功能被集成在了contrib模块中,因此我们首先需要安装OpenCV的扩展模块。安装过程可以参考【从零学习OpenCV 4】opencv_contrib扩展模块的安装。超分辨率被集成在dnn_superres模块中,如果小伙伴们电脑空间有限,可以只编译这一个模块。
近期有小伙伴反馈自己安装扩展模块失败,为了解决这个问题,小白近期在筹划搭建一个各个版本opencv-contrib编译完成的数据库。各位小伙伴随时关注我们公众号的动态。
2. 下载训练的模型
由于某些模型比较大,因此OpenCV代码库中没有包含他们,因此我们在使用的时候需要单独的下载经过训练的模型。目前,仅支持4种不同的超分辨率模型,他们可以实现2倍、3倍、4倍甚至8倍的图像方法。这些模型具体如下:
#include <opencv2/dnn_superres.hpp>#include <opencv2/imgproc.hpp>#include <opencv2/highgui.hpp>
using namespace std;using namespace cv;using namespace dnn;using namespace dnn_superres;
int main(int argc, char *argv[]){ //Create the module's object DnnSuperResImpl sr;
//Set the image you would like to upscale string img_path = "image.png"; Mat img = cv::imread(img_path);
//Read the desired model string path = "FSRCNN_x2.pb"; sr.readModel(path);
//Set the desired model and scale to get correct pre- and post-processing sr.setModel("fsrcnn", 2);
//Upscale Mat img_new; sr.upsample(img, img_new); cv::imwrite( "upscaled.png", img_new);
return 0;}//Read the desired modelstring path = "FSRCNN_x2.pb";sr.readModel(path);//Set the desired model and scale to get correct pre- and post-processingsr.setModel("fsrcnn", 2);//UpscaleMat img_new;sr.upsample(img, img_new);cv::imwrite( "upscaled.png", img_new);import cv2from cv2 import dnn_superres
# Create an SR objectsr = dnn_superres.DnnSuperResImpl_create()
# Read imageimage = cv2.imread('./input.png')
# Read the desired modelpath = "EDSR_x3.pb"sr.readModel(path)
# Set the desired model and scale to get correct pre- and post-processingsr.setModel("edsr", 3)
# Upscale the imageresult = sr.upsample(image)
# Save the imagecv2.imwrite("./upscaled.png", result)# Create an SR objectsr = dnn_superres.DnnSuperResImpl_create()

双线性插值放大3倍

FSRCNN放大3倍

ESDR放大3倍
赞 (0)
