资源简介
车流量统计matlab程序,可以对每辆车进行标号,亲自测试过,绝对可用
代码片段和文件信息
function multiobjectTracking()
% 创建用于读取视频,检测运动物体的系统对象,
%显示结果。
obj = setupSystemobjects();
tracks = initializeTracks(); % Create an empty array of tracks.
nextId = 1; % ID of the next track
% Detect moving objects and track them across video frames.
while ~isDone(obj.reader)
frame = readframe();
[centroids bboxes mask] = detectobjects(frame);
predictNewLocationsOfTracks();
[assignments unassignedTracks unassignedDetections] = ...
detectionToTrackAssignment();
updateAssignedTracks();
updateUnassignedTracks();
deleteLostTracks();
createNewTracks();
displayTrackingResults();
end
function obj = setupSystemobjects()
% 初始化视频 I/O
%创建对象用于读取视频 绘制每一帧的跟踪目标 播放视频
% 创建视频读取器.
obj.reader = vision.VideoFileReader(‘11.avi‘);
% 创建两个 视频播放窗口 一个用于播放视频
% 一个用于播放前景掩模.
obj.videoPlayer = vision.VideoPlayer(‘Position‘ [20 400 700 400]);
obj.maskPlayer = vision.VideoPlayer(‘Position‘ [740 400 700 400]);
%创建用于前景检测和斑点分析的系统对象
% The foreground detector is used to segment moving objects from
% the background. 它输出二值掩模 前景为1 背景为0.
obj.detector = vision.ForegroundDetector(‘NumGaussians‘ 3 ...
‘NumTrainingframes‘ 5 ‘MinimumBackgroundRatio‘ 0.7);
% Connected groups of foreground pixels are likely to correspond to moving
% objects. The blob analysis System object is used to find such groups
% (called ‘blobs‘ or ‘connected components‘) and compute their
% characteristics such as area centroid and the bounding box.
obj.blobAnalyser = vision.BlobAnalysis(‘BoundingBoxOutputPort‘ true ...
‘AreaOutputPort‘ true ‘CentroidOutputPort‘ true ...
‘MinimumBlobArea‘ 400);
end
function tracks = initializeTracks()
% create an empty array of tracks
tracks = struct(...
‘id‘ {} ...
‘bbox‘ {} ...
‘kalmanFilter‘ {} ...
‘age‘ {} ...
‘totalVisibleCount‘ {} ...
‘consecutiveInvisibleCount‘ {});
end
function frame = readframe()
frame = obj.reader.step();
end
function [centroids bboxes mask] = detectobjects(frame)
% Detect foreground.
mask = obj.detector.step(frame);
% Apply morphological operations to 清除噪声 填补空洞.
mask = imopen(mask strel(‘rectangle‘ [33]));
mask = imclose(mask strel(‘rectangle‘ [15 15]));
mask = imfill(mask ‘holes‘);
% 进行斑点分析 找到连接部件.
[~ centroids bboxes] = obj.blobAnalyser.step(mask);
end
function predictNewLocationsOfTracks()
for i = 1:length(tracks)
bbox = tracks(i).bbox;
% 预测轨道当前位置.
predictedCentroid = predict(tracks(i).kalmanFilter);
% 矩形框随质心移动而移动
predictedCentroid = int32(predi
- 上一篇:用于时频分析的广义s变换变换代码时频分析特别有用
- 下一篇:MIMO 成像算法
评论
共有 条评论