Ask Your Question
1

How do I select Algorithms at runtime?

asked 2015-07-07 15:39:46 -0600

matt.leotta gravatar image

updated 2015-07-07 20:11:40 -0600

In OpenCV 2.4 I could construct and configure Algorithms from the base class. If I knew I had a FeatureDetector, my code didn't need to know what kind of feature detector it was. I could use the create() member function with a string name like "SIFT" or "ORB" to construct an algorithm based on a string specified in a config file. I could similarly set algorithm parameters from the base class. Can I do something similar in OpenCV 3.0?

From what I can tell, in OpenCV 3.0 I need to explicitly construct the algorithm with its constructor and can only set parameters in the constructor, or in some cases by reading from a config file (which most algorithms don't yet support), or in some cases by member functions on the derived class (which most algorithms don't provide yet). Is this correct? Are there any plans for future OpenCV releases to bring back construction of algorithms from a string name and setting algorithm parameters without needing to know which algorithm it is?

For some context, I'm writing an open source application built on OpenCV that uses FeatureDetectors, DescriptorExtractors, DescriptorMatchers, etc. in an abstract way. This should compile across a range of OpenCV versions and build configurations (e.g. with or without contrib). At run time I'd like to allow the user to select and configure any detector, descriptor, and matcher that their build of OpenCV happens to support. OpenCV 2.4 had a nice solution for this. Although it was buggy, I miss it.

I suppose my questions are more roadmap questions.

  1. Are algorithms factories ever coming back in OpenCV 3.x?
  2. Is a general parameter configuration API ever coming back?
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-07-07 19:23:06 -0600

Eduardo gravatar image

For the first point, a possible solution:

std::map<std::string, cv::Ptr<cv::Feature2D> > feature;
//if-else on feature name
if(featureName == "SIFT") {
  feature[featureName] = cv::xfeatures2d::SIFT::create();
} else if(featureName == "SURF") {
  feature[featureName] = cv::xfeatures2d::SURF::create();
} //[...]

Same thing for your second point. I think that you can just recall the correct constructor on the smart pointer based on the feature name with the appropriate parameters.

As far as I know, there should be no significant downside as the feature2D class doesn't embedded important data (like a list of coordinates for example) that could be lost and would have to be copied with the call of the constructor with new parameters. Up to you to handle the different parameters (what parameter has changed) in your class.

edit flag offensive delete link more

Comments

1

This is not an ideal solution. It requires me to hard code conditional statements for every algorithm and then similarly for every parameter of every algorithm. It gets even uglier when some of the algorithms are only available if opencv_contrib is available. And algorithms may be added or parameters changed in future 3.0.x releases, which will required me to maintain all of these options, possibly with #ifdefs to support different OpenCV versions.

OpenCV 2.4 handled all of this nicely by allowing me to probe at run time (not compile time) which algorithms were available and what the parameters are available and of what type.

matt.leotta gravatar imagematt.leotta ( 2015-07-07 20:09:53 -0600 )edit
1

Sorry, I can't do better than this ;). By the way:

FeatureDetector::create() deleted forever. That class factory caused many problems, we decided that with std::map<> it's easy to implement it on user side.
Eduardo gravatar imageEduardo ( 2015-07-08 03:23:51 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-07-07 15:39:46 -0600

Seen: 189 times

Last updated: Jul 07 '15