matlab实现hog特征
%%matlab实现hog特征 %修改自http://www.cnblogs.com/tiandsp/archive/2013/05/24/3097503.html %input: img %output: final_descriptor clear all; close all; clc; %img=double(imread('lena.jpg')); %img=imread('man.png'); img=imread('e:/work/matlab/data/252.jpg'); img=rgb2gray(img); %简单起见,彩图转灰度图。后续可以改进。 img=imresize(img, [128 64]); img=double(img); [h, w, ~] = size(img); %下面是求cell cell_size=8; %step*step个像素作为一个cell. cell_size=pixels_per_cell orient=9; %方向直方图包含的方向数 angle_range=180/orient; %每个方向包含的角度数 h=round(h/cell_size)*cell_size; w=round(w/cell_size)*cell_size; img=img(1:h,1:w,:); %img = sqrt(img); %伽马校正。J=AI^r 此处取A=1,r=0.5 % 下面是求边缘 fy=[-1 0 1]; %定义竖直模版 fx=fy'; %定义水平模版 Gy=imfilter(img, fy, 'replicate'); %竖直梯度 Gx=imfilter(img, fx, 'replicate'); %水平梯度 Gmag=sqrt(Gx.^2+Gy.^2); %梯度幅值 %为每个cell计算其decriptor(梯度方向直方图,即一个1*orient规格的向量) cell_descriptors=zeros(orient, h/cell_size, w/cell_size); idx_y=1; for y=1:cell_size:h idx_x=1; for x=1:cell_size:w tmpx=Gx(y:y+cell_size-1, x:x+cell_size-1); tmpy=Gy(y:y+cell_size-1, x:x+cell_size-1); tmped=Gmag(y:y+cell_size-1,x:x+cell_size-1); tmped=tmped/sum(sum(tmped)); %局部边缘强度归一化 cell_hist=zeros(1, orient); %当前cell_size*cell_size像素统计角度直方图,就是cell for p=1:cell_size for q=1:cell_size ang=atan2(tmpy(p,q), tmpx(p,q)); %atan2返回的是[-pi,pi]之间的弧度值 ang=mod(ang*180/pi, 180); %先转角度,再划归到[0,180)之间。因为mod的参数现在不是整数,因此会大于179. ang=ang+0.0000001; %防止ang为0 bin_id = ceil(ang/angle_range);%得到的bin_id \in [1,9] cell_hist(bin_id)=cell_hist(bin_id)+tmped(p,q); %ceil向上取整,使用边缘强度加权。此处根据梯度方向进行vote,权值为梯度幅值 end end cell_descriptors(:,idx_y,idx_x) = cell_hist; idx_x = idx_x + 1; end idx_y = idx_y + 1; end %下面是计算feature,block_size*block_size个cell合成一个block %比如block_size取2 [~, h, w]=size(cell_descriptors); block_size=2; %cells_per_block=2,即每个block_size=2*8=16像素 stride=1; h_max=floor((h-block_size)/stride)+1; w_max=floor((w-block_size)/stride)+1; block_descriptors=zeros(block_size*block_size*orient, h_max, w_max); for i=1:h_max for j=1:w_max blk_mat=cell_descriptors(:,i:i+block_size-1, j:j+block_size-1); normed_blk_mat=zz_normalize(blk_mat); reshaped_blk_mat=reshape(normed_blk_mat, [1 block_size*block_size*orient]); block_descriptors(:,i,j)=reshaped_blk_mat; end end %将block_descriptors进行拼接,得到final_descriptor [d1,d2,d3]=size(block_descriptors); dimensions=d1*d2*d3; final_descriptor=zeros(1, dimensions); k=1; for i=1:d2 for j=1:d3 final_descriptor(k:k+d1-1)=block_descriptors(:,i,j); k=k+d1; end end
Greatness is never a given, it must be earned.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
2015-07-09 use shadowsocks in Linux
2015-07-09 Python使用opencv
2015-07-09 Python version 2.7 required, which was not found in the registry