Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Opencv 如何查找包含hog特征的Mat文件_Opencv_Svm - Fatal编程技术网

Opencv 如何查找包含hog特征的Mat文件

Opencv 如何查找包含hog特征的Mat文件,opencv,svm,Opencv,Svm,我想训练一个基于HOG的分类器来检测视频中的行人,为此我计算了HOG特征并将它们存储在带有适当标签的文本文件中。我转向使用CvSVM实用程序,在那里我需要一个Mat对象,其中包含所有正样本和负样本特征向量的信息。我找到了可以将这些featureVector文件文本文件转换为Mat的代码,但是 Hogfeat.create(ders.size(),1,CV_32FC1); for(int i=0;i<ders.size();i++) { Hogfeat.at<float>(

我想训练一个基于HOG的分类器来检测视频中的行人,为此我计算了HOG特征并将它们存储在带有适当标签的文本文件中。我转向使用CvSVM实用程序,在那里我需要一个Mat对象,其中包含所有正样本和负样本特征向量的信息。我找到了可以将这些featureVector文件文本文件转换为Mat的代码,但是

Hogfeat.create(ders.size(),1,CV_32FC1);

for(int i=0;i<ders.size();i++)
{
  Hogfeat.at<float>(i,0)=ders.at(i);

}

它提供了错误的断言。我可以看到错误是因为每个图像的featureVector大小为1*3780,但代码试图在每一新行加载featureVector。我的基本困惑来源是,需要将什么格式的Mat文件提供给CvSVM加载函数?。给出了Mat格式的概念,但这是针对2个特征向量,即高度和宽度作为特征向量,这是否也适用于行人检测?

此示例应包含我们在上述评论中讨论的情况

//dummy readers
std:: vector<float> 
dummyDerReaderForOneDer(const vector<float> &pattern)
{
    int i = std::rand() % pattern.size();
    int j = std::rand() % pattern.size();
    vector<float> patternPulNoise(pattern);
    std::random_shuffle(patternPulNoise.begin()+std::min(i,j),patternPulNoise.begin()+std::max(i,j));
    return patternPulNoise;
};

std:: vector<float> 
dummyDerReaderForManyDers(const vector<float> &posPattern,
                                const vector<float> &negPattern, 
                                    vector<float> labels,
                                        float posLabel, float negLabel)
{

    int i,j;
    vector<float> allPatternsInOneVector;

    for(i=0;i< labels.size(); i++)
    {
        vector<float> patternPulNoise;
        if(labels[i]==posLabel)
        {
            patternPulNoise  = dummyDerReaderForOneDer(posPattern);
        }
        if(labels[i]==negLabel)
        {
            patternPulNoise  = dummyDerReaderForOneDer(negPattern);
        }

        for(j=0;j< patternPulNoise.size(); j++)
        {
            allPatternsInOneVector.push_back(patternPulNoise[j]);
        }
    }
    return allPatternsInOneVector;
}



// main harness entry point for detector test
int main (int argc, const char * argv[])
{

    //dummy variables for example
    int posFiles = 128;
    int negFiles = 128;
    int dims = 3780;

    //setup some dummy data
    vector<float> dummyPosPattern;
    dummyPosPattern.assign(1000,1.f);
    dummyPosPattern.resize(dims );
    random_shuffle(dummyPosPattern.begin(),dummyPosPattern.end());

    vector<float> dummyNegPattern;
    dummyNegPattern.assign(1000,1.f);
    dummyNegPattern.resize(dims );
    random_shuffle(dummyNegPattern.begin(),dummyNegPattern.end());

    // the labels and lables mat
    float posLabel = 1.f;
    float negLabel = 2.f;
    cv::Mat cSvmLabels;

    //the data mat
    cv::Mat cSvmTrainingData;

    //dummy linear svm parmas
    SVMParams cSvmParams;
    cSvmParams.svm_type = cv::SVM::C_SVC;
    cSvmParams.C = 0.0100;
    cSvmParams.kernel_type = cv::SVM::LINEAR;
    cSvmParams.term_crit =  cv::TermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 1000000, FLT_EPSILON);




    cout << "creating training data. please wait" << endl;
    int i;
    for(i=0;i<posFiles;i++)
    {
        //your feature for one box from file
        vector<float> d = dummyDerReaderForOneDer(dummyPosPattern);

        //push back a new mat made from the vectors data, with copy  data flag on
        //this shows the format of the mat for a single example, (1 (row) X dims(col) ), as  training mat has each **row** as an example;
        //the push_back works like vector add adds each example to the bottom of the matrix
        cSvmTrainingData.push_back(cv::Mat(1,dims,CV_32FC1,d.data(),true));

        //push back a pos label to the labels mat
        cSvmLabels.push_back(posLabel);
    }

    //do same with neg files;
    for(i=0;i<negFiles;i++)
    {
        float a =  rand(); 
        vector<float> d = dummyDerReaderForOneDer(dummyNegPattern);
        cSvmTrainingData.push_back(cv::Mat(1,dims,CV_32FC1,d.data(),true));
        cSvmLabels.push_back(negLabel);
    }

    //have a look
    cv::Mat viz;
    cSvmTrainingData.convertTo(viz,CV_8UC3);
    viz = viz*255;
    cv::imshow("svmData", viz);
    cv::waitKey(10);
    cout << "press any key to continue" << endl;
    getchar();

    viz.release();

    //create the svm;
    cout << "training, please wait" << endl;

    CvSVM svm;
    svm.train(cSvmTrainingData,cSvmLabels,cv::Mat(),cv::Mat(),cSvmParams);

    cout << "testing, please wait" << endl;
    //test the svm with a large amount of new unseen fake one at a time
    int totExamples = 10000;
    float TP(0.f), FP(0.f), FN(0.f), TN(0.f);
    for(i=0;i<totExamples; i++)
    {
        vector<float> dPos = dummyDerReaderForOneDer(dummyPosPattern);
        cv::Mat dMatPos(1,dims,CV_32FC1,dPos.data(),true);
        float predLabelPos = svm.predict(dMatPos);

        if(predLabelPos == posLabel) TP++;
        else(FN++);

        vector<float> dNeg = dummyDerReaderForOneDer(dummyNegPattern);
        cv::Mat dMatNeg(1,dims,CV_32FC1,dNeg.data(),true);
        float predLabelNeg = svm.predict(dMatNeg);

        if(predLabelNeg == negLabel) TN++;
        else(FP++);

        if(i%1000==0)
            cout << "testing " << i << "of " <<  totExamples << endl; 
    }

    //http://en.wikipedia.org/wiki/Precision_and_recall
    float sensitivity  = TP / (TP + FN);
    float specificity = TN / (TN + FP);
    float precision = TP / (TP + FP);

    cout << "sensitivity: " <<  sensitivity << endl;
    cout << "specificity: " <<  specificity << endl;
    cout << "precision: " <<  precision << endl;

    cout << "press any key to continue" << endl;
    getchar();

    cout << "creating test data, please wait" << endl;
    //test the svm with a large amount of new unseen fake data all at one time
    TP=FP=FN=TN = 0.f;
    vector<float> testLabels;
    for(i=0;i<int(totExamples/2);i++)
    {
        testLabels.push_back(posLabel);
    }
    for(i;i<totExamples;i++)
    {
        testLabels.push_back(negLabel);
    }


    vector<float> allExamples = dummyDerReaderForManyDers(dummyPosPattern,dummyNegPattern,testLabels,posLabel,negLabel);

    //on big mat
    cv::Mat allExamplesMat(1,allExamples.size(),CV_32FC1,allExamples.data(),true);

    //need reshape to one example per row

    allExamplesMat = allExamplesMat.reshape(0,totExamples);

    //have a look
    allExamplesMat.convertTo(viz,CV_8UC3);
    viz = viz*255;
    cv::imshow("testData", viz);
    cv::waitKey(10);
    cout << "press any key to continue" << endl;
    getchar();
    viz.release();

    //test them all at once ( uses intel tbb :)
    cout << "predict all at once, please wait" << endl;  
    cv::Mat predLabels_mat;
    svm.predict(allExamplesMat,predLabels_mat);

    //evaluate
    for(i=0;i<testLabels.size();i++)
    {
        float testLabel = testLabels.at(i);
        float predLabel = predLabels_mat.at<float>(i);
        if(testLabel==predLabel)
        {
            if(testLabel==posLabel) TP++;
            else TN++;

        }
        else
        {
            if(testLabel==posLabel) FP++;
            else FN++;

        }

    }

    //http://en.wikipedia.org/wiki/Precision_and_recall
    sensitivity  = TP / (TP + FN);
    specificity = TN / (TN + FP);
    precision = TP / (TP + FP);

    cout << "sensitivity: " <<  sensitivity << endl;
    cout << "specificity: " <<  specificity << endl;
    cout << "precision: " <<  precision << endl;

    cout << "press any key to continue" << endl;
    getchar();
    return(0);
}

请提供更多信息。a:这是什么?向量,数组,数组向量,数组,番茄什么是Hogfeat?如果Hogfeat是一个cv::Mat,那么请修改您的代码,就像我们不知道这些东西是什么一样,那么我们怎么可能告诉您如何复制/移动它们,想想看!这将显示格式,请按照示例进行操作。我以为你会从我提供的链接中得到这个想法。嗯,它是一个点的向量,存储每个图像的特征。Hogfeat是一个Mat对象,它将存储这些特性,以便可以将这些特性提供给您提供的链接中第28行的函数,但是为了使该函数正常工作,这个Mat对象应该采用正确的格式,这就是我不清楚如何将这些特性放置在Mat对象中的地方!好的,在这种情况下,你需要把它分解回每个盒子的向量。图像可能包含许多框,因此会产生混淆。要分解长向量,您需要知道描述符。然后使用cv::mat-boxDescriptor1、descriptorSize、cv_32FC1、OneBoxDescVector.data、true从每个特征向量创建一个mat;构造器。你可以把你推回垫子,就像向量一样。因此,您可以对所有框allDataMat.push_backcv::Mat1,descriptorSize,CV_32FC1,allBoxDescVector[i]。数据为true,其中allBoxDescVector是向量,每个条目都是一个ders^^^请注意,这是descriptorSize,而不是ders.size,ders.size=框数*descriptorSize。你可以像你一样尝试在复制后重塑,但我自己没有尝试过。重塑描述符X个盒子,它需要每行矩阵一个描述符@QED真是太好了。我理解你的代码流程&你的风格很棒。我不明白的一件事是第一个函数std::random_shufflepatternPulNoise.begin+std::mini,j,patternPulNoise.begin中的行‌​+标准:maxi,j;它与读取描述符值有什么关系?正如我所看到的,它只是在剥离功能而已!我必须提到的是,viz的东西是伟大的,但不幸的是,我不知道它揭示了什么:但非常感谢你的时间!您好,这只是一个模拟特征向量的虚拟函数,随机洗牌只是确保每个模拟特征彼此略有不同,以便进行更真实的测试。您不需要这些函数,您将生成/读取真实的特征。由于您使用了两个不同的循环,即预测和测试,因此b/w预测和测试集的区别是什么。一个,使用一个特征预测一个实例,另一个将多个实例放入一个矩阵并预测所有实例,给出一系列预测,每个例子一个。这主要是为了说明在创建巨大的垫子后需要对矩阵进行重塑。现在我想在detect函数中使用这个经过训练的模型,它应该是单行向量形式,对吗?为了实现这一点,我遵循。但我不知道如何得到这些alpha值。你能告诉我在哪里可以找到它们吗?你认为代码写得好吗?