2013-02-25 18:33:00 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <string>
|
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/contrib.hpp"
|
|
|
|
#include "opencv2/superres.hpp"
|
2013-02-25 18:33:00 +08:00
|
|
|
#include "opencv2/superres/optical_flow.hpp"
|
2013-08-16 16:30:40 +08:00
|
|
|
#include "opencv2/opencv_modules.hpp"
|
|
|
|
|
|
|
|
#if defined(HAVE_OPENCV_OCL)
|
|
|
|
#include "opencv2/ocl/ocl.hpp"
|
|
|
|
#endif
|
2013-02-25 18:33:00 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
using namespace cv::superres;
|
2013-08-16 16:30:40 +08:00
|
|
|
bool useOclChanged;
|
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<DenseOpticalFlowExt> createOptFlow(const string& name, bool useGpu)
|
|
|
|
{
|
|
|
|
if (name == "farneback")
|
|
|
|
{
|
|
|
|
if (useGpu)
|
|
|
|
return createOptFlow_Farneback_GPU();
|
|
|
|
else
|
|
|
|
return createOptFlow_Farneback();
|
|
|
|
}
|
|
|
|
else if (name == "simple")
|
|
|
|
return createOptFlow_Simple();
|
|
|
|
else if (name == "tvl1")
|
|
|
|
{
|
|
|
|
if (useGpu)
|
|
|
|
return createOptFlow_DualTVL1_GPU();
|
|
|
|
else
|
|
|
|
return createOptFlow_DualTVL1();
|
|
|
|
}
|
|
|
|
else if (name == "brox")
|
|
|
|
return createOptFlow_Brox_GPU();
|
|
|
|
else if (name == "pyrlk")
|
|
|
|
return createOptFlow_PyrLK_GPU();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "Incorrect Optical Flow algorithm - " << name << endl;
|
|
|
|
}
|
2013-08-13 22:49:29 +08:00
|
|
|
return Ptr<DenseOpticalFlowExt>();
|
2013-02-25 18:33:00 +08:00
|
|
|
}
|
2013-08-16 16:30:40 +08:00
|
|
|
#if defined(HAVE_OPENCV_OCL)
|
|
|
|
static Ptr<DenseOpticalFlowExt> createOptFlow(const string& name)
|
|
|
|
{
|
|
|
|
if (name == "farneback")
|
|
|
|
{
|
|
|
|
return createOptFlow_Farneback_OCL();
|
|
|
|
}
|
|
|
|
else if (name == "simple")
|
|
|
|
{
|
|
|
|
useOclChanged = true;
|
|
|
|
std::cout<<"simple on OpenCL has not been implemented. Use CPU instead!\n";
|
|
|
|
return createOptFlow_Simple();
|
|
|
|
}
|
|
|
|
else if (name == "tvl1")
|
|
|
|
return createOptFlow_DualTVL1_OCL();
|
|
|
|
else if (name == "brox")
|
|
|
|
{
|
|
|
|
std::cout<<"brox has not been implemented!\n";
|
2013-08-13 22:49:29 +08:00
|
|
|
return Ptr<DenseOpticalFlowExt>();
|
2013-08-16 16:30:40 +08:00
|
|
|
}
|
|
|
|
else if (name == "pyrlk")
|
|
|
|
return createOptFlow_PyrLK_OCL();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "Incorrect Optical Flow algorithm - " << name << endl;
|
|
|
|
}
|
2013-08-13 22:49:29 +08:00
|
|
|
return Ptr<DenseOpticalFlowExt>();
|
2013-08-16 16:30:40 +08:00
|
|
|
}
|
|
|
|
#endif
|
2013-02-25 18:33:00 +08:00
|
|
|
int main(int argc, const char* argv[])
|
|
|
|
{
|
2013-08-16 16:30:40 +08:00
|
|
|
useOclChanged = false;
|
2013-02-25 18:33:00 +08:00
|
|
|
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) }"
|
2013-08-19 18:08:34 +08:00
|
|
|
"{ g | false | CPU as default device, cuda for CUDA and ocl for OpenCL }"
|
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();
|
2013-02-25 18:33:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2013-08-16 16:30:40 +08:00
|
|
|
string gpuOption = cmd.get<string>("gpu");
|
|
|
|
|
|
|
|
std::transform(gpuOption.begin(), gpuOption.end(), gpuOption.begin(), ::tolower);
|
2013-02-25 18:33:00 +08:00
|
|
|
|
2013-08-16 16:30:40 +08:00
|
|
|
bool useCuda = false;
|
|
|
|
bool useOcl = false;
|
|
|
|
|
|
|
|
if(gpuOption.compare("ocl") == 0)
|
|
|
|
useOcl = true;
|
|
|
|
else if(gpuOption.compare("cuda") == 0)
|
|
|
|
useCuda = true;
|
2013-02-25 18:33:00 +08:00
|
|
|
|
2013-08-16 16:30:40 +08:00
|
|
|
#ifndef HAVE_OPENCV_OCL
|
|
|
|
if(useOcl)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
cout<<"OPENCL is not compiled\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(HAVE_OPENCV_OCL)
|
|
|
|
std::vector<cv::ocl::Info>info;
|
|
|
|
if(useCuda)
|
|
|
|
{
|
|
|
|
CV_Assert(!useOcl);
|
|
|
|
info.clear();
|
|
|
|
}
|
2013-08-19 18:08:34 +08:00
|
|
|
|
2013-08-16 16:30:40 +08:00
|
|
|
if(useOcl)
|
|
|
|
{
|
|
|
|
CV_Assert(!useCuda);
|
|
|
|
cv::ocl::getDevice(info);
|
|
|
|
}
|
|
|
|
#endif
|
2013-02-25 18:33:00 +08:00
|
|
|
Ptr<SuperResolution> superRes;
|
2013-08-16 16:30:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
#if defined(HAVE_OPENCV_OCL)
|
|
|
|
if(useOcl)
|
|
|
|
{
|
|
|
|
Ptr<DenseOpticalFlowExt> of = createOptFlow(optFlow);
|
|
|
|
if (of.empty())
|
|
|
|
exit(-1);
|
|
|
|
if(useOclChanged)
|
|
|
|
{
|
|
|
|
superRes = createSuperResolution_BTVL1();
|
|
|
|
useOcl = !useOcl;
|
|
|
|
}else
|
|
|
|
superRes = createSuperResolution_BTVL1_OCL();
|
|
|
|
superRes->set("opticalFlow", of);
|
|
|
|
}
|
2013-02-25 18:33:00 +08:00
|
|
|
else
|
2013-08-16 16:30:40 +08:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (useCuda)
|
|
|
|
superRes = createSuperResolution_BTVL1_GPU();
|
|
|
|
else
|
|
|
|
superRes = createSuperResolution_BTVL1();
|
|
|
|
|
|
|
|
Ptr<DenseOpticalFlowExt> of = createOptFlow(optFlow, useCuda);
|
2013-08-19 18:08:34 +08:00
|
|
|
|
2013-08-16 16:30:40 +08:00
|
|
|
if (of.empty())
|
|
|
|
exit(-1);
|
|
|
|
superRes->set("opticalFlow", of);
|
|
|
|
}
|
2013-02-25 18:33:00 +08:00
|
|
|
|
|
|
|
superRes->set("scale", scale);
|
|
|
|
superRes->set("iterations", iterations);
|
|
|
|
superRes->set("temporalAreaRadius", temporalAreaRadius);
|
2013-03-30 04:47:22 +08:00
|
|
|
|
2013-02-25 18:33:00 +08:00
|
|
|
Ptr<FrameSource> frameSource;
|
2013-08-16 16:30:40 +08:00
|
|
|
if (useCuda)
|
2013-02-25 18:33:00 +08:00
|
|
|
{
|
|
|
|
// Try to use gpu Video Decoding
|
|
|
|
try
|
|
|
|
{
|
|
|
|
frameSource = createFrameSource_Video_GPU(inputVideoName);
|
|
|
|
Mat frame;
|
|
|
|
frameSource->nextFrame(frame);
|
|
|
|
}
|
|
|
|
catch (const cv::Exception&)
|
|
|
|
{
|
|
|
|
frameSource.release();
|
|
|
|
}
|
|
|
|
}
|
2013-08-13 22:49:29 +08:00
|
|
|
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-16 16:30:40 +08:00
|
|
|
#if defined(HAVE_OPENCV_OCL)
|
|
|
|
cout << "Mode : " << (useCuda ? "CUDA" : useOcl? "OpenCL" : "CPU") << endl;
|
|
|
|
#else
|
2013-08-23 16:34:35 +08:00
|
|
|
cout << "Mode : " << (useCuda ? "CUDA" : "CPU") << endl;
|
2013-08-16 16:30:40 +08:00
|
|
|
#endif
|
2013-02-25 18:33:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
superRes->setInput(frameSource);
|
|
|
|
|
|
|
|
VideoWriter writer;
|
|
|
|
|
|
|
|
for (int i = 0;; ++i)
|
|
|
|
{
|
|
|
|
cout << '[' << setw(3) << i << "] : ";
|
|
|
|
Mat result;
|
|
|
|
|
2013-08-16 16:30:40 +08:00
|
|
|
#if defined(HAVE_OPENCV_OCL)
|
|
|
|
cv::ocl::oclMat result_;
|
|
|
|
|
|
|
|
if(useOcl)
|
|
|
|
{
|
|
|
|
MEASURE_TIME(superRes->nextFrame(result_));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
MEASURE_TIME(superRes->nextFrame(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_OPENCV_OCL
|
|
|
|
if(useOcl)
|
|
|
|
{
|
|
|
|
if(!result_.empty())
|
|
|
|
{
|
|
|
|
result_.download(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
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())
|
2013-04-08 02:45:38 +08:00
|
|
|
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;
|
|
|
|
}
|