Browse Source

added: include bounding box label information from class ImageInfo (created by ImageLabeler) into class LocalizationResult

Johannes Ruehle 13 năm trước cách đây
mục cha
commit
807ece0a16
3 tập tin đã thay đổi với 110 bổ sung5 xóa
  1. 29 2
      cbaselib/ImageInfo.h
  2. 44 1
      cbaselib/LocalizationResult.cpp
  3. 37 2
      cbaselib/LocalizationResult.h

+ 29 - 2
cbaselib/ImageInfo.h

@@ -28,7 +28,22 @@ class QDomElement;
 namespace OBJREC
 {
 
-/** localization info + image filename + ? */
+/**
+ * @brief Class for loading label information created by the ImageLabeler tool.
+ *
+ * The label file format is xml consisting of several sections about the label desription,
+ * occuring objects (such as bounding boxes and polygons) and their label ids, and reference to the
+ * original image file.
+ *
+ * Note: Be aware of the code redundancy of the xml loading presented here and the separate code in the ImageLabeler tool.
+ * Both try to interprete/load the label xml but with different code, so changes at one code place do not affect the other one.
+ *
+ * Second note/todo: in future this class should complete be integrated into LocalizationResult, which also includes bounding box label info
+ * and is more integrated into NICE feature extraction and classification mechanism.
+ * Currently, to convert a loaded label file into a LocalizationResult, use function LocalizationResult::loadImageInfo() (currently only bounding box information).
+ *
+ * localization info + image filename + ?
+ */
 class ImageInfo
 {
 
@@ -53,7 +68,11 @@ class ImageInfo
 
   public:
 
-    /** simple constructor */
+    /**
+     * @brief simple constructor
+     * @param _imagefn image file name
+     * @param _lr Localization result containing label information
+     */
     ImageInfo ( const std::string & _imagefn, LocalizationResult *_lr ) :
         imagefn ( _imagefn ), lr ( _lr ), localization_info ( true ) {};
 
@@ -69,6 +88,14 @@ class ImageInfo
     {
       return imagefn;
     };
+
+    /**
+    * @brief Returns available localization information.
+    *
+    * Note: This class doesn't create a new LocalizationResult instance - it only returns the reference to a previously assigned instance (see constructor).
+    * To convert a loaded label file into a LocalizationResult, use function LocalizationResult::loadImageInfo() (currently only bounding box information).
+    * @see LocalizationResult::loadImageInfo()
+    */
     const LocalizationResult *localization () const
     {
       assert ( localization_info );

+ 44 - 1
cbaselib/LocalizationResult.cpp

@@ -14,6 +14,7 @@
 #include <core/image/LineT.h>
 
 #include "vislearning/cbaselib/LocalizationResult.h"
+#include "vislearning/cbaselib/ImageInfo.h"
 #include "core/basics/StringTools.h"
 
 // use this macro to show labeled images
@@ -354,11 +355,53 @@ void LocalizationResult::restore (istream & is, int format)
 		}
 
 		//sortEmpricalDepth();
-    } else {
+    }
+    else {
 		fthrow(IOException, "LocalizationResult::restore: file format not yet supported !");
     }
 }
 
+void LocalizationResult::loadImageInfo(std::string sFilename)
+{
+    try
+    {
+        ImageInfo info;
+        info.loadImageInfo(sFilename);
+
+        const std::list< OBJREC::BoundingBox > *listBBoxes = info.bboxes();
+
+        const double score = 1.0;
+        OBJREC::BoundingBox box;
+        std::list< OBJREC::BoundingBox >::const_iterator itBBoxes = listBBoxes->begin();
+        for(;itBBoxes != listBBoxes->end(); itBBoxes++)
+        {
+            box = *itBBoxes;
+            int id = box.id();
+
+            std::stringstream ss;
+            ss << id;
+
+            std::string classname = ss.str();
+            int classno = cn->classno(classname);
+            if(classno == -1)
+            {
+                fprintf (stderr, "LocalizationResult::loadImageInfo: no classno found for classname %s (using classno=-1)\n", classname.c_str());
+            }
+            ClassificationResult *r = new ClassificationResult ( classno, score, cn->getMaxClassno() );
+            SingleLocalizationResult *sr = new SingleLocalizationResult ( r, box.topLeft().x,
+                                                                             box.topLeft().y,
+                                                                             box.width(),
+                                                                             box.height() );
+            this->push_back ( sr );
+        }
+
+    }
+    catch(Exception e)
+    {
+        fthrow( Exception, "LocalizationResult::loadImageInfo: error loading image info (ImageLabeler xml format)");
+    }
+}
+
 void LocalizationResult::store (ostream & os, int format) const
 {
     if ( format == FILEFORMAT_PASCAL2006_RESULT ) 

+ 37 - 2
cbaselib/LocalizationResult.h

@@ -45,9 +45,24 @@ class SingleLocalizationResult
 
 
 	SingleLocalizationResult ( ClassificationResult *r, const NICE::Region & reg, int controlPoints = 0 );
-	SingleLocalizationResult ( ClassificationResult *r, int xi, int yi, int xa, int ya );
+
+    /**
+     * @brief constructor
+     * @param xi (left)
+     * @param yi (top)
+     * @param xa (width)
+     * @param ya (height)
+     */
+    SingleLocalizationResult ( ClassificationResult *r, int xi, int yi, int xa, int ya );
 	~SingleLocalizationResult ();
 
+    /**
+     * @brief get the bounding box
+     * @param xi (left)
+     * @param yi (top)
+     * @param xa (width)
+     * @param ya (height)
+     */
 	void getBoundingBox ( int & xi, int & yi, int & xa, int & ya ) const;
 	void getBoundingBox ( NICE::RectT<int> & rectangle ) const;
 
@@ -83,7 +98,7 @@ class LocalizationResult : public std::vector<SingleLocalizationResult *>, publi
     enum {
 		FILEFORMAT_PASCAL2006_RESULT = 0,
 		FILEFORMAT_PASCAL2006_GROUNDTRUTH,
-		FILEFORMAT_POLYGON
+        FILEFORMAT_POLYGON
     };
 
     LocalizationResult ( int xsize = -1, int ysize = -1 );
@@ -105,6 +120,26 @@ class LocalizationResult : public std::vector<SingleLocalizationResult *>, publi
 			bool invert = false,
 			int width = 1) const;
 
+    /**
+     * @brief Loads image label information from the file format supported by the ImageLabeler tool.
+     *
+     * This function ignores the label description (xml section < legend > ) and assumes, that the label names were loaded in advance
+     * and are present in the ClassNames reference member variable LocalizationResult::cn.
+     *
+     * Note: Uses class ImageInfo for loading the xml information and inserts them into this LocalizationResult class.
+     * A future TODO would be to directly include the ImageInfo loading code here (or as part of LocalizationResult) to substitute redundancy
+     * between the classes ImageInfo and LocalizationResult (meaning both use BoundingBoxes etc).
+     *
+     * Currently only rectangular bounding box information are transferred from the loaded ImageInfo instance to this class.
+     * So, trying to use for instance Polygon data created with the ImageLabeler will fail (empty LocalizationResult).
+     *
+     * @param sFilename file name of the image label file (usually *.dat, xml formatted)
+     * @see OBJREC::ImageInfo
+     * @author Johannes Rühle
+     * @date 2012-05-11
+     */
+    void loadImageInfo(std::string sFilename);
+
     void restore (std::istream & is, int format = 0);
     void store (std::ostream & os, int format = 0) const;
     void clear ();