VS2019+OpenCV4.0
在完成分類器的訓練,直接predict是沒有問題的,但是保存xml文件后,再用以下的語句加載訓練好的xml模型,predict出現了問題。
svm->save("svm.xml")
svm->load<SVM>("trained-svm.xml");
OpenCV Error: Assertion failed (samples.cols == var_count && samples.type() == CV_32F) in cv::ml::SV
在以下鏈接找到了解決辦法。
https://answers.opencv.org/question/103031/opencv-31-c-svmload-does-not-set-var_count-correctly/
SVM::load() is a static function, which returns a new instance.
you have to use it like:
svm = Algorithm::load<SVM>("svm.xml");
改成以上語句后predict成功!