当前位置: 首页 > news >正文

基于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]);trywhile 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);
  1. GPU加速(需Parallel Computing Toolbox):
gpuImg = gpuArray(frame);
[bboxes, scores] = detect(detector, gpuImg);
  1. 非极大值抑制(减少重叠检测):
% 自定义NMS函数
function pick = nms(boxes, overlapThresh)if isempty(boxes)pick = [];return;endx1 = 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
  1. 移动端优化
% 降低分辨率
inputSize = [160, 120];
detector = vision.PeopleDetector('UprightPeople_48x32', 'InputSize', inputSize);

参考代码 HOG 行人检测代码 www.youwenfan.com/contentcni/65187.html

常见问题解决方案1. 漏检问题

  • 调整检测尺度范围(NumScales参数)
  • 增加训练样本多样性
  1. 误检问题: 提高置信度阈值(MergeThreshold) 启用阴影去除算法:
frame = removeShadows(frame, 'Method', 'AdaptiveThreshold');
  1. 实时性优化: 使用积分图像加速:
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');
  1. 多传感器融合
% 结合雷达数据优化检测
radarData = readRadarData('sensor.mat');
[bboxes, scores] = detectWithSensorData(detector, frame, radarData);
  1. 动态场景适应
% 基于光流法的运动补偿
opticFlow = opticalFlowLK;
flow = estimateFlow(opticFlow, prevFrame);
compensatedFrame = applyFlow(frame, flow);
http://www.hskmm.com/?act=detail&tid=30966

相关文章:

  • 2025法兰保护罩厂家推荐:荣专科技,专业制造防溅保温优质产品!
  • 网络文件共享系统NFS服务搭建
  • 在CentOS 7.9系统上使用Docker部署RuoYi-Vue前后端分离系统
  • C# 泛型懒汉单例类
  • uni-app x使用uview-plus
  • MyEMS 支撑公共建筑低碳运营:多维度能耗建模逻辑与运行优化策略
  • 实时检测机器人广告点击的深度学习技术
  • MATLAB频散曲线绘制与相速度群速度分析
  • 密码算法的应用
  • 开源生态视角下 MyEMS 的能源管理系统国产化实践:架构设计与自主可控路径
  • 【IEEE出版】第七届机器学习、大数据与商务智能国际会议(MLBDBI 2025)
  • 2025 年国内优质货代公司最新推荐排行榜:深度解析头部企业服务能力,助力企业精准选合作伙伴泰国货代/印尼货代/马来货代/日本货代/东南亚货代公司推荐
  • 2025年鸡精生产设备厂家最新推荐排行榜,高效节能,智能控制,品质卓越的鸡精生产线公司推荐!
  • boofuzz学习
  • 2025年扑灭司林厂家最新推荐排行榜,高效环保扑灭司林,专业生产与优质服务口碑之选!
  • IvorySQL 亮相第 27 届中国国际软件博览会:开源创新,共筑软件新生态
  • uniapp 内嵌传值和接收
  • 鸿蒙项目实战(十一):事件通知EventBus
  • BLE动态修改广播地址
  • 2025年扑灭司林厂家最新推荐排行榜,高效环保扑灭司林,专业生产与市场口碑深度解析!
  • 国标GB28181算法算力平台EasyGBS在食品安全监管系统中的融合与应用方案
  • springcloud和dubbo有什么区别
  • 九种常见UML图(分类+图解)
  • Kruskal 重构树
  • 解决Win11 24H2 缺少Microsoft Print to PDF组件,重新添加出现0x800f0922错误的问题
  • “顾客需求必响应”!国标GB28181算法算力平台EasyGBS国标协议报警预案怎么弄?4步实操指南来了
  • 机器视觉双雄YOLO 和 OpenCV 到底有啥区别?别再傻傻分不清!
  • 基于定制开发开源AI智能名片S2B2C商城小应用的文案信息传达策略研究
  • AI工具学习02 - 使用 ChatGPT 进行 PRD 产品设计
  • mysql默认事务隔离级别,从入门到精通的完全指南