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 *.xml function MAT2XMLmodel_401(matmodel, xmlfile) matmodel = 'INRIA/inriaperson_final'; xmlfile = 'INRIA/inriaperson_final.xml'; load(matmodel); fid = fopen(xmlfile, 'w'); fprintf(fid, '<Model>\n'); %% %获取组件数 ncom = length(model.rules{model.start}); fprintf(fid, '\t<!-- Number of components -->\n'); fprintf(fid, '\t<NumComponents>%d</NumComponents>\n', ncom); %获取特征维数,固定值31维。model中没有记录 nfeature = 31; fprintf(fid, '\t<!-- Number of features -->\n'); fprintf(fid, '\t<P>%d</P>\n', nfeature); %Score threshold=model.thresh fprintf(fid, '\t<!-- Score threshold -->\n'); fprintf(fid, '\t<ScoreThreshold>%.16f</ScoreThreshold>\n', model.thresh); layer = 1; %对于每一个组件分别获取它的root filter、part filter、deformation filter for icom = 1:ncom fprintf(fid, '\t<Component>\n'); fprintf(fid, '\t\t<!-- Root filter description -->\n'); fprintf(fid, '\t\t<RootFilter>\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; end filternum = root; sizeX = model.filters(filternum).size(2); sizeY = model.filters(filternum).size(1); fprintf(fid, '\t\t\t<!-- Dimensions -->\n'); fprintf(fid, '\t\t\t<sizeX>%d</sizeX>\n', sizeX); fprintf(fid, '\t\t\t<sizeY>%d</sizeY>\n', sizeY); fprintf(fid, '\t\t\t<!-- Weights (binary representation) -->\n'); fprintf(fid, '\t\t\t<Weights>'); 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</Weights>\n'); fprintf(fid, '\t\t\t<!-- Linear term in score function -->\n'); fprintf(fid, '\t\t\t<LinearTerm>%.16f</LinearTerm>\n', ... % need verify model.rules{model.start}(icom).offset.w); fprintf(fid, '\t\t</RootFilter>\n'); fprintf(fid, '\t\t<!-- Part filters description -->\n'); fprintf(fid, '\t\t<PartFilters>\n'); %在每个component内,获取part filter的个数,并获取每个part的参数 npart = length(model.rules{model.start}(icom).rhs) -1 ; fprintf(fid, '\t\t\t<NumPartFilters>%d</NumPartFilters>\n', npart); %%获取每个part的相关参数[dx,dy,ds]和penalty[dx dy dxx dyy] for ipart = 2: npart+1 fprintf(fid, '\t\t\t<!-- Part filter ? description -->\n'); fprintf(fid, '\t\t\t<PartFilter>\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<sizeX>%d</sizeX>\n', sizeX); fprintf(fid, '\t\t\t\t<sizeY>%d</sizeY>\n', sizeY); fprintf(fid, '\t\t\t\t<!-- Weights (binary representation) -->\n'); fprintf(fid, '\t\t\t\t<Weights>'); 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</Weights>\n'); fprintf(fid, '\t\t\t\t<!-- Part filter offset -->\n'); fprintf(fid, '\t\t\t\t<V>\n'); fprintf(fid, '\t\t\t\t\t<Vx>%d</Vx>\n',model.rules{model.start}(icom).anchor{ipart}(1)+1); %[dx,dy,ds] fprintf(fid, '\t\t\t\t\t<Vy>%d</Vy>\n',model.rules{model.start}(icom).anchor{ipart}(2)+1); fprintf(fid, '\t\t\t\t</V>\n'); fprintf(fid, '\t\t\t\t<!-- Quadratic penalty function coefficients -->\n'); fprintf(fid, '\t\t\t\t<Penalty>\n'); fprintf(fid, '\t\t\t\t\t<dx>%.16f</dx>\n',model.rules{irule}.def.w(2)); fprintf(fid, '\t\t\t\t\t<dy>%.16f</dy>\n',model.rules{irule}.def.w(4)); fprintf(fid, '\t\t\t\t\t<dxx>%.16f</dxx>\n',model.rules{irule}.def.w(1)); fprintf(fid, '\t\t\t\t\t<dyy>%.16f</dyy>\n',model.rules{irule}.def.w(3)); fprintf(fid, '\t\t\t\t</Penalty>\n'); fprintf(fid, '\t\t\t</PartFilter>\n'); end fprintf(fid, '\t\t</PartFilters>\n'); fprintf(fid, '\t</Component>\n'); end fprintf(fid, '</Model>\n'); fclose(fid); end