0

我有一些图像,我使用 MATLAB 的imageLabler工具对其进行了注释。结果,我得到了 gtruth.mat 文件(其中包含所有图像的注释)。

现在我想使用这些注释训练一个简单的对象检测器。但是,我使用的对象检测器(dlib 对象检测器)只接受 XML 格式。

是否可以将该.mat文件转换为 XML 文件,以便我的所有注释保持不变并且我可以训练我的自定义对象检测器?如果是这样,我该怎么做?

4

1 回答 1

1

I will start by saying that this is only a partial solution to your problem, which should allow you to solve the rest of it on your own.


It appears that this question deals with converting a groundTruth object into dlib-friendly XML.

So first we need to ask ourselves, what is a groundTruth object? I have made a toy example using the Image Labeler and some images available in the MATLAB installation folder. Here's what I got:

gTruth = 
  groundTruth with properties:

          DataSource: [1×1 groundTruthDataSource]
    LabelDefinitions: [6×3 table]
           LabelData: [9×6 table]

>> gTruth.DataSource
ans = 
groundTruthDataSource for an image collection with properties

                      Source: {
                              ' ...\MATLAB\R2018b\help\symbolic\mupad_ref\adaptivemesh-d0e330.png';
                              ' ...\MATLAB\R2018b\help\symbolic\mupad_ref\adaptivemesh-d0e338.png';
                              ' ...\MATLAB\R2018b\help\symbolic\mupad_ref\adaptivemesh-d0e348.png'
                               ... and 6 more
                              }

>> gTruth.LabelDefinitions
ans =
  6×3 table
       Name         Type              Description        
    __________    _________    __________________________
    'Axis1'       Rectangle    ''                        
    'Axis2'       Rectangle    ''                        
    'Axis3'       Rectangle    ''                        
    'Equation'    Scene        'Image shows an equation.'
    'ThreeD'      Scene        'Image shows a 3D chart.' 
    'TwoD'        Scene        'Image shows a 2D chart.' 

>> gTruth.LabelData
ans =
  9×6 table
       Axis1           Axis2           Axis3        Equation    ThreeD    TwoD 
    ____________    ____________    ____________    ________    ______    _____
    [1×4 double]    [1×4 double]    [1×4 double]     false      true      false
    [1×4 double]    [1×4 double]    [1×4 double]     false      true      false
    [1×4 double]    [1×4 double]    [1×4 double]     false      true      false
    [1×4 double]    [1×4 double]    [1×4 double]     false      true      false
    []              []              []               true       false     false
    []              []              []               true       false     false
    []              []              []               true       false     false
    [1×4 double]    [1×4 double]    [1×4 double]     false      true      false
    [1×4 double]    [1×4 double]    []               false      false     true 

(The above contains 2 of the 3 available types of labels, the 3rd being Pixel Labels, which I decided to skip here.)

Now, what does a dlib-friendly XML look like? I'm not entirely sure, so I'll go with this one:

<?xml version='1.0' encoding='ISO-8859-1'?>
<?xml-stylesheet type='text/xsl' href='image_metadata_stylesheet.xsl'?>
<dataset>
<name>imglab dataset</name>
<comment>Created by imglab tool.</comment>
<images>
  <image file='rel-path\1.jpg'>
    <box top='26' left='33' width='78' height='73'>
      <label>LabelName</label>
      <part name='1' x='67' y='68'/>
    </box>
  </image>
  ...
<images>
</dataset>

So the mapping you need appears to be:

  • For every image create an <image> node whose file attribute points to gTruth.DataSource.Source{1...n}.
  • In images that have a Rectangle-type label, create a <box> node.
    • The 4-element vector we have in gTruth.LabelData needs to be transformed into e.g. top='26' left='33' width='78' height='73'.
    • The name of the label goes to the <label> node.
  • (Guess #1) Images that have more than one label will have more than one <label> node.
  • (Guess #2) Images that have a Scene-type label, will not have a <box> node enclosing <label>.
  • No idea what the <part> node is for.

Creating a converter based on the above should be straightforward. You could use struct2xml to help you, and/or xmlwrite.

于 2018-12-26T14:02:58.713 回答