www.gusucode.com > vision 源码程序 matlab案例代码 > vision/AssignDetectionsToTracksInASingleVideoFrameExample.m
%% Assign Detections to Tracks in a Single Video Frame % This example shows you how to assign a detection to a track for % a single video frame. %% % Set the predicted locations of objects in the current frame. % Obtain predictions using the Kalman filter System object. predictions = [1,1;2,2]; %% % Set the locations of the objects detected in the current frame. For % this example, there are 2 tracks and 3 new detections. Thus, at least % one of the detections is unmatched, which can indicate a new track. detections = [1.1,1.1;2.1,2.1;1.5,3]; %% % Preallocate a cost matrix. cost = zeros(size(predictions,1),size(detections,1)); %% % Compute the cost of each prediction matching a detection. The cost here, % is defined as the Euclidean distance between the prediction and the % detection. for i = 1:size(predictions, 1) diff = detections - repmat(predictions(i,:),[size(detections,1),1]); cost(i, :) = sqrt(sum(diff .^ 2,2)); end %% % Associate detections with predictions. Detection 1 should match to track % 1, and detection 2 should match to track 2. Detection 3 should be % unmatched. [assignment,unassignedTracks,unassignedDetections] = ... assignDetectionsToTracks(cost,0.2); figure; plot(predictions(:,1),predictions(:,2),'*',detections(:,1),... detections(:,2),'ro'); hold on; legend('predictions','detections'); for i = 1:size(assignment,1) text(predictions(assignment(i, 1),1)+0.1,... predictions(assignment(i,1),2)-0.1,num2str(i)); text(detections(assignment(i, 2),1)+0.1,... detections(assignment(i,2),2)-0.1,num2str(i)); end for i = 1:length(unassignedDetections) text(detections(unassignedDetections(i),1)+0.1,... detections(unassignedDetections(i),2)+0.1,'unassigned'); end xlim([0,4]); ylim([0,4]);