ImageInfo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. }
  268. //! A protected member parsing string data and returning a BoundingBox from it
  269. /*!
  270. * format is x;y;w;h where w - width and h - height
  271. */
  272. BoundingBox
  273. ImageInfo::BBoxFromData(
  274. QString *aBBoxData
  275. )
  276. {
  277. BoundingBox bbox;
  278. QString buffer;
  279. int startPos = 0;
  280. bool ok = 1;
  281. int counter = 0;
  282. for (int i = 0; i < aBBoxData->size(); i++) {
  283. if (';' != aBBoxData->at(i))
  284. continue;
  285. buffer = aBBoxData->mid(startPos, i - startPos);
  286. int bboxData = buffer.toInt(&ok, 10);
  287. if (!ok) {
  288. cout <<
  289. "BBoxFromData: "
  290. "bbox format is corrupted\n";
  291. break;
  292. }
  293. if (!counter) {
  294. bbox.setTopLeft(bboxData, 0);
  295. counter++;
  296. }
  297. else if (1 == counter) {
  298. int x = bbox.topLeft().x;
  299. bbox.setTopLeft(x, bboxData);
  300. counter++;
  301. }
  302. else if (2 == counter) {
  303. bbox.setWidth(bboxData);
  304. counter++;
  305. }
  306. else if (3 == counter) {
  307. bbox.setHeight(bboxData);
  308. counter++;
  309. }
  310. startPos = i + 1;
  311. }
  312. if (!bbox.isValid() || !ok) {
  313. cout <<
  314. "BBoxFromData: "
  315. "bbox format is corrupted\n";
  316. bbox.setTopLeft(0, 0);
  317. bbox.setBottomRight(0, 0);
  318. }
  319. return bbox;
  320. }
  321. //! A protected member parsing string data and returning a Polygon from it
  322. /*!
  323. * format is x0;y0;x1;y1;...
  324. */
  325. Polygon
  326. ImageInfo::polyFromData(
  327. QString *aPolyData
  328. )
  329. {
  330. Polygon poly;
  331. QPoint point;
  332. QString buffer;
  333. int startPos = 0;
  334. bool ok = 1;
  335. /* indicates whether coordinate x or y */
  336. bool evenFlag = 0;
  337. for (int i = 0; i < aPolyData->size(); i++) {
  338. /* ";" is a separator */
  339. if (';' != aPolyData->at(i))
  340. continue;
  341. buffer = aPolyData->mid(startPos, i - startPos);
  342. int polyCoor = buffer.toInt(&ok, 10);
  343. if (!ok) {
  344. cout <<
  345. "polyFromData: "
  346. "poly format is corrupted\n";
  347. break;
  348. }
  349. if (!evenFlag) {
  350. point.setX(polyCoor);
  351. evenFlag = 1;
  352. }
  353. else {
  354. point.setY(polyCoor);
  355. poly.push(point.x(), point.y());
  356. evenFlag = 0;
  357. }
  358. startPos = i + 1;
  359. }
  360. /* last coordinate was Xi what means an error or
  361. last converting from string was not successful */
  362. if (evenFlag || !ok) {
  363. cout <<
  364. "polyFromData: "
  365. "poly format is corrupted\n";
  366. //poly.clear();
  367. }
  368. return poly;
  369. }
  370. //!
  371. ImageT< unsigned int >
  372. ImageInfo::imageTFromData(
  373. const int &aWidth,
  374. const int &aHeight,
  375. QString *aPureData
  376. )
  377. {
  378. int startPos = 0;
  379. QString buffer = 0;
  380. ImageT< unsigned int > image(aWidth, aHeight);
  381. int y = 0;
  382. int x = 0;
  383. bool ok = 0;
  384. for (int i = 0; i < aPureData->size(); i++) {
  385. if ('\n' == aPureData->at(i)) {
  386. y++;
  387. x = 0;
  388. startPos = i + 1;
  389. continue;
  390. }
  391. /* ";" is a separator */
  392. if (';' != aPureData->at(i))
  393. continue;
  394. buffer = aPureData->mid(startPos, i - startPos);
  395. int pixel = buffer.toInt(&ok, 10);
  396. if (!ok) {
  397. cout <<
  398. "imageTFromData: "
  399. "pure data format is corrupted\n";
  400. image = 0;
  401. return image;
  402. /* NOTREACHED */
  403. }
  404. image.setPixel(x, y, pixel);
  405. x++;
  406. startPos = i + 1;
  407. }
  408. return image;
  409. }
  410. #endif //NICE_USELIB_QT4_XML
  411. //! returns pointer to labels_ list
  412. const std::list< CategoryInfo > *
  413. ImageInfo::labels() const
  414. {
  415. return &labels_;
  416. }
  417. //! returns pointer to bboxes_ list
  418. const std::list< BoundingBox > *
  419. ImageInfo::bboxes() const
  420. {
  421. return &bboxes_;
  422. }
  423. //! returns pointer to polys_ list
  424. const std::list< Polygon > *
  425. ImageInfo::polys() const
  426. {
  427. return &polys_;
  428. }
  429. //! returns ImageT object labeled_image_
  430. ImageT< unsigned int >
  431. ImageInfo::labeledImage() const
  432. {
  433. return labeled_image_;
  434. }
  435. //! returns tags
  436. std::string
  437. ImageInfo::tags() const
  438. {
  439. return tags_;
  440. }
  441. //! returns path to the original image
  442. std::string
  443. ImageInfo::imagePath() const
  444. {
  445. return image_path_;
  446. }
  447. //! returns string with the image description
  448. std::string
  449. ImageInfo::imageDescription() const
  450. {
  451. return image_description_;
  452. }
  453. //! returns path to the image segmented by ImageLabeler tool
  454. std::string
  455. ImageInfo::segmentedImagePath() const
  456. {
  457. return segmented_image_path_;
  458. }
  459. /*
  460. *
  461. */