opencv cv :: Ptr的动态强制转换(Dynamic cast for opencv cv::Ptr)

编程入门 行业动态 更新时间:2024-10-11 19:24:00
opencv cv :: Ptr的动态强制转换(Dynamic cast for opencv cv::Ptr)

我目前正在同一个类中用c ++实现不同的opencv机器学习工具。 因此,我cv::ml::StatModel类cv::ml::StatModel指针向下转换为子类cv::ml::SVM (例如)的问题。

在在线教程中,人们通常会发现opencv分类器的实现,如下所示:

cv::Ptr<cv::ml::SVM> classifier = cv::ml::SVM::create();

我想要做的是在类构造函数中进行动态转换。 这是我的类变量的一部分:

class Classifier{ private: cv::Ptr<cv::ml::StatModel> classifier; cv::Ptr<cv::ml::SVM> SVM; //... followed by more code };

在我的构造函数中:

Classifier::Classifier(const char* filename, cv::ml::SVM& svm){ bool load = Classifier::getData(filename); if (load==0){ cout << "Error opening file!" << endl; } else{ classifier = &svm; //Here I want to do something linke this, just with cv::Ptr: //the following line gives compiler errors because c++ does not know that cv::Ptr is a pointer... SVM = dynamic_cast<cv::ml::SVM*>(classifier); Classifier::setUpSVM(); } }

有谁知道opencv是否为cv::Ptr提供了dynamic_cast<T>()的替代方法? 或者有没有使用opencv指针的经验,而是使用机器学习API的“普通”c ++指针? 我想知道我是否会遇到这个实现的问题,因为常见的做法似乎是使用opencv cv::Ptr类。

I am currently implementing different opencv machine learning tools in c++ in the same class. Therefore I have the problem of downcasting pointers of the superclass cv::ml::StatModel to the subclass cv::ml::SVM (for example).

In online tutorials one usually finds the implementation of opencv classifiers like this:

cv::Ptr<cv::ml::SVM> classifier = cv::ml::SVM::create();

What I want to do is a dynamic cast in the class constructor. This is part of my class variables:

class Classifier{ private: cv::Ptr<cv::ml::StatModel> classifier; cv::Ptr<cv::ml::SVM> SVM; //... followed by more code };

And in my constructor:

Classifier::Classifier(const char* filename, cv::ml::SVM& svm){ bool load = Classifier::getData(filename); if (load==0){ cout << "Error opening file!" << endl; } else{ classifier = &svm; //Here I want to do something linke this, just with cv::Ptr: //the following line gives compiler errors because c++ does not know that cv::Ptr is a pointer... SVM = dynamic_cast<cv::ml::SVM*>(classifier); Classifier::setUpSVM(); } }

Does anyone know if opencv provides an alternative to dynamic_cast<T>() for cv::Ptr ? Or alternatively has experience with not using the opencv pointers, but instead "normal" c++ pointers to use the machine learning API? I am wondering if I will run into problems with this implementation, because common practice seems to be using the opencv cv::Ptr class.

最满意答案

是的,有一个专门为cv::Ptr编写的dynamic_cast<T>()的openCV替代品。 它被称为... dynamicCast() :D对于这个问题,还有const cast和static cast的类似替代方案。

Yes, there is an openCV alternative of dynamic_cast<T>() written specifically for cv::Ptr. It's called... dynamicCast() :D There are also analogical alternatives to const cast and static cast for that matter.

更多推荐

本文发布于:2023-07-18 04:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154794.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动态   cv   opencv   Ptr   cast

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!