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

このチュートリアルでは、'dnn_superres' インターフェースを用いて、学習済みニューラルネットワークによって動画をアップスケールする方法を学ぶ。

ビルド

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

サンプルのソースコード

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_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: 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";
26 return -1;
27 }
28
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]);
34
35 VideoCapture input_video(input_path);
36 int ex = static_cast<int>(input_video.get(CAP_PROP_FOURCC));
37 Size S = Size((int) input_video.get(CAP_PROP_FRAME_WIDTH) * scale,
38 (int) input_video.get(CAP_PROP_FRAME_HEIGHT) * scale);
39
40 VideoWriter output_video;
41 output_video.open(output_path, ex, input_video.get(CAP_PROP_FPS), S, true);
42
43 if (!input_video.isOpened())
44 {
45 std::cerr << "Could not open the video." << std::endl;
46 return -1;
47 }
48
49 DnnSuperResImpl sr;
50 sr.readModel(path);
51 sr.setModel(algorithm, scale);
52
53 for(;;)
54 {
55 Mat frame, output_frame;
56 input_video >> frame;
57
58 if ( frame.empty() )
59 break;
60
61 sr.upsample(frame, output_frame);
62 output_video << output_frame;
63
64 namedWindow("Upsampled video", WINDOW_AUTOSIZE);
65 imshow("Upsampled video", output_frame);
66
67 namedWindow("Original video", WINDOW_AUTOSIZE);
68 imshow("Original video", frame);
69
70 char c=(char)waitKey(25);
71 if(c==27)
72 break;
73 }
74
75 input_video.release();
76 output_video.release();
77
78 return 0;
79}
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
Class for video capturing from video files, image sequences or cameras.
Definition videoio.hpp:790
Video writer class.
Definition videoio.hpp:1077
virtual bool open(const String &filename, int fourcc, double fps, Size frameSize, bool isColor=true)
Initializes or reinitializes video writer.
virtual void release()
Closes the video writer.
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_superres;
  2. Dnn Superres オブジェクトの作成
    DnnSuperResImpl sr;
    DNN超解像オブジェクトをインスタンス化する。
  3. モデルの読み込み
    path = "models/ESPCN_x2.pb"
    sr.readModel(path);
    sr.setModel("espcn", 2);
    指定したパスからモデルを読み込み、アルゴリズムと拡大率を設定する。
  4. 動画の拡大
    for(;;)
    {
    Mat frame, output_frame;
    input_video >> frame;
    if ( frame.empty() )
    break;
    sr.upsample(frame, output_frame);
    ...
    }
    動画をフレームごとに処理して拡大する。