下面是我的代码,运行正常但经过长时间的处理后显示运行时错误
// Initialize constant values
const int nb_cars = files.size();
const int not_cars = files_no.size();
const int num_img = nb_cars + not_cars; // Get the number of images
// Initialize your training set.
cv::Mat training_mat(num_img,dictionarySize,CV_32FC1);
cv::Mat labels(0,1,CV_32FC1);
std::vector<string> all_names;
all_names.assign(files.begin(),files.end());
all_names.insert(all_names.end(), files_no.begin(), files_no.end());
// Load image and add them to the training set
int count = 0;
vector<string>::const_iterator i;
string Dir;
for (i = all_names.begin(); i != all_names.end(); ++i)
{
Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);
Mat row_img = cv::imread( Dir +*i, 0 );
detector.detect( row_img, keypoints);
RetainBestKeypoints(keypoints, 20); // retain top 10 key points
extractor->compute( row_img, keypoints, descriptors_1);
//uncluster.push_back(descriptors_1);
descriptors.reshape(1,1);
bow.add(descriptors_1);
++count;
}
int count_2=0;
vector<string>::const_iterator k;
Mat vocabulary = bow.cluster();
dextract.setVocabulary(vocabulary);
for (k = all_names.begin(); k != all_names.end(); ++k)
{
Dir=( (count_2 < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);
row_img = cv::imread( Dir +*k, 0 );
detector.detect( row_img, keypoints);
RetainBestKeypoints(keypoints, 20);
dextract.compute( row_img, keypoints, descriptors_1);
descriptors_1.reshape(1,1);
training_mat.push_back(descriptors_1);
labels.at< float >(count_2, 0) = (count_2<nb_cars)?1:-1;
++count_2;
}
错误:
OpenCv Error : Formats of input argument do not match() in unknown function , file ..\..\..\src\opencv\modules\core\src\matrix.cpp, line 652
我在第二个循环中将descriptor_1
重新整形为SVM的行,但错误未解决
答案 0 :(得分:0)
我认为你正在尝试使用比类数更少的功能进行聚类。
您可以从每张图片中拍摄更多图像或更多10个描述符。
答案 1 :(得分:0)
到目前为止,我发现我的错误是在标签上,当我标记图像时我得到了错误,并且上面的答案也是相关的,使用较少数量的图像也会导致错误,但在我的这不是原因,当我逐行开始检查错误时,错误从这里开始:
labels.at< float >(count_2, 0) = (count_2<nb_cars)?1:-1;
由于这一行:
Mat labels(0,1,CV_32FC1);
而不是:
Mat labels(num_img,1,CV_32FC1);
我应该使用
my_img.convertTo( training_mat.row(count_2), CV_32FC1 );