1. 程式人生 > >libsvm的資料格式及製作

libsvm的資料格式及製作

/*Function:int prepareFeatsData(string samples_path, string outfile, char* class_samples, uchar* char_class, int nclass, string extens)
Features:
對指定的樣本(數字和字母)的路徑下的各個指定的樣本檔案進行特徵提取,
連同類編號依次存入outfile指定檔案中。
Attention:
注意_finddata_t結構體和_findfirst函式的用法
in-parameter:
samples_path:所有樣本歸屬的資料夾
outfile:提取特徵後寫入的檔案
class_samples:各個樣本的資料夾標記
char_class:各樣本的類別標記
nclass:有多少類樣本
extens:樣本檔案的字尾副檔名(例如:"*.png")
out-parameter:
Return    : 處理的樣本總數
Author: Mengjia  Date:2017-1-5 16:05:47
*/ int prepareFeatsData(string samples_path, string outfile, char* class_samples, uchar* char_class, int nclass, string extens) { int itatol = 0; FILE *fp; fp = fopen(outfile.c_str(), "w+"); //train_samples\\train_lbp.txt long hFile;//int nn=0; for (int nsamp = 0; nsamp < nclass; nsamp++) {
string cur_folder = samples_path; cur_folder += '\\'; cur_folder += class_samples[nsamp];//樣本的資料夾標記 cur_folder += '\\'; string findfile = cur_folder; findfile += extens;//所要尋找的檔案格式全路徑 struct _finddata_t img_file; if ((hFile = _findfirst(findfile.c_str(), &img_file)) == -1L
)//"rawdata/*.png" { printf("no %s files in directory :%s\n", extens.c_str(), findfile.c_str()); //printf(findfile.c_str()); } do { string filename = cur_folder; filename += img_file.name; IplImage* pImg = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_UNCHANGED); if (pImg) { //nn++; itatol++; IplImage* pBImg = alignmentImg(pImg); IplImage* standarImg = sizeNormalization(pBImg, 32, 64); int scale_flag = SCALE_LBP_FEATURES; CvMat* lbp_feats = LBP_features(standarImg, 8, 8, 4, scale_flag); if (scale_flag == SCALE_LBP_FEATURES) { double*pData = (double*)(lbp_feats->data.db); fprintf(fp, "%d ", char_class[nsamp]); //寫入樣本類別 for (int i = 0; i < lbp_feats->cols * lbp_feats->rows; i++) { if (pData[i] != 0.) fprintf(fp, "%d%s%g ", i + 1, ":", pData[i]); //按格式寫入特徵 } fprintf(fp, "\t\n"); } else { uchar*pData = (uchar*)(lbp_feats->data.ptr); fprintf(fp, "%d ", char_class[nsamp]); //寫入樣本類別 for (int i = 0; i < lbp_feats->cols * lbp_feats->rows; i++) { if (pData[i] != 0.) fprintf(fp, "%d%s%d ", i + 1, ":", pData[i]); //按格式寫入特徵 } fprintf(fp, "\t\n"); } cvReleaseMat(&lbp_feats); cvReleaseImage(&pBImg); cvReleaseImage(&standarImg); cvReleaseImage(&pImg); } } while (_findnext(hFile, &img_file) == 0); _findclose(hFile); } fclose(fp); printf("total samples = %d\n", itatol); return itatol; }