【问题标题】:Maximum variable size allowed by the program is exceeded-MATLAB超出程序允许的最大变量大小-MATLAB
【发布时间】:2013-06-12 21:04:04
【问题描述】:

我在这个论坛上看到了一些类似的问题,但我没有找到真正解决这个问题的方法。

我有以下 matlab 代码,我在其中处理非常大的图像 (182MP):

%step 1: read the image
image=single(imread('image.tif'));

%step 2: read the image segmentation
regions=imread('image_segmentation.ppm');

%step 3: count the number of segments
number_of_regions=numel(unique(regions));

%step 4: regions label
regions_label=unique(regions);

for i=1:number_of_regions

    %pick the pixel indexes of the i'th region
    [x_region,y_region]=find(regions==label_regions(i));

    %the problem starts here

   ndvi_region=(image(x_region,y_region,1)-image(x_region,y_region,3))./(imagem(x_region,y_region,1)+image(x_region,y_region,3));

每次我使用特定区域运行代码时,matlab 都会返回错误:超出程序允许的最大变量大小。

我正在我学院的集群中运行具有 48GB RAM 的代码。问题仅在区域编号 43 及以下开始。其他地区运行正常。

我有什么聪明的方法可以运行这段代码吗?

【问题讨论】:

  • 您确定需要single 吗?即,通常Matlab使用int8int16,将内存使用量减少4(或2)倍......image.tif有多大(以像素为单位)?
  • 是的,我真的需要单身,计算 ndvi_region 至关重要。 image.tif 有 182 兆像素,它是一张遥感图像。感谢您的回复。
  • 这就是 Matlab 对"What is the maximum matrix size for each platform?" 的评价。 48GB RAM 的物理存在并不意味着 Matlab 可以访问所有这些。
  • 我还发现this answer by Rody Oldenhuis 对您的问题很感兴趣。
  • @Schorsch 感谢您的 cmets,它也有帮助。

标签: image matlab


【解决方案1】:

我相信问题出在你的使用上

image(x_region,y_region,1)

我怀疑您认为访问了该区域的 N 个元素;但实际上,您访问了 NxN 个元素!对于一个大区域,这很容易对你造成影响。一般来说,A(vec1, vec2) 通过 numel(vec2) 创建 numel(vec1) 的一部分。为了解决这个问题,您需要使用 sub2ind 函数来查找您需要的索引(或在您的 find 命令中使用单个参数,并相应地调整您的矩阵):

% option 1
[x_region, y_region]=find(regions==label_regions(i));
indx1 = sub2ind(size(image), x_region, y_region, 1*ones(size(x_region)));
indx3 = sub2ind(size(image), x_region, y_region, 3*ones(size(x_region)));
ndvi_region = (image(indx1) - image(indx3))./(image(indx1) + image(indx3));

% option 2
indx = find(regions==label_regions(i));
r_image = reshape(image, [], 3); % assuming you have XxYx3 image
ndvi_region = (r_image(indx, 1) - r_image(indx, 3))./(r_image(indx,1) + r_image(indx, 3));

第二个选项确实制作了图像的完整副本,因此选项 1 可能更快。

【讨论】:

  • 我尝试了您的解决方案,但 matlab 给出了错误:使用 sub2ind 时出错下标向量必须全部具有相同的大小。我会尝试修复它并给你一个反馈。谢谢。
  • 当然 - 对不起。在没有获得 Matlab 许可证的情况下这样做。我相信我最近的编辑修复了它。
  • 这适用于 ndvi_region,谢谢,但我还有一个问题:我也有一个地面实况二进制图像。我可以使用 indx1、indx2 访问它还是按照您的解释创建一个新索引?再次感谢您。
  • 很高兴听到这个消息!至于你的新问题:如果它是二进制图像,它可能没有第三维。我认为您应该能够使用第二个选项中的indx 而无需重塑(当您使用单个索引来索引二维数组时,它将“自动”工作)。我相信你会想出来的。
  • @mad:+1!在最坏的情况下(如果x_regiony_region 中没有重复,这将导致内存中整个图像的重复。鉴于它是一个相当大的图像,结合考虑到 Matlab 中内存限制的工作原理,这确实很可能是原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-02
  • 2022-07-12
  • 1970-01-01
  • 1970-01-01
  • 2022-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多