ImageInfo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /**
  2. * @file ImageInfo.cpp
  3. * @brief localization info + image filename + ?
  4. * @author Erik Rodner
  5. * @date 04/16/2008
  6. */
  7. #include <vislearning/nice_nonvis.h>
  8. #include <iostream>
  9. #include "vislearning/cbaselib/ImageInfo.h"
  10. /* Qt */
  11. #ifdef NICE_USELIB_QT4_XML
  12. #include <QFile>
  13. #include <QString>
  14. #include <QByteArray>
  15. #include <QDomDocument>
  16. #include <QDomNode>
  17. #include <QDomElement>
  18. #include <QPoint>
  19. #endif //NICE_USELIB_QT4_XML
  20. using namespace OBJREC;
  21. using namespace std;
  22. using namespace NICE;
  23. ImageInfo::~ImageInfo()
  24. {
  25. //if ( lr != NULL )
  26. //delete lr;
  27. }
  28. //! A member loading labeled image from formatted xml file.
  29. /*!
  30. * \param[in] filename a std::string containing a path to the file
  31. * we need to load data from
  32. *
  33. * \see loadLegendFromNode(QDomElement *)
  34. * \see BBoxFromData(QString *aBBoxData, int *ID)
  35. * \see polyFromData(QString *aPolyData, int *labelID)
  36. */
  37. bool
  38. ImageInfo::loadImageInfo(const string &aFilename)
  39. {
  40. #ifdef NICE_USELIB_QT4_XML
  41. QString filename(aFilename.data());
  42. QDomDocument doc("Image Labeler");
  43. QFile file(filename);
  44. if (!file.open(QIODevice::ReadOnly)) {
  45. cout << "loadImageInfo:Can not open such file\n";
  46. return false;
  47. /* NOTREACHED */
  48. }
  49. QString errMsg;
  50. if (!doc.setContent(&file, &errMsg)) {
  51. QByteArray array = errMsg.toAscii();
  52. cout << array.data();
  53. //showWarning(errMsg);
  54. file.close();
  55. return false;
  56. /* NOTREACHED */
  57. }
  58. file.close();
  59. /* getting all info */
  60. QDomElement elements = doc.documentElement();
  61. QDomNode rootNode = elements.firstChild();
  62. QString string_buffer;
  63. int width = -1;
  64. int height = -1;
  65. // cout << "\nlet the parsing begin!\n";
  66. while(!rootNode.isNull()) {
  67. QDomElement element = rootNode.toElement();
  68. if(!element.isNull()) {
  69. /* path to the image */
  70. if (element.tagName() == "image") {
  71. string_buffer = element.text();
  72. if (string_buffer.isEmpty()) {
  73. cout << "loadImageInfo:The file with data"
  74. " doesn't contain path to the image\n";
  75. return false;
  76. /* NOTREACHED */
  77. }
  78. QByteArray array = string_buffer.toAscii();
  79. image_path_ = string(array.data());
  80. }
  81. /* path to the segmented image */
  82. if (element.tagName() == "segmented") {
  83. string_buffer = element.text();
  84. if (string_buffer.isEmpty()) {
  85. continue;
  86. }
  87. QByteArray array = string_buffer.toAscii();
  88. segmented_image_path_ = string(array.data());
  89. }
  90. /* image description */
  91. else if (element.tagName() == "description") {
  92. string_buffer = element.text();
  93. if (string_buffer.isEmpty()) {
  94. continue;
  95. }
  96. QByteArray array = string_buffer.toAscii();
  97. image_description_ = string(array.data());
  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);
  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. )
  277. {
  278. BoundingBox bbox;
  279. QString buffer;
  280. int startPos = 0;
  281. bool ok = 1;
  282. int counter = 0;
  283. for (int i = 0; i < aBBoxData->size(); i++) {
  284. if (';' != aBBoxData->at(i))
  285. continue;
  286. buffer = aBBoxData->mid(startPos, i - startPos);
  287. int bboxData = buffer.toInt(&ok, 10);
  288. if (!ok) {
  289. cout <<
  290. "BBoxFromData: "
  291. "bbox format is corrupted\n";
  292. break;
  293. }
  294. if (!counter) {
  295. bbox.setTopLeft(bboxData, 0);
  296. counter++;
  297. }
  298. else if (1 == counter) {
  299. int x = bbox.topLeft().x;
  300. bbox.setTopLeft(x, bboxData);
  301. counter++;
  302. }
  303. else if (2 == counter) {
  304. bbox.setWidth(bboxData);
  305. counter++;
  306. }
  307. else if (3 == counter) {
  308. bbox.setHeight(bboxData);
  309. counter++;
  310. }
  311. startPos = i + 1;
  312. }
  313. if (!bbox.isValid() || !ok) {
  314. cout <<
  315. "BBoxFromData: "
  316. "bbox format is corrupted\n";
  317. bbox.setTopLeft(0, 0);
  318. bbox.setBottomRight(0, 0);
  319. }
  320. return bbox;
  321. }
  322. //! A protected member parsing string data and returning a Polygon from it
  323. /*!
  324. * format is x0;y0;x1;y1;...
  325. */
  326. Polygon
  327. ImageInfo::polyFromData(
  328. QString *aPolyData
  329. )
  330. {
  331. Polygon poly;
  332. QPoint point;
  333. QString buffer;
  334. int startPos = 0;
  335. bool ok = 1;
  336. /* indicates whether coordinate x or y */
  337. bool evenFlag = 0;
  338. for (int i = 0; i < aPolyData->size(); i++) {
  339. /* ";" is a separator */
  340. if (';' != aPolyData->at(i))
  341. continue;
  342. buffer = aPolyData->mid(startPos, i - startPos);
  343. int polyCoor = buffer.toInt(&ok, 10);
  344. if (!ok) {
  345. cout <<
  346. "polyFromData: "
  347. "poly format is corrupted\n";
  348. break;
  349. }
  350. if (!evenFlag) {
  351. point.setX(polyCoor);
  352. evenFlag = 1;
  353. }
  354. else {
  355. point.setY(polyCoor);
  356. poly.push(point.x(), point.y());
  357. evenFlag = 0;
  358. }
  359. startPos = i + 1;
  360. }
  361. /* last coordinate was Xi what means an error or
  362. last converting from string was not successful */
  363. if (evenFlag || !ok) {
  364. cout <<
  365. "polyFromData: "
  366. "poly format is corrupted\n";
  367. //poly.clear();
  368. }
  369. return poly;
  370. }
  371. //!
  372. ImageT< unsigned int >
  373. ImageInfo::imageTFromData(
  374. const int &aWidth,
  375. const int &aHeight,
  376. QString *aPureData
  377. )
  378. {
  379. int startPos = 0;
  380. QString buffer = 0;
  381. ImageT< unsigned int > image(aWidth, aHeight);
  382. int y = 0;
  383. int x = 0;
  384. bool ok = 0;
  385. for (int i = 0; i < aPureData->size(); i++) {
  386. if ('\n' == aPureData->at(i)) {
  387. y++;
  388. x = 0;
  389. startPos = i + 1;
  390. continue;
  391. }
  392. /* ";" is a separator */
  393. if (';' != aPureData->at(i))
  394. continue;
  395. buffer = aPureData->mid(startPos, i - startPos);
  396. int pixel = buffer.toInt(&ok, 10);
  397. if (!ok) {
  398. cout <<
  399. "imageTFromData: "
  400. "pure data format is corrupted\n";
  401. image = 0;
  402. return image;
  403. /* NOTREACHED */
  404. }
  405. image.setPixel(x, y, pixel);
  406. x++;
  407. startPos = i + 1;
  408. }
  409. return image;
  410. }
  411. #endif //NICE_USELIB_QT4_XML
  412. //! returns pointer to labels_ list
  413. const std::list< CategoryInfo > *
  414. ImageInfo::labels() const
  415. {
  416. return &labels_;
  417. }
  418. //! returns pointer to bboxes_ list
  419. const std::list< BoundingBox > *
  420. ImageInfo::bboxes() const
  421. {
  422. return &bboxes_;
  423. }
  424. //! returns pointer to polys_ list
  425. const std::list< Polygon > *
  426. ImageInfo::polys() const
  427. {
  428. return &polys_;
  429. }
  430. //! returns ImageT object labeled_image_
  431. ImageT< unsigned int >
  432. ImageInfo::labeledImage() const
  433. {
  434. return labeled_image_;
  435. }
  436. //! returns tags
  437. std::string
  438. ImageInfo::tags() const
  439. {
  440. return tags_;
  441. }
  442. //! returns path to the original image
  443. std::string
  444. ImageInfo::imagePath() const
  445. {
  446. return image_path_;
  447. }
  448. //! returns string with the image description
  449. std::string
  450. ImageInfo::imageDescription() const
  451. {
  452. return image_description_;
  453. }
  454. //! returns path to the image segmented by ImageLabeler tool
  455. std::string
  456. ImageInfo::segmentedImagePath() const
  457. {
  458. return segmented_image_path_;
  459. }
  460. /*
  461. *
  462. */