アップデート:
データを格納するには循環バッファーが必要であることを理解したので、使用できるソリューションを次に示します。オブジェクトの重心データを画像に保存していると言ったので、任意の数の測定値を保存する一般的なケースを示します (各重心に対して 1 つのピクセル インデックス値、または x 座標と y 座標に対して 2 つの値など)。 ...
まず、バッファを初期化します。
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 ループと、初期値が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
編集:
上記のコードを使用して、さまざまなバリエーションを作成できます。たとえば、それぞれ 10 時点の 2 つのデータ セットを格納する場合、nBufferを 20 に変更して、最初の 10 列に古いセットを格納し、最後の 10 列に新しいセットを格納できます。次に、if ステートメントを次のように変更します。
...
if (processTime == nBuffer/2),
...
これで、古い 10 個のデータ ポイントのセット ( centroidBuffer(:,1:10)内) と新しい 10 個のデータ ポイントのセット ( centroidBuffer(:,11:20)内)の両方を使用して処理を実行できます。