OpenCV trace VideoCapture 流程

本篇紀錄一下 OpenCV 的 VideoCapture 的啟動流程。

Trace 的 OpenCV 版本為:3.4.8 / 4.1.2
以下範例為 Python 的開啟 VideoCapture 範例,C++版本就先不列出來了,
以下紀錄為 VideoCapture 建構流程,以及每幀frame讀取出來的流程。

1
2
3
4
5
6
7
8
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2

cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
cv2.imshow('frame', frame)

VideoCapture 建構流程

VideoCapture::VideoCapture()建構子
從 VideoCapture::VideoCapture() 開始吧!
https://github.com/opencv/opencv/blob/master/modules/videoio/src/cap.cpp

modules/videoio/src/cap.cpplink
1
2
3
4
5
6
7
8
9
VideoCapture::VideoCapture(const String& filename, int apiPreference) : throwOnFail(false)
{
open(filename, apiPreference); // 1-1
}

VideoCapture::VideoCapture(int index, int apiPreference) : throwOnFail(false)
{
open(index, apiPreference); // 2-1
}

VideoCapture::open()
VideoCapture::open() 有兩種,一種是帶字串的,另一種是帶整數的。
帶字串的會呼叫 getAvailableBackends_CaptureByFilename
帶整數的會呼叫 getAvailableBackends_CaptureByIndex

modules/videoio/src/cap.cpplink
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
bool VideoCapture::open(const String& filename, int apiPreference) // 1-1
{
if (isOpened()) release();

const std::vector<VideoBackendInfo> backends =
cv::videoio_registry::getAvailableBackends_CaptureByFilename(); // 1-2

for (size_t i = 0; i < backends.size(); i++)
{
const VideoBackendInfo& info = backends[i];
if (apiPreference == CAP_ANY || apiPreference == info.id)
{
const Ptr<IBackend> backend = info.backendFactory->getBackend(); // 1-9 backendFactory
if (!backend.empty())
{
try
{
icap = backend->createCapture(filename); // 1-10
if (!icap.empty())
{
if (icap->isOpened())
return true;
icap.release();
}
} catch(...) {
// ...
}
}
}
}

return false;
}

bool VideoCapture::open(int cameraNum, int apiPreference) // 2-1
{
if (isOpened()) release();

if (apiPreference == CAP_ANY)
{
// interpret preferred interface (0 = autodetect)
int backendID = (cameraNum / 100) * 100;
if (backendID)
{
cameraNum %= 100;
apiPreference = backendID;
}
}

const std::vector<VideoBackendInfo> backends =
cv::videoio_registry::getAvailableBackends_CaptureByIndex(); // 2-2
for (size_t i = 0; i < backends.size(); i++)
{
const VideoBackendInfo& info = backends[i];
if (apiPreference == CAP_ANY || apiPreference == info.id)
{
const Ptr<IBackend> backend = info.backendFactory->getBackend();
if (!backend.empty())
{
try
{
icap = backend->createCapture(cameraNum);
if (!icap.empty())
{
if (icap->isOpened())
return true;
icap.release();
}
} catch(...) {
// ...
}
}
}
}

return false;
}

IBackend 與 IBackendFactory 的關係,IBackendFactory 就是專門生產 IBackend 的物件。
常見的 Singleton 設計模型 static VideoBackendRegistry& getInstance

https://github.com/opencv/opencv/blob/master/modules/videoio/src/videoio_registry.cpp

modules/videoio/src/videoio_registry.cpplink
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#define DECLARE_DYNAMIC_BACKEND(cap, name, mode) \
{ \
cap, (BackendMode)(mode), 1000, name, createPluginBackendFactory(cap, name) \
}

#define DECLARE_STATIC_BACKEND(cap, name, mode, createCaptureFile, createCaptureCamera, createWriter) \
{ \
cap, (BackendMode)(mode), 1000, name, createBackendFactory(createCaptureFile, createCaptureCamera, createWriter) \
}

// 1-5
static const struct VideoBackendInfo builtin_backends[] =
{
// ...
// Windows
#ifdef HAVE_DSHOW
DECLARE_STATIC_BACKEND(CAP_DSHOW, "DSHOW", MODE_CAPTURE_BY_INDEX, 0, create_DShow_capture, 0),
#endif

// Linux, some Unix
#if defined HAVE_CAMV4L2 // 1-6
DECLARE_STATIC_BACKEND(CAP_V4L2, "V4L2", MODE_CAPTURE_ALL, create_V4L_capture_file, create_V4L_capture_cam, 0),
#elif defined HAVE_VIDEOIO
DECLARE_STATIC_BACKEND(CAP_V4L, "V4L_BSD", MODE_CAPTURE_ALL, create_V4L_capture_file, create_V4L_capture_cam, 0),
#endif
// ...
};

class VideoBackendRegistry
{
protected:
std::vector<VideoBackendInfo> enabledBackends;
VideoBackendRegistry() // 1-4
{
const int N = sizeof(builtin_backends)/sizeof(builtin_backends[0]);
enabledBackends.assign(builtin_backends, builtin_backends + N); // 1-5 builtin_backends
for (int i = 0; i < N; i++)
{
VideoBackendInfo& info = enabledBackends[i];
info.priority = 1000 - i * 10;
}

// ...
std::sort(enabledBackends.begin(), enabledBackends.end(), sortByPriority); // 排序
}

// ...
static VideoBackendRegistry& getInstance() // 1-3, 2-3 常見的 instance 技巧!
{
static VideoBackendRegistry g_instance; // 1-4 VideoBackendRegistry()
return g_instance;
}
// ...
inline std::vector<VideoBackendInfo> getAvailableBackends_CaptureByIndex() const // 2-4
{
std::vector<VideoBackendInfo> result;
for (size_t i = 0; i < enabledBackends.size(); i++)
{
const VideoBackendInfo& info = enabledBackends[i];
if (info.mode & MODE_CAPTURE_BY_INDEX)
result.push_back(info);
}
return result;
}
inline std::vector<VideoBackendInfo> getAvailableBackends_CaptureByFilename() const // 1-8
{
std::vector<VideoBackendInfo> result;
for (size_t i = 0; i < enabledBackends.size(); i++)
{
const VideoBackendInfo& info = enabledBackends[i];
if (info.mode & MODE_CAPTURE_BY_FILENAME)
result.push_back(info);
}
return result;
}

};

namespace videoio_registry {

std::vector<VideoBackendInfo> getAvailableBackends_CaptureByIndex() // 2-2
{
// 2-3 getInstance, 2-4 getAvailableBackends_CaptureByIndex
const std::vector<VideoBackendInfo> result = VideoBackendRegistry::getInstance().getAvailableBackends_CaptureByIndex();
return result;
}
std::vector<VideoBackendInfo> getAvailableBackends_CaptureByFilename() // 1-2
{
// 1-3 getInstance, 1-8 getAvailableBackends_CaptureByFilename
const std::vector<VideoBackendInfo> result = VideoBackendRegistry::getInstance().getAvailableBackends_CaptureByFilename();
return result;
}
}

https://github.com/opencv/opencv/blob/master/modules/videoio/src/videoio_registry.hpp

modules/videoio/src/videoio_registry.hpplink
1
2
3
4
5
6
7
8
9
struct VideoBackendInfo {
VideoCaptureAPIs id;
BackendMode mode;
int priority; // 1000-<index*10> - default builtin priority
// 0 - disabled (OPENCV_VIDEOIO_PRIORITY_<name> = 0)
// >10000 - prioritized list (OPENCV_VIDEOIO_PRIORITY_LIST)
const char* name;
Ptr<IBackendFactory> backendFactory; // 1-9
};

https://github.com/opencv/opencv/blob/master/modules/videoio/src/backend.hpp

modules/videoio/src/backend.hpplink
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class IBackend
{
public:
virtual ~IBackend() {}
virtual Ptr<IVideoCapture> createCapture(int camera) const = 0;
virtual Ptr<IVideoCapture> createCapture(const std::string &filename) const = 0; // 1-10
virtual Ptr<IVideoWriter> createWriter(const std::string &filename, int fourcc, double fps, const cv::Size &sz, bool isColor) const = 0;
};

class IBackendFactory
{
public:
virtual ~IBackendFactory() {}
virtual Ptr<IBackend> getBackend() const = 0; // 要看是實作哪種 IBackendFactory
};

https://github.com/opencv/opencv/blob/master/modules/videoio/src/cap_interface.hpp

modules/videoio/src/cap_interface.hpplink
1
2
3
4
5
Ptr<IVideoCapture> create_DShow_capture(int index);
// ...
Ptr<IVideoCapture> create_V4L_capture_cam(int index); // 1-6, 1-7
// ...
Ptr<IVideoCapture> create_Images_capture(const std::string &filename);

https://github.com/opencv/opencv/blob/master/modules/videoio/src/cap_v4l.cpp

modules/videoio/src/cap_v4l.cpplink
1
2
3
4
5
6
7
8
9
10
Ptr<IVideoCapture> create_V4L_capture_cam(int index) // 1-7
{
cv::CvCaptureCAM_V4L* capture = new cv::CvCaptureCAM_V4L(); // 下一篇再介紹

if (capture->open(index))
return makePtr<LegacyCapture>(capture);

delete capture;
return NULL;
}

其它相關文章推薦
OpenCV FileStorage 用法與 YAML 檔案讀取寫入範例
如何看OpenCV當初編譯的編譯參設定
怎麼查詢 OpenCV 的版本