【发布时间】:2018-04-05 07:29:03
【问题描述】:
我正在编写一个将 cv::VideoCapture 类用于各种目的的类:处理实时视频流、处理离线图像序列、处理一些其他虚假数据。我的 OpenCV 版本是 3.4。显然,我想到的第一个解决方案是为我的类提供指向某个cv::VideoCapture 接口的指针:
class MyClass {
public:
MyClass(std::shared_ptr<SomeInterfaceToVideoCapture> camera);
}
我查看了 OpenCV 文档,实际上我发现,在标题 videoio.hpp 中确实有 IVideoCapture 的前向声明(这是接口的标准命名约定)。尽管cv::VideoCapture 似乎实际上并没有继承cv::IVideoCapture,但我还是试了一下:
class MyClass {
public:
MyClass(std::shared_ptr<cv::IVideoCapture> camera) {
if (camera->isOpened()) {
std::cerr << "VideoCapture device is not working" << std::endl;
}
}
}
// and later in GTest I create the instance like this:
MyClass myCl(new cv::VideoCapture(0));
我得到了错误:
error: invalid use of incomplete type ‘class cv::IVideoCapture’
if (camera->isOpened()) {
/usr/local/include/opencv2/videoio.hpp:581:7: note: forward declaration of ‘class cv::IVideoCapture’
class IVideoCapture;
所以,显然这个cv::IVideoCapture 接口从未定义过。但是,在cv::VideoCapture 类的同一个标头中,有两个受保护的字段:
protected:
Ptr<CvCapture> cap;
Ptr<IVideoCapture> icap;
我找不到任何关于如何使用 icap 字段的文档。当然,从技术上讲,我可以继承cv::VideoCapture,然后可以访问icap,但我看不到目的。
由于cv::VideoCapture 类中的所有方法都是虚拟的,我仍然可以覆盖测试、模拟或任何应用程序的默认行为,这很好。但是,出于好奇:我的问题是:cv::IVideoCapture 的前向声明的目的是什么以及受保护的字段 icap 的目的是什么。这是什么模式?
【问题讨论】:
-
may be of use这里用的比较多
-
感谢@GPPK。它看起来像 PImpl 成语(几乎)。