2016-06-22 20 views
0

現在、私は、ライブストリーム上の人をリアルタイムで検出するプログラムに取り組んでいます。したがって、私はOpenCV 3.1でC++を使用しています。 私のアプローチは、SVMを訓練し、HOG Detectorを使用することです。OpenCVでのSVMのトレーニング

私はSVMを訓練するときに、特にトレーニングマトリックスを構築することによって何かがうまくいかないと思います。 My Detectorは作成されたSVMを読み込み、hog.detectMultiscale()で使用します。 opencvの「getDefaulPeopleDetector」のdefault-svmを使用すると、簡単な画像で良い結果が得られました。

私は自分の訓練されたSVMを適用する必要があります。なぜなら私たちのテストセットアップはかなり難しく、デフォルトのものはそれをカバーしていないからです。 私の検出器を実行して得た結果は本当に悪いです。カバーされたウェブカメラ/黒画面上の「人」または画面全体のランダムな検出を検出します。ここで

は私のコードです:そのサイズがcoorectある場合

getImagesディレクトリ内の負荷のすべての画像をチェック(64x128)私はSVMによって使用されている訓練行列を作成するには、この方法では

//Reads and checks the images from the given directory 
static void getImages(vector<string>& fileNames, const string dirName) { 
    DIR *dir; 
    dir = opendir(dirName.c_str()); 
    struct dirent *ent; 
    string imgName; 
    string imgPath; 
    Mat img; 


    if (dir != NULL) { 
     while ((ent = readdir (dir)) != NULL) { 
     imgName = ent->d_name; 
      imgPath = dirName + "/" +imgName; 
     img = imread(imgPath); 
      if(!img.data) { 
     cout << imgPath << " has no data" << endl;  
      } else { 
      if(img.rows != IMG_ROW_SIZE && img.cols != IMG_COL_SIZE) { 
      cout << imgName << " size not correct: " << img.cols << "x" << img.rows <<endl; 
      } else { 
      cout << imgPath << " loaded" << endl; 
      fileNames.push_back(imgPath); 
      } 
      } 
     } 
     closedir (dir); 
    } else { 
     cout << dirName << " not present" << endl; 
    } 
} 

。列車()。 すべての画像について、私は豚の特徴を計算します。 1つの特徴ベクトルは、トレーニング行列の1つの行に挿入される。 すべての行には1つの画像の特徴が含まれています。 私はこれが私の主な方法である

static void initTrainingMats(Mat& trainingMat, vector<string>& positiveImages, vector<string>& negativeImages, Mat& labelMat) { 
HOGDescriptor hog; 
vector<float> hogFeature; 
Mat img; 

cout << "computing hog features.. "; 
for(int i = 0; i<trainingMat.rows; i ++) { // for-loop iterating through all images 
img = (i < positiveImages.size() ? imread(positiveImages.at(i)) : imread(negativeImages.at(i-positiveImages.size()))); 
labelMat.at<float>(i,1) = (i < positiveImages.size() ? 1 : -1); 
hog.compute(img, hogFeature, Size(8,8), Size(8,8)); 
for(int j = 0; j<trainingMat.cols; j++) { 
    trainingMat.at<float>(i,j) = hogFeature.at(j);  
} 
} 

SVM with images)hogfeaturesの計算が正しく実装されているかどうかわからないですか、マトリックスは、この記事で示した方法で作成されていない場合。 SVMを初期化して保存します。

int main(int, char**) 
{ 
    //vectors to store file path to each correct image 
    vector<string> positiveImages; 
    vector<string> negativeImages; 
    getImages(positiveImages, posDirName); 
    getImages(negativeImages, negDirName); 
    int allImages = positiveImages.size() + negativeImages.size(); 

    //initialize training matrix and label matrix 
    Mat trainingMat(allImages, 3780 ,CV_32FC1); //matrix with column size same as size of hog_sample vector 
    Mat labelMat(allImages, 1, CV_32SC1); 
    initTrainingMats(trainingMat, positiveImages, negativeImages, labelMat); 


    Ptr<SVM> svm = SVM::create(); 
    svm->setType(SVM::C_SVC); 
    svm->setKernel(SVM::LINEAR); 
    svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-10)); 
    svm->train(trainingMat, ROW_SAMPLE, labelMat); 
    svm->save("../../hog_video/build/svm_v2"); 

    cout<<"SVM stored successfully" << endl; 
    } 

私はちょっと私はあなたたちが私のために得たすべてのヘルプ/提案のための嬉しい、ここで立ち往生しています!

答えて

0

まず、どのように動作するかを理解する必要があります。これは、2Dデータセットの例です。

#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/ml/ml.hpp> 

using namespace cv; 

int main() 
{ 
    // Data for visual representation 
    int width = 512, height = 512; 
    Mat image = Mat::zeros(height, width, CV_8UC3); 

    // Set up training data 
    float labels[4] = {1.0, -1.0, -1.0, -1.0}; 
    Mat labelsMat(4, 1, CV_32FC1, labels); 

    float trainingData[4][2] = { {501, 10}, {255, 10}, {501, 255}, {10, 501} }; 
    Mat trainingDataMat(4, 2, CV_32FC1, trainingData); 

    // Set up SVM's parameters 
    CvSVMParams params; 
    params.svm_type = CvSVM::C_SVC; 
    params.kernel_type = CvSVM::LINEAR; 
    params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6); 

    // Train the SVM 
    CvSVM SVM; 
    SVM.train(trainingDataMat, labelsMat, Mat(), Mat(), params); 

    Vec3b green(0,255,0), blue (255,0,0); 
    // Show the decision regions given by the SVM 
    for (int i = 0; i < image.rows; ++i) 
     for (int j = 0; j < image.cols; ++j) 
     { 
      Mat sampleMat = (Mat_<float>(1,2) << j,i); 
      float response = SVM.predict(sampleMat); 

      if (response == 1) 
       image.at<Vec3b>(i,j) = green; 
      else if (response == -1) 
       image.at<Vec3b>(i,j) = blue; 
     } 

    // Show the training data 
    int thickness = -1; 
    int lineType = 8; 
    circle(image, Point(501, 10), 5, Scalar( 0, 0, 0), thickness, lineType); 
    circle(image, Point(255, 10), 5, Scalar(255, 255, 255), thickness, lineType); 
    circle(image, Point(501, 255), 5, Scalar(255, 255, 255), thickness, lineType); 
    circle(image, Point(10, 501), 5, Scalar(255, 255, 255), thickness, lineType); 

    // Show support vectors 
    thickness = 2; 
    lineType = 8; 
    int c  = SVM.get_support_vector_count(); 

    for (int i = 0; i < c; ++i) 
    { 
     const float* v = SVM.get_support_vector(i); 
     circle(image, Point((int) v[0], (int) v[1]), 6, Scalar(128, 128, 128), thickness, lineType); 
    } 

    imwrite("result.png", image);  // save the image 

    imshow("SVM Simple Example", image); // show it to the user 
    waitKey(0); 

} 
関連する問題