Add track age, total visible count, total invisible count in MOT

1 次查看(过去 30 天)
Hello, in the following MOT Matlab code I want to see and save the 'age', 'totalVisibleCount', and 'consecutiveInvisibleCount' of each track in the tracks struct. I also want to set this code in a way that, by using an assignment threshold, lost tracks are deleted. I would appreciate it if anyone could help me.
clc; clear; close;
% Load the object detections from store_box_score_label_center cell array
load('store_box_score_label_center.mat'); % detections are in a 4*N cell where N is the number of frames. first row = bboxs, second row = scores, third row = labels and last row = centers of bboxes
detections = store_box_score_label_center(4, :);
% Initialize the tracks
tracks = [];
track_id = 1;
% Process each frame
numFrames = size(detections, 2);
% Initialize the cell array to store centroids for all frames
allCentroidsTRACKS = cell(1, numFrames);
for t = 1:numFrames
% Get the current detections (centroids)
currentCentroids = detections{t};
% Predict the next location of the tracks
for i = 1:length(tracks)
tracks(i).centroid = kalman_predict(tracks(i).kalmanFilter);
end
% Calculate the cost matrix using centroids
costMatrix = calculateCostMatrix(tracks, currentCentroids);
AssignmentThreshold = 300;
% Assign detections to tracks using the Hungarian algorithm
[assignments, unassignedTracks, unassignedDetections] = ...
assignDetectionsToTracks(costMatrix, AssignmentThreshold);
% Update the assigned tracks
for ij = 1:size(assignments, 1)
trackIdx = assignments(ij, 1);
detectionIdx = assignments(ij, 2);
centroid1 = currentCentroids(detectionIdx, :);
tracks(trackIdx).kalmanFilter = kalman_correct(tracks(trackIdx).kalmanFilter, centroid1);
tracks(trackIdx).centroid = centroid1;
end
% Delete unassigned tracks
tracks(unassignedTracks) = [];
% Create new tracks for unassigned detections
for hh = 1:length(unassignedDetections)
detectionIdx2 = unassignedDetections(hh);
centroid = currentCentroids(detectionIdx2, :);
kalmanFilter = kalman_initialize(centroid);
newTrack = struct('id', track_id,...
'centroid', centroid, 'bbox', ...
store_box_score_label_center{1,t}(detectionIdx2, :),...
'kalmanFilter', kalmanFilter);
track_id = track_id + 1;
tracks = [tracks; newTrack];
end
% Store the centroids for the current frame
allCentroidsTRACKS{t} = vertcat(tracks(:).centroid);
allTrackIDS{t} = vertcat(tracks(:).id);
allTrackBBOXES{t} = vertcat(tracks(:).bbox);
save_tracks{t} = tracks;
% Plot the centroids for the current frame
figure(1);
image_show = imread(sprintf('frame_%04d.png', t+423));
imshow(image_show);
hold on;
colors = jet(length(tracks)); % Generate a colormap for the tracks
track_centroids = allCentroidsTRACKS{t};
trackIDs = allTrackIDS{t};
for io = 1:size(track_centroids, 1)
trackIdx1 = find([tracks.id] == trackIDs(io));
plot(track_centroids(io, 1), track_centroids(io, 2),...
'o', 'Color', colors(trackIdx1, :));
text(double(track_centroids(io, 1)),double(track_centroids(io, 2)),...
num2str(trackIDs(io)), 'Color', colors(trackIdx1, :),...
'FontSize', 8);
end
hold off;
saveas(gcf, fullfile('out_track2', sprintf('figure_%d.png', t+423 )));
end
%%
% Kalman filter functions
function kf = kalman_initialize(initial_state)
kf.x = [initial_state(1); 0; initial_state(2); 0]; % [x; vx; y; vy]
kf.P = eye(4) * 1e5;
kf.F = [1 1 0 0; 0 1 0 0; 0 0 1 1; 0 0 0 1];
kf.H = [1 0 0 0; 0 0 1 0];
kf.R = [25 0; 0 25];
kf.Q = eye(4) * 100;
end
function predicted_state = kalman_predict(kf)
kf.x = kf.F * kf.x;
kf.P = kf.F * kf.P * kf.F' + kf.Q;
predicted_state = kf.H * kf.x;
end
function kf = kalman_correct(kf, measurement)
y = measurement' - kf.H * kf.x;
S = kf.H * kf.P * kf.H' + kf.R;
K = kf.P * kf.H' / S;
kf.x = kf.x + K * y;
kf.P = (eye(4) - K * kf.H) * kf.P;
end
function costMatrix = calculateCostMatrix(tracks, centroids)
numTracks = length(tracks);
numCentroids = size(centroids, 1);
costMatrix = zeros(numTracks, numCentroids);
for i = 1:numTracks
trackCentroid = tracks(i).centroid;
for j = 1:numCentroids
detectionCentroid = centroids(j, :);
costMatrix(i, j) = pdist2(trackCentroid(:)', detectionCentroid(:)');
costMatrix(i, j);
end
end
end

回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by