将 SVM 与 SURF 结合使用时出错 [英] Getting error using SVM with SURF

查看:40
本文介绍了将 SVM 与 SURF 结合使用时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码,运行良好,但经过长时间处理后,它显示运行时错误

Below is my code , which is running fine but after a long processing it show me the run time error

// 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 ......srcopencvmodulescoresrcmatrix.cpp, line 652

我在第二个循环中制作了 descriptor_1 以重塑为 SVM 的行,但错误没有解决

I made the descriptor_1 in second loop to reshape into row for SVM , but error is not solving

推荐答案

到目前为止,我在 3 天后发现我的错误在于标签,当我标记图像时,我在那里得到了错误,是的,上面的答案也是相关的使用较少数量的图像也会导致错误,但在我的情况下,这不是原因,当我开始逐行检查错误时,错误从这里开始:

As far i find out after 3 days that my error is in labeling , when i label the image i got the error there , and yes above answer is also relevant that using less number of images also cause the error , but in my case this is not the reason , When i start checking error line by line , the error starts from here :

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 );

这篇关于将 SVM 与 SURF 结合使用时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆