Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

error C2248: 'CvSVM::CvSVM' : cannot access private member declared in class 'CvSVM'

I am trying to use bag of features model and train and SVM. I am using this tutorial and this sample code. I am using Windows 8 and OpenCV 2.4.10. I am running this on Visual Studio 2012. But i am continuously getting this error 'CvSVM::CvSVM' : cannot access private member declared in class 'CvSVM' I tried using OpenCV 2.4.9 but same error is still there.

I searched a lot for this and still could not find a proper solution for this. Please help me on this. Thanks in advance. Below is the code. I am using this Dirent API for Microsoft Visual Studio as well.

    #include <stdio.h>
    #include <stdlib.h>
    #include <opencv2/opencv.hpp>
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <dirent.h>
    #include <sys/stat.h>
    #include <sys/types.h>

    #include <opencv2/nonfree/nonfree.hpp>

    using namespace cv;
    using namespace std;

    int main (int argc, char * const argv[]) {
        string dir = "C:\\Users\\Rajind\\Downloads\\FoodcamClassifier-master\\FoodcamClassifier-master\\foodcamimages\\TRAIN", filepath;
        DIR *dp;
        struct dirent *dirp;
        struct stat filestat;

    dp = opendir( dir.c_str() );

    // detecting keypoints
    SurfFeatureDetector detector(1000);
    vector<KeyPoint> keypoints; 

    // computing descriptors
    Ptr<DescriptorExtractor > extractor(new SurfDescriptorExtractor());//  extractor;
    Mat descriptors;
    Mat training_descriptors(1,extractor->descriptorSize(),extractor->descriptorType());
    Mat img;

    cout << "------- build vocabulary ---------\n";

    cout << "extract descriptors.."<<endl;

    int count = 0;
    while (count++ < 15 && (dirp = readdir( dp )))
    {
        filepath = dir + "/" + dirp->d_name;

        // If the file is a directory (or is in some way invalid) we'll skip it 
        if (stat( filepath.c_str(), &filestat )) continue;
        if (S_ISDIR( filestat.st_mode ))         continue;

        img = imread(filepath);
        detector.detect(img, keypoints);
        extractor->compute(img, keypoints, descriptors);

        training_descriptors.push_back(descriptors);
        cout << ".";
    }
    cout << endl;
    closedir( dp );

    cout << "Total descriptors: " << training_descriptors.rows << endl;

    BOWKMeansTrainer bowtrainer(150); //num clusters
    bowtrainer.add(training_descriptors);
    cout << "cluster BOW features" << endl;
    Mat vocabulary = bowtrainer.cluster();

    Ptr<DescriptorMatcher > matcher(new BFMatcher(NORM_L2,false));
    BOWImgDescriptorExtractor bowide(extractor,matcher);
    bowide.setVocabulary(vocabulary);

    //setup training data for classifiers
    map<string,Mat> classes_training_data; classes_training_data.clear();

    cout << "------- train SVMs ---------\n";

    Mat response_hist;
    cout << "look in train data"<<endl;
    count = 0;
    char buf[255];

    ifstream ifs("training.txt");
    int total_samples = 0;

    do
    {
        ifs.getline(buf, 255);
        string line(buf);
        istringstream iss(line);
//      cout << line << endl;
        iss >> filepath;
        Rect r; char delim;
        iss >> r.x >> delim;
        iss >> r.y >> delim;
        iss >> r.width >> delim;
        iss >> r.height;
//      cout << r.x << "," << r.y << endl;
        string class_;
        iss >> class_;

        img = imread(filepath);
        r &= Rect(0,0,img.cols,img.rows);

        if(r.width != 0) {
            img = img(r); //crop to interesting region
        }

        char c__[] = {(char)atoi(class_.c_str()),'\0'};
        string c_(c__);
        cout << ".";
//      putText(img, c_, Point(20,20), CV_FONT_HERSHEY_PLAIN, 2.0, Scalar(255), 2);
//      imshow("pic",img);
        bowide.compute(img, keypoints, response_hist);

        if(classes_training_data.count(c_) == 0) { //not yet created...
            classes_training_data[c_].create(0,response_hist.cols,response_hist.type());
        }
        classes_training_data[c_].push_back(response_hist);
        total_samples++;
//      waitKey(0);
    } while (!ifs.eof());
    cout << endl;

    //train 1-vs-all SVMs
    map<string,CvSVM> classes_classifiers;
    for (map<string,Mat>::iterator it = classes_training_data.begin(); it != classes_training_data.end(); ++it) {
        string class_ = (*it).first;
        cout << "training class: " << class_ << ".." << endl;

        Mat samples(0,response_hist.cols,response_hist.type());
        Mat labels(0,1,CV_32FC1);

        //copy class samples and label
        samples.push_back(classes_training_data[class_]);
        Mat class_label = Mat::ones(classes_training_data[class_].rows, 1, CV_32FC1);
        labels.push_back(class_label);

        //copy rest samples and label
        for (map<string,Mat>::iterator it1 = classes_training_data.begin(); it1 != classes_training_data.end(); ++it1) {
            string not_class_ = (*it1).first;
            if(not_class_[0] == class_[0]) continue;
            samples.push_back(classes_training_data[not_class_]);
            class_label = Mat::zeros(classes_training_data[not_class_].rows, 1, CV_32FC1);
            labels.push_back(class_label);
        }

        Mat samples_32f; samples.convertTo(samples_32f, CV_32F);
        classes_classifiers[class_].train(samples_32f,labels);
    }


    cout << "------- test ---------\n";

    //evaluate
    dir = "/Users/royshilkrot/Downloads/foodcamimages/TEST";
    dp = opendir( dir.c_str() );
    count = 0;
    while (count++ < 5 && (dirp = readdir( dp )))
    {
        filepath = dir + "/" + dirp->d_name;

        // If the file is a directory (or is in some way invalid) we'll skip it 
        if (stat( filepath.c_str(), &filestat )) continue;
        if (S_ISDIR( filestat.st_mode ))         continue;
        if (dirp->d_name[0] == '.')                  continue; //hidden file!

        cout << "eval file " << filepath << endl;

        img = imread(filepath);
        bowide.compute(img, keypoints, response_hist);

        //test vs. SVMs
        for (map<string,CvSVM>::iterator it = classes_classifiers.begin(); it != classes_classifiers.end(); ++it) {
            float res = (*it).second.predict(response_hist,false);
            cout << "class: " << (*it).first << ", response: " << res << endl;
        }

//      cout << ".";
    }
    cout << endl;
    closedir( dp );

    cout <<"done"<<endl;        
    return 0;
}

error C2248: 'CvSVM::CvSVM' : cannot access private member declared in class 'CvSVM'

I am trying to use bag of features model and train and SVM. I am using this tutorial and this sample code. I am using Windows 8 and OpenCV 2.4.10. I am running this on Visual Studio 2012. But i am continuously getting this error 'CvSVM::CvSVM' : cannot access private member declared in class 'CvSVM' I tried using OpenCV 2.4.9 but same error is still there.

I searched a lot for this and still could not find a proper solution for this. Please help me on this. Thanks in advance. Below is the code. I am using this Dirent API for Microsoft Visual Studio as well.

EDIT: Lines 146 gives the error. classes_classifiers[class_].train(samples_32f,labels);

    #include <stdio.h>
    #include <stdlib.h>
    #include <opencv2/opencv.hpp>
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <dirent.h>
    #include <sys/stat.h>
    #include <sys/types.h>

    #include <opencv2/nonfree/nonfree.hpp>

    using namespace cv;
    using namespace std;

    int main (int argc, char * const argv[]) {
        string dir = "C:\\Users\\Rajind\\Downloads\\FoodcamClassifier-master\\FoodcamClassifier-master\\foodcamimages\\TRAIN", filepath;
        DIR *dp;
        struct dirent *dirp;
        struct stat filestat;

    dp = opendir( dir.c_str() );

    // detecting keypoints
    SurfFeatureDetector detector(1000);
    vector<KeyPoint> keypoints; 

    // computing descriptors
    Ptr<DescriptorExtractor > extractor(new SurfDescriptorExtractor());//  extractor;
    Mat descriptors;
    Mat training_descriptors(1,extractor->descriptorSize(),extractor->descriptorType());
    Mat img;

    cout << "------- build vocabulary ---------\n";

    cout << "extract descriptors.."<<endl;

    int count = 0;
    while (count++ < 15 && (dirp = readdir( dp )))
    {
        filepath = dir + "/" + dirp->d_name;

        // If the file is a directory (or is in some way invalid) we'll skip it 
        if (stat( filepath.c_str(), &filestat )) continue;
        if (S_ISDIR( filestat.st_mode ))         continue;

        img = imread(filepath);
        detector.detect(img, keypoints);
        extractor->compute(img, keypoints, descriptors);

        training_descriptors.push_back(descriptors);
        cout << ".";
    }
    cout << endl;
    closedir( dp );

    cout << "Total descriptors: " << training_descriptors.rows << endl;

    BOWKMeansTrainer bowtrainer(150); //num clusters
    bowtrainer.add(training_descriptors);
    cout << "cluster BOW features" << endl;
    Mat vocabulary = bowtrainer.cluster();

    Ptr<DescriptorMatcher > matcher(new BFMatcher(NORM_L2,false));
    BOWImgDescriptorExtractor bowide(extractor,matcher);
    bowide.setVocabulary(vocabulary);

    //setup training data for classifiers
    map<string,Mat> classes_training_data; classes_training_data.clear();

    cout << "------- train SVMs ---------\n";

    Mat response_hist;
    cout << "look in train data"<<endl;
    count = 0;
    char buf[255];

    ifstream ifs("training.txt");
    int total_samples = 0;

    do
    {
        ifs.getline(buf, 255);
        string line(buf);
        istringstream iss(line);
//      cout << line << endl;
        iss >> filepath;
        Rect r; char delim;
        iss >> r.x >> delim;
        iss >> r.y >> delim;
        iss >> r.width >> delim;
        iss >> r.height;
//      cout << r.x << "," << r.y << endl;
        string class_;
        iss >> class_;

        img = imread(filepath);
        r &= Rect(0,0,img.cols,img.rows);

        if(r.width != 0) {
            img = img(r); //crop to interesting region
        }

        char c__[] = {(char)atoi(class_.c_str()),'\0'};
        string c_(c__);
        cout << ".";
//      putText(img, c_, Point(20,20), CV_FONT_HERSHEY_PLAIN, 2.0, Scalar(255), 2);
//      imshow("pic",img);
        bowide.compute(img, keypoints, response_hist);

        if(classes_training_data.count(c_) == 0) { //not yet created...
            classes_training_data[c_].create(0,response_hist.cols,response_hist.type());
        }
        classes_training_data[c_].push_back(response_hist);
        total_samples++;
//      waitKey(0);
    } while (!ifs.eof());
    cout << endl;

    //train 1-vs-all SVMs
    map<string,CvSVM> classes_classifiers;
    for (map<string,Mat>::iterator it = classes_training_data.begin(); it != classes_training_data.end(); ++it) {
        string class_ = (*it).first;
        cout << "training class: " << class_ << ".." << endl;

        Mat samples(0,response_hist.cols,response_hist.type());
        Mat labels(0,1,CV_32FC1);

        //copy class samples and label
        samples.push_back(classes_training_data[class_]);
        Mat class_label = Mat::ones(classes_training_data[class_].rows, 1, CV_32FC1);
        labels.push_back(class_label);

        //copy rest samples and label
        for (map<string,Mat>::iterator it1 = classes_training_data.begin(); it1 != classes_training_data.end(); ++it1) {
            string not_class_ = (*it1).first;
            if(not_class_[0] == class_[0]) continue;
            samples.push_back(classes_training_data[not_class_]);
            class_label = Mat::zeros(classes_training_data[not_class_].rows, 1, CV_32FC1);
            labels.push_back(class_label);
        }

        Mat samples_32f; samples.convertTo(samples_32f, CV_32F);
        classes_classifiers[class_].train(samples_32f,labels);
    }


    cout << "------- test ---------\n";

    //evaluate
    dir = "/Users/royshilkrot/Downloads/foodcamimages/TEST";
    dp = opendir( dir.c_str() );
    count = 0;
    while (count++ < 5 && (dirp = readdir( dp )))
    {
        filepath = dir + "/" + dirp->d_name;

        // If the file is a directory (or is in some way invalid) we'll skip it 
        if (stat( filepath.c_str(), &filestat )) continue;
        if (S_ISDIR( filestat.st_mode ))         continue;
        if (dirp->d_name[0] == '.')                  continue; //hidden file!

        cout << "eval file " << filepath << endl;

        img = imread(filepath);
        bowide.compute(img, keypoints, response_hist);

        //test vs. SVMs
        for (map<string,CvSVM>::iterator it = classes_classifiers.begin(); it != classes_classifiers.end(); ++it) {
            float res = (*it).second.predict(response_hist,false);
            cout << "class: " << (*it).first << ", response: " << res << endl;
        }

//      cout << ".";
    }
    cout << endl;
    closedir( dp );

    cout <<"done"<<endl;        
    return 0;
}

error C2248: 'CvSVM::CvSVM' : cannot access private member declared in class 'CvSVM'

I am trying to use bag of features model and train and SVM. I am using this tutorial and this sample code. I am using Windows 8 and OpenCV 2.4.10. I am running this on Visual Studio 2012. But i am continuously getting this error 'CvSVM::CvSVM' : cannot access private member declared in class 'CvSVM' I tried using OpenCV 2.4.9 but same error is still there.

I searched a lot for this and still could not find a proper solution for this. Please help me on this. Thanks in advance. Below is the code. I am using this Dirent API for Microsoft Visual Studio as well.

EDIT: Lines 146 gives the error. classes_classifiers[class_].train(samples_32f,labels);

This is complete output from visual studio when compiling the main.cpp. ------ Build started: Project: ConsoleApplication1, Configuration: Debug x64 ------ 1> main.cpp 1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\utility(138): error C2248: 'CvSVM::CvSVM' : cannot access private member declared in class 'CvSVM' 1> D:\Program Files\opencv\build\include\opencv2/ml/ml.hpp(553) : see declaration of 'CvSVM::CvSVM' 1> D:\Program Files\opencv\build\include\opencv2/ml/ml.hpp(452) : see declaration of 'CvSVM' 1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\map(198) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair<const std::basic_string<_elem,_traits,_alloc="">&,CvSVM>(_Other1,_Other2 &&,void *)' being compiled 1> with 1> [ 1> _Ty1=std::basic_string<char,std::char_traits<char>,std::allocator<char>>, 1> _Ty2=CvSVM, 1> _Elem=char, 1> _Traits=std::char_traits<char>, 1> _Alloc=std::allocator<char>, 1> _Other1=const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &, 1> _Other2=CvSVM 1> ] 1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\map(198) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair<const std::basic_string<_elem,_traits,_alloc="">&,CvSVM>(_Other1,_Other2 &&,void *)' being compiled 1> with 1> [ 1> _Ty1=std::basic_string<char,std::char_traits<char>,std::allocator<char>>, 1> _Ty2=CvSVM, 1> _Elem=char, 1> _Traits=std::char_traits<char>, 1> _Alloc=std::allocator<char>, 1> _Other1=const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &, 1> _Other2=CvSVM 1> ] 1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\map(191) : while compiling class template member function 'CvSVM &std::map<_Kty,_Ty>::operator ' 1> with 1> [ 1> _Kty=std::string, 1> _Ty=CvSVM, 1> _Elem=char, 1> _Traits=std::char_traits<char>, 1> _Alloc=std::allocator<char> 1> ] 1> main.cpp(146) : see reference to function template instantiation 'CvSVM &std::map<_Kty,_Ty>::operator ' being compiled 1> with 1> [ 1> _Kty=std::string, 1> _Ty=CvSVM, 1> _Elem=char, 1> _Traits=std::char_traits<char>, 1> _Alloc=std::allocator<char> 1> ] 1> main.cpp(123) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled 1> with 1> [ 1> _Kty=std::string, 1> _Ty=CvSVM 1> ] ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    #include <stdio.h>
    #include <stdlib.h>
    #include <opencv2/opencv.hpp>
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <dirent.h>
    #include <sys/stat.h>
    #include <sys/types.h>

    #include <opencv2/nonfree/nonfree.hpp>

    using namespace cv;
    using namespace std;

    int main (int argc, char * const argv[]) {
        string dir = "C:\\Users\\Rajind\\Downloads\\FoodcamClassifier-master\\FoodcamClassifier-master\\foodcamimages\\TRAIN", filepath;
        DIR *dp;
        struct dirent *dirp;
        struct stat filestat;

    dp = opendir( dir.c_str() );

    // detecting keypoints
    SurfFeatureDetector detector(1000);
    vector<KeyPoint> keypoints; 

    // computing descriptors
    Ptr<DescriptorExtractor > extractor(new SurfDescriptorExtractor());//  extractor;
    Mat descriptors;
    Mat training_descriptors(1,extractor->descriptorSize(),extractor->descriptorType());
    Mat img;

    cout << "------- build vocabulary ---------\n";

    cout << "extract descriptors.."<<endl;

    int count = 0;
    while (count++ < 15 && (dirp = readdir( dp )))
    {
        filepath = dir + "/" + dirp->d_name;

        // If the file is a directory (or is in some way invalid) we'll skip it 
        if (stat( filepath.c_str(), &filestat )) continue;
        if (S_ISDIR( filestat.st_mode ))         continue;

        img = imread(filepath);
        detector.detect(img, keypoints);
        extractor->compute(img, keypoints, descriptors);

        training_descriptors.push_back(descriptors);
        cout << ".";
    }
    cout << endl;
    closedir( dp );

    cout << "Total descriptors: " << training_descriptors.rows << endl;

    BOWKMeansTrainer bowtrainer(150); //num clusters
    bowtrainer.add(training_descriptors);
    cout << "cluster BOW features" << endl;
    Mat vocabulary = bowtrainer.cluster();

    Ptr<DescriptorMatcher > matcher(new BFMatcher(NORM_L2,false));
    BOWImgDescriptorExtractor bowide(extractor,matcher);
    bowide.setVocabulary(vocabulary);

    //setup training data for classifiers
    map<string,Mat> classes_training_data; classes_training_data.clear();

    cout << "------- train SVMs ---------\n";

    Mat response_hist;
    cout << "look in train data"<<endl;
    count = 0;
    char buf[255];

    ifstream ifs("training.txt");
    int total_samples = 0;

    do
    {
        ifs.getline(buf, 255);
        string line(buf);
        istringstream iss(line);
//      cout << line << endl;
        iss >> filepath;
        Rect r; char delim;
        iss >> r.x >> delim;
        iss >> r.y >> delim;
        iss >> r.width >> delim;
        iss >> r.height;
//      cout << r.x << "," << r.y << endl;
        string class_;
        iss >> class_;

        img = imread(filepath);
        r &= Rect(0,0,img.cols,img.rows);

        if(r.width != 0) {
            img = img(r); //crop to interesting region
        }

        char c__[] = {(char)atoi(class_.c_str()),'\0'};
        string c_(c__);
        cout << ".";
//      putText(img, c_, Point(20,20), CV_FONT_HERSHEY_PLAIN, 2.0, Scalar(255), 2);
//      imshow("pic",img);
        bowide.compute(img, keypoints, response_hist);

        if(classes_training_data.count(c_) == 0) { //not yet created...
            classes_training_data[c_].create(0,response_hist.cols,response_hist.type());
        }
        classes_training_data[c_].push_back(response_hist);
        total_samples++;
//      waitKey(0);
    } while (!ifs.eof());
    cout << endl;

    //train 1-vs-all SVMs
    map<string,CvSVM> classes_classifiers;
    for (map<string,Mat>::iterator it = classes_training_data.begin(); it != classes_training_data.end(); ++it) {
        string class_ = (*it).first;
        cout << "training class: " << class_ << ".." << endl;

        Mat samples(0,response_hist.cols,response_hist.type());
        Mat labels(0,1,CV_32FC1);

        //copy class samples and label
        samples.push_back(classes_training_data[class_]);
        Mat class_label = Mat::ones(classes_training_data[class_].rows, 1, CV_32FC1);
        labels.push_back(class_label);

        //copy rest samples and label
        for (map<string,Mat>::iterator it1 = classes_training_data.begin(); it1 != classes_training_data.end(); ++it1) {
            string not_class_ = (*it1).first;
            if(not_class_[0] == class_[0]) continue;
            samples.push_back(classes_training_data[not_class_]);
            class_label = Mat::zeros(classes_training_data[not_class_].rows, 1, CV_32FC1);
            labels.push_back(class_label);
        }

        Mat samples_32f; samples.convertTo(samples_32f, CV_32F);
        classes_classifiers[class_].train(samples_32f,labels);
    }


    cout << "------- test ---------\n";

    //evaluate
    dir = "/Users/royshilkrot/Downloads/foodcamimages/TEST";
    dp = opendir( dir.c_str() );
    count = 0;
    while (count++ < 5 && (dirp = readdir( dp )))
    {
        filepath = dir + "/" + dirp->d_name;

        // If the file is a directory (or is in some way invalid) we'll skip it 
        if (stat( filepath.c_str(), &filestat )) continue;
        if (S_ISDIR( filestat.st_mode ))         continue;
        if (dirp->d_name[0] == '.')                  continue; //hidden file!

        cout << "eval file " << filepath << endl;

        img = imread(filepath);
        bowide.compute(img, keypoints, response_hist);

        //test vs. SVMs
        for (map<string,CvSVM>::iterator it = classes_classifiers.begin(); it != classes_classifiers.end(); ++it) {
            float res = (*it).second.predict(response_hist,false);
            cout << "class: " << (*it).first << ", response: " << res << endl;
        }

//      cout << ".";
    }
    cout << endl;
    closedir( dp );

    cout <<"done"<<endl;        
    return 0;
}

error C2248: 'CvSVM::CvSVM' : cannot access private member declared in class 'CvSVM'

I am trying to use bag of features model and train and SVM. I am using this tutorial and this sample code. I am using Windows 8 and OpenCV 2.4.10. I am running this on Visual Studio 2012. But i am continuously getting this error 'CvSVM::CvSVM' : cannot access private member declared in class 'CvSVM' I tried using OpenCV 2.4.9 but same error is still there.

I searched a lot for this and still could not find a proper solution for this. Please help me on this. Thanks in advance. Below is the code. I am using this Dirent API for Microsoft Visual Studio as well.

EDIT: Lines 146 gives the error. classes_classifiers[class_].train(samples_32f,labels);

This is complete output from visual studio when compiling the main.cpp. main.cpp.

------ Build started: Project: ConsoleApplication1, Configuration: Debug x64 ------
1>  main.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\utility(138): error C2248: 'CvSVM::CvSVM' : cannot access private member declared in class 'CvSVM'
1>          D:\Program Files\opencv\build\include\opencv2/ml/ml.hpp(553) : see declaration of 'CvSVM::CvSVM'
1>          D:\Program Files\opencv\build\include\opencv2/ml/ml.hpp(452) : see declaration of 'CvSVM'
1>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\map(198) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair<const std::basic_string<_elem,_traits,_alloc="">&,CvSVM>(_Other1,_Other2 std::basic_string<_Elem,_Traits,_Alloc>&,CvSVM>(_Other1,_Other2 &&,void *)' **)' being compiled
1>          with
1>          [
1>              _Ty1=std::basic_string<char,std::char_traits<char>,std::allocator<char>>,
1>              _Ty2=CvSVM,
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Alloc=std::allocator<char>,
1>              _Other1=const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &,
1>              _Other2=CvSVM
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\map(198) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair<const std::basic_string<_elem,_traits,_alloc="">&,CvSVM>(_Other1,_Other2 std::basic_string<_Elem,_Traits,_Alloc>&,CvSVM>(_Other1,_Other2 &&,void *)' **)' being compiled
1>          with
1>          [
1>              _Ty1=std::basic_string<char,std::char_traits<char>,std::allocator<char>>,
1>              _Ty2=CvSVM,
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Alloc=std::allocator<char>,
1>              _Other1=const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &,
1>              _Other2=CvSVM
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\map(191) : while compiling class template member function 'CvSVM &std::map<_Kty,_Ty>::operator '
[](const std::basic_string<_Elem,_Traits,_Alloc> &)'
1>          with
1>          [
1>              _Kty=std::string,
1>              _Ty=CvSVM,
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Alloc=std::allocator<char>
1>          ]
1>          main.cpp(146) : see reference to function template instantiation 'CvSVM &std::map<_Kty,_Ty>::operator ' [](const std::basic_string<_Elem,_Traits,_Alloc> &)' being compiled
1>          with
1>          [
1>              _Kty=std::string,
1>              _Ty=CvSVM,
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Alloc=std::allocator<char>
1>          ]
1>          main.cpp(123) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1>          with
1>          [
1>              _Kty=std::string,
1>              _Ty=CvSVM
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

==========

    #include <stdio.h>
    #include <stdlib.h>
    #include <opencv2/opencv.hpp>
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <dirent.h>
    #include <sys/stat.h>
    #include <sys/types.h>

    #include <opencv2/nonfree/nonfree.hpp>

    using namespace cv;
    using namespace std;

    int main (int argc, char * const argv[]) {
        string dir = "C:\\Users\\Rajind\\Downloads\\FoodcamClassifier-master\\FoodcamClassifier-master\\foodcamimages\\TRAIN", filepath;
        DIR *dp;
        struct dirent *dirp;
        struct stat filestat;

    dp = opendir( dir.c_str() );

    // detecting keypoints
    SurfFeatureDetector detector(1000);
    vector<KeyPoint> keypoints; 

    // computing descriptors
    Ptr<DescriptorExtractor > extractor(new SurfDescriptorExtractor());//  extractor;
    Mat descriptors;
    Mat training_descriptors(1,extractor->descriptorSize(),extractor->descriptorType());
    Mat img;

    cout << "------- build vocabulary ---------\n";

    cout << "extract descriptors.."<<endl;

    int count = 0;
    while (count++ < 15 && (dirp = readdir( dp )))
    {
        filepath = dir + "/" + dirp->d_name;

        // If the file is a directory (or is in some way invalid) we'll skip it 
        if (stat( filepath.c_str(), &filestat )) continue;
        if (S_ISDIR( filestat.st_mode ))         continue;

        img = imread(filepath);
        detector.detect(img, keypoints);
        extractor->compute(img, keypoints, descriptors);

        training_descriptors.push_back(descriptors);
        cout << ".";
    }
    cout << endl;
    closedir( dp );

    cout << "Total descriptors: " << training_descriptors.rows << endl;

    BOWKMeansTrainer bowtrainer(150); //num clusters
    bowtrainer.add(training_descriptors);
    cout << "cluster BOW features" << endl;
    Mat vocabulary = bowtrainer.cluster();

    Ptr<DescriptorMatcher > matcher(new BFMatcher(NORM_L2,false));
    BOWImgDescriptorExtractor bowide(extractor,matcher);
    bowide.setVocabulary(vocabulary);

    //setup training data for classifiers
    map<string,Mat> classes_training_data; classes_training_data.clear();

    cout << "------- train SVMs ---------\n";

    Mat response_hist;
    cout << "look in train data"<<endl;
    count = 0;
    char buf[255];

    ifstream ifs("training.txt");
    int total_samples = 0;

    do
    {
        ifs.getline(buf, 255);
        string line(buf);
        istringstream iss(line);
//      cout << line << endl;
        iss >> filepath;
        Rect r; char delim;
        iss >> r.x >> delim;
        iss >> r.y >> delim;
        iss >> r.width >> delim;
        iss >> r.height;
//      cout << r.x << "," << r.y << endl;
        string class_;
        iss >> class_;

        img = imread(filepath);
        r &= Rect(0,0,img.cols,img.rows);

        if(r.width != 0) {
            img = img(r); //crop to interesting region
        }

        char c__[] = {(char)atoi(class_.c_str()),'\0'};
        string c_(c__);
        cout << ".";
//      putText(img, c_, Point(20,20), CV_FONT_HERSHEY_PLAIN, 2.0, Scalar(255), 2);
//      imshow("pic",img);
        bowide.compute(img, keypoints, response_hist);

        if(classes_training_data.count(c_) == 0) { //not yet created...
            classes_training_data[c_].create(0,response_hist.cols,response_hist.type());
        }
        classes_training_data[c_].push_back(response_hist);
        total_samples++;
//      waitKey(0);
    } while (!ifs.eof());
    cout << endl;

    //train 1-vs-all SVMs
    map<string,CvSVM> classes_classifiers;
    for (map<string,Mat>::iterator it = classes_training_data.begin(); it != classes_training_data.end(); ++it) {
        string class_ = (*it).first;
        cout << "training class: " << class_ << ".." << endl;

        Mat samples(0,response_hist.cols,response_hist.type());
        Mat labels(0,1,CV_32FC1);

        //copy class samples and label
        samples.push_back(classes_training_data[class_]);
        Mat class_label = Mat::ones(classes_training_data[class_].rows, 1, CV_32FC1);
        labels.push_back(class_label);

        //copy rest samples and label
        for (map<string,Mat>::iterator it1 = classes_training_data.begin(); it1 != classes_training_data.end(); ++it1) {
            string not_class_ = (*it1).first;
            if(not_class_[0] == class_[0]) continue;
            samples.push_back(classes_training_data[not_class_]);
            class_label = Mat::zeros(classes_training_data[not_class_].rows, 1, CV_32FC1);
            labels.push_back(class_label);
        }

        Mat samples_32f; samples.convertTo(samples_32f, CV_32F);
        classes_classifiers[class_].train(samples_32f,labels);
    }


    cout << "------- test ---------\n";

    //evaluate
    dir = "/Users/royshilkrot/Downloads/foodcamimages/TEST";
    dp = opendir( dir.c_str() );
    count = 0;
    while (count++ < 5 && (dirp = readdir( dp )))
    {
        filepath = dir + "/" + dirp->d_name;

        // If the file is a directory (or is in some way invalid) we'll skip it 
        if (stat( filepath.c_str(), &filestat )) continue;
        if (S_ISDIR( filestat.st_mode ))         continue;
        if (dirp->d_name[0] == '.')                  continue; //hidden file!

        cout << "eval file " << filepath << endl;

        img = imread(filepath);
        bowide.compute(img, keypoints, response_hist);

        //test vs. SVMs
        for (map<string,CvSVM>::iterator it = classes_classifiers.begin(); it != classes_classifiers.end(); ++it) {
            float res = (*it).second.predict(response_hist,false);
            cout << "class: " << (*it).first << ", response: " << res << endl;
        }

//      cout << ".";
    }
    cout << endl;
    closedir( dp );

    cout <<"done"<<endl;        
    return 0;
}