我正在尝试使用功能模型和训练和SVM。我正在使用this教程和this示例代码。我使用的是Windows 8和OpenCV 2.4.10。我在Visual Studio 2012上运行它。但我不断收到此错误'CvSVM :: CvSVM':无法访问类'CvSVM'中声明的私有成员我尝试使用OpenCV 2.4.9但仍然存在相同的错误。
我为此搜索了很多,仍然无法找到适当的解决方案。请帮帮我。提前致谢。下面是代码。我也在使用这个用于Microsoft Visual Studio的Dirent API。
编辑:这是Visual Studio在编译时的完整输出。第146行给出了错误。
------ 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 [](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;
}
答案 0 :(得分:2)
出现错误是因为您无法访问CvSVM的复制构造函数和复制赋值运算符。这将需要复制没有公共拷贝构造函数的CvSVM对象。
只需看看CvSVM的定义:
// SVM model
class CV_EXPORTS_W CvSVM : public CvStatModel
{
public:
...
private:
CvSVM(const CvSVM&);
CvSVM& operator = (const CvSVM&);
};
你应该尝试使用指针 - 如果你坚持使用CvSVMs的容器:
std::map<std::string, std::unique_ptr<CvSVM>>