Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use OpenCV to realize Super-resolution processing based on Deep Learning

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use OpenCV to achieve super-resolution processing based on deep learning". The content in the article is simple and clear, and it is easy to learn and understand. please follow the editor's ideas to study and learn "how to use OpenCV to achieve super-resolution processing based on deep learning".

OpenCV is a very powerful tool library for computer vision processing. Many friends need to learn the use of OpenCV when getting started with image processing. However, with the development of computer vision technology, more and more algorithms emerge. People gradually think that OpenCV is relatively backward and give up using OpenCV.

However, OpenCV is actually an open source code base that keeps pace with the times. Is gradually absorbing and embracing the latest algorithms. In this article, we introduce how to use OpenCV to implement image super resolution (SR) based on deep learning. The advantage of using OpenCV is that we don't need to know anything about image super-resolution to use this code and achieve image super-resolution.

Specific operation steps:

1. Install the OpenCV contrib module

The super-resolution function in OpenCV is integrated into the contrib module, so we first need to install the extension module of OpenCV. For the installation process, please refer to [learn from OpenCV 4] the installation of the opencv_contrib extension module. Super resolution is integrated into the dnn_superres module, and if your friends have limited computer space, you can compile only this module.

Recently, some partners reported that they failed to install the extension module. In order to solve this problem, Xiaobai is planning to build a database compiled by various versions of opencv-contrib. Dear friends, keep an eye on our official account at any time.

two。 Download the training model

Because some models are large, they are not included in the OpenCV code library, so we need to download the trained models separately when using them. At present, only four different super-resolution models are supported, and they can achieve 2x, 3x, 4x or even 8x image methods. These models are as follows:

EDSR: this is the best performing model. But this model is also the largest, so it will run slowly.

ESPCN: this model has the characteristics of high speed and good effect, and the model is small. It can process the video in real time (depending on the size of the image).

FSRCNN: this is also a small model with fast and accurate inference capabilities. Real-time video frequency escalation can also be carried out.

LapSRN: this is a medium-sized model that features a maximum magnification of 8 times.

The official account replied to "SR Model" to get the way to download the four models.

3. Realize super resolution by program

We first give the complete program of C++, and then introduce every line of code in the program. The complete program is as follows: # include # include # include

Using namespace std;using namespace cv;using namespace dnn;using namespace dnn_superres

Int main (int argc, char * argv []) {/ / Create the module's object DnnSuperResImpl sr

/ / Set the image you would like to upscale string img_path = "image.png"; Mat img = cv::imread (img_path)

/ / Read the desired model string path = "FSRCNN_x2.pb"; sr.readModel (path)

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

/ / Upscale Mat img_new; sr.upsample (img, img_new); cv::imwrite ("upscaled.png", img_new)

Return 0;}

First, load the model we selected and input it into the variables of the neural network. It is important to note the address where the model file exists, and this article is placed in the root directory of the program. / / Read the desired modelstring path = "FSRCNN_x2.pb"; sr.readModel (path)

Then set the type and magnification factor of the model. The model selected in this paper is fsrcnn, and the magnification factor is 2. / / Set the desired model and scale to get correct pre- and post-processingsr.setModel ("fsrcnn", 2)

The models you can choose are "edsr", "fsrcnn", "lapsrn" and "espcn". These parameters are the four models we just introduced. It should be noted that the magnification of each model is inconsistent. The first three models can be magnified 2, 3, 4 times, and the last model can be magnified 2, 3, 4, 8 times.

After that, the super-resolution is magnified by the upsample () function.

/ / UpscaleMat img_new;sr.upsample (img, img_new); cv::imwrite ("upscaled.png", img_new)

The above is the C++ code, and then the code for Python to achieve super resolution is given.

Import cv2from cv2 import dnn_superres

# Create an SR objectsr = dnn_superres.DnnSuperResImpl_create ()

# Read imageimage = cv2.imread ('. / input.png')

# Read the desired modelpath = "EDSR_x3.pb" sr.readModel (path)

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

# Upscale the imageresult = sr.upsample (image)

# Save the imagecv2.imwrite (". / upscaled.png", result)

Unlike C++ code, when using python code, you need to declare it with the following code. # Create an SR objectsr = dnn_superres.DnnSuperResImpl_create ()

4. Processing result

Input image

3 times magnification by bilinear interpolation

FSRCNN is magnified 3 times

ESDR is magnified 3 times

Thank you for your reading, the above is the content of "how to use OpenCV to achieve super-resolution processing based on deep learning". After the study of this article, I believe you have a deeper understanding of how to use OpenCV to achieve super-resolution processing based on deep learning. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report