ImageInfo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /**
  2. * @file ImageInfo.cpp
  3. * @brief localization info + image filename + ?
  4. * @author Erik Rodner
  5. * @date 04/16/2008
  6. */
  7. #include "core/image/ImageT.h"
  8. #include "core/vector/VectorT.h"
  9. #include "core/vector/MatrixT.h"
  10. #include <iostream>
  11. #include "vislearning/cbaselib/ImageInfo.h"
  12. /* Qt */
  13. #ifdef NICE_USELIB_QT4_XML
  14. #include <QFile>
  15. #include <QString>
  16. #include <QByteArray>
  17. #include <QDomDocument>
  18. #include <QDomNode>
  19. #include <QDomElement>
  20. #include <QPoint>
  21. #endif //NICE_USELIB_QT4_XML
  22. using namespace OBJREC;
  23. using namespace std;
  24. using namespace NICE;
  25. ImageInfo::~ImageInfo()
  26. {
  27. //if ( lr != NULL )
  28. //delete lr;
  29. }
  30. //! A member loading labeled image from formatted xml file.
  31. /*!
  32. * \param[in] filename a std::string containing a path to the file
  33. * we need to load data from
  34. *
  35. * \see loadLegendFromNode(QDomElement *)
  36. * \see BBoxFromData(QString *aBBoxData, int *ID)
  37. * \see polyFromData(QString *aPolyData, int *labelID)
  38. */
  39. bool
  40. ImageInfo::loadImageInfo(const string &aFilename)
  41. {
  42. #ifdef NICE_USELIB_QT4_XML
  43. QString filename(aFilename.data());
  44. QDomDocument doc("Image Labeler");
  45. QFile file(filename);
  46. if (!file.open(QIODevice::ReadOnly)) {
  47. cout << "loadImageInfo:Can not open such file\n";
  48. return false;
  49. /* NOTREACHED */
  50. }
  51. QString errMsg;
  52. if (!doc.setContent(&file, &errMsg)) {
  53. QByteArray array = errMsg.toAscii();
  54. cout << array.data();
  55. //showWarning(errMsg);
  56. file.close();
  57. return false;
  58. /* NOTREACHED */
  59. }
  60. file.close();
  61. /* getting all info */
  62. QDomElement elements = doc.documentElement();
  63. QDomNode rootNode = elements.firstChild();
  64. QString string_buffer;
  65. int width = -1;
  66. int height = -1;
  67. // cout << "\nlet the parsing begin!\n";
  68. while(!rootNode.isNull()) {
  69. QDomElement element = rootNode.toElement();
  70. if(!element.isNull()) {
  71. /* path to the image */
  72. if (element.tagName() == "image") {
  73. string_buffer = element.text();
  74. if (string_buffer.isEmpty()) {
  75. cout << "loadImageInfo:The file with data"
  76. " doesn't contain path to the image\n";
  77. return false;
  78. /* NOTREACHED */
  79. }
  80. QByteArray array = string_buffer.toAscii();
  81. image_path_ = string(array.data());
  82. }
  83. /* path to the segmented image */
  84. if (element.tagName() == "segmented") {
  85. string_buffer = element.text();
  86. if ( !string_buffer.isEmpty() ) {
  87. QByteArray array = string_buffer.toAscii();
  88. segmented_image_path_ = string(array.data());
  89. }
  90. }
  91. /* image description */
  92. else if (element.tagName() == "description") {
  93. string_buffer = element.text();
  94. if ( !string_buffer.isEmpty()) {
  95. QByteArray array = string_buffer.toAscii();
  96. image_description_ = string(array.data());
  97. }
  98. }
  99. /* tags */
  100. else if (element.tagName() == "tags") {
  101. string_buffer = element.text();
  102. if (string_buffer.isEmpty()) {
  103. rootNode = rootNode.nextSibling();
  104. cout << "tags are empty\n";
  105. continue;
  106. }
  107. QByteArray array = string_buffer.toAscii();
  108. //TODO: make parsing into the string list
  109. tags_ = string(array.data());
  110. }
  111. /* legend */
  112. else if (element.tagName() == "legend") {
  113. loadLegendFromElement(&element);
  114. }
  115. /* objects */
  116. else if (element.tagName() == "objects") {
  117. QDomNode subNode = element.firstChild();
  118. QDomElement subElement;
  119. while(!subNode.isNull()) {
  120. subElement = subNode.toElement();
  121. if (subElement.isNull() || subElement.text().isEmpty()) {
  122. subNode = subNode.nextSibling();
  123. continue;
  124. }
  125. string_buffer = subElement.attribute("id");
  126. bool ok = 1;
  127. int id = string_buffer.toInt(&ok, 10);
  128. if (!ok) {
  129. cout << "loadImageInfo: "
  130. "poly id format is corrupted\n";
  131. subNode = subNode.nextSibling();
  132. continue;
  133. }
  134. string_buffer = subElement.text();
  135. if (subElement.tagName() == "bbox") {
  136. BoundingBox bbox = BBoxFromData(&string_buffer, id);
  137. //bbox.setID(id);
  138. bboxes_.push_back(bbox);
  139. }
  140. if (subElement.tagName() == "poly") {
  141. Polygon poly = polyFromData(&string_buffer);
  142. poly.setID(id);
  143. polys_.push_back(poly);
  144. }
  145. subNode = subNode.nextSibling();
  146. }
  147. }
  148. /* image size */
  149. else if (element.tagName() == "image_size") {
  150. string_buffer = element.text();
  151. if (string_buffer.isEmpty()) {
  152. cout << "loadImageInfo: "
  153. "image size format is corrupted\n";
  154. return false;
  155. /* NOTREACHED */
  156. }
  157. QString buffer;
  158. int size = string_buffer.size();
  159. bool ok = 0;
  160. for (int i = 0; i < size; i++) {
  161. /* ";" is a separator */
  162. if (';' != string_buffer.at(i))
  163. continue;
  164. buffer = string_buffer.mid(0, i);
  165. width = buffer.toInt(&ok, 10);
  166. if (!ok) {
  167. cout <<
  168. "loadImageInfo: "
  169. "image size format is corrupted\n";
  170. return false;
  171. /* NOTREACHED */
  172. }
  173. buffer = string_buffer.mid(i + 1, size - (i + 1));
  174. height = buffer.toInt(&ok, 10);
  175. if (!ok) {
  176. cout <<
  177. "loadImageInfo: "
  178. "image size format is corrupted";
  179. return false;
  180. /* NOTREACHED */
  181. }
  182. break;
  183. }
  184. }
  185. else if (element.tagName() == "pure_data") {
  186. string_buffer = element.text();
  187. labeled_image_ = imageTFromData(width, height, &string_buffer);
  188. }
  189. }
  190. rootNode = rootNode.nextSibling();
  191. }
  192. #endif //NICE_USELIB_QT4_XML
  193. return true;
  194. }
  195. #ifdef NICE_USELIB_QT4_XML
  196. //! A member loading legend from xml node
  197. /*!
  198. * \param[in] anElement a pointer to the object containing all the legend
  199. */
  200. void
  201. ImageInfo::loadLegendFromElement(QDomElement *anElement)
  202. {
  203. if (!anElement) {
  204. return;
  205. /* NOTREACHED */
  206. }
  207. QDomNode subNode = anElement->firstChild();
  208. QDomElement subElement;
  209. while(!subNode.isNull()) {
  210. subElement = subNode.toElement();
  211. if (!subElement.isNull() && !subElement.text().isEmpty())
  212. loadCategoryInfo(&subElement);
  213. subNode = subNode.nextSibling();
  214. }
  215. }
  216. //! Loads one category info(label) from xml QDomElement
  217. /*!
  218. * \param[in] anElement an object containing category info data
  219. */
  220. bool
  221. ImageInfo::loadCategoryInfo(QDomElement *anElement)
  222. {
  223. QString string_buffer;
  224. int id = -1;
  225. bool isMain;
  226. uint color = 0xff000000;
  227. /* id attribute */
  228. string_buffer = anElement->attribute("id");
  229. bool ok = 0;
  230. id = string_buffer.toInt(&ok, 10);
  231. if (!ok) {
  232. cout <<
  233. "loadCategoryInfo: "
  234. "label id format is corrupted\n";
  235. return false;
  236. /* NOTREACHED */
  237. }
  238. /* isMain attribute */
  239. string_buffer = anElement->attribute("isMain");
  240. isMain = string_buffer.toInt(&ok, 2);
  241. if (!ok) {
  242. cout <<
  243. "loadCategoryInfo: "
  244. "label isMain flag format is corrupted\n";
  245. return false;
  246. /* NOTREACHED */
  247. }
  248. /* color attribute */
  249. string_buffer = anElement->attribute("color");
  250. color = string_buffer.toUInt(&ok, 16);
  251. if (!ok) {
  252. cout <<
  253. "loadCategoryInfo: "
  254. "label color format is corrupted\n";
  255. return false;
  256. /* NOTREACHED */
  257. }
  258. /* converting label name from QString to std::string*/
  259. string_buffer = anElement->text();
  260. QByteArray array = string_buffer.toAscii();
  261. std::string labelName(array.data());
  262. CategoryInfo label;
  263. label.setID(id);
  264. label.setCategoryName(labelName);
  265. label.setColor(color);
  266. labels_.push_back(label);
  267. return true;
  268. }
  269. //! A protected member parsing string data and returning a BoundingBox from it
  270. /*!
  271. * format is x;y;w;h where w - width and h - height
  272. */
  273. BoundingBox
  274. ImageInfo::BBoxFromData(
  275. QString *aBBoxData,
  276. int &id
  277. )
  278. {
  279. BoundingBox bbox;
  280. bbox.setID(id);
  281. QString buffer;
  282. int startPos = 0;
  283. bool ok = 1;
  284. int counter = 0;
  285. for (int i = 0; i < aBBoxData->size(); i++) {
  286. if (';' != aBBoxData->at(i))
  287. continue;
  288. buffer = aBBoxData->mid(startPos, i - startPos);
  289. int bboxData = buffer.toInt(&ok, 10);
  290. if (!ok) {
  291. cout <<
  292. "BBoxFromData: "
  293. "bbox format is corrupted\n";
  294. break;
  295. }
  296. if (!counter) {
  297. bbox.setTopLeft(bboxData, 0);
  298. counter++;
  299. }
  300. else if (1 == counter) {
  301. int x = bbox.topLeft().x;
  302. bbox.setTopLeft(x, bboxData);
  303. counter++;
  304. }
  305. else if (2 == counter) {
  306. bbox.setWidth(bboxData);
  307. counter++;
  308. }
  309. else if (3 == counter) {
  310. bbox.setHeight(bboxData);
  311. counter++;
  312. }
  313. startPos = i + 1;
  314. }
  315. if (!bbox.isValid() || !ok) {
  316. cout <<
  317. "BBoxFromData: "
  318. "bbox format is corrupted\n";
  319. bbox.setTopLeft(0, 0);
  320. bbox.setBottomRight(0, 0);
  321. }
  322. return bbox;
  323. }
  324. //! A protected member parsing string data and returning a Polygon from it
  325. /*!
  326. * format is x0;y0;x1;y1;...
  327. */
  328. Polygon
  329. ImageInfo::polyFromData(
  330. QString *aPolyData
  331. )
  332. {
  333. Polygon poly;
  334. QPoint point;
  335. QString buffer;
  336. int startPos = 0;
  337. bool ok = 1;
  338. /* indicates whether coordinate x or y */
  339. bool evenFlag = 0;
  340. for (int i = 0; i < aPolyData->size(); i++) {
  341. /* ";" is a separator */
  342. if (';' != aPolyData->at(i))
  343. continue;
  344. buffer = aPolyData->mid(startPos, i - startPos);
  345. int polyCoor = buffer.toInt(&ok, 10);
  346. if (!ok) {
  347. cout <<
  348. "polyFromData: "
  349. "poly format is corrupted\n";
  350. break;
  351. }
  352. if (!evenFlag) {
  353. point.setX(polyCoor);
  354. evenFlag = 1;
  355. }
  356. else {
  357. point.setY(polyCoor);
  358. poly.push(point.x(), point.y());
  359. evenFlag = 0;
  360. }
  361. startPos = i + 1;
  362. }
  363. /* last coordinate was Xi what means an error or
  364. last converting from string was not successful */
  365. if (evenFlag || !ok) {
  366. cout <<
  367. "polyFromData: "
  368. "poly format is corrupted\n";
  369. //poly.clear();
  370. }
  371. return poly;
  372. }
  373. //!
  374. ImageT< unsigned int >
  375. ImageInfo::imageTFromData(
  376. const int &aWidth,
  377. const int &aHeight,
  378. QString *aPureData
  379. )
  380. {
  381. int startPos = 0;
  382. QString buffer = 0;
  383. ImageT< unsigned int > image(aWidth, aHeight);
  384. int y = 0;
  385. int x = 0;
  386. bool ok = 0;
  387. for (int i = 0; i < aPureData->size(); i++) {
  388. if ('\n' == aPureData->at(i)) {
  389. y++;
  390. x = 0;
  391. startPos = i + 1;
  392. continue;
  393. }
  394. /* ";" is a separator */
  395. if (';' != aPureData->at(i))
  396. continue;
  397. buffer = aPureData->mid(startPos, i - startPos);
  398. int pixel = buffer.toInt(&ok, 10);
  399. if (!ok) {
  400. cout <<
  401. "imageTFromData: "
  402. "pure data format is corrupted\n";
  403. image = 0;
  404. return image;
  405. /* NOTREACHED */
  406. }
  407. image.setPixel(x, y, pixel);
  408. x++;
  409. startPos = i + 1;
  410. }
  411. return image;
  412. }
  413. #endif //NICE_USELIB_QT4_XML
  414. //! returns pointer to labels_ list
  415. const std::list< CategoryInfo > *
  416. ImageInfo::labels() const
  417. {
  418. return &labels_;
  419. }
  420. //! returns pointer to bboxes_ list
  421. const std::list< BoundingBox > *
  422. ImageInfo::bboxes() const
  423. {
  424. return &bboxes_;
  425. }
  426. //! returns pointer to polys_ list
  427. const std::list< Polygon > *
  428. ImageInfo::polys() const
  429. {
  430. return &polys_;
  431. }
  432. //! returns ImageT object labeled_image_
  433. ImageT< unsigned int >
  434. ImageInfo::labeledImage() const
  435. {
  436. return labeled_image_;
  437. }
  438. //! returns tags
  439. std::string
  440. ImageInfo::tags() const
  441. {
  442. return tags_;
  443. }
  444. //! returns path to the original image
  445. std::string
  446. ImageInfo::imagePath() const
  447. {
  448. return image_path_;
  449. }
  450. //! returns string with the image description
  451. std::string
  452. ImageInfo::imageDescription() const
  453. {
  454. return image_description_;
  455. }
  456. //! returns path to the image segmented by ImageLabeler tool
  457. std::string
  458. ImageInfo::segmentedImagePath() const
  459. {
  460. return segmented_image_path_;
  461. }
  462. /*
  463. *
  464. */