How to create map with pointer to function to cv tracking ?
Good morning, I would like to create map with pointer function with tracking functions, i tried many solution but aren't working :/
I would like to avoid many if like :
cv::Ptr<cv::Tracker>
TestTrackingVideo::createTrackerByName(std::string &trackerType) {
cv::Ptr<cv::Tracker> tracker;
if (trackerType == trackerTypes[0])
tracker = cv::TrackerBoosting::create();
else if (trackerType == trackerTypes[1])
tracker = cv::TrackerMIL::create();
else if (trackerType == trackerTypes[2])
tracker = cv::TrackerKCF::create();
else if (trackerType == trackerTypes[3])
tracker = cv::TrackerTLD::create();
else if (trackerType == trackerTypes[4])
tracker = cv::TrackerMedianFlow::create();
else if (trackerType == trackerTypes[5])
tracker = cv::TrackerGOTURN::create();
else if (trackerType == trackerTypes[6])
tracker = cv::TrackerMOSSE::create();
else if (trackerType == trackerTypes[7])
tracker = cv::TrackerCSRT::create();
else {
std::cout << "Incorrect tracker name" << std::endl;
std::cout << "Available trackers are: " << std::endl;
for (std::vector<std::string>::iterator it = trackerTypes.begin();
it != trackerTypes.end(); ++it)
std::cout << " " << *it << std::endl;
}
return tracker;
}
I would like to create map with pointer to function like :
std::map<std::string, cv::Ptr<cv::Tracker> (*)()> trackerTypes_map =
{{"BOOSTINGS", &cv::TrackerBoosting::create},
{"MIL", &cv::TrackerMIL::create}};
And call it :
cv::Ptr<cv::Tracker>
createTrackerByName(std::string &trackerType) {
auto &iter = this->trackerTypes_map.find(trackerType);
if (iter == this->trackerTypes_map.end()) {
throw "tracker type not found !";
}
return (this->trackerTypes_map.second)();
}
When i tried to compile, i have this error :
error : could not convert '{{"BOOSTINGS", (& cv::TrackerBoosting::create)}, {"BOOSTING", (& cv::TrackerBoosting::create)}}' from '<brace-enclosed initializer list>' to 'std::map<std::__cxx11::basic_string<char>, cv::Ptr<cv::TrackerBoosting> (*)()>'
{"BOOSTING", &cv::TrackerBoosting::create}};
^
I don't know why isn't working, is something missing ? Wrong type ?
if i may ask - what is the actual use-case of this ? (looks like over-engineering)
and why double
Ptr
here:cv::Ptr<cv::Ptr<cv::TrackerBoosting> > (*)()
?&cv::TrackerMOSSE::create
is a different type than&cv::TrackerKCF::create
, but your map needs a consistent type forsecond
it won't ever work, i guess ;(xDMy computer school, do not like that we do several if, It is an error when looking for a solution,
I use the "cv::Tracker" template in mat, the other heirs, this one ? it worked with the mother class in template during lessons, I can't understand, maybe we need a special syntax with cv::Ptr?
I changed the original post a bit, when we call the pointer to function.
"My computer school," -- hehe, ok ;)
there are 3 problems:
Ptr<cv::TrackerBoosting>
to another returning a type likePtr<cv::Tracker>