ImageInfo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. continue;
  88. }
  89. QByteArray array = string_buffer.toAscii();
  90. segmented_image_path_ = string(array.data());
  91. }
  92. /* image description */
  93. else if (element.tagName() == "description") {
  94. string_buffer = element.text();
  95. if (string_buffer.isEmpty()) {
  96. continue;
  97. }
  98. QByteArray array = string_buffer.toAscii();
  99. image_description_ = string(array.data());
  100. }
  101. /* tags */
  102. else if (element.tagName() == "tags") {
  103. string_buffer = element.text();
  104. if (string_buffer.isEmpty()) {
  105. rootNode = rootNode.nextSibling();
  106. cout << "tags are empty\n";
  107. continue;
  108. }
  109. QByteArray array = string_buffer.toAscii();
  110. //TODO: make parsing into the string list
  111. tags_ = string(array.data());
  112. }
  113. /* legend */
  114. else if (element.tagName() == "legend") {
  115. loadLegendFromElement(&element);
  116. }
  117. /* objects */
  118. else if (element.tagName() == "objects") {
  119. QDomNode subNode = element.firstChild();
  120. QDomElement subElement;
  121. while(!subNode.isNull()) {
  122. subElement = subNode.toElement();
  123. if (subElement.isNull() || subElement.text().isEmpty()) {
  124. subNode = subNode.nextSibling();
  125. continue;
  126. }
  127. string_buffer = subElement.attribute("id");
  128. bool ok = 1;
  129. int id = string_buffer.toInt(&ok, 10);
  130. if (!ok) {
  131. cout << "loadImageInfo: "
  132. "poly id format is corrupted\n";
  133. subNode = subNode.nextSibling();
  134. continue;
  135. }
  136. string_buffer = subElement.text();
  137. if (subElement.tagName() == "bbox") {
  138. BoundingBox bbox = BBoxFromData(&string_buffer);
  139. bbox.setID(id);
  140. bboxes_.push_back(bbox);
  141. }
  142. if (subElement.tagName() == "poly") {
  143. Polygon poly = polyFromData(&string_buffer);
  144. poly.setID(id);
  145. polys_.push_back(poly);
  146. }
  147. subNode = subNode.nextSibling();
  148. }
  149. }
  150. /* image size */
  151. else if (element.tagName() == "image_size") {
  152. string_buffer = element.text();
  153. if (string_buffer.isEmpty()) {
  154. cout << "loadImageInfo: "
  155. "image size format is corrupted\n";
  156. return false;
  157. /* NOTREACHED */
  158. }
  159. QString buffer;
  160. int size = string_buffer.size();
  161. bool ok = 0;
  162. for (int i = 0; i < size; i++) {
  163. /* ";" is a separator */
  164. if (';' != string_buffer.at(i))
  165. continue;
  166. buffer = string_buffer.mid(0, i);
  167. width = buffer.toInt(&ok, 10);
  168. if (!ok) {
  169. cout <<
  170. "loadImageInfo: "
  171. "image size format is corrupted\n";
  172. return false;
  173. /* NOTREACHED */
  174. }
  175. buffer = string_buffer.mid(i + 1, size - (i + 1));
  176. height = buffer.toInt(&ok, 10);
  177. if (!ok) {
  178. cout <<
  179. "loadImageInfo: "
  180. "image size format is corrupted";
  181. return false;
  182. /* NOTREACHED */
  183. }
  184. break;
  185. }
  186. }
  187. else if (element.tagName() == "pure_data") {
  188. string_buffer = element.text();
  189. labeled_image_ = imageTFromData(width, height, &string_buffer);
  190. }
  191. }
  192. rootNode = rootNode.nextSibling();
  193. }
  194. #endif //NICE_USELIB_QT4_XML
  195. return true;
  196. }
  197. #ifdef NICE_USELIB_QT4_XML
  198. //! A member loading legend from xml node
  199. /*!
  200. * \param[in] anElement a pointer to the object containing all the legend
  201. */
  202. void
  203. ImageInfo::loadLegendFromElement(QDomElement *anElement)
  204. {
  205. if (!anElement) {
  206. return;
  207. /* NOTREACHED */
  208. }
  209. QDomNode subNode = anElement->firstChild();
  210. QDomElement subElement;
  211. while(!subNode.isNull()) {
  212. subElement = subNode.toElement();
  213. if (!subElement.isNull() && !subElement.text().isEmpty())
  214. loadCategoryInfo(&subElement);
  215. subNode = subNode.nextSibling();
  216. }
  217. }
  218. //! Loads one category info(label) from xml QDomElement
  219. /*!
  220. * \param[in] anElement an object containing category info data
  221. */
  222. bool
  223. ImageInfo::loadCategoryInfo(QDomElement *anElement)
  224. {
  225. QString string_buffer;
  226. int id = -1;
  227. bool isMain;
  228. uint color = 0xff000000;
  229. /* id attribute */
  230. string_buffer = anElement->attribute("id");
  231. bool ok = 0;
  232. id = string_buffer.toInt(&ok, 10);
  233. if (!ok) {
  234. cout <<
  235. "loadCategoryInfo: "
  236. "label id format is corrupted\n";
  237. return false;
  238. /* NOTREACHED */
  239. }
  240. /* isMain attribute */
  241. string_buffer = anElement->attribute("isMain");
  242. isMain = string_buffer.toInt(&ok, 2);
  243. if (!ok) {
  244. cout <<
  245. "loadCategoryInfo: "
  246. "label isMain flag format is corrupted\n";
  247. return false;
  248. /* NOTREACHED */
  249. }
  250. /* color attribute */
  251. string_buffer = anElement->attribute("color");
  252. color = string_buffer.toUInt(&ok, 16);
  253. if (!ok) {
  254. cout <<
  255. "loadCategoryInfo: "
  256. "label color format is corrupted\n";
  257. return false;
  258. /* NOTREACHED */
  259. }
  260. /* converting label name from QString to std::string*/
  261. string_buffer = anElement->text();
  262. QByteArray array = string_buffer.toAscii();
  263. std::string labelName(array.data());
  264. CategoryInfo label;
  265. label.setID(id);
  266. label.setCategoryName(labelName);
  267. label.setColor(color);
  268. labels_.push_back(label);
  269. return true;
  270. }
  271. //! A protected member parsing string data and returning a BoundingBox from it
  272. /*!
  273. * format is x;y;w;h where w - width and h - height
  274. */
  275. BoundingBox
  276. ImageInfo::BBoxFromData(
  277. QString *aBBoxData
  278. )
  279. {
  280. BoundingBox bbox;
  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. */