0

我正在使用带有 bsq 类型的交错(带顺序)的 ENVI 图像文件(.hdr 标签)。我正在尝试对大约 350MB 大的图像进行主成分转换。为了做到这一点,我必须在较小的内存块中单独处理它。如果文件只能在一块内存中使用,则下面的代码将按预期工作。但是,如果需要多次迭代,则只有最后一个块按预期出现,即,所有先前的块都被写入,就好像它们一遍又一遍地重复相同像素一样。

extern void pca_xform(envi_header file){

    #define BLOCK_SIZE 3000000

          /*Calculates covariance_matrix, eigen_matrix, mean_vector 
    and allocates pixel_vector and xform_pixel_vector*/

      int size = (file.lines)*(file.samples), i;

      printf("Transforming\n");

      int n = size/BLOCK_SIZE;
      int r = size - ((BLOCK_SIZE)*n);
      int block;

      for(i=0,block=0;i<n+1;++i,++block){
        int actual_size = BLOCK_SIZE;
        if(i==n){
          actual_size = r;
        }
        int k;
        io_bsq_read_pixel(file, i*BLOCK_SIZE, actual_size, pixel_vector);
        for(k=0;k<actual_size;++k){
          pca_normalize_pixel_vector(mean_vector, pixel_vector[k]);
        }
        for(k=0;k<actual_size;++k){
          pca_xform_pixel_vector(file, eigen_matrix, pixel_vector[k], xform_pixel_vector[k]);
        }
        io_bsq_write_pixel(file, i*BLOCK_SIZE, actual_size, xform_pixel_vector);
      }

      return;
    }

这里是写功能。

extern void io_bsq_write_pixel(envi_header file, int start, int number, gsl_vector** pixel_vector){
  FILE* fp = fopen(file.new_filename, "wb");
  if(!fp){
    fprintf(stderr, "Failed to open target: %s\n", file.new_filename);
    exit(EXIT_FAILURE);
  }
  int size = (file.samples) * (file.lines);
  int i, j;
  double d;
  for(i=0;i<file.bands;++i){
    fseek(fp, sizeof(double)*((size*i)+start), SEEK_SET);
    for(j=0;j<number;++j){
      d = gsl_vector_get(pixel_vector[j], i);
      fwrite(&d, sizeof(double), 1, fp);
    }
  }
  fclose(fp);
  return;
}

我得出的结论是,意外行为是由于该函数本身或在 pca_xform 函数中对它的一些不当调用所致。为此,我简单地使用了以下代码 insted,它按顺序写入像素(bip 交错)。

for(i=0;i<size:++i){
   gsl_vector_fwrite(fp, xform_pixel_vector[i]);
 }

但是,我想将我的输出文件保留为带顺序文件。我花了很多时间试图找到解决方案,在这里和那里调整代码,但问题的解决方案仍然让我望而却步。

4

1 回答 1

0

我终于设法找到了问题的根源。现在的问题是,因为我倾向于相信文件中书写程序的位置。然而,它仍然在io_bsq_write_pixel. 每次我调用该函数时,它都会随后调用fwrite(file.new_filename, "w"). 以“w”模式打开文件会删除我之前的所有工作。为了解决这个问题,我在调用之前初始化了文件pca_xform,然后更改了io_bsq_write_pixel

    extern void io_bsq_write_pixel(envi_header file, int start, int number, gsl_vector** pixel_vector){
  FILE* fp = fopen(file.new_filename, "r+b");
  if(!fp){
    fprintf(stderr, "Failed to open target: %s\n", file.new_filename);
    exit(EXIT_FAILURE);
  }
  int size = (file.samples) * (file.lines);
  int i, j;
  double d;
  for(i=0;i<file.bands;++i){
    fseek(fp, sizeof(double)*((size*i)+start), SEEK_SET);
    for(j=0;j<number;++j){
      d = gsl_vector_get(pixel_vector[j], i);
      fwrite(&d, sizeof(double), 1, fp);
    }
  }
  fclose(fp);
  return;
}

这样,使用“r+b”并首先初始化文件,我保证它将打开文件进行简单更新,而不会删除我以前的工作。

于 2016-04-06T22:23:46.843 回答