Creating a buffer matrix for continuous measurements

I run programming in MATLAB, and I am having problems creating a buffer matrix. I am trying to do the following:

I constantly get an image from a webcam, and after segmentation I get a centroid of a moving target. I need to store centroid data for processing, but I do not want it to take up too much memory. For example, if I was time t=inf, I thought of storing 10 time points of data in a matrix, such as a circular buffer, and then writing and erasing old data, because I need to work with both, the actual data in time (t) and previous data in time ( t-1).

+3
source share
4 answers
buffSize = 10;
circBuff = nan(1,buffSize);
for newest = 1:1000;
    circBuff = [newest circBuff(1:end-1)]
end

I tested this, it does not take noticeable time to run in MATLAB. The profiler did not detect any bottlenecks with the code.

+7
source

UPDATE:

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

Initialize the buffer first:

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

. while loop , TRUE ( FALSE, ):

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

: ( ) centroidBuffer ( ), , .

, nBuffer, , :

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:

. , 10 , nBuffer 20, 10 , 10 , if :

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

, 10 ( centroidBuffer (:, 1:10)), 10 ( centroidBuffer. (:, 11:20))

+2

, . , , - :

circBuff (:,:, mod (counter, numFrames)) = newData; , , . .

, Dan

+2
centroidBuffer = [centroidBuffer(:,2:end) centroidData(:)];

, . , , Matlab , . , .

circBuff(:,:,mod(counter,numFrames)) = newData

, , .

, :

http://www.mathworks.com/matlabcentral/fileexchange/47025-circvbuf-m

- :

% create a circular vector buffer
    bufferSz = 1000;
    vectorLen= 7;
    cvbuf = circVBuf(int64(bufferSz),int64(vectorLen));

% fill buffer with 99 vectors
    vecs = zeros(99,vectorLen,'double');
    cvbuf.append(vecs);

% loop over lastly appended vectors of the circVBuf:
    new = cvbuf.new;
    lst = cvbuf.lst;
    for ix=new:lst
       vec(:) = cvbuf.raw(:,ix);
    end

% or direct array operation on lastly appended vectors in the buffer (no copy => fast)
    new = cvbuf.new;
    lst = cvbuf.lst;
    mean = mean(cvbuf.raw(3:7,new:lst));

, , , , , circVBuf .

. "" "" - , . enter image description here

+1

All Articles