Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion modules/dnn_superres/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,81 @@
# Super Resolution using Convolutional Neural Networks

This module contains several learning-based algorithms for upscaling an image.
This repository contains an OpenCV module, which uses neural networks to upscale images.
This superesolution module, originally created as a GSoC 2018 project, contains methods to upscale images using
the following super-resolution algorithms:

- EDSR, Bee Lim, et al. "Enhanced deep residual networks for single image super-resolution." https://arxiv.org/abs/1707.02921
- ESPCN, Shi, Wenzhe, et al. "Real-time single image and video super-resolution using an efficient sub-pixel convolutional neural network." https://arxiv.org/abs/1609.05158
- FSRCNN, Dong, Chao, Chen Change Loy, and Xiaoou Tang. "Accelerating the super-resolution convolutional neural network." https://arxiv.org/abs/1608.00367
- LapSRN, Lai, Wei-Sheng, et al. "Fast and accurate image super-resolution with deep laplacian pyramid networks." https://arxiv.org/abs/1710.01992
- SRGAN, Ledig, Christian, et al. "Photo-realistic single image super-resolution using a generative adversarial network." https://arxiv.org/abs/1609.04802
- RDN, Zhang, Yulun, et al. "Residual dense network for image super-resolution." https://arxiv.org/abs/1802.08797

## Copyright Notice and Citation

This module is part of the OpenCV project and is available under the Apache 2.0 license.

When using the SRGAN and RDN models in academic projects, please cite the appropriate papers:

For SRGAN:
```
@InProceedings{Ledig_2017_CVPR,
author = {Ledig, Christian and Theis, Lucas and Huszar, Ferenc and Caballero, Jose and Cunningham, Andrew and Acosta, Alejandro and Aitken, Andrew and Tejani, Alykhan and Totz, Johannes and Wang, Zehan and Shi, Wenzhe},
title = {Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network},
booktitle = {Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
month = {July},
year = {2017}
}
```

For RDN:
```
@inproceedings{zhang2018residual,
title={Residual Dense Network for Image Super-Resolution},
author={Zhang, Yulun and Tian, Yapeng and Kong, Yu and Zhong, Bineng and Fu, Yun},
booktitle={CVPR},
year={2018}
}
```

## Patent Information

To the best of our knowledge, the implemented algorithms (SRGAN and RDN) are not encumbered by patents that would restrict their use in this project. These implementations are based on academic research papers and are provided for academic and research purposes.

## Usage

The dnn_superres module allows for super-resolution using neural networks. The network takes as input a image of a certain size, and outputs a larger one. Here's a brief example of how to use the provided C++ API:

```c++
using namespace std;
using namespace cv;
using namespace dnn_superres;

int main(int argc, char* argv[])
{
cv::Mat img = cv::imread("img.png");
cv::Mat img_new;

// Create the Dnn Superres object
cv::dnn_superres::DnnSuperResImpl sr;

// Read the desired model
path = "models/FSRCNN_x4.pb"
sr.readModel(path);

// Set the desired model and scale to get correct pre- and post-processing
sr.setModel("fsrcnn", 4);

// Upscale
sr.upsample(img, img_new);

// Save the result
cv::imwrite("./img_upscaled.png", img_new);

return 0;
}
```

Run the following command to build this module:

```make
Expand Down
16 changes: 12 additions & 4 deletions modules/dnn_superres/include/opencv2/dnn_superres.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
/** @defgroup dnn_superres DNN used for super resolution

This module contains functionality for upscaling an image via convolutional neural networks.
The following four models are implemented:
The following models are implemented:

- EDSR <https://arxiv.org/abs/1707.02921>
- ESPCN <https://arxiv.org/abs/1609.05158>
- FSRCNN <https://arxiv.org/abs/1608.00367>
- LapSRN <https://arxiv.org/abs/1710.01992>
- SRGAN <https://arxiv.org/abs/1609.04802>
- RDN <https://arxiv.org/abs/1802.08797>

*/

Expand All @@ -29,12 +31,14 @@ namespace dnn_superres
//! @{

/** @brief A class to upscale images via convolutional neural networks.
The following four models are implemented:
The following models are implemented:

- edsr
- espcn
- fsrcnn
- lapsrn
- srgan
- rdn
*/

class CV_EXPORTS_W DnnSuperResImpl
Expand All @@ -59,8 +63,8 @@ class CV_EXPORTS_W DnnSuperResImpl
*/
CV_WRAP static Ptr<DnnSuperResImpl> create();

// /** @brief Empty constructor
// */
/** @brief Empty constructor
*/
DnnSuperResImpl();

/** @brief Constructor which immediately sets the desired model
Expand All @@ -69,6 +73,8 @@ class CV_EXPORTS_W DnnSuperResImpl
- __espcn__
- __fsrcnn__
- __lapsrn__
- __srgan__
- __rdn__
@param scale Integer specifying the upscale factor
*/
DnnSuperResImpl(const String& algo, int scale);
Expand All @@ -90,6 +96,8 @@ class CV_EXPORTS_W DnnSuperResImpl
- __espcn__
- __fsrcnn__
- __lapsrn__
- __srgan__
- __rdn__
@param scale Integer specifying the upscale factor
*/
CV_WRAP void setModel(const String& algo, int scale);
Expand Down
82 changes: 81 additions & 1 deletion modules/dnn_superres/misc/python/test/test_dnn_superres.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/usr/bin/env python
# This file is part of OpenCV project.
# It is subject to the license terms in the LICENSE file found in the top-level directory
# of this distribution and at http://opencv.org/license.html.

import os
import cv2 as cv

Expand Down Expand Up @@ -44,5 +48,81 @@ def test_single_output(self):
self.assertEqual(sr.getScale(), 2)
self.assertEqual(sr.getAlgorithm(), "espcn")

@unittest.skipIf('OPENCV_TEST_DATA_PATH' not in os.environ,
"OPENCV_TEST_DATA_PATH is not defined")
def test_srgan(self):
# Get test data paths
dnn_superres_test_path = os.environ['OPENCV_TEST_DATA_PATH'] + "/cv/dnn_superres/"
img_path = dnn_superres_test_path + "butterfly.png"
srgan_path = dnn_superres_test_path + "SRGAN_x4.pb"

# Create an SR object
sr = cv.dnn_superres.DnnSuperResImpl_create()

# Read image
image = cv.imread(img_path)
inp_h, inp_w, inp_c = image.shape

# Read the desired model
sr.readModel(srgan_path)

# Set the desired model and scale to get correct pre- and post-processing
sr.setModel("srgan", 4)

# Upscale the image
result = sr.upsample(image)
out_h, out_w, out_c = result.shape

# CHECK...
# if result is not empty
self.assertFalse(result is None)

# upsampled image is correct size
self.assertEqual(out_h, inp_h*4)
self.assertEqual(out_w, inp_w*4)
self.assertEqual(out_c, inp_c)

# get functions work
self.assertEqual(sr.getScale(), 4)
self.assertEqual(sr.getAlgorithm(), "srgan")

@unittest.skipIf('OPENCV_TEST_DATA_PATH' not in os.environ,
"OPENCV_TEST_DATA_PATH is not defined")
def test_rdn(self):
# Get test data paths
dnn_superres_test_path = os.environ['OPENCV_TEST_DATA_PATH'] + "/cv/dnn_superres/"
img_path = dnn_superres_test_path + "butterfly.png"
rdn_path = dnn_superres_test_path + "RDN_x3.pb"

# Create an SR object
sr = cv.dnn_superres.DnnSuperResImpl_create()

# Read image
image = cv.imread(img_path)
inp_h, inp_w, inp_c = image.shape

# Read the desired model
sr.readModel(rdn_path)

# Set the desired model and scale to get correct pre- and post-processing
sr.setModel("rdn", 3)

# Upscale the image
result = sr.upsample(image)
out_h, out_w, out_c = result.shape

# CHECK...
# if result is not empty
self.assertFalse(result is None)

# upsampled image is correct size
self.assertEqual(out_h, inp_h*3)
self.assertEqual(out_w, inp_w*3)
self.assertEqual(out_c, inp_c)

# get functions work
self.assertEqual(sr.getScale(), 3)
self.assertEqual(sr.getAlgorithm(), "rdn")

if __name__ == '__main__':
NewOpenCVTests.bootstrap()
NewOpenCVTests.bootstrap()
36 changes: 36 additions & 0 deletions modules/dnn_superres/samples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.


set(OPENCV_SUPERRES_SAMPLE_REQUIRED_DEPS
opencv_core
opencv_imgproc
opencv_dnn_superres
opencv_highgui
opencv_imgcodecs
)

ocv_add_executable(dnn_superres
dnn_superres.cpp
)

ocv_add_executable(dnn_superres_benchmark
dnn_superres_benchmark.cpp
)

ocv_add_executable(dnn_superres_srgan_rdn_demo
dnn_superres_srgan_rdn_demo.cpp
)

ocv_target_link_libraries(dnn_superres
${OPENCV_SUPERRES_SAMPLE_REQUIRED_DEPS}
)

ocv_target_link_libraries(dnn_superres_benchmark
${OPENCV_SUPERRES_SAMPLE_REQUIRED_DEPS}
)

ocv_target_link_libraries(dnn_superres_srgan_rdn_demo
${OPENCV_SUPERRES_SAMPLE_REQUIRED_DEPS}
)
Loading