Ask Your Question

SachinTS's profile - activity

2015-04-16 00:27:37 -0600 asked a question openCV error:“Sizes of input arguments do not match” while trying to detect objects using HoG features and SVM

I am working on object detection using HoG Features and SVM. I am calculating HoG features for 64X128 images using the code in this (Integral Histogram for fast Calculation of HOG Features) and this (Calculation of Hog Features) for both positive(pos.xml) and Negative(neg.xml) images. I am training the SVM with the pos.xml and neg.xml . When I try to detect using the xml file saved using SVM training , it gives me following error. (image size used for detection is 64X128)

OpenCV Error: Sizes of input arguments do not match (The sample size is differen t from what has been used for training) in cvPreparePredictData, file ....... .\opencv\modules\ml\src\inner_functions.cpp, line 1114

My code for detecting images.

    Mat img= imread("path\\a91.jpg"); // 64X128 pixels
    CvSVM svm;
    svm.load("svm1.xml");
    Mat example;
    cvtColor(img,example,CV_RGB2GRAY);
    example = example.reshape(0,1);
    //cout  <<type2str(example.type()) <<endl;



    example.convertTo(example, CV_32FC1);

    //cout  <<type2str(example.type()) <<endl;
    cout<<svm.predict(example)<<endl;

SVM training code :

void trainSVM(CvMat* pos_mat, CvMat* neg_mat, char *savexml, char *pos_file = NULL, char *neg_file = NULL) 
{


    /* Read the feature vectors for positive samples */
    if (pos_file != NULL) 
        {
        printf("positive loading...\n");
        pos_mat = (CvMat*) cvLoad(pos_file);
        printf("positive loaded\n");
    }

    /* Read the feature vectors for negative samples */
    if (neg_file != NULL)
    {
        neg_mat = (CvMat*) cvLoad(neg_file);
        printf("negative loaded\n");
    }

    int n_positive, n_negative;
    n_positive = pos_mat->rows;
    n_negative = neg_mat->rows; 
    int feature_vector_length = pos_mat->cols;
    int total_samples;
    total_samples = n_positive + n_negative;

    CvMat* trainData = cvCreateMat(total_samples,feature_vector_length, CV_32FC1);

    CvMat* trainClasses = cvCreateMat(total_samples,1, CV_32FC1 );

    CvMat trainData1, trainData2, trainClasses1,
    trainClasses2;

    printf("Number of positive Samples : %d\n",pos_mat->rows);

    /*Copy the positive feature vectors to training
    data*/

    cvGetRows(trainData, &trainData1, 0, n_positive);
    cvCopy(pos_mat, &trainData1);
    cvReleaseMat(&pos_mat);

    /*Copy the negative feature vectors to training
    data*/

    cvGetRows(trainData, &trainData2, n_positive,total_samples);

    cvCopy(neg_mat, &trainData2);
    cvReleaseMat(&neg_mat);

    printf("Number of negative Samples : %d\n",trainData2.rows);

    /*Form the training classes for positive and
    negative samples. Positive samples belong to class
    1 and negative samples belong to class 2 */

    cvGetRows(trainClasses, &trainClasses1, 0, n_positive);
    cvSet(&trainClasses1, cvScalar(1));

    cvGetRows(trainClasses, &trainClasses2, n_positive, total_samples);

    cvSet(&trainClasses2, cvScalar(2));


    /* Train a linear support vector machine to learn from
    the training data. The parameters may played and
    experimented with to see their effects*/

    CvSVM svm(trainData, trainClasses, 0, 0, CvSVMParams(CvSVM::C_SVC, CvSVM::LINEAR, 0, 0, 0, 2, 0, 0, 0, cvTermCriteria(CV_TERMCRIT_EPS,0, 0.01)));

    printf("SVM Training Complete!!\n");

    /*Save the learnt model*/

    if (savexml != NULL) {
        svm.save(savexml);
    }
    cvReleaseMat(&trainClasses);
    cvReleaseMat(&trainData);

}

Please help me with this. I am stuck here. Thank you.

2015-04-16 00:27:36 -0600 asked a question openCV error:“Sizes of input arguments do not match” while trying to detect objects using HoG features and SVM

I am working on object detection using HoG Features and SVM. I am calculating HoG features for 64X128 images using the code in this (Integral Histogram for fast Calculation of HOG Features) and this (Calculation of Hog Features) for both positive(pos.xml) and Negative(neg.xml) images. I am training the SVM with the pos.xml and neg.xml . When I try to detect using the xml file saved using SVM training , it gives me following error. (image size used for detection is 64X128)

OpenCV Error: Sizes of input arguments do not match (The sample size is differen t from what has been used for training) in cvPreparePredictData, file ....... .\opencv\modules\ml\src\inner_functions.cpp, line 1114

My code for detecting images.

    Mat img= imread("path\\a91.jpg"); // 64X128 pixels
    CvSVM svm;
    svm.load("svm1.xml");
    Mat example;
    cvtColor(img,example,CV_RGB2GRAY);
    example = example.reshape(0,1);
    //cout  <<type2str(example.type()) <<endl;



    example.convertTo(example, CV_32FC1);

    //cout  <<type2str(example.type()) <<endl;
    cout<<svm.predict(example)<<endl;

SVM training code :

void trainSVM(CvMat* pos_mat, CvMat* neg_mat, char *savexml, char *pos_file = NULL, char *neg_file = NULL) 
{


    /* Read the feature vectors for positive samples */
    if (pos_file != NULL) 
        {
        printf("positive loading...\n");
        pos_mat = (CvMat*) cvLoad(pos_file);
        printf("positive loaded\n");
    }

    /* Read the feature vectors for negative samples */
    if (neg_file != NULL)
    {
        neg_mat = (CvMat*) cvLoad(neg_file);
        printf("negative loaded\n");
    }

    int n_positive, n_negative;
    n_positive = pos_mat->rows;
    n_negative = neg_mat->rows; 
    int feature_vector_length = pos_mat->cols;
    int total_samples;
    total_samples = n_positive + n_negative;

    CvMat* trainData = cvCreateMat(total_samples,feature_vector_length, CV_32FC1);

    CvMat* trainClasses = cvCreateMat(total_samples,1, CV_32FC1 );

    CvMat trainData1, trainData2, trainClasses1,
    trainClasses2;

    printf("Number of positive Samples : %d\n",pos_mat->rows);

    /*Copy the positive feature vectors to training
    data*/

    cvGetRows(trainData, &trainData1, 0, n_positive);
    cvCopy(pos_mat, &trainData1);
    cvReleaseMat(&pos_mat);

    /*Copy the negative feature vectors to training
    data*/

    cvGetRows(trainData, &trainData2, n_positive,total_samples);

    cvCopy(neg_mat, &trainData2);
    cvReleaseMat(&neg_mat);

    printf("Number of negative Samples : %d\n",trainData2.rows);

    /*Form the training classes for positive and
    negative samples. Positive samples belong to class
    1 and negative samples belong to class 2 */

    cvGetRows(trainClasses, &trainClasses1, 0, n_positive);
    cvSet(&trainClasses1, cvScalar(1));

    cvGetRows(trainClasses, &trainClasses2, n_positive, total_samples);

    cvSet(&trainClasses2, cvScalar(2));


    /* Train a linear support vector machine to learn from
    the training data. The parameters may played and
    experimented with to see their effects*/

    CvSVM svm(trainData, trainClasses, 0, 0, CvSVMParams(CvSVM::C_SVC, CvSVM::LINEAR, 0, 0, 0, 2, 0, 0, 0, cvTermCriteria(CV_TERMCRIT_EPS,0, 0.01)));

    printf("SVM Training Complete!!\n");

    /*Save the learnt model*/

    if (savexml != NULL) {
        svm.save(savexml);
    }
    cvReleaseMat(&trainClasses);
    cvReleaseMat(&trainData);

}

Please help me with this. I am stuck here. Thank you.