Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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
C++ 基于支持向量机的目标检测_C++_Opencv_Svm_Libsvm_Opencv3.0 - Fatal编程技术网

C++ 基于支持向量机的目标检测

C++ 基于支持向量机的目标检测,c++,opencv,svm,libsvm,opencv3.0,C++,Opencv,Svm,Libsvm,Opencv3.0,我是新来的。我曾经使用HAAR级联进行对象检测。现在我正在尝试实现支持向量机用于目标检测。我在网上搜索,了解了一些基本知识。 在编码C++时,我想使用LIbSVM。我遇到了很多问题。 谁能解释一下用它进行物体检测的一步一步的过程。 顺便说一句,我查过了。但我不能再做任何事情了 我还得到了这段代码,用于训练我的SVM并将其保存到xml文件中。 现在我想要一个代码,它可以接受这个xml并在测试用例中检测对象 #include <opencv2/core/core.hpp> #includ

我是新来的。我曾经使用HAAR级联进行对象检测。现在我正在尝试实现支持向量机用于目标检测。我在网上搜索,了解了一些基本知识。 在编码C++时,我想使用LIbSVM。我遇到了很多问题。 谁能解释一下用它进行物体检测的一步一步的过程。 顺便说一句,我查过了。但我不能再做任何事情了

我还得到了这段代码,用于训练我的SVM并将其保存到xml文件中。 现在我想要一个代码,它可以接受这个xml并在测试用例中检测对象

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <cv.h>
#include <highgui.h>
#include <cvaux.h>
#include <iostream>
#include <vector>
#include<string.h>
using namespace std;
using namespace cv;

int main ( int argc, char** argv )
{
    cout << "OpenCV Training SVM Automatic Number Plate Recognition\n";
    cout << "\n";

    char* path_Plates;
    char* path_NoPlates;
    int numPlates;
    int numNoPlates;
    int imageWidth=150;
    int imageHeight=150;

    //Check if user specify image to process
    if(1)
    {
        numPlates= 11;
        numNoPlates= 90 ;
        path_Plates= "/home/kaushik/opencv_work/Manas6/Pics/Positive_Images/";
        path_NoPlates= "/home/kaushik/opencv_work/Manas6/Pics/Negative_Images/i";

    }else{
        cout << "Usage:\n" << argv[0] << " <num Plate Files> <num Non Plate Files> <path to plate folder files> <path to non plate files> \n";
        return 0;
    }

    Mat classes;//(numPlates+numNoPlates, 1, CV_32FC1);
    Mat trainingData;//(numPlates+numNoPlates, imageWidth*imageHeight, CV_32FC1 );

    Mat trainingImages;
    vector<int> trainingLabels;

    for(int i=1; i<= numPlates; i++)
    {

        stringstream ss(stringstream::in | stringstream::out);
        ss<<path_Plates<<i<<".jpg";
        try{

            const char* a = ss.str().c_str();
            printf("\n%s\n",a);
            Mat img = imread(ss.str(), CV_LOAD_IMAGE_UNCHANGED);
            img= img.clone().reshape(1, 1);
            //imshow("Window",img);
            //cout<<ss.str();
            trainingImages.push_back(img);
            trainingLabels.push_back(1);
        }
        catch(Exception e){;}
    }

    for(int i=0; i< numNoPlates; i++)
    {
        stringstream ss(stringstream::in | stringstream::out);
        ss << path_NoPlates<<i << ".jpg";
        try
        {
            const char* a = ss.str().c_str();
            printf("\n%s\n",a);
            Mat img=imread(ss.str(), 0);
            //imshow("Win",img);
            img= img.clone().reshape(1, 1);
            trainingImages.push_back(img);
            trainingLabels.push_back(0);
            //cout<<ss.str();
        }
        catch(Exception e){;}
    }

    Mat(trainingImages).copyTo(trainingData);
    //trainingData = trainingData.reshape(1,trainingData.rows);
    trainingData.convertTo(trainingData, CV_32FC1);
    Mat(trainingLabels).copyTo(classes);

    FileStorage fs("SVM.xml", FileStorage::WRITE);
    fs << "TrainingData" << trainingData;
    fs << "classes" << classes;
    fs.release();

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间cv;
int main(int argc,字符**argv)
{

cout这是一个简单的代码,您可以使用xml文件进行测试:

#include "highgui.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "cv.h"
#include <vector>
#include <string.h>
#include <ml.h>
#include <iostream>

#include <io.h>
using namespace cv;
using namespace std;

int main()
{   
    FileStorage fs;
    fs.open("SVM.xml", FileStorage::READ);
    Mat trainingData;
    Mat classes;
    fs["TrainingData"] >> trainingData;
    fs["classes"] >> classes;

    CvSVMParams SVM_params;
    SVM_params.svm_type = CvSVM::C_SVC;
    SVM_params.kernel_type = CvSVM::LINEAR; //CvSVM::LINEAR;
    SVM_params.degree = 1;
    SVM_params.gamma = 1;
    SVM_params.coef0 = 0;
    SVM_params.C = 1;
    SVM_params.nu = 0;
    SVM_params.p = 0;
    SVM_params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 1000, 0.01);

    CvSVM svm(trainingData, classes, Mat(), Mat(), SVM_params);


    Mat src = imread("D:\\SVM\\samples\\\pos\\10.jpg");
    Mat gray;
    cvtColor(src, gray, CV_BGR2GRAY);
    Mat p = gray.reshape(1, 1);
    p.convertTo(p, CV_32FC1);

    int response = (int)svm.predict( p );
    if(response ==1 )
    {
        cout<<"this is a object!"<<endl;
        cout<<endl;
    }
    else
    {
        cout<<"no object detected!"<<endl;
        cout<<endl;
    } 

    return 0;
}
#包括“highgui.h”
#包括“opencv2/imgproc/imgproc.hpp”
#包括“cv.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
int main()
{   
文件存储;
open(“SVM.xml”,FileStorage::READ);
Mat训练数据;
垫类;
fs[“培训数据”]>>培训数据;
fs[“类”]>>类;
CvSVMParams SVM_参数;
SVM_params.SVM_type=CvSVM::C_SVC;
SVM_params.kernel_type=CvSVM::LINEAR;//CvSVM::LINEAR;
SVM_参数度=1;
SVM_参数γ=1;
SVM_params.coef0=0;
SVM_参数C=1;
SVM_params.nu=0;
SVM_参数p=0;
SVM参数术语临界值=CvTerm标准(CV term临界值,1000,0.01);
CvSVM(训练数据、类、Mat()、Mat()、svm参数);
Mat src=imread(“D:\\SVM\\samples\\\pos\\10.jpg”);
席灰色;
CVT颜色(src、灰色、CV_BGr2灰色);
Mat p=灰色。重塑(1,1);
p、 convertTo(p,CV_32FC1);
int响应=(int)支持向量机预测(p);
如果(响应==1)
{
库特