Răsfoiți Sursa

changed class structure of ImageInfo for more flexibility for own derived implementations

Johannes Ruehle 12 ani în urmă
părinte
comite
e1984613a0
2 a modificat fișierele cu 285 adăugiri și 201 ștergeri
  1. 260 195
      cbaselib/ImageInfo.cpp
  2. 25 6
      cbaselib/ImageInfo.h

+ 260 - 195
cbaselib/ImageInfo.cpp

@@ -53,221 +53,285 @@ ImageInfo::~ImageInfo()
 bool
 bool
 ImageInfo::loadImageInfo(const string &aFilename)
 ImageInfo::loadImageInfo(const string &aFilename)
 {
 {
-	#ifdef NICE_USELIB_QT4_XML
+    #ifdef NICE_USELIB_QT4_XML
 
 
-	QString filename(aFilename.data());
-	QDomDocument doc("Image Labeler");
-	QFile file(filename);
-	if (!file.open(QIODevice::ReadOnly)) {
-		cout << "loadImageInfo:Can not open such file\n";
-		return false;
-		/* NOTREACHED */
-	}
+    QString filename(aFilename.data());
+    QDomDocument doc("Image Labeler");
+    QFile file(filename);
+    if (!file.open(QIODevice::ReadOnly)) {
+        cout << "loadImageInfo:Can not open such file\n";
+        return false;
+        /* NOTREACHED */
+    }
 
 
-	QString errMsg;
-	if (!doc.setContent(&file, &errMsg)) {
-		QByteArray array = errMsg.toAscii();
-		cout << array.data();
-		//showWarning(errMsg);
-		file.close();
-		return false;
-		/* NOTREACHED */
-	}
+    QString errMsg;
+    if (!doc.setContent(&file, &errMsg)) {
+        QByteArray array = errMsg.toAscii();
+        cout << array.data();
+        //showWarning(errMsg);
+        file.close();
+        return false;
+        /* NOTREACHED */
+    }
 
 
-	file.close();
+    file.close();
 
 
-	/* getting all info */
-	QDomElement elements = doc.documentElement();
-	QDomNode rootNode = elements.firstChild();
-	QString string_buffer;
-	int width = -1;
-	int height = -1;
+    /* getting all info */
+    QDomElement elements = doc.documentElement();
+    QDomNode rootNode = elements.firstChild();
+
+    m_iImageWidth = -1;
+    m_iImageHeight = -1;
 
 
 //	cout << "\nlet the parsing begin!\n";
 //	cout << "\nlet the parsing begin!\n";
-	while(!rootNode.isNull()) {
-		QDomElement element = rootNode.toElement();
-		if(!element.isNull()) {
-			/* path to the image */
-			if (element.tagName() == "image") {
-
-                string_buffer = element.text();
-
-                if (string_buffer.isEmpty())
-                {
-					cout << "loadImageInfo:The file with data"
-							" doesn't contain path to the image\n";
-					return false;
-				}
-                if( QFileInfo(string_buffer).isRelative() )
-                {
-                    QString asd = QFileInfo( QString(aFilename.c_str()) ).absoluteDir().absolutePath();
-
-                    QString qwe = QFileInfo( asd + "/" + string_buffer ).absoluteFilePath();
-
-                    string_buffer = qwe;
-                }
-
-                image_path_ = string_buffer.toStdString();
-			}
-			/* path to the segmented image */
-			if (element.tagName() == "segmented") {
-				string_buffer = element.text();
-				if ( !string_buffer.isEmpty() ) {
-					QByteArray array = string_buffer.toAscii();
-					segmented_image_path_ = string(array.data());
-				}
-			}
-			/* image description */
-			else if (element.tagName() == "description") {
-				string_buffer = element.text();
-				if ( !string_buffer.isEmpty()) {
-					QByteArray array = string_buffer.toAscii();
-					image_description_ = string(array.data());
-				}
-			}
-			/* tags */
-			else if (element.tagName() == "tags") {
-				string_buffer = element.text();
-                if ( !string_buffer.isEmpty()) {
-                    QByteArray array = string_buffer.toAscii();
-                    //TODO: make parsing into the string list
-                    tags_ = string(array.data());
-                }
-			}
-			/* legend */
-			else if (element.tagName() == "legend") {
-				loadLegendFromElement(&element);
-			}
-			/* objects */
-			else if (element.tagName() == "objects") {
-				QDomNode subNode = element.firstChild();
-				QDomElement subElement;
-
-				while(!subNode.isNull()) {
-					subElement = subNode.toElement();
-
-					if (subElement.isNull() || subElement.text().isEmpty()) {
-						subNode = subNode.nextSibling();
-						continue;
-					}
-
-					string_buffer = subElement.attribute("id");
-					bool ok = 1;
-					int id = string_buffer.toInt(&ok, 10);
-
-					if (!ok) {
-						cout << "loadImageInfo: "
-								"poly id format is corrupted\n";
-						subNode = subNode.nextSibling();
-						continue;
-					}
-
-                    // try reading a unique object/bounding box id, which identifies
-                    // this object against all others (not a label)
-                    string_buffer = subElement.attribute("uniqueObjectId");
-                    ok = 1;
-                    int uniqueObjectId = string_buffer.toInt(&ok, 10);
-                    if(!ok)
-                        uniqueObjectId = -1;
-
-
-                    string_buffer = subElement.text();
-					if (subElement.tagName() == "bbox") {
-                        BoundingBox bbox;
-                        bool bValid = BBoxFromData(&string_buffer, id, bbox);
-                        if( bValid )
-                        {
-                            bbox.unique_id_ = uniqueObjectId;
-
-                            bboxes_.push_back(bbox);
-                        }
-					}
-					if (subElement.tagName() == "poly") {
-                        Polygon poly;
-                        bool bValid = polyFromData(&string_buffer, poly);
-                        if(bValid)
-                        {
-                            poly.setID(id);
-                            poly.unique_id_ = uniqueObjectId;
-
-                            polys_.push_back(poly);
-                        }
-
-					}
-
-
-
-					subNode = subNode.nextSibling();
-				}
-			}
-			/* image size */
-			else if (element.tagName() == "image_size") {
-				string_buffer = element.text();
-				if (string_buffer.isEmpty()) {
-					cout << "loadImageInfo: "
-							"image size format is corrupted\n";
-					return false;
-					/* NOTREACHED */
-				}
-
-				QString buffer;
-				int size = string_buffer.size();
-				bool ok = 0;
-				for (int i = 0; i < size; i++) {
-					/* ";" is a separator */
-					if (';' != string_buffer.at(i))
-						continue;
-
-					buffer = string_buffer.mid(0, i);
-
-					width = buffer.toInt(&ok, 10);
-					if (!ok) {
-						cout <<
-							"loadImageInfo: "
-							"image size format is corrupted\n";
-						return false;
-						/* NOTREACHED */
-					}
-
-					buffer = string_buffer.mid(i + 1, size - (i + 1));
-
-					height = buffer.toInt(&ok, 10);
-
-					if (!ok) {
-						cout <<
-							"loadImageInfo: "
-							"image size format is corrupted";
-						return false;
-						/* NOTREACHED */
-					}
-					break;
-				}
-			}
-			else if (element.tagName() == "pure_data") {
-				string_buffer = element.text();
-				labeled_image_ = imageTFromData(width, height, &string_buffer);
-			}
-		}
-		rootNode = rootNode.nextSibling();
-	}
+    while(!rootNode.isNull()) {
+        QDomElement element = rootNode.toElement();
+        if(!element.isNull()) {
+            // path to the image
+            if (element.tagName() == "image") {
+                if( !this->extractSectionImage( &element, aFilename) )
+                    return false;
+            }
+            // path to the segmented image
+            if (element.tagName() == "segmented") {
+                if( !this->extractSectionSegmented(&element) )
+                    return false;
+            }
+            // image description
+            else if (element.tagName() == "description") {
+                if( !this->extractSectionDescription(&element) )
+                    return false;
+            }
+            // tags
+            else if (element.tagName() == "tags") {
+                if( !this->extractSectionTags(&element) )
+                    return false;
+            }
+            // legend
+            else if (element.tagName() == "legend") {
+                extractSectionLegend(&element);
+            }
+            // objects
+            else if (element.tagName() == "objects") {
+                if( !this->extractSectionObjects(&element) )
+                    return false;
+            }
+            // image size
+            else if (element.tagName() == "image_size") {
+                if( !this->extractImageSize(&element) )
+                    return false;
+            }
+            else if (element.tagName() == "pure_data") {
+                if( !this->extractSectionPureData(&element) )
+                    return false;
+            }
+        }
+        rootNode = rootNode.nextSibling();
+    }
 
 
-	#endif //NICE_USELIB_QT4_XML
+    #endif //NICE_USELIB_QT4_XML
 
 
-	return true;
+    return true;
 }
 }
 
 
 #ifdef NICE_USELIB_QT4_XML
 #ifdef NICE_USELIB_QT4_XML
 
 
+bool ImageInfo::extractSectionImage(QDomElement *element , const std::string &p_sImageInfoFilename)
+{
+    QString string_buffer = element->text();
+
+    if (string_buffer.isEmpty())
+    {
+        cout << "loadImageInfo:The file with data"
+                " doesn't contain path to the image\n";
+        return false;
+    }
+    if( QFileInfo(string_buffer).isRelative() )
+    {
+        QString asd = QFileInfo( QString(p_sImageInfoFilename.c_str()) ).absoluteDir().absolutePath();
+
+        QString qwe = QFileInfo( asd + "/" + string_buffer ).absoluteFilePath();
+
+        string_buffer = qwe;
+    }
+
+    image_path_ = string_buffer.toStdString();
+    return true;
+}
+bool ImageInfo::extractSectionSegmented(QDomElement *element )
+{
+    QString string_buffer = element->text();
+    if ( !string_buffer.isEmpty() ) {
+        QByteArray array = string_buffer.toAscii();
+        segmented_image_path_ = string(array.data());
+    }
+    return true;
+}
+bool ImageInfo::extractSectionDescription(QDomElement *element )
+{
+    QString string_buffer = element->text();
+    if ( !string_buffer.isEmpty()) {
+        QByteArray array = string_buffer.toAscii();
+        image_description_ = string(array.data());
+    }
+    return true;
+}
+bool ImageInfo::extractSectionTags(QDomElement *element )
+{
+    QString string_buffer = element->text();
+    if ( !string_buffer.isEmpty())
+    {
+        QByteArray array = string_buffer.toAscii();
+        //TODO: make parsing into the string list
+        tags_ = string(array.data());
+    }
+    return true;
+}
+
+bool ImageInfo::extractSectionObjects(QDomElement *element )
+{
+    QDomNode subNode = element->firstChild();
+    QDomElement subElement;
+
+    while(!subNode.isNull()) {
+        subElement = subNode.toElement();
+
+        if (subElement.isNull() || subElement.text().isEmpty()) {
+            subNode = subNode.nextSibling();
+            continue;
+        }
+
+        if (subElement.tagName() == "bbox") {
+            if ( !this->extractObjectRectangle(&subElement) )
+                return false;
+        }
+        if (subElement.tagName() == "poly") {
+           if ( !this->extractObjectPolygon(&subElement) )
+               return false;
+        }
+
+        subNode = subNode.nextSibling();
+    }
+    return true;
+}
+bool ImageInfo::extractObjectPolygon(QDomElement *element )
+{
+    QString string_buffer = element->attribute("id");
+    bool ok = 1;
+    int id = string_buffer.toInt(&ok, 10);
+
+    if (!ok) {
+        cout << "loadImageInfo: "
+                "poly id format is corrupted\n";
+        return false;
+    }
+
+    // try reading a unique object/bounding box id, which identifies
+    // this object against all others (not a label)
+    string_buffer = element->attribute("uniqueObjectId");
+    ok = 1;
+    int uniqueObjectId = string_buffer.toInt(&ok, 10);
+    if(!ok)
+        uniqueObjectId = -1;
+
+    string_buffer = element->text();
+    Polygon poly;
+    bool bValid = polyFromData(&string_buffer, poly);
+    if( !bValid )
+        return false;
+
+    poly.setID(id);
+    poly.unique_id_ = uniqueObjectId;
+
+    polys_.push_back(poly);
+
+    return true;
+}
+bool ImageInfo::extractObjectRectangle(QDomElement *element )
+{
+    QString string_buffer = element->attribute("id");
+    bool ok = 1;
+    int id = string_buffer.toInt(&ok, 10);
+
+    if (!ok)
+    {
+        cout << "loadImageInfo: "
+                "poly id format is corrupted\n";
+        return false;
+    }
+
+    // try reading a unique object/bounding box id, which identifies
+    // this object against all others (not a label)
+    string_buffer = element->attribute("uniqueObjectId");
+    ok = 1;
+    int uniqueObjectId = string_buffer.toInt(&ok, 10);
+    if(!ok)
+        uniqueObjectId = -1;
+
+    string_buffer = element->text();
+    BoundingBox bbox;
+    bool bValid = BBoxFromData(&string_buffer, id, bbox);
+    if( !bValid )
+        return false;
+
+    bbox.unique_id_ = uniqueObjectId;
+    bboxes_.push_back(bbox);
+
+    return true;
+}
+
+bool ImageInfo::extractImageSize(QDomElement *element )
+{
+    QString string_buffer = element->text();
+    if (string_buffer.isEmpty()) {
+        cout << "loadImageInfo: "
+                "image size format is corrupted\n";
+        return false;
+    }
+
+    QStringList coordsList = string_buffer.split(";", QString::SkipEmptyParts);
+
+    try
+    {
+        if( coordsList.size() == 2)
+        {
+            bool ok = false;
+            this->m_iImageWidth = QVariant(coordsList[0]).toInt( &ok );
+            if( !ok ) return false;
+            this->m_iImageHeight = QVariant(coordsList[1]).toInt( &ok );
+            if( !ok ) return false;
+
+            return true;
+        }
+    } catch(std::exception &e)
+    {
+        std::cout << "exception: image size format is corrupted" << e.what() << std::endl;
+        return false;
+    }
+
+    return false;
+}
+bool ImageInfo::extractSectionPureData(QDomElement *element )
+{
+    if( m_iImageHeight < 0 || m_iImageWidth < 0)
+        return false;
+
+    QString string_buffer = element->text();
+    labeled_image_ = imageTFromData(m_iImageWidth, m_iImageHeight, &string_buffer);
+
+    return true;
+}
+
+
 //! A member loading legend from xml node
 //! A member loading legend from xml node
 /*!
 /*!
  * \param[in] anElement a pointer to the object containing all the legend
  * \param[in] anElement a pointer to the object containing all the legend
  */
  */
 void
 void
-ImageInfo::loadLegendFromElement(QDomElement *anElement)
+ImageInfo::extractSectionLegend(QDomElement *anElement)
 {
 {
 	if (!anElement) {
 	if (!anElement) {
 		return;
 		return;
-		/* NOTREACHED */
 	}
 	}
 	QDomNode subNode = anElement->firstChild();
 	QDomNode subNode = anElement->firstChild();
 	QDomElement subElement;
 	QDomElement subElement;
@@ -280,6 +344,7 @@ ImageInfo::loadLegendFromElement(QDomElement *anElement)
 
 
 		subNode = subNode.nextSibling();
 		subNode = subNode.nextSibling();
 	}
 	}
+
 }
 }
 
 
 //! Loads one category info(label) from xml QDomElement
 //! Loads one category info(label) from xml QDomElement

+ 25 - 6
cbaselib/ImageInfo.h

@@ -54,17 +54,30 @@ class ImageInfo
 
 
 #ifdef NICE_USELIB_QT4_XML
 #ifdef NICE_USELIB_QT4_XML
 
 
-    bool polyFromData( QString *aPolyData, Polygon &p_Poly);
-    bool BBoxFromData(QString *aBBoxData, int &id , BoundingBox &p_bbox);
+    virtual bool polyFromData( QString *aPolyData, Polygon &p_Poly);
+    virtual bool BBoxFromData(QString *aBBoxData, int &id , BoundingBox &p_bbox);
+
+    virtual void extractSectionLegend ( QDomElement *anElement );
+    virtual bool extractSectionImage(QDomElement *element, const std::string &p_sImageInfoFilename );
+    virtual bool extractSectionSegmented(QDomElement *element );
+    virtual bool extractSectionDescription(QDomElement *element );
+    virtual bool extractSectionTags(QDomElement *element );
+    virtual bool extractSectionObjects(QDomElement *element );
+    virtual bool extractObjectPolygon(QDomElement *element );
+    virtual bool extractObjectRectangle(QDomElement *element );
+    virtual bool extractImageSize(QDomElement *element );
+    virtual bool extractSectionPureData(QDomElement *element );
+    virtual bool loadCategoryInfo ( QDomElement *anElement );
+
 
 
-    void loadLegendFromElement ( QDomElement *anElement );
-    bool loadCategoryInfo ( QDomElement *anElement );
     NICE::ImageT< unsigned int > imageTFromData (
     NICE::ImageT< unsigned int > imageTFromData (
       const int &aWidth,
       const int &aWidth,
       const int &aHeight,
       const int &aHeight,
       QString *aPureData
       QString *aPureData
     );
     );
 
 
+
+
 #endif //NICE_USELIB_QT4_XML
 #endif //NICE_USELIB_QT4_XML
 
 
   public:
   public:
@@ -107,7 +120,7 @@ class ImageInfo
       return localization_info;
       return localization_info;
     };
     };
 
 
-    bool loadImageInfo ( const std::string &aFilename );
+    virtual bool loadImageInfo ( const std::string &aFilename );
 
 
     const std::list< CategoryInfo > * labels() const;
     const std::list< CategoryInfo > * labels() const;
     const std::list< BoundingBox > * bboxes() const;
     const std::list< BoundingBox > * bboxes() const;
@@ -118,7 +131,11 @@ class ImageInfo
     std::string imageDescription() const;
     std::string imageDescription() const;
     std::string segmentedImagePath() const;
     std::string segmentedImagePath() const;
 
 
-  private:
+    void setListOfPolygons( std::list< Polygon > &p_polys )
+    {
+        polys_ = p_polys;
+    }
+  protected:
     std::list< CategoryInfo > labels_;
     std::list< CategoryInfo > labels_;
     std::list< BoundingBox > bboxes_;
     std::list< BoundingBox > bboxes_;
     std::list< Polygon > polys_;
     std::list< Polygon > polys_;
@@ -128,6 +145,8 @@ class ImageInfo
     std::string image_path_;
     std::string image_path_;
     std::string image_description_;
     std::string image_description_;
     std::string segmented_image_path_;
     std::string segmented_image_path_;
+    int m_iImageWidth;
+    int m_iImageHeight;
 
 
 };
 };