In this tutorial you will learn how to use the 'dnn_superres' interface to upscale video via pre-trained neural networks.
Building
When building OpenCV, run the following command to build all the contrib module:
cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules/
Or only build the dnn_superres module:
cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules/dnn_superres
Or make sure you check the dnn_superres module in the GUI version of CMake: cmake-gui.
Source Code of the sample
14using namespace dnn_superres;
16int main(
int argc,
char *argv[])
21 cout <<
"usage: Arg 1: input video path" << endl;
22 cout <<
"\t Arg 2: output video path" << endl;
23 cout <<
"\t Arg 3: algorithm | edsr, espcn, fsrcnn or lapsrn" << endl;
24 cout <<
"\t Arg 4: scale | 2, 3, 4 or 8 \n";
25 cout <<
"\t Arg 5: path to model file \n";
29 string input_path = string(argv[1]);
30 string output_path = string(argv[2]);
31 string algorithm = string(argv[3]);
32 int scale = atoi(argv[4]);
33 string path = string(argv[5]);
36 int ex =
static_cast<int>(input_video.get(CAP_PROP_FOURCC));
37 Size S =
Size((
int) input_video.get(CAP_PROP_FRAME_WIDTH) * scale,
38 (
int) input_video.get(CAP_PROP_FRAME_HEIGHT) * scale);
41 output_video.
open(output_path, ex, input_video.get(CAP_PROP_FPS), S,
true);
43 if (!input_video.isOpened())
45 std::cerr <<
"Could not open the video." << std::endl;
51 sr.setModel(algorithm, scale);
55 Mat frame, output_frame;
61 sr.upsample(frame, output_frame);
62 output_video << output_frame;
64 namedWindow(
"Upsampled video", WINDOW_AUTOSIZE);
65 imshow(
"Upsampled video", output_frame);
67 namedWindow(
"Original video", WINDOW_AUTOSIZE);
68 imshow(
"Original video", frame);
70 char c=(char)waitKey(25);
75 input_video.release();
n-dimensional dense array class
Definition mat.hpp:828
bool empty() const
Returns true if the array has no elements.
Template class for specifying the size of an image or rectangle.
Definition types.hpp:335
Class for video capturing from video files, image sequences or cameras.
Definition videoio.hpp:735
Video writer class.
Definition videoio.hpp:1013
virtual bool open(const String &filename, int fourcc, double fps, Size frameSize, bool isColor=true)
Initializes or reinitializes video writer.
virtual void release()
Closes the video writer.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
Explanation
- Set header and namespaces
using namespace dnn_superres;
- Create the Dnn Superres object Instantiate a dnn super-resolution object.
- Read the model
path = "models/ESPCN_x2.pb"
sr.readModel(path);
sr.setModel("espcn", 2);
Read the model from the given path and sets the algorithm and scaling factor.
- Upscale a video
for(;;)
{
input_video >> frame;
break;
sr.upsample(frame, output_frame);
...
}
Process and upsample video frame by frame.