简体   繁体   English

opencv cv :: Ptr的动态强制转换

[英]Dynamic cast for opencv cv::Ptr

I am currently implementing different opencv machine learning tools in c++ in the same class. 我目前正在同一个类中用c ++实现不同的opencv机器学习工具。 Therefore I have the problem of downcasting pointers of the superclass cv::ml::StatModel to the subclass cv::ml::SVM (for example). 因此,我cv::ml::StatModelcv::ml::StatModel指针向下转换为子类cv::ml::SVM (例如)的问题。

In online tutorials one usually finds the implementation of opencv classifiers like this: 在在线教程中,人们通常会发现opencv分类器的实现,如下所示:

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 ? 有谁知道opencv是否为cv::Ptr提供了dynamic_cast<T>()的替代方法? Or alternatively has experience with not using the opencv pointers, but instead "normal" c++ pointers to use the machine learning API? 或者有没有使用opencv指针的经验,而是使用机器学习API的“普通”c ++指针? I am wondering if I will run into problems with this implementation, because common practice seems to be using the opencv cv::Ptr class. 我想知道我是否会遇到这个实现的问题,因为常见的做法似乎是使用opencv cv::Ptr类。

Yes, there is an openCV alternative of dynamic_cast<T>() written specifically for cv::Ptr . 是的,有一个专门为cv::Ptr编写的dynamic_cast<T>()的openCV替代品。 It's called... dynamicCast() :D There are also analogical alternatives to const cast and static cast for that matter. 它被称为... dynamicCast() :D对于此问题,还有const cast和static cast的类似替代方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM