简体   繁体   English

将原始编码的nrrd数据文件读入double

[英]Reading in raw encoded nrrd data file into double

Does anyone know how to read in a file with raw encoding? 有谁知道如何使用原始编码读取文件? So stumped.... I am trying to read in floats or doubles (I think). 太难受了。。。我想读浮点数或双打(我认为)。 I have been stuck on this for a few weeks. 我已经坚持了几个星期。 Thank you! 谢谢!

File that I am trying to read from: http://www.sci.utah.edu/~gk/DTI-data/gk2/gk2-rcc-mask.raw 我尝试从中读取的文件: http : //www.sci.utah.edu/~gk/DTI-data/gk2/gk2-rcc-mask.raw

Description of raw encoding: hello://teem.sourceforge.net/nrrd/format.html#encoding (change hello to http to go to page) - "raw" - The data appears on disk exactly the same as in memory, in terms of byte values and byte ordering. 原始编码的说明:hello://teem.sourceforge.net/nrrd/format.html#encoding(将hello更改为http转到页面)-“ raw”-数据在磁盘上的显示与在内存中的完全相同,在字节值和字节顺序的术语。 Produced by write() and fwrite(), suitable for read() or fread(). 由write()和fwrite()产生,适用于read()或fread()。

Info of file: http://www.sci.utah.edu/~gk/DTI-data/gk2/gk2-rcc-mask.nhdr - I think the only things that matter here are the big endian (still trying to understand what that means from google) and raw encoding. 文件信息: http : //www.sci.utah.edu/~gk/DTI-data/gk2/gk2-rcc-mask.nhdr-我认为这里唯一重要的是大字节序(仍然试图理解Google表示什么)和原始编码。

My current approach, uncertain if it's correct: 我目前的方法,不确定是否正确:

 //Function ripped off from example of c++ ifstream::read reference page

void scantensor(string filename){
    ifstream tdata(filename, ifstream::binary); // not sure if I should put ifstream::binary here

    // other things I tried
    // ifstream tdata(filename)  ifstream tdata(filename, ios::in)

    if(tdata){
            tdata.seekg(0, tdata.end);
            int length = tdata.tellg();
            tdata.seekg(0, tdata.beg);

            char* buffer = new char[length];

            tdata.read(buffer, length);

            tdata.close();

            double* d;
            d = (double*) buffer;

    } else cerr << "failed" << endl;
}

/*  P.S. I attempted to print the first 100 elements of the array.

    Then I print 100 other elements at some arbitrary array indices (i.e. 9,900 - 10,000).  I actually kept increasing the number of 0's until I ran out of bound at 100,000,000 (I don't think that's how it works lol but I was just playing around to see what happens)

    Here's the part that makes me suspicious: so the ifstream different has different constructors like the ones I tried above.

    the first 100 values are always the same.

    if I use ifstream::binary, then I get some values for the 100 arbitrary printing
    if I use the other two options, then I get -6.27744e+066 for all 100 of them

    So for now I am going to assume that ifstream::binary is the correct one.  The thing is, I am not sure if the file I provided is how binary files actually look like.  I am also unsure if these are the actual numbers that I am supposed to read in or just casting gone wrong.  I do realize that my casting from char* to double* can be unsafe, and I got that from one of the threads.

*/

I really appreciate it! 我真的很感激!

Edit 1: Right now the data being read in using the above method is apparently "incorrect" since in paraview the values are: 编辑1:现在,使用上述方法读取的数据显然是“错误的”,因为在paraview中,这些值是:

Dxx,Dxy,Dxz,Dyy,Dyz,Dzz
[0, 1], [-15.4006, 13.2248], [-5.32436, 5.39517], [-5.32915, 5.96026], [-17.87, 19.0954], [-6.02961, 5.24771], [-13.9861, 14.0524]

It's a 3 x 3 symmetric matrix, so 7 distinct values, 7 ranges of values.

The floats that I am currently parsing from the file right now are very large (ie -4.68855e-229, -1.32351e+120). 我目前正在从文件中解析的浮点数非常大(即-4.68855e-229,-1.32351e + 120)。

Perhaps somebody knows how to extract the floats from Paraview? 也许有人知道如何从Paraview中提取花车?

Since you want to work with doubles, I recommend to read the data from file as buffer of doubles: 由于您要使用双打,因此建议从文件中读取数据作为双打的缓冲区:

const long machineMemory = 0x40000000; // 1 GB

FILE* file = fopen("c:\\data.bin", "rb");

if (file)
{
    int size = machineMemory / sizeof(double);

    if (size > 0)
    {
      double* data = new double[size];

      int read(0);
      while (read = fread(data, sizeof(double), size, file))
      {
         // Process data here (read = number of doubles)
      }

      delete [] data;
   }

   fclose(file);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM