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

相关推荐
莫逸风1 小时前
【AgentScope】HarnessAgent 学习指南
大数据·人工智能
武子康1 小时前
调查研究-153 Cloudflare 能部署网站吗?2026 年完整对比 Vercel / Netlify / 自建服务器
大数据·运维·服务器·人工智能·部署·devops·opc
IvanCodes1 小时前
Agent开发入门:提示词工程
人工智能·agent
麦哲思科技任甲林1 小时前
白话Skills之七:编写AI Skill的原则
人工智能·prompt·agent·ai编程·skills
weixin_397574091 小时前
从“对接大模型“到“生成AI服务“:下一代企业AI应用开发框
人工智能
ConardLi1 小时前
啊?我刚开源的 Skills 已经 7K Star 了?!
前端·人工智能·后端
玩c#的小杜同学1 小时前
一周 AI 新鲜事|2026.05.25—2026.05.31
人工智能·程序人生·ai·c#·程序员创富
Esaka_Forever1 小时前
few‑shot learning(少样本学习)
人工智能·学习
逻辑君1 小时前
Foresight研究报告【20260019】
人工智能·数学建模
旦莫2 小时前
AI测试Agent的两种架构路径:谁做主控?
人工智能·python·架构·自动化·ai测试