一、HOG特征提取实现
1. 核心算法流程
functionhog_feat=compute_hog(img)% 输入:灰度图像(单通道)% 输出:归一化后的HOG特征向量% 参数设置cell_size=8;% cell尺寸(像素)block_size=2;% block包含的cell数num_bins=9;% 方向直方图分箱数[rows,cols]=size(img);% 1. 计算梯度[Gx,Gy]=imgradientxy(img,'sobel');grad_mag=sqrt(Gx.^2+Gy.^2);grad_dir=atan2(Gy,Gx)*180/pi;% 转换为角度% 2. 构建梯度直方图num_cells_x=floor(cols/cell_size);num_cells_y=floor(rows/cell_size);hog_cell=zeros(num_cells_y,num_cells_x,num_bins);fori=1:num_cells_yforj=1:num_cells_x% 提取当前cell区域cell_img=grad_mag((i-1)*cell_size+1:i*cell_size,...(j-1)*cell_size+1:j*cell_size);cell_dir=grad_dir((i-1)*cell_size+1:i*cell_size,...(j-1)*cell_size+1:j*cell_size);% 计算方向直方图hist=zeros(1,num_bins);fork=1:numel(cell_dir)angle=cell_dir(k);ifangle<0,angle=angle+180;endbin=round(angle/(180/num_bins))+1;hist(bin)=hist(bin)+cell_mag(k);endhog_cell(i,j,:)=hist/sum(hist);% 归一化endend% 3. Block归一化hog_feat=[];fori=1:block_size:num_cells_y-block_size+1forj=1:block_size:num_cells_x-block_size+1block=hog_cell(i:i+block_size-1,j:j+block_size-1,:);block=block(:);% 展平block=block/norm(block+eps);% L2归一化hog_feat=[hog_feat;block];endendend2. 关键优化点
Gamma校正:预处理阶段进行对比度归一化
img=sqrt(img);% Gamma=0.5的近似实现方向插值:采用双线性插值提升方向估计精度
重叠Block:设置50%重叠区域提升特征连续性
二、SVM分类器训练
1. 数据准备
% 加载INRIA数据集pos_dir='INRIAPerson/pos/';neg_dir='INRIAPerson/neg/';% 提取正样本特征pos_feat=[];fori=1:numel(dir(pos_dir))img=imread(fullfile(pos_dir,dir(pos_dir){i}));img=imresize(img,[64,128]);pos_feat=[pos_feat;compute_hog(img)];endlabels_pos=ones(size(pos_feat,1),1);% 提取负样本特征neg_feat=[];fori=1:numel(dir(neg_dir))img=imread(fullfile(neg_dir,dir(neg_dir){i}));img=imresize(img,[64,128]);neg_feat=[neg_feat;compute_hog(img)];endlabels_neg=-ones(size(neg_feat,1),1);% 合并数据集features=[pos_feat;neg_feat];labels=[labels_pos;labels_neg];2. 训练流程
% 划分训练集和测试集cv=cvpartition(size(features,1),'HoldOut',0.3);train_feat=features(cv.training,:);train_labels=labels(cv.training,:);test_feat=features(cv.test,:);test_labels=labels(cv.test,:);% 训练SVM模型model=fitcsvm(train_feat,train_labels,...'KernelFunction','linear',...'BoxConstraint',1,...'Standardize',true);% 性能评估predicted=predict(model,test_feat);accuracy=sum(predicted==test_labels)/numel(test_labels);disp(['测试集准确率: ',num2str(accuracy*100,'%.2f'),'%']);三、行人检测实现
1. 滑动窗口检测
functiondetections=sliding_window_detection(img,model,step=8)[rows,cols]=size(img);detections=[];fory=1:step:rows-64forx=1:step:cols-128window=imcrop(img,[x,y,128,64]);hog_feat=compute_hog(window);ifpredict(model,hog_feat)==1detections=[detections;x,y,128,64];endendendend2. 非极大值抑制(NMS)
functionkeep=nms(boxes,overlap)ifisempty(boxes),return;end% 转换为x1,y1,x2,y2格式x1=boxes(:,1);y1=boxes(:,2);x2=x1+boxes(:,3)-1;y2=y1+boxes(:,4)-1;% 计算面积area=(x2-x1+1).*(y2-y1+1);% 按置信度排序[~,idx]=sort(predictions,'descend');x1=x1(idx);y1=y1(idx);x2=x2(idx);y2=y2(idx);area=area(idx);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_ratio=(w.*h)./(area(idx(1:last-1))+area(i)-w.*h);% 删除重叠过高的框idx(idx(1:last-1)&overlap_ratio>overlap)=[];endend四、完整检测流程
% 加载测试图像img=imread('test.jpg');img_gray=rgb2gray(img);% 检测detections=sliding_window_detection(img_gray);% 非极大值抑制keep=nms(detections,0.3);final_detections=detections(keep,:);% 可视化结果figure;imshow(img);hold on;fori=1:size(final_detections,1)rectangle('Position',final_detections(i,:),...'EdgeColor','r','LineWidth',2);endhold off;参考代码 HOG特征提取在进行SVM行人检测www.youwenfan.com/contentcsn/81104.html
五、扩展应用
- 实时检测:结合YOLOv3实现混合检测框架
- 多目标跟踪:集成卡尔曼滤波器进行轨迹预测
- 异常检测:分析时序行人行为特征