opencv实践项目-实现超分辨率

1. 安装OpenCV contrib模块

OpenCV中的超分辨率功能被集成在了contrib模块中,因此我们首先需要安装OpenCV的扩展模
块。安装过程可以参考【从零学习OpenCV 4】opencv_contrib扩展模块的安装。超分辨率被集成在
dnn_superres模块中

2. 下载训练的模型

由于某些模型比较大,因此OpenCV代码库中没有包含他们,因此我们在使用的时候需要单独的下载
经过训练的模型。目前,仅支持4种不同的超分辨率模型,他们可以实现2倍、3倍、4倍甚至8倍的图
像方法。这些模型具体如下:
EDSR:这个是表现最好的模型。但是这个模型也是最大的,所以运行速度会比较慢。
ESPCN:这个模型具有速度快,效果好的特点,并且模型较小。它可以进行对视频进行实时处理(取
决于图像大小)。
FSRCNN:这也是具有快速准确推断功能的小型模型。也可以进行实时视频升频。
LapSRN:这是一个中等大小的模型,它的特点是最大可以将图像放大8倍。

模型下载地址:https://github.com/Saafke/FSRCNN_Tensorflow/blob/master/models/FSRCNN_x2.pb

3. 超分实现

#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 = "C:\\Users\\Administrator\\Desktop\\1.jpg";
	Mat img = cv::imread(img_path);
	//Read the desired model
	string path = "E:\\code\\Yolov5_Tensorrt_Win10-master\\build\\Release\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;
}

Guess you like

Origin blog.csdn.net/wyw0000/article/details/130145275