简体   繁体   English

计算 HOG 功能时,opencv 断言失败错误 438

[英]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.我在字符识别问题中使用了 Hog 特征。通过在 OpenCV 中的 Hog 描述符类中使用计算函数。 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;
}

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 : HOGDescriptor::compute内部使用alignSize(size_t sz, int n)函数,它的主体中有一个断言:

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.这个断言指出输入必须是 2 的幂。据我所知,这适用于单元格和块大小(在您的代码中为 10 和 10)。 So to get rid of this assertion you need to change them to 8 or any number of power of 2 (ie 2, 4, 8, 16, 32, ...).因此,要摆脱此断言,您需要将它们更改为 8 或任意数量的 2 的幂(即 2, 4, 8, 16, 32, ...)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM