I am trying to achieve a very simple thing in MATLAB. I have a sequence of images in a 4D array in the following format: (framenumber, height, width, RGB).
My stack is quite small, it’s size is: 99 480 640 3
What I am trying to achieve is to detect if there was a change in motion between n consequtive images (the images taken with a fixed camera about moving objects).
For this, my most obvious idea is to subtract different images in the stack, and keep them in an other stack.
Here is the very simple code I tried to run:
vec = 1:size(stack,1)-5
dif = uint8(squeeze(abs(stack(vec,:,:,:) - stack(vec+5,:,:,:))));
However it gives me out of memory error.
Error using -
Out of memory. Type HELP MEMORY for your options.
If I don’t use squeeze (do I need to use squeeze here?) I even get a windows system error about memory being low.
As a fallback I wrote a for loop, which seems to work, but only if I use an uint8 cast. If not, it runs out of memory and gives windows system error.
for i = 1:size(stack,1)-5
dif(i,:,:,:) = uint8(abs(stack(i,:,:,:) - stack(i+5,:,:,:)));
end
Also, it takes 20 seconds to process this not so big dataset (only 99 frames).
Can you tell me how should I write a function which doesn’t try to keep everything in memory? This command should only require a very little memory for the 2 images being compared, however MATLAB tries to do all the operations in memory for some reason. The whole dataset should be 91 MB in memory so it clearly shouldn’t be a problem.
Also, can you explain if I need to use squeeze or not? When I try to get a single image, then
dif = stack(1,:,:,:) - stack(5,:,:,:);
produces a 4D array, while
dif = squeeze(stack(1,:,:,:) - stack(5,:,:,:));
produces an image format. But in the for loop above, it produces the same results both with and without squeeze. Why is this happening?
Regarding SQUEEZE,
stackis a 4D matrix, sodif = stack(1,:,:,:) - stack(5,:,:,:);is also a 4D matrix, but the first dimension is singleton. So when you assign the result into an existing matrix (with indexing), the elements gets filled in order, and any singleton dimensions have no effect. However, putting the result into a new variable, the shape of the 4D matrix is maintained…You should be aware of your data types, if all values are integers in the [0,255] range, then cast the whole stack of images as UINT8 to begin with. As I mentioned in the comments, a 4D double matrix of such dimensions would require around 700MB of contiguous memory.
Now in the for-loop, you should preallocate the
difmatrix:Finally eventhough vectorized calls could be faster, they do require more memory as they try to allocate the entire space needed at one (as opposed to using a loop which only needs space to hold one single image at-a-time)