- Update Track Structure: Add fields for age, totalVisibleCount, and consecutiveInvisibleCount in the track structure.
- Modify Track Update Logic: Update these fields during the tracking loop.
- Delete Lost Tracks: Use the assignment threshold to remove tracks that have been invisible for too long.
Add track age, total visible count, total invisible count in MOT
9 次查看(过去 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 个评论
回答(1 个)
Umang Pandey
2024-10-29,6:45
To modify your MATLAB code to track and save the age, totalVisibleCount, and consecutiveInvisibleCount of each track, you'll need to update the track structure and modify the logic to manage these properties. Additionally, you can implement a mechanism to delete lost tracks based on an assignment threshold.
Here's the updated code:
clc; clear; close all;
% 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;
% Define parameters
maxInvisibleFrames = 5; % Maximum number of consecutive invisible frames before a track is deleted
AssignmentThreshold = 300; % Assignment cost threshold
% Process each frame
numFrames = size(detections, 2);
% Initialize the cell array to store centroids for all frames
allCentroidsTRACKS = cell(1, numFrames);
allTrackIDS = cell(1, numFrames);
allTrackBBOXES = cell(1, numFrames);
save_tracks = 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);
tracks(i).age = tracks(i).age + 1;
tracks(i).consecutiveInvisibleCount = tracks(i).consecutiveInvisibleCount + 1;
end
% Calculate the cost matrix using centroids
costMatrix = calculateCostMatrix(tracks, currentCentroids);
% 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;
tracks(trackIdx).totalVisibleCount = tracks(trackIdx).totalVisibleCount + 1;
tracks(trackIdx).consecutiveInvisibleCount = 0;
end
% Delete tracks that have been invisible for too long
lostTrackIndices = [tracks.consecutiveInvisibleCount] > maxInvisibleFrames;
tracks(lostTrackIndices) = [];
% 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, ...
'age', 1, ...
'totalVisibleCount', 1, ...
'consecutiveInvisibleCount', 0);
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(:)');
end
end
end
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!