📄 lhixml2struct.m
字号:
function v = LHIxml2struct(xml)
%
% Translate an xml string into a matlab struct
%
addpath('xml_toolbox');
parse = xml_parseany(xml);
v.annotation = parseXMLelements(parse);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Parse XML elements
function v = parseXMLelements(a)
%
v = []; annotation = [];
%Object
POList = {};objects={};
POList = a.POs{1}.PO; %PO
objects = a.Scene{1}.Object; %scene
numobjects = length(objects);
POIDIndexing=[];
%find indexing for each PO
for i=1:length(POList),
POIDIndexing(i)= str2num(POList{i}.ATTRIBUTE.ID);
end
%Do not consider subpart (Only Image->Object->Part)
count = 1;
for i=1:numobjects,
object = [];
try
poid = str2num(objects{i}.ATTRIBUTE.PO_ID); %get ID
object = getObject(POList,find(POIDIndexing==poid));
if (isfield(objects{i}.ChildIDs{1},'CONTENT'))
child = str2num(objects{i}.ChildIDs{1}.CONTENT);
numofchild = length(child);
else
numofchild = 0;
end
for j=1:numofchild,
part = [];
part = getObject(POList,find(POIDIndexing==child(j)));
object = setfield(object,'parts',{j},part);
end
if (~isfield(object,'parts'))
object.parts = [];
end
v = setfield(v,'object',{count},object);
count = count + 1;
catch
%v.object(i) = object;
disp(['error in object',num2str(i)]);
end
end
if (isfield(a,'ThreeDLabel'))
geometry = getGeometry(a.ThreeDLabel{1});
v = setfield(v,'geometry',geometry);
else
v.geometry = [];
end
%% Get geometry info (vanishing points, long line)
% called by parseXMLelements()
function geometry = getGeometry(geo)
geometry = [];
if (isfield(geo.VanishPointData{1},'VanishPoint'))
numofVP = length(geo.VanishPointData{1}.VanishPoint);
vp = [];
for i=1:numofVP
vp = getVPoint(geo.VanishPointData{1}.VanishPoint{i});
geometry = setfield(geometry,'VanishPoint',{i},vp);
end
else
geometry.VanishPoint = [];
end
if (isfield(geo.LongLineData{1},'LongLine'))
numofLongLine = length(geo.LongLineData{1}.LongLine);
Longline = [];
for i = 1:numofLongLine
Longline = getLongline(geo.LongLineData{1}.LongLine{i});
geometry = setfield(geometry,'LongLine',{i},Longline);
end
else
geometry.LongLine = [];
end
%% Get Vanishing point called by getGeometry()
function vp = getVPoint(VanishPoint)
vp = [];
vp = setfield(vp,'x',VanishPoint.ATTRIBUTE.XCoordinate);
vp = setfield(vp,'y',VanishPoint.ATTRIBUTE.YCoordinate);
%% Get Long line called by getGeometry()
function line = getLongline(LongLine)
line = [];
line = setfield(line,'X1',LongLine.ATTRIBUTE.StartX);
line = setfield(line,'X2',LongLine.ATTRIBUTE.EndX);
line = setfield(line,'Y1',LongLine.ATTRIBUTE.StartY);
line = setfield(line,'Y2',LongLine.ATTRIBUTE.EndY);
%% Get object called by parseXMLelements()
function object = getObject(POList, index)
object = [];
PO = POList{index};
%name
POName = getPOname(PO);
object = setfield(object, 'name', POName);
%labelmap
numRegion = str2num(PO.regionData{1}.ATTRIBUTE.num);
if (numRegion > 0)
labelmap = getPOLabelMap(PO);
object = setfield(object, 'labelmap',labelmap);
end
%Regions Polygons
if (numRegion > 0)
regions = [];
regions = getPORegions(PO);
object = setfield(object, 'regions', regions);
end
if (~isfield(object,'regions'))
object.labelmap = [];
object.regions = [];
end
%Subgraph
numSubgraph = str2num(PO.SubGraphData{1}.ATTRIBUTE.num);
if (numSubgraph > 0)
subgraphs = [];
subgraphs = getPOSubgraphs(PO);
object = setfield(object, 'subgraphs',subgraphs);
end
if (~isfield(object,'regions')&&~isfield(object,'subgraphs'))
error('null object');
end
if (~isfield(object,'subgraphs'))
object.subgraphs = [];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Get name of PO (union of syntax name and semantic name)
%% called by parseXMLelements()
function name = getPOname(PO)
isSyntax = (~strcmp(getPOSyntaxname(PO),'null'));
isSemantic = (~strcmp(getPOSemanticname(PO),'null'));
if ( isSyntax && isSemantic)
name = strcat(getPOSyntaxname(PO),' ',getPOSemanticname(PO));
elseif (isSyntax)
name = getPOSyntaxname(PO);
elseif (isSemantic)
name = getPOSemanticname(PO);
else
name = 'null';
end
%% read syntax name
function name = getPOSyntaxname(PO)
if (isfield(PO.POSyntaxName{1},'CONTENT'))
name = PO.POSyntaxName{1}.CONTENT;
name = strtok(name,'"'); % get rid of "
if (strcmp(name,'GenericObject')==1)
name = 'null';
end
else
name = 'null';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% read semantic name
function name = getPOSemanticname(PO)
if (isfield(PO.POName{1},'CONTENT'))
name = PO.POName{1}.CONTENT;
name = strtok(name,'"'); % get rid of "
else
name = 'null';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% called by getObject(POList, index)
function labelMap = getPOLabelMap(PO)
labelMap = [];
labelMap.R = PO.regionData{1}.ATTRIBUTE.maskColor_R;
labelMap.G = PO.regionData{1}.ATTRIBUTE.maskColor_G;
labelMap.B = PO.regionData{1}.ATTRIBUTE.maskColor_B;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% called by getObject(POList, index)
function region = getPORegions(PO)
region = [];
num = length(PO.regionData{1}.Region); ni = 1;
for i=1:num,
keypoints = getKeyPoints(PO.regionData{1}.Region{i}.BaseContour_keypoints{1}.KeyPoint);
region = setfield(region,{ni},'pt',keypoints);
ni = ni + 1;
subnum = 0;
if isfield(PO.regionData{1}.Region{i}.SubContours{1}, 'subcontour')
subnum = length(PO.regionData{1}.Region{i}.SubContours{1}.subcontour);
end
for j=1:subnum
subkeypoints = getKeyPoints(PO.regionData{1}.Region{i}.SubContours{1}.subcontour{j}.KeyPoint);
region = setfield(region,{ni},'pt',subkeypoints);
ni = ni + 1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function pts = getKeyPoints(keypoint)
pts = [];
num = length(keypoint);
for i=1:num,
x = keypoint{i}.ATTRIBUTE.xCoordinate;
y = keypoint{i}.ATTRIBUTE.yCoordinate;
pts = setfield(pts,{i},'x',x);
pts = setfield(pts,{i},'y',y);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% getObject(POList, index)
function subgraphs = getPOSubgraphs(PO)
subgraphs = [];
num = length(PO.SubGraphData{1}.SubGraph);
for i = 1:num,
curves=[];
curNum = length(PO.SubGraphData{1}.SubGraph{i}.Curve);
for j = 1:curNum,
keypoints = getKeyPoints(PO.SubGraphData{1}.SubGraph{i}.Curve{j}.Keypoint);
curves = setfield(curves,{j},'pt',keypoints);
end
subgraphs = setfield(subgraphs,{i},'curves',curves);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -