Image acquisition toolbox: Predict how many imgs can be buffered in RAM?

5 views (last 30 days)
Hello,
I am looking for a way to predict how many frames (approx.) I can capture to the RAM of my computer using the image processing toolbox. I would prefer this over logging to HD.
Is that somehow possible by knowing the size of a frame and knowing the free memory (how...?) Thank you!!
Error event occurred at 14:17:15 for video input object: Mono8-gentl-1.
Unable to allocate memory for an incoming image frame due to insufficient free physical memory.
  2 Comments
William Thielicke
William Thielicke on 12 Aug 2022
I am trying to monitor the memory during image acqusition, but there is no logic that I can see.
During capture I am printing this in a loop until the out of memory error occurs:
[bla blubb]=memory;
disp('__')
disp(['mem avail: ' num2str(bla.MemAvailableAllArrays / 1024 /1024/1024)])
disp(['mem matlab: ' num2str(bla.MemUsedMATLAB / 1024 /1024/1024)])
disp(['mem all array: ' num2str(bla.MemAvailableAllArrays / 1024 /1024/1024)])
disp(['mem system avail: ' num2str(blubb.SystemMemory.Available / 1024 /1024/1024)])
The first print out says:
mem avail: 15.4484
mem matlab: 3.986
mem all array: 15.4484
mem system avail: 15.4484
And the last before he error says:
mem avail: 7.8086
mem matlab: 11.6643
mem all array: 7.8086
mem system avail: 7.8086
It doesn't seem that I could use these numbers to predict when the out-of-memory error occurs?
William Thielicke
William Thielicke on 12 Aug 2022
Oh well, maybe this works?
%before the acqusition:
initialAvailableMemory = bla.MemAvailableAllArrays - bla.MemUsedMATLAB
%during acquisition:
remainingMemory = initialAvailableMemory - bla.MemUsedMATLAB
if remainingMemory < X
stop image acquisition %(but how....)
end

Sign in to comment.

Accepted Answer

William Thielicke
William Thielicke on 4 Oct 2023
Edited: William Thielicke on 4 Oct 2023
I found a solution. Here is the function that I use:
function [max_possible_images] = PIVlab_capture_max_possible_images(ROI,bitmode)
if bitmode>8
bitmode=16; %even if data from camera is only 10 or 12 bit, the datatype it will be stored in is uint16.
end
[~,systemview] = memory;
mem_free=systemview.PhysicalMemory.Available/1024^3;
ram_reserve=1; %how much RAM (in GB) is needed for Matlab operations not including image capture
% twice the RAM is needed to use getdata, because getdata creates a copy of the array (why.....!?!?!?)
max_possible_images=floor((mem_free-ram_reserve) / (ROI(3)*ROI(4)*bitmode/8/1024^3 *2)); %max possible images
ROI is the region of interest of the images in x,y, width, height. Bitmode indicates if the camera captures in 8 bit or more. This seems to work pretty good.
The key was to use systemview.PhysicalMemory.Available, which is identical to the numbers that windows taskmanager shows. Only this number seems to make sense, all the other numbers seem to be unrelated to the problem (at least to me).

More Answers (1)

Siraj
Siraj on 28 Sep 2023
Hi!
I understand that you would like to determine the maximum number of images you can load into MATLAB's memory without encountering insufficient free physical memory. You are seeking a way to predict this limit beforehand.
Based on my understanding, accurately determining the exact number of images that can be loaded into MATLAB's memory is challenging. However, you can use the "whos" function to find the size of a single image and then divide the "MemAvailableAllArrays" by the memory required by one image. This will give you an approximate number of images that can be loaded. Keep in mind that this approach does not account for the processing overhead that may consume additional memory when loading images into MATLAB.
To learn more about the "whos" function in MATLAB, you can refer to the following link:
Refer to the following code for better understanding.
% Read an image into MATLAB
image = imread("Images/random_image_01.png");
% Get the memory usage of the image
image_memory = whos("image").bytes;
% Check the available memory
[user,sys] = memory;
% Calculate the approximate number of images that can be loaded
num_of_images = user.MemAvailableAllArrays / image_memory
Additionally, I suggest trying the following suggestions to see if you can load your entire dataset without exhausting the available memory:
Check that the Java Heap Memory is not set to a very large value because that might restrict the amount of memory available to perform computations.
I recommend using an "ImageDatastore" object when dealing with a large collection of images that may not fit entirely in memory. The "ImageDatastore" object allows you to efficiently work with image data by loading and processing images in batches.
To learn more about the "ImageDatastore" object and its functionality in MATLAB, you can refer to the following link:
Hope this helps.

Tags

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!