基于MATLAB的HOG+SVM行人检测
基于MATLAB的HOG+SVM行人检测
%% 1. 数据准备与预处理
% 加载正负样本数据集(需自行准备)
load('pedestrian_dataset.mat'); % 包含trainData/trainLabels和testData/testLabels
% 图像预处理函数
function processedImg = preprocess(img)
% 灰度化
grayImg = rgb2gray(img);
% 高斯滤波去噪
blurredImg = imgaussfilt(grayImg, 2);
% 直方图均衡化
equalizedImg = histeq(blurredImg);
processedImg = imresize(equalizedImg, [64, 128]); % 统一尺寸
end
% 预处理训练集
trainDataPreprocessed = arrayfun(@(i) preprocess(trainData(:,:,i)), 1:size(trainData,3), 'UniformOutput', false);
testDataPreprocessed = arrayfun(@(i) preprocess(testData(:,:,i)), 1:size(testData,3), 'UniformOutput', false);
%% 2. HOG特征提取
% 定义HOG参数
cellSize = [8,8];
blockSize = [2,2];
blockStride = [1,1];
numBins = 9;
% 提取训练特征
trainFeatures = [];
for i = 1:numel(trainDataPreprocessed)
img = trainDataPreprocessed{i};
hogFeat = extractHOGFeatures(img, 'CellSize', cellSize, ...
'BlockSize', blockSize, 'BlockStride', blockStride, 'NumBins', numBins);
trainFeatures = [trainFeatures; hogFeat];
end
% 提取测试特征
testFeatures = [];
for i = 1:numel(testDataPreprocessed)
img = testDataPreprocessed{i};
hogFeat = extractHOGFeatures(img, 'CellSize', cellSize, ...
'BlockSize', blockSize, 'BlockStride', blockStride, 'NumBins', numBins);
testFeatures = [testFeatures; hogFeat];
end
%% 3. SVM分类器训练
% 训练参数设置
svmOptions = statset('Display','iter');
svmModel = fitcsvm(trainFeatures, trainLabels, ...
'KernelFunction', 'linear', ...
'BoxConstraint', 1, ...
'Standardize', true, ...
'Options', svmOptions);
%% 4. 检测器构建
% 创建检测器对象
detector = vision.PeopleDetector('UprightPeople_96x48', 'MergeThreshold', 30);
% 或使用自定义训练的SVM检测器
detector = vision.ObjectDetector('Custom', 'Model', svmModel, ...
'FeatureExtractor', @hogFeatureExtractor);
%% 5. 实时检测演示
% 打开摄像头
vid = videoinput('winvideo', 1, 'YUY2_640x480');
vid.FramesPerTrigger = Inf;
vid.ReturnedColorspace = 'rgb';
preview(vid); % 预览画面
% 创建显示窗口
h = figure('Name','Real-time Pedestrian Detection', ...
'Position',[100 100 800 600]);
try
while ishandle(h)
% 获取视频帧
frame = getsnapshot(vid);
% 检测行人
[bboxes, scores] = detect(detector, frame);
% 筛选高置信度结果
validIdx = scores > 0.4;
bboxes = bboxes(validIdx,:);
scores = scores(validIdx);
% 绘制检测结果
if ~isempty(bboxes)
frame = insertObjectAnnotation(frame, 'rectangle',...
bboxes, sprintf('Score %.2f', scores(1)),...
'Color','red','LineWidth',3);
end
% 显示结果
imshow(frame);
title(sprintf('Detected %d pedestrians',size(bboxes,1)));
drawnow;
end
catch
end
%% 6. 清理资源
stop(vid);
delete(vid);
clear vid;
%% 辅助函数:HOG特征提取
function features = hogFeatureExtractor(img)
% 图像预处理
grayImg = rgb2gray(img);
% 提取HOG特征
[features, ~] = extractHOGFeatures(grayImg, 'CellSize',[8,8],...
'BlockSize',[2,2], 'BlockStride',[1,1], 'NumBins',9);
end
关键参数说明(基于搜索结果优化)| 参数名 | 推荐值 | 作用说明 |
|----------------|-----------|------------------------------|
| cellSize| | 梯度方向统计单元尺寸 |
| blockSize| | 归一化块尺寸 |
| numBins| 9 | 梯度方向量化级数 |
| blockStride| | 块间滑动步长 |
| MergeThreshold| 30 | 检测框合并阈值 |
性能优化策略1. 多尺度检测(改进检测精度):
detector = vision.PeopleDetector('UprightPeople_96x48', 'NumScales', 5, 'ScaleStep', 1.2);
- GPU加速(需Parallel Computing Toolbox):
gpuImg = gpuArray(frame);
[bboxes, scores] = detect(detector, gpuImg);
- 非极大值抑制(减少重叠检测):
% 自定义NMS函数
function pick = nms(boxes, overlapThresh)
if isempty(boxes)
pick = [];
return;
end
x1 = boxes(:,1); y1 = boxes(:,2);
x2 = boxes(:,3); y2 = boxes(:,4);
areas = (x2-x1+1).*(y2-y1+1);
[~, idx] = sort(scores, 'descend');
pick = [];
while ~isempty(idx)
last = length(idx);
i = idx(last);
pick = [pick; i];
xx1 = max(x1(i), x1(idx(1:last-1)));
yy1 = max(y1(i), y1(idx(1:last-1)));
xx2 = min(x2(i), x2(idx(1:last-1)));
yy2 = min(y2(i), y2(idx(1:last-1)));
w = max(0, xx2 - xx1 + 1);
h = max(0, yy2 - yy1 + 1);
overlap = (w.*h)./areas(idx(1:last-1));
idx(idx(overlap > overlapThresh)) = [];
end
end
典型应用场景优化建议1. 监控视频处理:
% 视频文件处理
video = VideoReader('traffic.mp4');
while hasFrame(video)
frame = readFrame(video);
% 降采样加速
smallFrame = imresize(frame, 0.5);
% 检测...
end
- 移动端优化:
% 降低分辨率
inputSize = [160, 120];
detector = vision.PeopleDetector('UprightPeople_48x32', 'InputSize', inputSize);
参考代码 HOG 行人检测代码 www.youwenfan.com/contentcni/65187.html
常见问题解决方案1. 漏检问题:
- 调整检测尺度范围(
NumScales参数) - 增加训练样本多样性
- 误检问题: 提高置信度阈值(
MergeThreshold) 启用阴影去除算法:
frame = removeShadows(frame, 'Method', 'AdaptiveThreshold');
- 实时性优化: 使用积分图像加速:
hogParams = struct('UseIntegralImage', true);
detector = vision.PeopleDetector('UprightPeople_96x48', 'HOGParams', hogParams);
扩展应用方向1. 深度学习融合:
% 使用预训练CNN提取特征
net = alexnet;
inputSize(net) = [227 227 3];
features = activations(net, imresize(frame, [227 227]), 'fc7', 'OutputAs', 'rows');
- 多传感器融合:
% 结合雷达数据优化检测
radarData = readRadarData('sensor.mat');
[bboxes, scores] = detectWithSensorData(detector, frame, radarData);
- 动态场景适应:
% 基于光流法的运动补偿
opticFlow = opticalFlowLK;
flow = estimateFlow(opticFlow, prevFrame);
compensatedFrame = applyFlow(frame, flow);

浙公网安备 33010602011771号