如何创建CvSVM的向量

时间:2014-02-05 23:05:27

标签: c++ opencv

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

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.8 OpenCV ver 2.4.8

1 个答案:

答案 0 :(得分:2)

由于CvSVM(又名SVM)不可复制,因此您需要在向量中存储指向它的指针。您可以使用OpenCV智能指针cv :: Ptr&lt;&gt;要做到这一点。请记住使用运算符访问SVM的方法 - &gt;然后

这是一个解决方法。

这解决了这个问题。

#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;
}