syam
syam

Reputation: 23

opencv assertion failed error-438 while computing HOG features

I use Hog features in character recognition problem .By using compute function in Hog descriptor class in OpenCV. I get this error:

OpenCV Error: Assertion failed ((n & (n - 1)) == 0) in cv::alignSize, file C:\opencv-3.2.0\modules\core\include\opencv2/core/utility.hpp, line 438

here is the code,there is no build errors. this code has already run in my system but could not run in another system. that system shows above error during running

#include <iostream>
#include <opencv2/opencv.hpp>
#include "databasereader.h"
#include "tinydir.h"
using namespace std;
using namespace cv;

int main()
{
     DatabaseReader dr;

    dr.readTrainingFiles();

    std::vector<int> labels= dr.getTrainLabels();
    std::vector<std::string>trainingFileNames = dr.getTrainFileNames();

    Mat trainingData;
    std::vector<int>trainingLabels;
    Mat img_gray;
    Size newSize(20,20);

    cout << "size =" << trainingFileNames.size()<<endl;

    for(unsigned int index=0;index<trainingFileNames.size();index++)
    {
        cout<<"file  "<<labels[index]<<"  "<<trainingFileNames[index]<<endl;
        Mat img=imread(trainingFileNames[index]);

        resize(img, img, newSize);
        imshow("india",img);

        cvtColor(img, img_gray, CV_RGB2GRAY);

        HOGDescriptor hog(
                    Size(20,20), //winSize
                    Size(10,10), //blocksize
                    Size(5,5), //blockStride,
                    Size(10,10), //cellSize,
                    9, //nbins,
                    1, //derivAper,
                    -1, //winSigma,
                    0, //histogramNormType,
                    0.2, //L2HysThresh,
                    1,//gammal correction,
                    64,//nlevels=64
                    1);//Use signed gradients

        vector<float>  descriptor;
        hog.compute(img_gray,descriptor);

        Mat vec(descriptor);
        vec = vec.reshape(0,1);
        //vector of images
        trainingData.push_back(vec);
        trainingLabels.push_back(labels[index]);
    }
    //convertion
    trainingData.convertTo(trainingData,CV_32FC1);
    cout<<"training started"<<endl;

    Ptr<cv::ml::SVM> svm= cv::ml::SVM::create();
    svm->setType(cv::ml::SVM::C_SVC);
    svm->setKernel(cv::ml::SVM::POLY);
    svm->setTermCriteria(cv::TermCriteria(TermCriteria::MAX_ITER,100, 1e-6));
    svm->setGamma(3);
    svm->setDegree(2);
    svm->setC(100);
    svm->train(trainingData,cv::ml::ROW_SAMPLE,trainingLabels);
    svm->save("classifier.xml");
    cout<<"training completed"<<endl;
    return 0;
}

Upvotes: 2

Views: 383

Answers (1)

HMD
HMD

Reputation: 2226

The assertion you are getting is normal, I don't know how you are not getting it on other system (maybe parameters are different on other system).

HOGDescriptor::compute internally use alignSize(size_t sz, int n) function which has an assertion in it's body :

assert((n & (n - 1)) == 0); // n is a power of 2

This assertion state that input must be a number of the power of 2. This applies to cell and block size (which are 10 and 10 in your code) as far as I know. So to get rid of this assertion you need to change them to 8 or any number of power of 2 (i.e 2, 4, 8, 16, 32, ...).

Upvotes: 1

Related Questions