123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- /**
- * @file ImageInfo.cpp
- * @brief localization info + image filename + ?
- * @author Erik Rodner
- * @date 04/16/2008
- */
- #include "core/image/ImageT.h"
- #include "core/vector/VectorT.h"
- #include "core/vector/MatrixT.h"
- #include <iostream>
- #include "vislearning/cbaselib/ImageInfo.h"
- /* Qt */
- #ifdef NICE_USELIB_QT4_XML
- #include <QFile>
- #include <QString>
- #include <QByteArray>
- #include <QDomDocument>
- #include <QDomNode>
- #include <QDomElement>
- #include <QPoint>
- #endif //NICE_USELIB_QT4_XML
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- ImageInfo::~ImageInfo()
- {
- //if ( lr != NULL )
- //delete lr;
- }
- //! A member loading labeled image from formatted xml file.
- /*!
- * \param[in] filename a std::string containing a path to the file
- * we need to load data from
- *
- * \see loadLegendFromNode(QDomElement *)
- * \see BBoxFromData(QString *aBBoxData, int *ID)
- * \see polyFromData(QString *aPolyData, int *labelID)
- */
- bool
- ImageInfo::loadImageInfo(const string &aFilename)
- {
- #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 errMsg;
- if (!doc.setContent(&file, &errMsg)) {
- QByteArray array = errMsg.toAscii();
- cout << array.data();
- //showWarning(errMsg);
- file.close();
- return false;
- /* NOTREACHED */
- }
- file.close();
- /* getting all info */
- QDomElement elements = doc.documentElement();
- QDomNode rootNode = elements.firstChild();
- QString string_buffer;
- int width = -1;
- int height = -1;
- // 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;
- /* NOTREACHED */
- }
- QByteArray array = string_buffer.toAscii();
- image_path_ = string(array.data());
- }
- /* path to the segmented image */
- if (element.tagName() == "segmented") {
- string_buffer = element.text();
- if (string_buffer.isEmpty()) {
- continue;
- }
- 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()) {
- continue;
- }
- QByteArray array = string_buffer.toAscii();
- image_description_ = string(array.data());
- }
- /* tags */
- else if (element.tagName() == "tags") {
- string_buffer = element.text();
- if (string_buffer.isEmpty()) {
- rootNode = rootNode.nextSibling();
- cout << "tags are empty\n";
- continue;
- }
- 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;
- }
- string_buffer = subElement.text();
- if (subElement.tagName() == "bbox") {
- BoundingBox bbox = BBoxFromData(&string_buffer);
- bbox.setID(id);
- bboxes_.push_back(bbox);
- }
- if (subElement.tagName() == "poly") {
- Polygon poly = polyFromData(&string_buffer);
- poly.setID(id);
- 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();
- }
- #endif //NICE_USELIB_QT4_XML
- return true;
- }
- #ifdef NICE_USELIB_QT4_XML
- //! A member loading legend from xml node
- /*!
- * \param[in] anElement a pointer to the object containing all the legend
- */
- void
- ImageInfo::loadLegendFromElement(QDomElement *anElement)
- {
- if (!anElement) {
- return;
- /* NOTREACHED */
- }
- QDomNode subNode = anElement->firstChild();
- QDomElement subElement;
- while(!subNode.isNull()) {
- subElement = subNode.toElement();
- if (!subElement.isNull() && !subElement.text().isEmpty())
- loadCategoryInfo(&subElement);
- subNode = subNode.nextSibling();
- }
- }
- //! Loads one category info(label) from xml QDomElement
- /*!
- * \param[in] anElement an object containing category info data
- */
- bool
- ImageInfo::loadCategoryInfo(QDomElement *anElement)
- {
- QString string_buffer;
- int id = -1;
- bool isMain;
- uint color = 0xff000000;
- /* id attribute */
- string_buffer = anElement->attribute("id");
- bool ok = 0;
- id = string_buffer.toInt(&ok, 10);
- if (!ok) {
- cout <<
- "loadCategoryInfo: "
- "label id format is corrupted\n";
- return false;
- /* NOTREACHED */
- }
- /* isMain attribute */
- string_buffer = anElement->attribute("isMain");
- isMain = string_buffer.toInt(&ok, 2);
- if (!ok) {
- cout <<
- "loadCategoryInfo: "
- "label isMain flag format is corrupted\n";
- return false;
- /* NOTREACHED */
- }
- /* color attribute */
- string_buffer = anElement->attribute("color");
- color = string_buffer.toUInt(&ok, 16);
- if (!ok) {
- cout <<
- "loadCategoryInfo: "
- "label color format is corrupted\n";
- return false;
- /* NOTREACHED */
- }
- /* converting label name from QString to std::string*/
- string_buffer = anElement->text();
- QByteArray array = string_buffer.toAscii();
- std::string labelName(array.data());
- CategoryInfo label;
- label.setID(id);
- label.setCategoryName(labelName);
- label.setColor(color);
- labels_.push_back(label);
- }
- //! A protected member parsing string data and returning a BoundingBox from it
- /*!
- * format is x;y;w;h where w - width and h - height
- */
- BoundingBox
- ImageInfo::BBoxFromData(
- QString *aBBoxData
- )
- {
- BoundingBox bbox;
- QString buffer;
- int startPos = 0;
- bool ok = 1;
- int counter = 0;
- for (int i = 0; i < aBBoxData->size(); i++) {
- if (';' != aBBoxData->at(i))
- continue;
- buffer = aBBoxData->mid(startPos, i - startPos);
- int bboxData = buffer.toInt(&ok, 10);
- if (!ok) {
- cout <<
- "BBoxFromData: "
- "bbox format is corrupted\n";
- break;
- }
- if (!counter) {
- bbox.setTopLeft(bboxData, 0);
- counter++;
- }
- else if (1 == counter) {
- int x = bbox.topLeft().x;
- bbox.setTopLeft(x, bboxData);
- counter++;
- }
- else if (2 == counter) {
- bbox.setWidth(bboxData);
- counter++;
- }
- else if (3 == counter) {
- bbox.setHeight(bboxData);
- counter++;
- }
- startPos = i + 1;
- }
- if (!bbox.isValid() || !ok) {
- cout <<
- "BBoxFromData: "
- "bbox format is corrupted\n";
- bbox.setTopLeft(0, 0);
- bbox.setBottomRight(0, 0);
- }
- return bbox;
- }
- //! A protected member parsing string data and returning a Polygon from it
- /*!
- * format is x0;y0;x1;y1;...
- */
- Polygon
- ImageInfo::polyFromData(
- QString *aPolyData
- )
- {
- Polygon poly;
- QPoint point;
- QString buffer;
- int startPos = 0;
- bool ok = 1;
- /* indicates whether coordinate x or y */
- bool evenFlag = 0;
- for (int i = 0; i < aPolyData->size(); i++) {
- /* ";" is a separator */
- if (';' != aPolyData->at(i))
- continue;
- buffer = aPolyData->mid(startPos, i - startPos);
- int polyCoor = buffer.toInt(&ok, 10);
- if (!ok) {
- cout <<
- "polyFromData: "
- "poly format is corrupted\n";
- break;
- }
- if (!evenFlag) {
- point.setX(polyCoor);
- evenFlag = 1;
- }
- else {
- point.setY(polyCoor);
- poly.push(point.x(), point.y());
- evenFlag = 0;
- }
- startPos = i + 1;
- }
- /* last coordinate was Xi what means an error or
- last converting from string was not successful */
- if (evenFlag || !ok) {
- cout <<
- "polyFromData: "
- "poly format is corrupted\n";
- //poly.clear();
- }
- return poly;
- }
- //!
- ImageT< unsigned int >
- ImageInfo::imageTFromData(
- const int &aWidth,
- const int &aHeight,
- QString *aPureData
- )
- {
- int startPos = 0;
- QString buffer = 0;
- ImageT< unsigned int > image(aWidth, aHeight);
- int y = 0;
- int x = 0;
- bool ok = 0;
- for (int i = 0; i < aPureData->size(); i++) {
- if ('\n' == aPureData->at(i)) {
- y++;
- x = 0;
- startPos = i + 1;
- continue;
- }
- /* ";" is a separator */
- if (';' != aPureData->at(i))
- continue;
- buffer = aPureData->mid(startPos, i - startPos);
- int pixel = buffer.toInt(&ok, 10);
- if (!ok) {
- cout <<
- "imageTFromData: "
- "pure data format is corrupted\n";
- image = 0;
- return image;
- /* NOTREACHED */
- }
- image.setPixel(x, y, pixel);
- x++;
- startPos = i + 1;
- }
- return image;
- }
- #endif //NICE_USELIB_QT4_XML
- //! returns pointer to labels_ list
- const std::list< CategoryInfo > *
- ImageInfo::labels() const
- {
- return &labels_;
- }
- //! returns pointer to bboxes_ list
- const std::list< BoundingBox > *
- ImageInfo::bboxes() const
- {
- return &bboxes_;
- }
- //! returns pointer to polys_ list
- const std::list< Polygon > *
- ImageInfo::polys() const
- {
- return &polys_;
- }
- //! returns ImageT object labeled_image_
- ImageT< unsigned int >
- ImageInfo::labeledImage() const
- {
- return labeled_image_;
- }
- //! returns tags
- std::string
- ImageInfo::tags() const
- {
- return tags_;
- }
- //! returns path to the original image
- std::string
- ImageInfo::imagePath() const
- {
- return image_path_;
- }
- //! returns string with the image description
- std::string
- ImageInfo::imageDescription() const
- {
- return image_description_;
- }
- //! returns path to the image segmented by ImageLabeler tool
- std::string
- ImageInfo::segmentedImagePath() const
- {
- return segmented_image_path_;
- }
- /*
- *
- */
|