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