我在字符识别问题中使用Hog功能。通过在OpenCV的Hog描述符类中使用计算功能。我收到此错误:
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
这是代码,没有构建错误。该代码已经在我的系统中运行,但是无法在另一个系统中运行。该系统在运行过程中显示上述错误
#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;
}
答案 0 :(得分:1)
您得到的断言是正常的,我不知道您在其他系统上是如何得到它的(也许其他系统上的参数是不同的)。
HOGDescriptor::compute
内部使用alignSize(size_t sz, int n)
函数,该函数在其主体中具有一个断言:
assert((n & (n - 1)) == 0); // n is a power of 2
此断言状态表明输入必须为2的幂。据我所知,这适用于单元格和块大小(在代码中分别为10和10)。因此,要摆脱此断言,您需要将其更改为8或任意数量的2的幂(即2、4、8、16、32,...)。