利用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#include#includeusing namespace std;using namespace cv;using namespace dnn;using namespace dnn_superres;int main(int argc, char *argv[]){//Create the module's objectDnnSuperResImpl sr;//Set the image you would like to upscalestring img_path = "image.png";Mat img = cv::imread(img_path);//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);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倍
交流群
欢迎加入公众号读者群一起和同行交流,目前有SLAM、三维视觉、传感器、自动驾驶、计算摄影、检测、分割、识别、医学影像、GAN、算法竞赛等微信群(以后会逐渐细分),请扫描下面微信号加群,备注:”昵称+学校/公司+研究方向“,例如:”张三 + 上海交大 + 视觉SLAM“。请按照格式备注,否则不予通过。添加成功后会根据研究方向邀请进入相关微信群。请勿在群内发送广告,否则会请出群,谢谢理解~

