我正在尝试将输入图像中的数字分类到我的代码中 我创建了一个OCR_3.xlm文件,我认为没有问题, 但是当我尝试训练CvANN_MLP时,我得到一个错误,上面写着:
OpenCV错误:断言失败(y == 0 ||(数据&& dims> = 1&&(unsigned)y<(unsigned)size.p [0 ]))在cv :: Mat :: ptr中,文件D:\ opencv \ build \ include \ opencv2 \ core \ Mat.hpp,第429行
这是我的代码:
int main(int argc, char** argv)
{
FileStorage fs;
fs.open("OCR_3.xml", FileStorage::READ);
Mat trainData;
Mat classes;
fs["TrainingData"] >> trainData;
fs["classes"] >> classes;
int const nlayers = 10;
CvANN_MLP ann;
int const numCharacters = 9;
int buffer[] = { trainData.cols, nlayers, numCharacters };
Mat const layers(1, 3, CV_32SC1, buffer);
ann.create(layers, CvANN_MLP::SIGMOID_SYM, 1, 1);
Mat trainClasses(trainData.rows, numCharacters, CV_32FC1);
for (int i = 0; i != trainClasses.rows; ++i){
int const labels = *classes.ptr<int>(i);
auto train_ptr = trainClasses.ptr<float>(i);
for (int k = 0; k != trainClasses.cols; ++k){
*train_ptr = k != labels ? 0 : 1;
++train_ptr;
}
}
Mat const weights = cv::Mat::ones(1, trainData.rows, CV_32FC1);
ann.train(trainData, trainClasses, weights);
return 0;
}
我一直在研究这个问题大约3个月,我真的不知道问题是什么,请你帮我解决一下这个问题,也许.xml文件中存在问题
您可以从此URL下载我的ocr_3.xml文件:
https://www.dropbox.com/s/g32cm20f13hffpu/OCR_3.xml?dl=0
答案 0 :(得分:0)
阅读本教程,了解如何准备数据文件 https://github.com/bytefish/opencv/blob/master/machinelearning/doc/machinelearning.pdf
注意:最新版本的OpenCv - 3.0 - 机器学习模块被重写 因此,在制作正确的数据后,文件代码必须适应新版本
http://docs.opencv.org/master/db/dfa/tutorial_transition_guide.html