⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writefeatures.c

📁 klt, a tracking algorithm
💻 C
📖 第 1 页 / 共 2 页
字号:
  if (KLT_verbose >= 1 && fname != NULL)  {    fprintf(stderr,              "(KLT) Writing feature history to %s file: '%s'\n",             (fmt == NULL ? "binary" : "text"), fname);  }  if (fmt != NULL) {  /* text file or stderr */    fp = _printSetupTxt(fname, fmt, format, &type);    _printHeader(fp, format, FEATURE_HISTORY, fh->nFrames, 0);	    for (i = 0 ; i < fh->nFrames ; i++)  {      fprintf(fp, "%5d | ", i);      _printFeatureTxt(fp, fh->feature[i], format, type);      fprintf(fp, "\n");    }    _printShutdown(fp);  } else {  /* binary file */    fp = _printSetupBin(fname);    fwrite(binheader_fh, sizeof(char), BINHEADERLENGTH, fp);     fwrite(&(fh->nFrames), sizeof(int), 1, fp);    for (i = 0 ; i < fh->nFrames ; i++)  {      _printFeatureBin(fp, fh->feature[i]);    }    fclose(fp);  }}void KLTWriteFeatureTable(  KLT_FeatureTable ft,  char *fname,   char *fmt){  FILE *fp;  char format[100];  char type;  int i, j;  if (KLT_verbose >= 1 && fname != NULL)  {    fprintf(stderr,              "(KLT) Writing feature table to %s file: '%s'\n",             (fmt == NULL ? "binary" : "text"), fname);  }  if (fmt != NULL) {  /* text file or stderr */    fp = _printSetupTxt(fname, fmt, format, &type);    _printHeader(fp, format, FEATURE_TABLE, ft->nFrames, ft->nFeatures);    for (j = 0 ; j < ft->nFeatures ; j++)  {      fprintf(fp, "%7d | ", j);      for (i = 0 ; i < ft->nFrames ; i++)        _printFeatureTxt(fp, ft->feature[j][i], format, type);      fprintf(fp, "\n");    }    _printShutdown(fp);  } else {  /* binary file */    fp = _printSetupBin(fname);    fwrite(binheader_ft, sizeof(char), BINHEADERLENGTH, fp);     fwrite(&(ft->nFrames), sizeof(int), 1, fp);    fwrite(&(ft->nFeatures), sizeof(int), 1, fp);    for (j = 0 ; j < ft->nFeatures ; j++)  {      for (i = 0 ; i < ft->nFrames ; i++)  {        _printFeatureBin(fp, ft->feature[j][i]);      }    }    fclose(fp);  }}static structureType _readHeader(  FILE *fp,  int *nFrames,  int *nFeatures,  KLT_BOOL *binary){#define LINELENGTH 100  char line[LINELENGTH];  structureType id;	  /* If file is binary, then read data and return */  fread(line, sizeof(char), BINHEADERLENGTH, fp);  line[BINHEADERLENGTH] = 0;  if (strcmp(line, binheader_fl) == 0)  {    assert(nFeatures != NULL);    fread(nFeatures, sizeof(int), 1, fp);    *binary = TRUE;    return FEATURE_LIST;  } else if (strcmp(line, binheader_fh) == 0)  {    assert(nFrames != NULL);    fread(nFrames, sizeof(int), 1, fp);    *binary = TRUE;    return FEATURE_HISTORY;  } else if (strcmp(line, binheader_ft) == 0)  {    assert(nFrames != NULL);    assert(nFeatures != NULL);    fread(nFrames, sizeof(int), 1, fp);    fread(nFeatures, sizeof(int), 1, fp);    *binary = TRUE;    return FEATURE_TABLE;    /* If file is NOT binary, then continue.*/  } else {    rewind(fp);    *binary = FALSE;  }  /* Skip comments until warning line */  while (strcmp(line, warning_line) != 0)  {    fgets(line, LINELENGTH, fp);    if (feof(fp))      KLTError("(_readFeatures) File is corrupted -- Couldn't find line:\n"               "\t%s\n", warning_line);  }  /* Read 'Feature List', 'Feature History', or 'Feature Table' */  while (fgetc(fp) != '-');  while (fgetc(fp) != '\n');  fgets(line, LINELENGTH, fp);  if (strcmp(line, "KLT Feature List\n") == 0) id = FEATURE_LIST;  else if (strcmp(line, "KLT Feature History\n") == 0) id = FEATURE_HISTORY;  else if (strcmp(line, "KLT Feature Table\n") == 0) id = FEATURE_TABLE;  else    KLTError("(_readFeatures) File is corrupted -- (Not 'KLT Feature List', "             "'KLT Feature History', or 'KLT Feature Table')");  /* If there's an incompatibility between the type of file */  /* and the parameters passed, exit now before we attempt */  /* to write to non-allocated memory.  Higher routine should */  /* detect and handle this error. */  if ((id == FEATURE_LIST && nFeatures == NULL) ||      (id == FEATURE_HISTORY && nFrames == NULL) ||      (id == FEATURE_TABLE && (nFeatures == NULL || nFrames == NULL)))    return id;  /* Read nFeatures and nFrames */  while (fgetc(fp) != '-');  while (fgetc(fp) != '\n');  fscanf(fp, "%s", line);  if (id == FEATURE_LIST)  {    if (strcmp(line, "nFeatures") != 0)      KLTError("(_readFeatures) File is corrupted -- "               "(Expected 'nFeatures', found '%s' instead)", line);  } else if (strcmp(line, "nFrames") != 0)    KLTError("(_readFeatures) File is corrupted -- "             "(Expected 'nFrames', found '%s' instead)", line);  fscanf(fp, "%s", line);  if (strcmp(line, "=") != 0)    KLTError("(_readFeatures) File is corrupted -- "             "(Expected '=', found '%s' instead)", line);  if (id == FEATURE_LIST) fscanf(fp, "%d", nFeatures);  else fscanf(fp, "%d", nFrames);  /* If 'Feature Table', then also get nFeatures */  if (id == FEATURE_TABLE)  {    fscanf(fp, "%s", line);    if (strcmp(line, ",") != 0)      KLTError("(_readFeatures) File '%s' is corrupted -- "               "(Expected 'comma', found '%s' instead)", line);    fscanf(fp, "%s", line);    if (strcmp(line, "nFeatures") != 0)      KLTError("(_readFeatures) File '%s' is corrupted -- "               "(2 Expected 'nFeatures ', found '%s' instead)", line);    fscanf(fp, "%s", line);    if (strcmp(line, "=") != 0)      KLTError("(_readFeatures) File '%s' is corrupted -- "               "(2 Expected '= ', found '%s' instead)", line);    fscanf(fp, "%d", nFeatures);  }  /* Skip junk before data */  while (fgetc(fp) != '-');  while (fgetc(fp) != '\n');  return id;#undef LINELENGTH}static void _readFeatureTxt(  FILE *fp,  KLT_Feature feat){  while (fgetc(fp) != '(');  fscanf(fp, "%f,%f)=%d", &(feat->x), &(feat->y), &(feat->val));}static void _readFeatureBin(  FILE *fp,  KLT_Feature feat){  fread(&(feat->x), sizeof(KLT_locType), 1, fp);  fread(&(feat->y), sizeof(KLT_locType), 1, fp);  fread(&(feat->val), sizeof(int), 1, fp);}/********************************************************************* * KLTReadFeatureList * KLTReadFeatureHistory * KLTReadFeatureTable * * If the first parameter (fl, fh, or ft) is NULL, then the  * corresponding structure is created. */KLT_FeatureList KLTReadFeatureList(  KLT_FeatureList fl_in,  char *fname){  FILE *fp;  KLT_FeatureList fl;  int nFeatures;  structureType id;  int indx;  KLT_BOOL binary; 		/* whether file is binary or text */  int i;  fp = fopen(fname, "rb");  if (fp == NULL)  KLTError("(KLTReadFeatureList) Can't open file '%s' "                            "for reading", fname);  if (KLT_verbose >= 1)     fprintf(stderr,  "(KLT) Reading feature list from '%s'\n", fname);  id = _readHeader(fp, NULL, &nFeatures, &binary);  if (id != FEATURE_LIST)     KLTError("(KLTReadFeatureList) File '%s' does not contain "             "a FeatureList", fname);  if (fl_in == NULL)  {    fl = KLTCreateFeatureList(nFeatures);    fl->nFeatures = nFeatures;  }  else  {    fl = fl_in;    if (fl->nFeatures != nFeatures)      KLTError("(KLTReadFeatureList) The feature list passed "               "does not contain the same number of features as "               "the feature list in file '%s' ", fname);  }  if (!binary) {  /* text file */    for (i = 0 ; i < fl->nFeatures ; i++)  {      fscanf(fp, "%d |", &indx);      if (indx != i) KLTError("(KLTReadFeatureList) Bad index at i = %d"                              "-- %d", i, indx);      _readFeatureTxt(fp, fl->feature[i]);    }  } else {  /* binary file */    for (i = 0 ; i < fl->nFeatures ; i++)  {      _readFeatureBin(fp, fl->feature[i]);    }  }  fclose(fp);  return fl;}KLT_FeatureHistory KLTReadFeatureHistory(  KLT_FeatureHistory fh_in,  char *fname){  FILE *fp;  KLT_FeatureHistory fh;  int nFrames;  structureType id;  int indx;  KLT_BOOL binary; 		/* whether file is binary or text */  int i;  fp = fopen(fname, "rb");  if (fp == NULL)  KLTError("(KLTReadFeatureHistory) Can't open file '%s' "                            "for reading", fname);  if (KLT_verbose >= 1) fprintf(stderr,  "(KLT) Reading feature history from '%s'\n", fname);  id = _readHeader(fp, &nFrames, NULL, &binary);  if (id != FEATURE_HISTORY) KLTError("(KLTReadFeatureHistory) File '%s' does not contain "                                      "a FeatureHistory", fname);  if (fh_in == NULL)  {    fh = KLTCreateFeatureHistory(nFrames);    fh->nFrames = nFrames;  }  else  {    fh = fh_in;    if (fh->nFrames != nFrames)      KLTError("(KLTReadFeatureHistory) The feature history passed "               "does not contain the same number of frames as "               "the feature history in file '%s' ", fname);  }  if (!binary) {  /* text file */    for (i = 0 ; i < fh->nFrames ; i++)  {      fscanf(fp, "%d |", &indx);      if (indx != i)         KLTError("(KLTReadFeatureHistory) Bad index at i = %d"                 "-- %d", i, indx);      _readFeatureTxt(fp, fh->feature[i]);    }  } else {  /* binary file */    for (i = 0 ; i < fh->nFrames ; i++)  {      _readFeatureBin(fp, fh->feature[i]);    }  }  fclose(fp);  return fh;}KLT_FeatureTable KLTReadFeatureTable(  KLT_FeatureTable ft_in,  char *fname){  FILE *fp;  KLT_FeatureTable ft;  int nFrames;  int nFeatures;  structureType id;  int indx;  KLT_BOOL binary; 		/* whether file is binary or text */  int i, j;  fp = fopen(fname, "rb");  if (fp == NULL)  KLTError("(KLTReadFeatureTable) Can't open file '%s' "                            "for reading", fname);  if (KLT_verbose >= 1) fprintf(stderr,  "(KLT) Reading feature table from '%s'\n", fname);  id = _readHeader(fp, &nFrames, &nFeatures, &binary);  if (id != FEATURE_TABLE) KLTError("(KLTReadFeatureTable) File '%s' does not contain "                                    "a FeatureTable", fname);  if (ft_in == NULL)  {    ft = KLTCreateFeatureTable(nFrames, nFeatures);    ft->nFrames = nFrames;    ft->nFeatures = nFeatures;  }  else  {    ft = ft_in;					    if (ft->nFrames != nFrames || ft->nFeatures != nFeatures)      KLTError("(KLTReadFeatureTable) The feature table passed "               "does not contain the same number of frames and "               "features as the feature table in file '%s' ", fname);  }  if (!binary) {  /* text file */    for (j = 0 ; j < ft->nFeatures ; j++)  {      fscanf(fp, "%d |", &indx);      if (indx != j)         KLTError("(KLTReadFeatureTable) Bad index at j = %d"                 "-- %d", j, indx);      for (i = 0 ; i < ft->nFrames ; i++)        _readFeatureTxt(fp, ft->feature[j][i]);    }  } else {  /* binary file */    for (j = 0 ; j < ft->nFeatures ; j++)  {      for (i = 0 ; i < ft->nFrames ; i++)        _readFeatureBin(fp, ft->feature[j][i]);    }  }  fclose(fp);  return ft;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -