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 等),并对图像进行放大。

使用步骤

  1. 创建 DnnSuperRes 对象
cpp 复制代码
#include <opencv2/dnn_superres.hpp>
cv::dnn_superres::DnnSuperResImpl sr;

或者使用智能指针方式:

cpp 复制代码
Ptr<cv::dnn_superres::DnnSuperResImpl> sr = makePtr<cv::dnn_superres::DnnSuperResImpl>();
  1. 加载模型

OpenCV 的超分辨率模块支持以下模型架构:

  • edsr
  • espcn
  • fsrcnn
  • lapsrn

示例代码:

cpp 复制代码
sr.readModel("EDSR_x3.pb"); // 替换为你的模型路径
sr.setModel("edsr", 3);     // 指定模型类型和放大倍数
  1. 超分推理
cpp 复制代码
Mat img = imread("input.jpg");
Mat result;

sr.upsample(img, result);

imwrite("output.jpg", result);

示例代码

cpp 复制代码
#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

相关推荐
rengang6613 分钟前
软件工程新纪元:AI协同编程架构师的修养与使命
人工智能·软件工程·ai编程·ai协同编程架构师
IT_陈寒25 分钟前
Python+AI实战:用LangChain构建智能问答系统的5个核心技巧
前端·人工智能·后端
亚马逊云开发者42 分钟前
Amazon Bedrock AgentCore Memory:亚马逊云科技的托管记忆解决方案
人工智能
言之。1 小时前
Chroma 开源的 AI 应用搜索与检索数据库(即向量数据库)
数据库·人工智能·开源
tomlone1 小时前
《AI的未来:从“召唤幽灵”到学会反思》
人工智能
编码浪子1 小时前
对LlamaFactory的一点见解
人工智能·机器学习·数据挖掘
长桥夜波1 小时前
【第十八周】机器学习笔记07
人工智能·笔记·机器学习
luoganttcc2 小时前
是凯恩斯主义主导 西方的经济决策吗
大数据·人工智能·金融·哲学
好奇龙猫2 小时前
AI学习:SPIN -win-安装SPIN-工具过程 SPIN win 电脑安装=accoda 环境-第五篇:代码修复]
人工智能·学习
远山枫谷2 小时前
如何通过nodean安装n8n以及可能遇到的问题
人工智能