用 YOLOv4 检测 + DeepSORT 跟踪实现方案(含matlab 2024下载配置安装教程)
方法1:使用预训练YOLOv4模型(基于COCO数据集,支持80类目标) detector = yolov4ObjectDetector;% 方法2:加载自定义训练的YOLOv4模型(若有) % load('my_yolov4_detector.mat', 'trainedDetector');
·
文章目录
下面是基于 MATLAB 的 YOLOv4 目标检测 + DeepSORT 多目标跟踪完整实现方案,可实时跟踪视频中的多类目标(如行人、车辆),并保持目标 ID 稳定(即使短暂遮挡也能持续跟踪)。
matlab 2024下载安装配置教程
matlab下载安装配置教程:https://blog.csdn.net/2501_93482651/article/details/154799463?spm=1011.2415.3001.10575&sharefrom=mp_manage_link
核心流程
- YOLOv4 检测:每帧图像中检测目标(输出边界框、类别、置信度)。
- DeepSORT 跟踪:基于目标检测结果,通过外观特征和运动模型关联跨帧目标,分配唯一 ID 并更新轨迹。
完整代码实现
步骤 1:环境准备与参数配置
% 检查必要工具箱
if ~license('test', 'Computer Vision Toolbox') || ~license('test', 'Deep Learning Toolbox')
error('请安装 Computer Vision Toolbox 和 Deep Learning Toolbox');
end
% 配置参数
confidenceThreshold = 0.5; % 检测置信度阈值(过滤低置信度目标)
videoPath = 'traffic_video.mp4'; % 视频路径(可替换为摄像头:'camera')
targetClasses = {'person', 'car', 'truck', 'bicycle'}; % 需跟踪的目标类别
步骤 2:加载 YOLOv4 检测器(预训练或自定义)
% 方法1:使用预训练YOLOv4模型(基于COCO数据集,支持80类目标)
detector = yolov4ObjectDetector;
% 方法2:加载自定义训练的YOLOv4模型(若有)
% load('my_yolov4_detector.mat', 'trainedDetector');
% detector = trainedDetector;
步骤 3:初始化 DeepSORT 跟踪器
% 配置DeepSORT跟踪器参数
tracker = trackerDeepSORT( ...
'MaxAge', 30, ... % 目标丢失后保留ID的最大帧数(遮挡容错)
'DetectionThreshold', 0.3, ... % 关联检测的最低置信度
'AppearanceFeature', 'cnn', ... % 用CNN提取外观特征(提升遮挡后重识别能力)
'TrackerOutput', 'BoundingBox' ... % 输出跟踪目标的边界框
);
步骤 4:处理视频/摄像头并实现实时跟踪
% 打开视频源(摄像头或视频文件)
if strcmp(videoPath, 'camera')
video = videoinput('winvideo', 1); % Windows摄像头(Linux用'v4l2')
preview(video); % 预览摄像头
else
video = VideoReader(videoPath);
end
% 创建视频播放器(优化显示速度)
player = vision.VideoPlayer('Name', 'YOLOv4 + DeepSORT 多目标跟踪');
% 处理每一帧
while true
% 获取当前帧
if strcmp(videoPath, 'camera')
frame = getsnapshot(video);
else
if ~hasFrame(video)
break; % 视频结束
end
frame = readFrame(video);
end
% 步骤A:用YOLOv4检测目标
[bboxes, scores, labels] = detect(detector, frame);
% 筛选目标(仅保留指定类别和高置信度结果)
validIdx = ismember(labels, targetClasses) & (scores > confidenceThreshold);
bboxes = bboxes(validIdx, :); % 有效边界框
scores = scores(validIdx); % 有效置信度
labels = labels(validIdx); % 有效类别
% 步骤B:用DeepSORT跟踪目标(关联ID)
[tracks, info] = step(tracker, frame, bboxes, scores);
% 步骤C:标注跟踪结果(显示类别、ID和置信度)
if ~isempty(tracks)
% 获取跟踪ID和对应类别
trackIds = arrayfun(@(t) t.Id, tracks, 'UniformOutput', false);
trackedLabels = labels(info.AssignedIds); % 关联检测结果与跟踪ID
% 生成标注文本(类别 + ID + 置信度)
annotations = strcat( ...
trackedLabels, ...
" (ID:", trackIds, ")", ...
" (", arrayfun(@(s) sprintf('%.2f', s), scores(info.AssignedIds), 'UniformOutput', false), ")" ...
);
% 在图像上绘制边界框和标注
frame = insertObjectAnnotation(frame, 'rectangle', bboxes(info.AssignedIds,:), annotations);
end
% 显示结果
step(player, frame);
% 按ESC键退出(ASCII码27)
if ishandle(player) && strcmp(get(player, 'CurrentKey'), char(27))
break;
end
end
% 释放资源
if strcmp(videoPath, 'camera')
stop(video);
delete(video);
end
release(player);
close all;
关键参数说明
-
YOLOv4 检测参数
confidenceThreshold:过滤低置信度目标(如设为0.5,仅保留置信度>50%的检测结果),降低跟踪干扰。- 输入尺寸:YOLOv4默认输入416×416,可通过
detector.InputSize调整(尺寸越大精度越高,但速度越慢)。
-
DeepSORT 跟踪参数
MaxAge:目标丢失后保留ID的最大帧数(如设为30,适合30帧/秒的视频,允许1秒遮挡后仍能恢复跟踪)。AppearanceFeature:设为'cnn'时,跟踪器会提取目标外观特征(如颜色、纹理),大幅提升遮挡后重识别能力。
效果优化技巧
-
提升跟踪稳定性
- 若目标频繁丢失ID,可增大
MaxAge(如40),或提高confidenceThreshold减少误检干扰。 - 对小目标,可将YOLOv4输入尺寸调至608×608(
detector.InputSize = [608,608,3]),提升检测精度。
- 若目标频繁丢失ID,可增大
-
加速实时处理
- 减小输入尺寸(如320×320),或使用轻量版YOLOv4(
yolov4-tiny)。 - 限制跟踪类别(
targetClasses只保留需要的类别,如仅跟踪'car')。
- 减小输入尺寸(如320×320),或使用轻量版YOLOv4(
-
处理遮挡场景
- 确保
AppearanceFeature设为'cnn',利用外观特征而非仅依赖位置关联目标。
- 确保
适用场景
- 交通监控:跟踪车辆、行人,统计流量或违规行为(如闯红灯)。
- 商场客流分析:跟踪顾客移动轨迹,统计停留区域。
- 工业场景:跟踪生产线上的零件或设备,监控流程是否正常。
通过上述代码,可快速实现高精度、高稳定性的多目标跟踪系统,兼顾实时性和鲁棒性。
更多推荐

所有评论(0)