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 the 'dnn_superres' module:
cmake -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules -Dopencv_dnn_superres=ON <opencv_source_dir>
Or make sure you check the dnn_superres module in the GUI version of CMake: cmake-gui.
Source Code of the sample
14 using namespace dnn_superres;
16 int 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]);
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;
65 imshow(
"Upsampled video", output_frame);
68 imshow(
"Original video", frame);
75 input_video.release();
76 output_video.release();
Explanation
- Set header and namespaces
using namespace std;
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(;;)
{
Mat frame, output_frame;
input_video >> frame;
if ( frame.empty() )
break;
sr.upsample(frame, output_frame);
...
}
Process and upsample video frame by frame.