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::Ptr<cv::Tracker>> (*)()> trackerTypes_map =
{{"BOOSTINGS", &cv::TrackerBoosting::create},
{"MIL", &cv::TrackerMIL::create}};
And call it :
auto &iter = this->trackerTypes_map.find(str);
if (iter == this->trackerTypes_map.end()) {
return nullptr;
}
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::Ptr<cv::TrackerBoosting> > (*)()>'
{"BOOSTING", &cv::TrackerBoosting::create}};
^
I don't know why isn't working, is something missing ? Wrong type ?