mad
mad

Reputation: 2789

Maximum variable size allowed by the program is exceeded-MATLAB

I saw some similar issues in this forum but I didn't find a real solution to this problem.

I have the following matlab code, in which I work with very big images (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));

every time I run the code with specific regions matlab returns the error: Maximum variable size allowed by the program is exceeded.

I'm running the code with 48GB of RAM in my colllege's cluster. The problem starts only in region number 43 and below. The other regions run ok.

Is there a smart way for me to run this code?

Upvotes: 1

Views: 2888

Answers (1)

Floris
Floris

Reputation: 46375

I believe the problem is in your use of

image(x_region,y_region,1)

I suspect that you think that accesses the N elements of the region; but in fact, you access NxN elements! For a large region, that can easily blow up on you. In general, A(vec1, vec2) creates a section of numel(vec1) by numel(vec2). To solve this, you need to use the sub2ind function to find just the indices you need (or use a single parameter in your find command, and shape your matrix accordingly):

% 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));

The second option does make a complete copy of the image, so option 1 is probably quicker.

Upvotes: 3

Related Questions