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

このチュートリアルでは、'dnn_superres' インターフェースを使用して、複数出力の学習済みニューラルネットワークによって画像をアップスケールする方法を学ぶ。OpenCVのdnnモジュールは、ノード名を与えれば、1回の推論で複数のノードにアクセスすることをサポートしている。現在、1回の推論実行で複数の出力を得ることができるモデルが1つ含まれており、それがLapSRNモデルである。LapSRNは1回のフォワードパスで複数の出力をサポートする。現在では2x、4x、8x、および (2x, 4x)、(2x, 4x, 8x) の超解像をサポートできる。アップロードされている学習済みモデルファイルには、次の出力ノード名がある:

  • 2xモデル: NCHW_output
  • 4xモデル: NCHW_output_2x, NCHW_output_4x
  • 8xモデル: NCHW_output_2x, NCHW_output_4x, NCHW_output_8x

ビルド

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 モジュールにチェックが入っていることを確認する。

サンプルのソースコード

次のコマンドでサンプルコードを実行する

./bin/example_dnn_superres_dnn_superres_multioutput path/to/image.png 2,4 NCHW_output_2x,NCHW_output_4x \
path/to/opencv_contrib/modules/dnn_superres/models/LapSRN_x4.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#include <sstream>
8
9#include <opencv2/imgproc.hpp>
10#include <opencv2/highgui.hpp>
11
12using namespace std;
13using namespace cv;
14using namespace dnn_superres;
15
16int main(int argc, char *argv[])
17{
18 // Check for valid command line arguments, print usage
19 // if insufficient arguments were given.
20 if (argc < 4) {
21 cout << "usage: Arg 1: image | Path to image" << endl;
22 cout << "\t Arg 2: scales in a format of 2,4,8\n";
23 cout << "\t Arg 3: output node names in a format of nchw_output_0,nchw_output_1\n";
24 cout << "\t Arg 4: path to model file \n";
25 return -1;
26 }
27
28 string img_path = string(argv[1]);
29 string scales_str = string(argv[2]);
30 string output_names_str = string(argv[3]);
31 std::string path = string(argv[4]);
32
33 //Parse the scaling factors
34 std::vector<int> scales;
35 char delim = ',';
36 {
37 std::stringstream ss(scales_str);
38 std::string token;
39 while (std::getline(ss, token, delim)) {
40 scales.push_back(atoi(token.c_str()));
41 }
42 }
43
44 //Parse the output node names
45 std::vector<String> node_names;
46 {
47 std::stringstream ss(output_names_str);
48 std::string token;
49 while (std::getline(ss, token, delim)) {
50 node_names.push_back(token);
51 }
52 }
53
54 // Load the image
55 Mat img = cv::imread(img_path);
56 Mat original_img(img);
57 if (img.empty())
58 {
59 std::cerr << "Couldn't load image: " << img << "\n";
60 return -2;
61 }
62
63 //Make dnn super resolution instance
64 DnnSuperResImpl sr;
65 int scale = *max_element(scales.begin(), scales.end());
66 std::vector<Mat> outputs;
67 sr.readModel(path);
68 sr.setModel("lapsrn", scale);
69
70 sr.upsampleMultioutput(img, outputs, scales, node_names);
71
72 for(unsigned int i = 0; i < outputs.size(); i++)
73 {
74 cv::namedWindow("Upsampled image", WINDOW_AUTOSIZE);
75 cv::imshow("Upsampled image", outputs[i]);
76 //cv::imwrite("./saved.jpg", img_new);
77 cv::waitKey(0);
78 }
79
80 return 0;
81}
n-dimensional dense array class
Definition mat.hpp:840
bool empty() const
Returns true if the array has no elements.
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
void scale(cv::Mat &mat, const cv::Mat &range, const T min, const T max)
Definition quality_utils.hpp:90
Definition core.hpp:107
STL namespace.

解説

  1. ヘッダと名前空間の設定
    using namespace std;
    using namespace cv;
    using namespace dnn_superres;
  2. Dnn Superresオブジェクトを作成する

    DnnSuperResImpl sr;

    dnn超解像オブジェクトをインスタンス化する。

  3. モデルを読み込む

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

    指定したパスからモデルを読み込む。

  4. モデルを設定する

    sr.setModel("lapsrn", 8);

    アルゴリズムとスケーリング係数を設定する。ここには最後の(最大の)スケーリング係数を指定する。

  5. ノード名とスケーリング係数を指定する

    std::vector<int> scales{2, 4, 8}
    std::vector<int> node_names{'NCHW_output_2x','NCHW_output_4x','NCHW_output_8x'}

    スケーリング係数と、モデル内の出力ノード名を設定する。

  6. 画像を拡大する

    Mat img = cv::imread(img_path);
    std::vector<Mat> outputs;
    sr.upsampleMultioutput(img, outputs, scales, node_names);

    推論を実行する。出力画像はMatのベクトルに格納される。