OpenCV 5.0.0
Open Source Computer Vision
読み込み中...
検索中...
見つかりません
🤖 AIによる機械翻訳(非公式) — これは OpenCV 5.0.0 公式リファレンス(英語)を AI (Claude) で自動翻訳したものです。訳に誤りを含む場合があります。正確な情報は 公式英語版(原文) を参照してください。
画像の超解像: 単一出力

このチュートリアルでは、'dnn_superres' インターフェースを使って、事前学習済みニューラルネットワークによって画像を拡大する方法を学ぶ。C++とPythonで動作する。

ビルド

OpenCVをビルドする際、contribモジュールをすべてビルドするには次のコマンドを実行する。

cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules/

または dnn_superres モジュールのみをビルドする。

cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules/dnn_superres

または、CMakeのGUI版である cmake-gui で dnn_superres モジュールにチェックが入っていることを確認する。

サンプルのソースコード

次のようにしてサンプルコードを実行できる。

<path_of_your_opencv_build_directory>/bin/example_dnn_superres_dnn_superres <path_to_image.png> <algo_string> <upscale_int> <model_path.pb>

例:

/home/opencv/build/bin/example_dnn_superres_dnn_superres /home/image.png edsr 2 /home/EDSR_x2.pb
1// This file is part of OpenCV project.
2// It is subject to the license terms in the LICENSE file found in the top-level directory
3// of this distribution and at http://opencv.org/license.html.
4
5#include <iostream>
6
8
9#include <opencv2/imgproc.hpp>
10#include <opencv2/highgui.hpp>
11
12using namespace std;
13using namespace cv;
14using namespace dnn;
15using namespace dnn_superres;
16
17int main(int argc, char *argv[])
18{
19 // Check for valid command line arguments, print usage
20 // if insufficient arguments were given.
21 if ( argc < 4 ) {
22 cout << "usage: Arg 1: image | Path to image" << endl;
23 cout << "\t Arg 2: algorithm | bilinear, bicubic, edsr, espcn, fsrcnn or lapsrn" << endl;
24 cout << "\t Arg 3: scale | 2, 3 or 4 \n";
25 cout << "\t Arg 4: path to model file \n";
26 return -1;
27 }
28
29 string img_path = string(argv[1]);
30 string algorithm = string(argv[2]);
31 int scale = atoi(argv[3]);
32 string path = "";
33
34 if( argc > 4)
35 path = string(argv[4]);
36
37 // Load the image
38 Mat img = cv::imread(img_path);
39 Mat original_img(img);
40 if ( img.empty() )
41 {
42 std::cerr << "Couldn't load image: " << img << "\n";
43 return -2;
44 }
45
46 //Make dnn super resolution instance
47 DnnSuperResImpl sr;
48
49 Mat img_new;
50
51 if( algorithm == "bilinear" ){
52 resize(img, img_new, Size(), scale, scale, 2);
53 }
54 else if( algorithm == "bicubic" )
55 {
56 resize(img, img_new, Size(), scale, scale, 3);
57 }
58 else if( algorithm == "edsr" || algorithm == "espcn" || algorithm == "fsrcnn" || algorithm == "lapsrn" )
59 {
60 sr.readModel(path);
61 sr.setModel(algorithm, scale);
62 sr.upsample(img, img_new);
63 }
64 else{
65 std::cerr << "Algorithm not recognized. \n";
66 }
67
68 if ( img_new.empty() )
69 {
70 std::cerr << "Upsampling failed. \n";
71 return -3;
72 }
73 cout << "Upsampling succeeded. \n";
74
75 // Display image
76 cv::namedWindow("Initial Image", WINDOW_AUTOSIZE);
77 cv::imshow("Initial Image", img_new);
78 //cv::imwrite("./saved.jpg", img_new);
79 cv::waitKey(0);
80
81 return 0;
82}
Comma-separated Matrix Initializer.
Definition mat.hpp:964
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:338
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
Definition core.hpp:107
STL namespace.

解説

  1. ヘッダと名前空間を設定する

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

    必要であれば、上記のコードのように名前空間を設定できる。

  2. Dnn Superresオブジェクトを作成する

    DnnSuperResImpl sr;

    これは単にオブジェクトを作成し、カスタムのdnnレイヤーを登録し、クラスの関数にアクセスできるようにするためのものである。

  3. モデルを読み込む

    path = "models/FSRCNN_x2.pb"
    sr.readModel(path);

    これは .pb ファイルからTensorFlowモデルを読み込む。ここで 'path' は事前学習済みTensorFlowモデルのいずれかのパスファイルである。モデルはOpenCVのGitHubの 'dnn_superres' モジュールからダウンロードできる。

  4. モデルを設定する

    sr.setModel("fsrcnn", 2);

    実行したいモデルに応じて、アルゴリズムと拡大率を設定する必要がある。これは、.pb ファイルの名前を変更しても、目的のアルゴリズムとスケールを知るためである。たとえば、FSRCNN_x2.pb を選んだ場合、アルゴリズムとスケールはそれぞれ 'fsrcnn' と 2 になる。(その他のアルゴリズムの選択肢には "edsr"、"espcn"、"lapsrn" がある。)

  5. 画像を拡大する

    Mat img = cv::imread(img_path);
    Mat img_new;
    sr.upsample(img, img_new);

    これで任意の画像を拡大できる。標準の 'imread' 関数で画像を読み込み、出力画像用に新しいMatを作成する。あとは単純に拡大するだけである。拡大された画像は 'img_new' に格納される。

Pythonでの例

import cv2
from cv2 import dnn_superres
# Create an SR object - only function that differs from c++ code
sr = dnn_superres.DnnSuperResImpl_create()
# Read image
image = cv2.imread('./image.png')
# Read the desired model
path = "EDSR_x4.pb"
sr.readModel(path)
# Set the desired model and scale to get correct pre- and post-processing
sr.setModel("edsr", 4)
# Upscale the image
result = sr.upsample(image)
# Save the image
cv2.imwrite("./upscaled.png", result)

元画像:

FSRCNNによる拡大画像:

バイキュービック補間による拡大画像: