解析錯誤是因爲當你沒有指定你想生成POS圖像樣本的數量,createsamples將使用默認值是1000.但是,如果您的註釋文本文檔包含少於1000個對象的邊界框,則會出現分析錯誤。您仍然可以使用.vec文件進行級聯培訓。唯一的問題是號碼的信息不正確。有兩種方法可以解決它。
您手動計算文本文檔中的對象邊界框的數量。並指定小於或等於選項「-num」 的數字的值,例如createsamples -info xxxxx.txt -vec pos.vec -num [值]
您可以修改OPENCV_ROOT_DIR/modules/haartraining/createsamples.cpp。當未指定-num,則設定正的樣本數爲對象邊界框的文本文檔中的數
代碼片斷: 在createsamples.cpp INT NUM = 0;
在cvsamples.cpp
void icvWriteVecHeader(FILE* file, int count, int width, int height)
{
int vecsize;
short tmp;
fseek (file , 0 , SEEK_SET);
/* number of samples */
fwrite(&count, sizeof(count), 1, file);
/* vector size */
vecsize = width * height;
fwrite(&vecsize, sizeof(vecsize), 1, file);
/* min/max values */
tmp = 0;
fwrite(&tmp, sizeof(tmp), 1, file);
fwrite(&tmp, sizeof(tmp), 1, file);
fseek (file , 0 , SEEK_END);
}
int cvCreateTrainingSamplesFromInfo(const char* infoname, const char* vecfilename,
int num,
int showsamples,
int winwidth, int winheight)
{
char fullname[PATH_MAX];
char* filename;
FILE* info;
FILE* vec;
IplImage* src=0;
IplImage* sample;
int line;
int error;
int i;
int x, y, width, height;
int total;
assert(infoname != NULL);
assert(vecfilename != NULL);
total = 0;
if(!icvMkDir(vecfilename))
{
#if CV_VERBOSE
fprintf(stderr, "Unable to create directory hierarchy: %s\n", vecfilename);
#endif /* CV_VERBOSE */
return total;
}
info = fopen(infoname, "r");
if(info == NULL)
{
#if CV_VERBOSE
fprintf(stderr, "Unable to open file: %s\n", infoname);
#endif /* CV_VERBOSE */
return total;
}
vec = fopen(vecfilename, "wb");
if(vec == NULL)
{
#if CV_VERBOSE
fprintf(stderr, "Unable to open file: %s\n", vecfilename);
#endif /* CV_VERBOSE */
fclose(info);
return total;
}
sample = cvCreateImage(cvSize(winwidth, winheight), IPL_DEPTH_8U, 1);
icvWriteVecHeader(vec, num, sample->width, sample->height);
if(showsamples)
{
cvNamedWindow("Sample", CV_WINDOW_AUTOSIZE);
}
strcpy(fullname, infoname);
filename = strrchr(fullname, '\\');
if(filename == NULL)
{
filename = strrchr(fullname, '/');
}
if(filename == NULL)
{
filename = fullname;
}
else
{
filename++;
}
while (num<=0 || total<num)
{
int count;
error = (fscanf(info, "%s %d", filename, &count) != 2);
if(!error)
{
src = cvLoadImage(fullname, 0);
error = (src == NULL);
if(error)
{
#if CV_VERBOSE
fprintf(stderr, "Unable to open image: %s\n", fullname);
#endif /* CV_VERBOSE */
}
}
else
if (num <= 0) break;
for(i = 0; i < count; i++, total++)
{
error = (fscanf(info, "%d %d %d %d", &x, &y, &width, &height) != 4);
if(error) break;
cvSetImageROI(src, cvRect(x, y, width, height));
cvResize(src, sample, width >= sample->width &&
height >= sample->height ? CV_INTER_AREA : CV_INTER_LINEAR);
if(showsamples)
{
cvShowImage("Sample", sample);
if(cvWaitKey(0) == 27)
{
showsamples = 0;
}
}
icvWriteVecSample(vec, sample);
if (num > 0 && total >= num) break;
}
if (num<=0)
icvWriteVecHeader(vec, total, sample->width, sample->height);
if(src)
{
cvReleaseImage(&src);
}
if(error)
{
#if CV_VERBOSE
fprintf(stderr, "%s(%d) : parse error", infoname, line);
#endif /* CV_VERBOSE */
break;
}
}
if(sample)
{
cvReleaseImage(&sample);
}
fclose(vec);
fclose(info);
return total;
}
你可以通過整個命令行調用嗎? – red1ynx
我編輯了問題以包含指定的命令行。 Aslo文本文件與圖像位於同一目錄中。 – Seb