>

2014년 4월 22일 화요일

Allocating multiple CvSVMs of the same model file.

CvSVM does not allow a copy operation.

CvSVM svm(svmFile);
CvSVM svm2 = svm; // this is impossible

However, as can be found in the CvSVM source codes

svm.load (char *filename) calls
     CvStatModel::load (filename)
           1. Allocates CvFileStorage *fs;
           2. CvFileNode *root,*model_node;
           Then, it calls
           CvSVM::read (fs, model_node);

Therefore, utilizing the function CvStatModel::load(), you prepare (fs, model_node) to allocate multiple CvSVMs.

void testMultipleCsSVM()
{
    std::vector<CvSVM*> ptrSVM(10); // ten CvSVM pointers
    CvFileStorage* fs = 0;
    CvFileNode* model_node = 0;
    
    fs = cvOpenFileStorage( svmFile.c_str(), 0, CV_STORAGE_READ );

    if( !fs ) {
        cerr << "file open error: " << svmFile << endl;
        exit (0);
        }
    
    CvFileNode* root = cvGetRootFileNode( fs );
    if( root->data.seq->total > 0 )
        model_node = (CvFileNode*)cvGetSeqElem( root->data.seq, 0 );
    
    for (int i=0; i<ptrSVM.size(); i++)
        {
        ptrSVM[i] = new CvSVM;
        ptrSVM[i]->read (fs, model_node);
        }

}

End.

댓글 없음:

댓글 쓰기