Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a buffer matrix for continuous measurements

I'm starting programming in MATLAB and I have some problems creating a buffer matrix. I'm trying to do the following:

I'm continuously obtaining an image from a webcam and after segmentation I obtain the centroid of a moving target. I need to store the centroid data for processing but I don't want it to occupy too much memory. For example, if I was a time t=inf, I was thinking of storing 10 time points of data in a matrix, like a circular buffer, then writing and erasing the older data, because I need work with both, the actual data in time (t) and a previous data in time (t-1).

like image 752
carlos Avatar asked Apr 16 '26 07:04

carlos


2 Answers

buffSize = 10;
circBuff = nan(1,buffSize);
for newest = 1:1000;
    circBuff = [newest circBuff(1:end-1)]
end

I have tested this, it takes no appreciable time to run in MATLAB. The profiler did not find any bottlenecks with the code.

like image 88
MatlabDoug Avatar answered Apr 19 '26 12:04

MatlabDoug


UPDATE:

Since I now understand that you need a circular buffer to store data, here's a solution you can use. Since you said you were storing centroid data of objects in an image, I will give you a general case for storing an arbitrary number of measurements (either 1 pixel index value for each centroid, or 2 values for x and y coordinates, etc.)...

First, initialize the buffer:

nBuffer = 10;  % You can set this to whatever number of time points
               %   you want to store data for
nSamples = 2;  % You can set this to the number of data values you
               %   need for each point in time
centroidBuffer = zeros(nSamples,nBuffer);  % Initialize the buffer to zeroes

Next, you will have your continuous loop. You can use a while loop and a flag variable that initially has the value TRUE (and which you can set to FALSE to stop the loop):

keepLooping = true;
while keepLooping,
  % Capture your image
  % Compute the centroid data and place it in the vector "centroidData"
  centroidBuffer = [centroidBuffer(:,2:end) centroidData(:)];
  % Do whatever processing you want to do on centroidBuffer
  % Choose to set keepLooping to false, if you want
end

This works in the following way: at each time point, the first column (i.e. the oldest data) in centroidBuffer is removed and a new column (i.e. the new data) is appended to the end. In this way, the buffer matrix is always the same size.

If you don't want to perform your processing at every time step, but instead only after every nBuffer time points so that it is operating on a new set of data each time, then replace the above code with the following:

keepLooping = true;
processTime = 0;
while keepLooping,
  % Capture your image
  % Compute the centroid data and place it in the vector "centroidData"
  centroidBuffer = [centroidBuffer(:,2:end) centroidData(:)];
  processTime = processTime+1;
  if (processTime == nBuffer),
    % Do whatever processing you want to do on centroidBuffer
    processTime = 0;
  end
  % Choose to set keepLooping to false, if you want
end

EDIT:

There are a number of variations you can make with the above code. For example, if you want to store two sets of data with 10 time points each, you can change nBuffer to 20 to store the old set in the first 10 columns and the new set in the last 10 columns. Then, change the if statement to:

  ...
  if (processTime == nBuffer/2),
  ...

And now you can perform your processing using both the older set of 10 data points (in centroidBuffer(:,1:10)) and the newer set of 10 data points (in centroidBuffer(:,11:20)).

like image 39
gnovice Avatar answered Apr 19 '26 12:04

gnovice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!