前言
博主在交叉編譯環境移植代碼編譯的過程中使用SVM出現錯誤,但是在通常使用的ubuntu系統程序是可以正常運行的,其中有關SVM的load函數的使用。
ubuntu code block
void StateEstimation::loadSVMModel(std::string svm_model_path) { svm_model = cv::ml::SVM::load(svm_model_path.c_str()); if(svm_model.empty()) { std::cout << "SVM model load err." << std::endl; } }
cross platform code block
void StateEstimation::loadSVMModel(std::string svm_model_path) // void StateEstimation::loadSVMModel() { // svm_model = cv::ml::SVM::load(svm_model_path.c_str()); svm_model = cv::ml::SVM::load<cv::ml::SVM>(svm_model_path.c_str()); //OK. // svm_model = cv::ml::StatModel::load(svm_model_path); // svm_model = cv::ml::SVM::load<cv::ml::SVM>("../model/SVM_model.xml"); //OK. if(svm_model.empty()) { std::cout << "SVM model load err." << std::endl; } }
error
src/stateEstimation.cpp: In member function 'void StateEstimation::loadSVMModel()': src/stateEstimation.cpp:47:49: error: no matching function for call to 'cv::ml::SVM::load(const string&)' svm_model = cv::ml::SVM::load(svm_model_path); ^ In file included from /opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi/usr/include/opencv2/opencv.hpp:46:0, from include/stateEstimation.hpp:12, from src/stateEstimation.cpp:12: /opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi/usr/include/opencv2/core.hpp:3027:44: note: candidate: template<class _Tp> static cv::Ptr<T> cv::Algorithm::load(const cv::String&, const cv::String&) template<typename _Tp> static Ptr<_Tp> load(const String& filename, const String& objname=String()) ^ /opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi/usr/include/opencv2/core.hpp:3027:44: note: template argument deduction/substitution failed: src/stateEstimation.cpp:47:49: note: couldn't deduce template parameter '_Tp' svm_model = cv::ml::SVM::load(svm_model_path);
交叉編譯環境中出錯,最開始一直糾結在load的參數類型上,后來發現主要cv::ml::SVM是基於cv::Algorithm的,load函數的調用會有所不同。
cv::ml::SVM::load
static Ptr<SVM> cv::ml::SVM::load(const String& filepath)
cv::Algorithm::load
template<typename _Tp > static Ptr<_Tp> cv::Algorithm::load(const String& filename, const String& objname = String())
調用實例如下
Ptr<SVM> svm = Algorithm::load<SVM>("my_svm_model.xml");
還需要注意的是不同版本對同一個功能的函數調用可能會不同。
參考
2. opencv_SVM_load;
完