opencv/samples/gpu/super_resolution.cpp

164 lines
4.8 KiB
C++
Raw Normal View History

2013-02-25 18:33:00 +08:00
#include <iostream>
#include <iomanip>
#include <string>
#include <ctype.h>
2013-03-22 00:59:18 +08:00
#include "opencv2/core.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/superres.hpp"
2013-02-25 18:33:00 +08:00
#include "opencv2/superres/optical_flow.hpp"
#include "opencv2/opencv_modules.hpp"
#include "tick_meter.hpp"
2013-02-25 18:33:00 +08:00
using namespace std;
using namespace cv;
using namespace cv::superres;
2013-02-25 18:33:00 +08:00
#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)
2013-02-25 18:33:00 +08:00
{
if (name == "farneback")
{
if (useGpu)
return cv::superres::createOptFlow_Farneback_CUDA();
2013-02-25 18:33:00 +08:00
else
return cv::superres::createOptFlow_Farneback();
2013-02-25 18:33:00 +08:00
}
/*else if (name == "simple")
return createOptFlow_Simple();*/
2013-02-25 18:33:00 +08:00
else if (name == "tvl1")
{
if (useGpu)
return cv::superres::createOptFlow_DualTVL1_CUDA();
2013-02-25 18:33:00 +08:00
else
return cv::superres::createOptFlow_DualTVL1();
2013-02-25 18:33:00 +08:00
}
else if (name == "brox")
return cv::superres::createOptFlow_Brox_CUDA();
2013-02-25 18:33:00 +08:00
else if (name == "pyrlk")
return cv::superres::createOptFlow_PyrLK_CUDA();
2013-02-25 18:33:00 +08:00
else
cerr << "Incorrect Optical Flow algorithm - " << name << endl;
return Ptr<cv::superres::DenseOpticalFlowExt>();
}
2013-02-25 18:33:00 +08:00
int main(int argc, const char* argv[])
{
CommandLineParser cmd(argc, argv,
2013-03-22 00:59:18 +08:00
"{ v video | | Input video }"
"{ 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, simple, tvl1, brox, pyrlk) }"
"{ g | false | CPU as default device, cuda for CUDA }"
2013-03-22 00:59:18 +08:00
"{ h help | false | Print help message }"
2013-02-25 18:33:00 +08:00
);
if (cmd.get<bool>("help"))
{
cout << "This sample demonstrates Super Resolution algorithms for video sequence" << endl;
2013-03-22 00:59:18 +08:00
cmd.printMessage();
return EXIT_SUCCESS;
2013-02-25 18:33:00 +08:00
}
const string inputVideoName = cmd.get<string>("video");
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);
2013-02-25 18:33:00 +08:00
bool useCuda = gpuOption.compare("cuda") == 0;
2013-02-25 18:33:00 +08:00
Ptr<SuperResolution> superRes;
if (useCuda)
superRes = createSuperResolution_BTVL1_CUDA();
2013-02-25 18:33:00 +08:00
else
superRes = createSuperResolution_BTVL1();
Ptr<cv::superres::DenseOpticalFlowExt> of = createOptFlow(optFlow, useCuda);
if (of.empty())
return EXIT_FAILURE;
superRes->setOpticalFlow(of);
2013-02-25 18:33:00 +08:00
superRes->setScale(scale);
superRes->setIterations(iterations);
superRes->setTemporalAreaRadius(temporalAreaRadius);
2013-03-30 04:47:22 +08:00
2013-02-25 18:33:00 +08:00
Ptr<FrameSource> frameSource;
if (useCuda)
2013-02-25 18:33:00 +08:00
{
// Try to use gpu Video Decoding
try
{
2013-07-24 17:55:18 +08:00
frameSource = createFrameSource_Video_CUDA(inputVideoName);
2013-02-25 18:33:00 +08:00
Mat frame;
frameSource->nextFrame(frame);
}
catch (const cv::Exception&)
{
frameSource.release();
}
}
if (!frameSource)
2013-02-25 18:33:00 +08:00
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;
2013-08-23 16:34:35 +08:00
cout << "Mode : " << (useCuda ? "CUDA" : "CPU") << endl;
2013-02-25 18:33:00 +08:00
}
superRes->setInput(frameSource);
VideoWriter writer;
for (int i = 0;; ++i)
{
cout << '[' << setw(3) << i << "] : ";
Mat result;
MEASURE_TIME(superRes->nextFrame(result));
2013-02-25 18:33:00 +08:00
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());
2013-02-25 18:33:00 +08:00
writer << result;
}
}
return 0;
}