我在GitHub上找到了此代码,并且希望将其与新版本OpenCV一起使用(该代码大约在8年前编写) 我尝试更新它,但是我的技能真的很差(我第一次使用OpenCV) 作者使用MNIST数据集进行训练
bool DigitRecognizer::train(char *trainPath, char *labelsPath)
{
FILE *fp = fopen(trainPath, "rb");
FILE *fp2 = fopen(labelsPath, "rb");
if(!fp || !fp2)
return false;
// Read bytes in flipped order
int magicNumber = readFlippedInteger(fp);
numImages = readFlippedInteger(fp);
numRows = readFlippedInteger(fp);
numCols = readFlippedInteger(fp);
fseek(fp2, 0x08, SEEK_SET);
if(numImages > MAX_NUM_IMAGES) numImages = MAX_NUM_IMAGES;
// Go through each training data entry and figure out a
// center for each digit
int size = numRows*numCols;
CvMat *trainingVectors = cvCreateMat(numImages, size, CV_32FC1);
CvMat *trainingClasses = cvCreateMat(numImages, 1, CV_32FC1);
memset(trainingClasses->data.ptr, 0, sizeof(float)*numImages);
BYTE *temp = new BYTE[size];
BYTE tempClass=0;
for(int i=0;i<numImages;i++)
{
fread((void*)temp, size, 1, fp);
fread((void*)(&tempClass), sizeof(BYTE), 1, fp2);
trainingClasses->data.fl[i] = tempClass;
for(int k=0;k<size;k++)
trainingVectors->data.fl[i*size+k] = temp[k]; ///sumofsquares;
}
knn->train(trainingVectors, trainingClasses);
fclose(fp);
fclose(fp2);
return true;
}
如何在新的OpenCV版本中使用它?