数据集中标签.mat转.xml

针对BIT-vehicle数据集建立的转化程序,根据数据集给出的标签.mat转为训练需要的.xml文件

data=load('VehicleInfo.mat')
cars=data.VehicleInfo;
for n=1:length(cars)
    n;
    car=cars(n);
    %%
    %每一辆车新建一个xml文件存储车的信息
    carName=car.name;
    %图片的高度
    carHeight=car.height;
    %图片的宽度
    carWidth=car.width;
    %新建xml文件
    annotation = com.mathworks.xml.XMLUtils.createDocument('annotation');
    annotationRoot = annotation.getDocumentElement;  
    %定义子节点,xml的存储路径
    folder=annotation.createElement('folder');
    folder.appendChild(annotation.createTextNode(sprintf('%s','F:\Cars Dataset\lable_test\')));%这里为xml存放的目录
    annotationRoot.appendChild(folder);
    %图片的名称,包含后缀名                                           把-4删掉显示,jpg
    jpgName=annotation.createElement('filename');
    jpgName.appendChild(annotation.createTextNode(sprintf('%s',carName(1:end))));
    annotationRoot.appendChild(jpgName);
    %source就不添加了
    %添加图片的size
    jpgSize=annotation.createElement('size');
    annotationRoot.appendChild(jpgSize);
    %定义size的子节点
        %图片宽度
        width=annotation.createElement('width');
        width.appendChild(annotation.createTextNode(sprintf('%i',carWidth)));
        jpgSize.appendChild(width);
        
        %图片高度
        height=annotation.createElement('height');
        height.appendChild(annotation.createTextNode(sprintf('%i',carHeight)));
        jpgSize.appendChild(height);
        
        %图片深度,彩色图片3
        depth=annotation.createElement('depth');
        depth.appendChild(annotation.createTextNode(sprintf('%i',3)));
        jpgSize.appendChild(depth);
        
        segmented=annotation.createElement('segmented');
        segmented.appendChild(annotation.createTextNode(sprintf('%i',0)));%表示已经标注过了
        annotationRoot.appendChild(segmented);
        %接下来是每一辆车的标注信息
        
    %%
   
 
    carVehicles=car.vehicles;
    L=length(carVehicles);
    if L>1
        carName
        L
    end
    for nn=1:length(carVehicles)
        vehicle=carVehicles(nn);
        %标注框的最左侧坐标
        vLeft=vehicle.left;
        %标注框的最上边坐标
        vTop=vehicle.top;
        %标注框的最右侧坐标
        vRight=vehicle.right;
        %标注框最下面坐标
        vBottom=vehicle.bottom;
        %车的类别
        vCategory=vehicle.category;
        %图片中有多少个标注对象
        carNvehicles=car.nVehicles;
        %在这里生成每一符图片的txt文件,用于
        %%注意一张图片中可能会有多个对象
        %%matlab直接生成xml对象???
        %将这一辆车的信息注入xml中
        object=annotation.createElement('object');
        annotationRoot.appendChild(object);
        %标注框类别名称
        categoryName=annotation.createElement('name');
        categoryName.appendChild(annotation.createTextNode(sprintf('%s',vCategory)));
        object.appendChild(categoryName);
        
        pose=annotation.createElement('pose');
        pose.appendChild(annotation.createTextNode(sprintf('%s','Unspecified')));
        object.appendChild(pose);
        
        truncated=annotation.createElement('truncated');
        truncated.appendChild(annotation.createTextNode(sprintf('%i',0)));
        object.appendChild(truncated);
        
        Difficult=annotation.createElement('Difficult');
        Difficult.appendChild(annotation.createTextNode(sprintf('%i',0)));
        object.appendChild(Difficult);
        
        bndbox=annotation.createElement('bndbox');
        object.appendChild(bndbox);
        
        xmin=annotation.createElement('xmin');
        xmin.appendChild(annotation.createTextNode(sprintf('%i',vLeft)));
        bndbox.appendChild(xmin);
        
        ymin=annotation.createElement('ymin');
        ymin.appendChild(annotation.createTextNode(sprintf('%i',vTop)));
        bndbox.appendChild(ymin);
        
        xmax=annotation.createElement('xmax');
        xmax.appendChild(annotation.createTextNode(sprintf('%i',vRight)));
        bndbox.appendChild(xmax);
        
        ymax=annotation.createElement('ymax');
        ymax.appendChild(annotation.createTextNode(sprintf('%i',vBottom)));
        bndbox.appendChild(ymax);
        
    end
     %存储xml
    savePath=['F:\Cars Dataset\lable_test\',carName(1:end-3),'xml'];
    xmlwrite(savePath,annotation);      
end

 

你可能感兴趣的:(数据集中标签.mat转.xml)