opencv例程文件夹内有mat2xml.m文件,但是不好用,提示未能找到rootfilter,说明与mat中格式并不统一。下面的matlab代码可以无问题的实现mat2xml功能:
% jelly 2013-08-12% Convert *.mat format model in the source example in
% Discriminatively Trained Deformable Part Models "voc-release4.01"
% to opencv's latentSVM detect input format *.xmlfunction MAT2XMLmodel_401(matmodel, xmlfile)matmodel = 'INRIA/inriaperson_final';
xmlfile = 'INRIA/inriaperson_final.xml';
load(matmodel);
fid = fopen(xmlfile, 'w');
fprintf(fid, '\n');
%%
%获取组件数
ncom = length(model.rules{model.start});
fprintf(fid, '\t\n');
fprintf(fid, '\t%d \n', ncom);
%获取特征维数,固定值31维。model中没有记录
nfeature = 31;
fprintf(fid, '\t\n');
fprintf(fid, '\t%d
\n', nfeature);
%Score threshold=model.thresh
fprintf(fid, '\t\n');
fprintf(fid, '\t%.16f\n', model.thresh);
layer = 1;
%对于每一个组件分别获取它的root filter、part filter、deformation filter
for icom = 1:ncom
fprintf(fid, '\t\n');
fprintf(fid, '\t\t\n');
fprintf(fid, '\t\t\n');
% attention: X,Y swap
rhs = model.rules{model.start}(icom).rhs;
% assume the root filter is first on the rhs of the start rules
if model.symbols(rhs(1)).type == 'T'
% handle case where there's no deformation model for the root
root = model.symbols(rhs(1)).filter;
else
% handle case where there is a deformation model for the root
root = model.symbols(model.rules{rhs(1)}(layer).rhs).filter;
endfilternum = root;
sizeX = model.filters(filternum).size(2);
sizeY = model.filters(filternum).size(1);
fprintf(fid, '\t\t\t\n');
fprintf(fid, '\t\t\t%d\n', sizeX);
fprintf(fid, '\t\t\t%d\n', sizeY);
fprintf(fid, '\t\t\t\n');
fprintf(fid, '\t\t\t');
for iY = 1:sizeY
for iX = 1:sizeX
% original mat has 32 which is larger than nfeature=31 by 1
fwrite(fid, model.filters(filternum).w(iY,iX,1:nfeature), 'double');
% need verify
end
end
fprintf(fid, '\t\t\t \n');
fprintf(fid, '\t\t\t\n');
fprintf(fid, '\t\t\t%.16f \n', ...% need verify
model.rules{model.start}(icom).offset.w);
fprintf(fid, '\t\t \n');
fprintf(fid, '\t\t\n');
fprintf(fid, '\t\t\n');
%在每个component内,获取part filter的个数,并获取每个part的参数
npart = length(model.rules{model.start}(icom).rhs) -1 ;
fprintf(fid, '\t\t\t%d \n', npart);
%%获取每个part的相关参数[dx,dy,ds]和penalty[dx dy dxx dyy]
for ipart = 2: npart+1
fprintf(fid, '\t\t\t\n');
fprintf(fid, '\t\t\t\n');
irule = model.rules{model.start}(icom).rhs(ipart);
filternum = model.symbols(model.rules{irule}.rhs).filter;
sizeX = model.filters(filternum).size(2);
sizeY = model.filters(filternum).size(1);
fprintf(fid, '\t\t\t\t%d\n', sizeX);
fprintf(fid, '\t\t\t\t%d\n', sizeY);
fprintf(fid, '\t\t\t\t\n');
fprintf(fid, '\t\t\t\t');
for iY = 1:sizeY
for iX = 1:sizeX
% original mat has 32 which is larger than nfeature=31 by 1
fwrite(fid, model.filters(filternum).w(iY,iX,1:nfeature), 'double');
% need verify
end
end
fprintf(fid, '\t\t\t\t \n');
fprintf(fid, '\t\t\t\t\n');
fprintf(fid, '\t\t\t\t\n');
fprintf(fid, '\t\t\t\t\t%d \n',model.rules{model.start}(icom).anchor{ipart}(1)+1);
%[dx,dy,ds]
fprintf(fid, '\t\t\t\t\t%d \n',model.rules{model.start}(icom).anchor{ipart}(2)+1);
fprintf(fid, '\t\t\t\t \n');
fprintf(fid, '\t\t\t\t\n');
fprintf(fid, '\t\t\t\t【(3) windows下可运行的mat转xml,VOC-release4.01 DPM训练的model(mat)转为OpenCV latentsvm可以加载的model(xml)】\n');
fprintf(fid, '\t\t\t\t\t%.16f \n',model.rules{irule}.def.w(2));
fprintf(fid, '\t\t\t\t\t%.16f \n',model.rules{irule}.def.w(4));
fprintf(fid, '\t\t\t\t\t%.16f \n',model.rules{irule}.def.w(1));
fprintf(fid, '\t\t\t\t\t%.16f \n',model.rules{irule}.def.w(3));
fprintf(fid, '\t\t\t\t\n');
fprintf(fid, '\t\t\t\n');
endfprintf(fid, '\t\t\n');
fprintf(fid, '\t \n');
end
fprintf(fid, ' \n');
fclose(fid);
end
推荐阅读
- MATLAB图形界面|基于Matlab的汽车出入库计时计费系统
- 召回率 Recall、精确度Precision、准确率Accuracy
- 模板匹配(Match Template) 修改版
- 模式识别|马尔科夫原理及应用场景
- 模式识别相似性测度距离计算---几种距离对比
- Python 实现步态能量图