OpenCv - 在svm列车中断言失败

时间:2016-06-26 08:22:16

标签: c++ opencv machine-learning computer-vision svm

我可以找到我发生这种情况的原因。我有5个类,并制作标签和trainData,但它断言断言失败。为什么?标签是1x1x884(我也试过1x884x1,它是相同的),fmat 1 x 884 x 680400,所有float32。错误是: error

和代码:

HOGDescriptor hog;
    vector<vector< float>> features;
    int labels[884] = {};

    int brojac = 0;
    cv::String path1("C:/Users/Kristina/Desktop/Kompjuterska vizija/PrviProjekat/PrviProjekat/Chihuahua/*.jpg"); //select only jpg
    vector<cv::String> fn;
    vector<cv::Mat> data;
    cv::glob(path1, fn, true); // recurse
    for (size_t k = 0; k<fn.size(); ++k)
    {
        cv::Mat im = cv::imread(fn[k]);
        if (im.empty()) continue; //only proceed if sucsessful
        // you probably want to do some preprocessing
        cvtColor(im, im, CV_RGB2GRAY);
        resize(im, im, Size(200, 200));//resize image
        vector< float> descriptorsValues;
        hog.compute(im, descriptorsValues);
        features.push_back(descriptorsValues);
        labels[brojac] = 1;
        brojac++;
        data.push_back(im);

    }

    cv::String path2("C:/Users/Kristina/Desktop/Kompjuterska vizija/PrviProjekat/PrviProjekat/ShihTzu/*.jpg"); //select only jpg
    vector<cv::String> fn2;
    vector<cv::Mat> data2;
    cv::glob(path2, fn2, true); // recurse
    for (size_t k = 0; k<fn2.size(); ++k)
    {
        cv::Mat im = cv::imread(fn2[k]);
        vector< float> descriptorsValues;
        if (im.empty()) continue; //only proceed if sucsessful
        // you probably want to do some preprocessing
        cvtColor(im, im, CV_RGB2GRAY);
        resize(im, im, Size(200, 200));//resize image
        hog.compute(im, descriptorsValues);
        features.push_back(descriptorsValues);
        labels[brojac] = 2;
        brojac++;
        data.push_back(im);
    }
    cv::String path3("C:/Users/Kristina/Desktop/Kompjuterska vizija/PrviProjekat/PrviProjekat/Yorkshireterrier/*.jpg"); //select only jpg
    vector<cv::String> fn3;
    vector<cv::Mat> data3;
    cv::glob(path3, fn3, true); // recurse
    for (size_t k = 0; k<fn3.size(); ++k)
    {
        vector< float> descriptorsValues;
        cv::Mat im = cv::imread(fn3[k]);
        if (im.empty()) continue; //only proceed if sucsessful
        // you probably want to do some preprocessing
        cvtColor(im, im, CV_RGB2GRAY);
        resize(im, im, Size(200, 200));//resize image
        hog.compute(im, descriptorsValues);
        features.push_back(descriptorsValues);
        labels[brojac] = 3;
        brojac++;
        data.push_back(im);
    }
    cv::String path4("C:/Users/Kristina/Desktop/Kompjuterska vizija/PrviProjekat/PrviProjekat/vizsla/*.jpg"); //select only jpg
    vector<cv::String> fn4;
    vector<cv::Mat> data4;
    cv::glob(path4, fn4, true); // recurse
    for (size_t k = 0; k<fn4.size(); ++k)
    {
        cv::Mat im = cv::imread(fn4[k]);
        vector< float> descriptorsValues;
        if (im.empty()) continue; //only proceed if sucsessful
        // you probably want to do some preprocessing
        cvtColor(im, im, CV_RGB2GRAY);
        resize(im, im, Size(200, 200));//resize image
        hog.compute(im, descriptorsValues);
        features.push_back(descriptorsValues);
        labels[brojac] = 4;
        brojac++;
        data.push_back(im);
    }
    cv::String path5("C:/Users/Kristina/Desktop/Kompjuterska vizija/PrviProjekat/PrviProjekat/pug/*.jpg"); //select only jpg
    vector<cv::String> fn5;
    vector<cv::Mat> data5;
    cv::glob(path5, fn5, true); // recurse
    for (size_t k = 0; k<fn5.size(); ++k)
    {
        cv::Mat im = cv::imread(fn5[k]);
        vector< float> descriptorsValues;
        if (im.empty()) continue; //only proceed if sucsessful
        // you probably want to do some preprocessing
        cvtColor(im, im, CV_RGB2GRAY);
        resize(im, im, Size(200, 200));//resize image
        hog.compute(im, descriptorsValues);
        features.push_back(descriptorsValues);
        labels[brojac] = 5;
        brojac++;
        data.push_back(im);
    }

    Mat labelsMat(884, 1, CV_32FC1, labels);
    Mat fmat(features[0].size(), features.size(), CV_32FC1, features.data()); 

    Ptr<SVM> svm = SVM::create();
    svm->setType(SVM::C_SVC);
    svm->setKernel(SVM::LINEAR);
    svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
    svm->train(fmat, ml::SampleTypes::ROW_SAMPLE, labelsMat);

0 个答案:

没有答案