在Matlab中存储固定大小和数据类型的多个图像的所有可能且节省内存的方法有哪些? [英] What are all the possible and memory efficient ways to store multiple images of fixed size and data types in Matlab?

查看:218
本文介绍了在Matlab中存储固定大小和数据类型的多个图像的所有可能且节省内存的方法有哪些?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解最节省内存的方法,以便在Matlab中存储相同大小的多个图像.

I am trying to understand the most memory efficient ways to store multiple images of the same size in Matlab.

我在这里写的所有内容都是基于我的小知识,可能并不准确.

Everything I write here is based on my little knowledge and may not be accurate.

到目前为止,我知道我们可以读取单元格数组,多维数组和结构中的图像.

So far, I know that we can read images in cell arrays, multi-dimensional arrays, structs.

num_imgs = 100;
nrow = 512;
ncol = 512;
cellArray = cell(1,num_imgs);
cellArray(1,:) = {zeros(nrow,ncol,'logical')};
threeDArray = zeros(nrow,ncol,num_imgs,'logical');
structArray(1:num_imgs ) = struct('Image', zeros(nrow,ncol,'logical'));

Name               Size                    Bytes  Class      Attributes

  cellArray          1x100                26225600  cell                 
  ncol               1x1                         8  double               
  nrow               1x1                         8  double               
  num_imgs           1x1                         8  double               
  structArray        1x100                26225664  struct               
  threeDArray      512x512x100            26214400  logical       

threeDArray开始的

更好,因为它不需要连续数组的任何指针.

From this threeDArray is better as it doesn't need any pointers for consecutive arrays.

cellArray是第二好的,因为它只需要为每个额外的8字节数组(即多100 * 8字节)分配指针.

whereas, cellArray is second best as it only needs pointers for each array of 8 Bytes extra (ie, 100*8 bytes more).

最后,我认为根据每个字段的标签,结构需要多一点.

Finally, structs need a little bit more depending on tag of each field, I think.

现在

还有其他可行的方法吗?

Are there any other possible ways to do this.

哪种方式最有效地利用内存来读取,写入以及影响代码性能的其他重要参数?

Which is the most memory efficient way for reading, writing and any other important parameters that affect the performance of your code?

我了解单元格具有指针,因此cellArray中的每个元素都不必连续存储在内存中,而threeDArray需要连续内存.

I understand that cells have pointers so each element in cellArray doesn't have to be stored contiguously in memory, whereas threeDArray needs contiguous memory.

有人可以详细解释影响性能的这种因素吗?

Can someone explain this kind of factors which affect the performance in detail?

推荐答案

cellArray是第二好的,因为它只需要为每个额外的8字节数组(即多100 * 8字节)分配指针.

cellArray is second best as it only needs pointers for each array of 8 Bytes extra (ie, 100*8 bytes more).

这不是事实.每个数组都有一个标头"(指定其类型,大小等的内存块).R2017a中的标头为104字节(我认为在最新版本中它更大一些).单元阵列可容纳阵列,因此您在3D阵列测试中看到的差异:

This is not true. Each array has a "header" (a block of memory that specifies its type, size, etc.) The header in R2017a is 104 bytes (I think it's a little larger in the latest release). A cell array holds arrays, so the difference you see in your test with the 3D array:

26225600 - 26214400 = 11200

100 * (104 + 8) = 11200

单元格数组是一个指向数组(每个字节104个字节及其数据的指针)的指针数组(每个8个字节).

The cell array is an array of pointers (8 bytes each) to arrays (104 bytes + whatever their data is).

对于图像(它是一个相当大的数据块),这112字节的开销可以忽略不计.其他考虑因素,例如访问速度,变得更加重要.

For an image, which is a fairly large block of data, this 112 byte overhead is negligible. Other considerations, such as speed of access, become more important.

在MATLAB中,两个数组可以指向相同的数据.所以做类似的事情

In MATLAB, two arrays can point to the same data. So doing something like

I = C{4};

不会在C{4}创建数组的副本,而是数组I引用它.但是,如果您使用3D阵列,则:

doesn't create a copy of the array at C{4}, instead the array I references it. But if you use a 3D array, then:

I = A(:,:,4);

进行复制是因为I无法引用另一个数组的子集,它必须引用整个对象.

does make a copy because I cannot reference a subset of another array, it must reference the whole thing.

因此,使用3D阵列处理单个图像需要大量来回复制像素数据,而这在单元格阵列中是不必要的.

Thus, using a 3D array, processing individual images requires a lot of copying back and forth of pixel data, which would not be necessary in a cell array.

这里的struct数组不是相关的数据结构,它等效于cell数组,除了索引更为复杂(我不知道这是否转换为运行时增加).也就是说,S(4).ImageC{4}涉及更多.但是,如果要为每个图像存储其他信息,则结构数组可能会有用.

A struct array is not a relevant data structure here, it would be equivalent to the cell array, except the indexing is more complicated (I don't know if this translates to runtime increase or not). That is, S(4).Image is more involved than C{4}. However, if you want to store additional information for each image, a struct array could be useful.

正如您所注意到的,struct数组仅比cell数组大64个字节.这将存储字段名称Image.同样,担心这种内存量并不是很值得.

As you noticed, the struct array is only 64 bytes larger than the cell array. This stores the field name Image. Again, not really worth while worrying about this amount of memory.

以下是在MATLAB中处理数据的其他方法的简短摘要,对我而言,这些方法似乎都不合理:

Here is a short summary of other ways to handle data in MATLAB, none of which seem reasonable to me:

  • 自定义对象类型:在这里您仍在处理下面的普通数组,因此这里没有优点或缺点.如果要添加特定于图像的方法,但不更改处理内存的方式,则这些类型非常有用.它们似乎确实增加了一些时间开销.

  • Custom object type: here you are still dealing with normal arrays underneath, so there is no advantage or disadvantage here. These types are nice if you want to add methods specific to your images, but don't change the way memory is handled. They do seem to add some time overhead.

使用高阵列,适用于非常大的阵列内存中无法容纳的数据,但我认为没有人会考虑使用这种数组进行图像分析.

Use tall arrays, suitable for very large data that doesn't fit in memory, but I don't think anybody would consider doing image analysis with such an array.

使用内存映射文件,对加快文件访问速度很有用,但在这种情况下并没有帮助.

Use memory-mapped files, useful to speed up file access, but doesn't really help in this case.

从MATLAB与Java或Python对话,并让他们进行内存处理.但是您最好还是完全跳过MATLAB并转到其他环境.

Talk to Java or Python from MATLAB, and have them do the memory handling. But then you might as well skip MATLAB altogether and go to a different environment.

所以我真的认为处理多个图像的两个有意义的选择是单元格数组(或其他异构容器,例如结构或自定义对象)或3D数组.我什么也不会考虑.

So I really think that the two meaningful options for handling multiple images are either a cell array (or other heterogeneous container such as a struct or custom object), or a 3D array. I would not consider anything else.

总结:使用单元格数组.

这篇关于在Matlab中存储固定大小和数据类型的多个图像的所有可能且节省内存的方法有哪些?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆