1. 程式人生 > >基於CodeBook的背景差分演算法(C++)

基於CodeBook的背景差分演算法(C++)

/***********************************************************
//從文字檔案中讀取資料,並以矩陣Mat的形式儲存到YML檔案;
//檔案中一行資料代表一組特徵向量,且相鄰分量的預設分割符為一個空格;
//該函式接受引數:文字檔名和分割符;
***********************************************************/
void txtConvertToMat(string& Filename, char& delimiter, OutputArray dst)
{
    fstream fs(Filename.data(),ios::in);
    char
data[500]; fs.getline(data,500); bool flags = false; vector<vector<double>> hu; int N = 8; while(data != NULL) { int count = 0; int pos = 0; int length = sizeof(data); char tmp[20] = "\0"; cout.setf(ios::fixed); cout.precision(6
); vector<double> huTmp; for(int i = 0; i < length; i++) { if(data[i] == '\0') { if(count == 0) { flags = true; break; } tmp[pos+1] = '\0'; double
d = atof(tmp); huTmp.push_back(d); hu.push_back(huTmp); huTmp.clear(); pos = 0; count = 0; break; }else if(data[i] == delimiter) { tmp[pos+1] = '\0'; double d = atof(tmp); huTmp.push_back(d); pos = 0; count++; }else { tmp[pos] = data[i]; pos++; } } //cout << "------------------------" << endl; if(flags) break; fs.getline(data,500); } int huSize = hu.size(); dst.create(huSize,N,CV_32FC1); Mat m = dst.getMat(); for(int i = 0; i < huSize; i++) { for(int j = 0; j < N; j++) { m.row(i).col(j) = hu[i][j]; } } char xmlFilename[100]; cout << Filename << endl; int filenameLength = Filename.size(); string tmpp = Filename.substr(0,filenameLength-4); tmpp = tmpp + ".xml"; cout << tmpp << endl; FileStorage filel(tmpp,FileStorage::WRITE); filel << "Hu" << m; filel.release(); } /***********************************************************