Seastar Seastar - 1 year ago 111
C++ Question

Determine type of StatModel in opencv c++

I am currently implementing a general classification program where the user inputs a labeled dataset and a machine learning technique from opencv and the program trains this Classifier.
Since for some classifiers (in my example Neural Nets) the input data structure needs to be different (not an integer as class label, but an array composed of index -1/1 for each class), I apply a converter of the integer class label to an array class label. Since I only need to do this for the Classifier ANN_MLP and not the other possible classifiers, I am looking for a way to check which kind of StatModel (superclass of all classifiers in opencv) my input classifier is.

So I am looking for something like this:

void validate(cv::ml::StatModel* classifier){
if(classifier.ofType(cv::ml::ANN_MLP))
do something
else
do something else
}


Does anyone know if there is a functionality in opencv to check for the type of sub-/superclasses?

I tried to check

if(typeid(cv::ml::ANN_MLP).hash_code() == typeid(classifier).hash_code())


and

if(typeid(cv::ml::ANN_MLP)==typeid(classifier))


from C++ equivalent of instanceof, but this always returns false, probably because the classifier is cast to a StatModel if used in this function? But should the underlying structure of the subclass not be saved somewhere? Sorry if this is a stupid question, but I am new to this kind of "casting"/instance-problems...
Any help is much apprechiated!! Thanks:)

Andrea

Answer Source

You forgot that the parameter passed is of pointer type: StatModel*. As such, you need to either compare it to a pointer type of cv::ml::ANN_MLP or strip it from the type passed in.

Alternatively, dynamic_cast, albeit slower, will also work for subtypes of ANN_MLP.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download