简体   繁体   English

将数据集文件(十六进制值)读取到内存块中(第2部分)

[英]Reading a dataset file(Hex values) onto a block of memory-part 2

I have a block of code that is trying to read the data from a dataset on to a randomly allocated block of memory. 我有一段代码试图从数据集中读取数据到随机分配的内存块中。 I don't know what exactly is inside the dataset but they access matrix values(Hex values) and put on to a memory location. 我不知道数据集中到底有什么,但是它们访问矩阵值(十六进制值)并放在存储位置。 And it works perfectly fine! 而且效果很好!

const unsigned int img_size = numel_img * sizeof(float);// (1248*960)4bytes= 4.79MB   
for (unsigned int i=0; i<p_rctheader->glb_numImg; ++i)// 0 to 496(Total no of images)
{
    const unsigned int cur_proj = i; // absolute projection number

    // read proj mx
    double* const pProjMx = pProjMatrixBuffers + cur_proj * 12;
    ifsData.read((char*) (pProjMx), 12 * sizeof(double));
    ifsData.seekg(img_size, ios::cur); 
        }

where pProjMatrixBuffers is pProjMatrixBuffers在哪里

double** pProjMatrixBuffers = new double* [rctheader.glb_numImg];   //
pProjMatrixBuffers[0] = new double[rctheader.glb_numImg * 12];  //
for (unsigned int i=1; i<rctheader.glb_numImg; ++i) {
    pProjMatrixBuffers[i] = pProjMatrixBuffers[0] + i * 12;
}

There is a another read operation after this : 此后还有另一个读取操作:

rctgdata.adv_pProjBuffers = new float* [num_proj_buffers];// 124 buffers
rctgdata.adv_pProjBuffers[0] = new float[num_proj_buffers * numel_img];// (1.198MB per image*124)*4bytes
    // set it to zero
memset(rctgdata.adv_pProjBuffers[0], 0, num_proj_buffers * numel_img * sizeof(float));
for (unsigned int i=1; i<num_proj_buffers; ++i) {
rctgdata.adv_pProjBuffers[i] = rctgdata.adv_pProjBuffers[0] + i * numel_img;
}

for (unsigned int i=0; i<numProjsInIteration; ++i)// (0 to 124)
{
const unsigned int cur_proj = numProcessedProjs + i; // absolute projection number// 0+124->124+124->248+124->372+124

// read proj mx
ifsData.read((char*) (pProjMatrixBuffers[cur_proj]), 12 * sizeof(double));
 // read image data
ifsData.read((char*) (rctgdata.adv_pProjBuffers[i]), numel_img * sizeof(float));
}

** * ** * EDITS * ** * ** * ** * ** * ** * ** * ** * ** * ** * Basically this code, reads Projection matrix from the dataset which is 12 doubles followed by 1248*960 image pixels.(floats). ** * ** * 编辑 * ** * ** * ** * ** * ** * ** * ** * ** * ** *基本上,该代码从数据集中读取Projection矩阵,其后为12倍1248 * 960图像像素。 This goes on for 124 times inside for loop. 这在for循环中进行了124次。 Q1.If you see in the above code, pProjMatrixBuffers[cur_proj] is read twice, which could have been done once. Q1。如果您在上面的代码中看到,则pProjMatrixBuffers [cur_proj]被读取了两次,这本可以执行一次。 (Correct me if I am wrong). (如果我错了,请纠正我)。 Q2.How will rctgdata.adv_pProjBuffers[i] know where to start accessing the data from in the dataset? Q2。rctgdata.adv_pProjBuffers [i]如何知道从哪里开始从数据集中访问数据? I mean location in the dataset. 我的意思是在数据集中的位置。 I am sorry if I have confused you. 如果您感到困惑,我很抱歉。 Please ask me for more information if needed. 如果需要,请询问我更多信息。 Thank you so much for all the help in advance!! 非常感谢您提前提供的所有帮助!!

To answer Q2, you allocate one big block of memory with new double[header.numImg * 12] , and you also allocate a bunch of row pointers with new double* [header.numImg] . 要回答第二个问题,您可以使用new double[header.numImg * 12]分配一个大的内存块,还使用new double* [header.numImg]分配一堆行指针。 The first row pointer [0] points at the beginning of the memory (because it was used in the new call). 第一行指针[0]指向内存的开头(因为在new调用中使用了它)。 The for loop then sets each row pointer [i] to point into the big block at 12-item increments (so each row should have 12 items in it). 然后,for循环将每个行指针[i]设置为以12个项目的增量指向大块(因此,每行中应包含12个项目)。 So for instance [1] points at the 12th item in the big block, [2] points at the 24th item, etc. 因此,例如[1]指向大块中的第12个项目,[2]指向第24个项目,依此类推。

I haven't quite figured out what you mean by Q1 yet. 我还不太清楚您对第一季度的意思。

There is no way a 2-dimensional MxN-array can be allocated as such using new. 不能使用new这样分配二维MxN数组。 The workaround in this code consists of an allocation of a 1-dimensional array of M pointers and another allocation of an array for the MxN elements. 此代码中的解决方法包括分配一个M指针的一维数组和另一个分配给MxN元素的数组。 Then the M pointers are set to point to the M first elements of each row within the array for the elements. 然后,将M个指针设置为指向该元素数组中每一行的M个第一元素。

Here we have two 2-dimensional arrays which I call (for obvious reasons) D and F. It's not clear how big D is - what's the value of rctheader.glb_numImg? 在这里,我们有两个二维数组(由于明显的原因),它们称为D和F。尚不清楚D有多大-rctheader.glb_numImg的值是多少?

The first loop reads 12 doubles into a row of D and skips the float data for a row of F, doing a seekg with the appropriate positive offset to be added to the current position (ie, forward). 第一个循环读取12,将其加倍成D行,并跳过F行的float数据,以适当的正偏移量进行搜索,以将其添加到当前位置(即,向前)。 This is done rctheader.glb_numImg times. 这完成了rctheader.glb_numImg次。

There is something I don't see in this code: a single seekg back to the beginning of the file, after the first loop and before the second loop. 我在这段代码中看不到某些东西:在第一个循环之后和第二个循环之前,一个单向搜索返回到文件的开头。

The second loop reads (once more) 12 doubles for each of the 124 rows and then, in one fell swoop, 1248*960 floats for each row. 第二个循环为124行中的每一行读取(一次以上)12次双倍,然后一次下降一次,为每行1248 * 960浮点数。 There is no need to reposition after these reads since the data for the second image immediately follows the data for the first image, and so on. 这些读取之后无需重新定位,因为第二个图像的数据紧随第一个图像的数据,依此类推。 (It's slightly irritating that num_proj_buffers and numProjsInIteration should have the same value, ie, 124.) (num_proj_buffers和numProjsInIteration应该具有相同的值,即124,这有点令人讨厌。)

It looks as if the second read loop would re-read what the first loop read. 似乎第二个读取循环将重新读取第一个循环读取的内容。 But since I don't know for sure that p_rctheader->glb_numImg is also 124, I can't really confirm that. 但是由于我不确定p_rctheader-> glb_numImg是否也是124,因此我无法真正确认。

Calculating the size of what is read by 123 iterations of the second loop as 计算第二个循环的123次迭代读取的内容的大小为

(1248*960*4 + 12*8)*124

this would account for ~0.5 GB - but the file size was reported as being ~2.5 GB. 这将占〜0.5 GB-但据报告文件大小为〜2.5 GB。

Also note that one index within the second loop is computed as 另请注意,第二个循环中的一个索引计算为

unsigned int cur_proj = numProcessedProjs + i;

but the initial setting of numProcessedProjs is unclear. 但是numProcessedProjs的初始设置不清楚。

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

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