OpenCV中超分辨率(Super Resolution)模块类cv::dnn_superres::DnnSuperResImpl
- 操作系统:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 编程语言:C++11
算法描述
OpenCV中超分辨率(Super Resolution)模块的一个内部实现类。它属于dnn_superres模块,用于加载和运行基于深度学习的图像超分辨率模型。
这个类是 OpenCV 中用于执行 深度学习超分辨率推理 的主要类。你可以用它来加载预训练的超分辨率模型(如 EDSR、ESPCN、FSRCNN、LapSRN 等),并对图像进行放大。
使用步骤
- 创建 DnnSuperRes 对象
#include <opencv2/dnn_superres.hpp>
cv::dnn_superres::DnnSuperResImpl sr;
或者使用智能指针方式:
Ptr<cv::dnn_superres::DnnSuperResImpl> sr = makePtr<cv::dnn_superres::DnnSuperResImpl>();
- 加载模型
OpenCV 的超分辨率模块支持以下模型架构:
- edsr
- espcn
- fsrcnn
- lapsrn
示例代码:
sr.readModel("EDSR_x3.pb"); // 替换为你的模型路径
sr.setModel("edsr", 3); // 指定模型类型和放大倍数
- 超分推理
Mat img = imread("input.jpg");
Mat result;sr.upsample(img, result);imwrite("output.jpg", result);
示例代码
#include <opencv2/dnn_superres.hpp>
#include <opencv2/opencv.hpp>int main()
{using namespace cv;using namespace cv::dnn_superres;// 创建超分辨率对象DnnSuperResImpl sr;// 加载模型sr.readModel( "FSRCNN_x3.pb" );sr.setModel( "fsrcnn", 3 );// 读取图像Mat img = imread( "/media/dingxin/data/study/OpenCV/sources/images/Lenna.png" );if ( img.empty() ){std::cerr << "Failed to load image!" << std::endl;return -1;}// 超分辨率推理Mat result;sr.upsample( img, result );// 保存结果imwrite( "output.jpg", result );imshow( "Original", img );imshow( "Super Resolved", result );waitKey( 0 );return 0;
}
运行结果
图像确实变得很大,清晰度也没变
代码中模型文件下载地址:https://download.csdn.net/download/jndingxin/91263821