gpt4 book ai didi

c++ - 如何创建 CvSVM vector

转载 作者:太空宇宙 更新时间:2023-11-03 22:32:04 24 4
gpt4 key购买 nike

我想创建一个 Opencv CvSVM 对象的 std::vector。当我编译这段代码时:

typedef vector<CvSVM> svm_vec;

svm_vec svm_data = svm_vec();

发生错误:

    In file included from 2dpca.cpp:5:0:
/usr/include/c++/4.8/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, const _T2&) [with _T1 = CvSVM; _T2 = CvSVM]’:
/usr/include/c++/4.8/bits/stl_uninitialized.h:75:53: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const CvSVM*, std::vector<CvSVM> >; _ForwardIterator = CvSVM*; bool _TrivialValueTypes = false]’
/usr/include/c++/4.8/bits/stl_uninitialized.h:117:41: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const CvSVM*, std::vector<CvSVM> >; _ForwardIterator = CvSVM*]’
/usr/include/c++/4.8/bits/stl_uninitialized.h:258:63: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const CvSVM*, std::vector<CvSVM> >; _ForwardIterator = CvSVM*; _Tp = CvSVM]’
/usr/include/c++/4.8/bits/stl_vector.h:316:32: required from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = CvSVM; _Alloc = std::allocator<CvSVM>]’
2dpca.cpp:79:29: required from here
/usr/local/include/opencv2/ml/ml.hpp:553:5: error: ‘CvSVM::CvSVM(const CvSVM&)’ is private
CvSVM(const CvSVM&);
^
In file included from /usr/include/c++/4.8/bits/stl_tempbuf.h:60:0,
from /usr/include/c++/4.8/bits/stl_algo.h:62,
from /usr/include/c++/4.8/algorithm:62,
from /usr/local/include/opencv2/core/core.hpp:56,
from 2dpca.cpp:1:
/usr/include/c++/4.8/bits/stl_construct.h:83:7: error: within this context
::new(static_cast<void*>(__p)) _T1(__value);

编译器:g++ 4.8OpenCV 版本 2.4.8

最佳答案

由于 CvSVM(又名 SVM)不可复制,您需要在 vector 中存储指向它的指针。您可以使用 OpenCV 智能指针 cv::Ptr<> 来做到这一点。请记住使用运算符 -> then 访问 SVM 的方法。

这是一个解决方法。

这解决了问题。

#include <opencv2\opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;

int main()
{
vector<Ptr<SVM>> svm_bank;

for (int i = 0; i < 3; ++i)
{
Mat trainData;
Mat responses;

/*Code for trainingData and
responses */

SVM *new_model;
new_model = new SVM;
new_model->train(trainData, responses);

svm_bank.push_back(new_model);
}

for (int i = 0; i < 3; ++i)
{
Mat samples;
Mat results;
svm_bank[i]->predict(samples, results);
}

return 0;
}

关于c++ - 如何创建 CvSVM vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21590540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com