OPENCV例子opencv-4.5.5\samples\gpu\super_resolution.cpp的代码分析

该示例展示了如何使用OpenCV库实现视频的超分辨率处理,特别是通过创建双边TV-L1超分辨率算法。代码中提供了不同光学流算法的选择,并支持GPU加速。程序从输入视频中读取帧,应用超分辨率算法并输出结果,可以调整缩放因子、迭代次数等参数。
摘要由CSDN通过智能技术生成

此示例演示视频序列的超分辨率算法,超分辨率是指放大或改善图像细节的过程。

该示例使用:创建双边 TV-L1 超分辨率。createSuperResolution_BTVL1()

TV:Total Variation 全变分,有L1 和L2

示例全局调用情况如下:

 

示例main函数调用情况如下:

 

示例main函数流程图情况如下:

 

示例main函数UML逻辑图情况如下:

 

示例源代码如下:

#include <iostream>

#include <iomanip>

#include <string>

#include <ctype.h>

#include "opencv2/core.hpp"

#include "opencv2/core/utility.hpp"

#include "opencv2/highgui.hpp"

#include "opencv2/imgproc.hpp"

#include "opencv2/superres.hpp"

#include "opencv2/superres/optical_flow.hpp"

#include "opencv2/opencv_modules.hpp"

using namespace std;

using namespace cv;

using namespace cv::superres;

#define MEASURE_TIME(op) \

    { \

        TickMeter tm; \

        tm.start(); \

        op; \

        tm.stop(); \

        cout << tm.getTimeSec() << " sec" << endl; \

    }

static Ptr<cv::superres::DenseOpticalFlowExt> createOptFlow(const string& name, bool useGpu)

{

    if (name == "farneback")

    {

        if (useGpu)

            return cv::superres::createOptFlow_Farneback_CUDA();

        else

            return cv::superres::createOptFlow_Farneback();

    }

    /*else if (name == "simple")

        return createOptFlow_Simple();*/

    else if (name == "tvl1")

    {

        if (useGpu)

            return cv::superres::createOptFlow_DualTVL1_CUDA();

        else

            return cv::superres::createOptFlow_DualTVL1();

    }

    else if (name == "brox")

        return cv::superres::createOptFlow_Brox_CUDA();

    else if (name == "pyrlk")

        return cv::superres::createOptFlow_PyrLK_CUDA();

    else

        cerr << "Incorrect Optical Flow algorithm - " << name << endl;

    return Ptr<cv::superres::DenseOpticalFlowExt>();

}

int main(int argc, const char* argv[])

{

    CommandLineParser cmd(argc, argv,

        "{ v video      |           | Input video (mandatory)}"

        "{ o output     |           | Output video }"

        "{ s scale      | 4         | Scale factor }"

        "{ i iterations | 180       | Iteration count }"

        "{ t temporal   | 4         | Radius of the temporal search area }"

        "{ f flow       | farneback | Optical flow algorithm (farneback, tvl1, brox, pyrlk) }"

        "{ g gpu        | false     | CPU as default device, cuda for CUDA }"

        "{ h help       | false     | Print help message }"

    );

    const string inputVideoName = cmd.get<string>("video");

    if (cmd.get<bool>("help") || inputVideoName.empty())

    {

        cout << "This sample demonstrates Super Resolution algorithms for video sequence" << endl;

        cmd.printMessage();

        return EXIT_SUCCESS;

    }

    const string outputVideoName = cmd.get<string>("output");

    const int scale = cmd.get<int>("scale");

    const int iterations = cmd.get<int>("iterations");

    const int temporalAreaRadius = cmd.get<int>("temporal");

    const string optFlow = cmd.get<string>("flow");

    string gpuOption = cmd.get<string>("gpu");

    std::transform(gpuOption.begin(), gpuOption.end(), gpuOption.begin(), ::tolower);

    bool useCuda = gpuOption.compare("cuda") == 0;

    Ptr<SuperResolution> superRes;

    if (useCuda)

        superRes = createSuperResolution_BTVL1_CUDA();

    else

        superRes = createSuperResolution_BTVL1();

    Ptr<cv::superres::DenseOpticalFlowExt> of = createOptFlow(optFlow, useCuda);

    if (of.empty())

        return EXIT_FAILURE;

    superRes->setOpticalFlow(of);

    superRes->setScale(scale);

    superRes->setIterations(iterations);

    superRes->setTemporalAreaRadius(temporalAreaRadius);

    Ptr<FrameSource> frameSource;

    if (useCuda)

    {

        // Try to use gpu Video Decoding

        try

        {

            frameSource = createFrameSource_Video_CUDA(inputVideoName);

            Mat frame;

            frameSource->nextFrame(frame);

        }

        catch (const cv::Exception&)

        {

            frameSource.release();

        }

    }

    if (!frameSource)

        frameSource = createFrameSource_Video(inputVideoName);

    // skip first frame, it is usually corrupted

    {

        Mat frame;

        frameSource->nextFrame(frame);

        cout << "Input           : " << inputVideoName << " " << frame.size() << endl;

        cout << "Scale factor    : " << scale << endl;

        cout << "Iterations      : " << iterations << endl;

        cout << "Temporal radius : " << temporalAreaRadius << endl;

        cout << "Optical Flow    : " << optFlow << endl;

        cout << "Mode            : " << (useCuda ? "CUDA" : "CPU") << endl;

    }

    superRes->setInput(frameSource);

    VideoWriter writer;

    for (int i = 0;; ++i)

    {

        cout << '[' << setw(3) << i << "] : " << flush;

        Mat result;

        MEASURE_TIME(superRes->nextFrame(result));

        if (result.empty())

            break;

        imshow("Super Resolution", result);

        if (waitKey(1000) > 0)

            break;

        if (!outputVideoName.empty())

        {

            if (!writer.isOpened())

                writer.open(outputVideoName, VideoWriter::fourcc('X', 'V', 'I', 'D'), 25.0, result.size());

            writer << result;

        }

    }

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qqq9668

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值