ImageLabeler.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * \file ImageLabeler.h
  3. *
  4. * Created on: Oct 4, 2011
  5. * Author: Gapchich Vladislav
  6. */
  7. #ifndef __IMAGELABELER_H__
  8. #define __IMAGELABELER_H__
  9. #include "ImageHolder.h"
  10. #include "LineEditForm.h"
  11. #include "OptionsForm.h"
  12. #include <QMainWindow>
  13. #include <QDir>
  14. #include "ui_ImageLabeler.h"
  15. /* forward declarations */
  16. class QMenuBar;
  17. class QMenu;
  18. class QAction;
  19. class QHBoxLayout;
  20. class QVBoxLayout;
  21. class QGridLayout;
  22. class QPushButton;
  23. class QPixmap;
  24. class QLabel;
  25. class QScrollArea;
  26. class QFrame;
  27. class QListWidget;
  28. class QListWidgetItem;
  29. class QButtonGroup;
  30. class QDomDocument;
  31. class QDomElement;
  32. class QSettings;
  33. //! Structure keeps path to the image and it's flags
  34. /*
  35. * \see loadInfo(QString filename)
  36. * \see loadPascalFile(QString aFilename, QString aPath)
  37. *
  38. * Image could be labeled before so you can load all the info about it
  39. * using loadInfo(QString filename)
  40. * pas_ means it was loaded from the file with PASCAL format
  41. */
  42. struct Image {
  43. QString image_;
  44. bool labeled_;
  45. bool pas_;
  46. };
  47. //! \brief Main widget which contains all GUI elements
  48. //! and connect them with each other.
  49. /*
  50. * \\see ImageHolder
  51. */
  52. class ImageLabeler : public QMainWindow, public Ui::ImageLabelerGui
  53. {
  54. Q_OBJECT
  55. protected:
  56. /* events */
  57. void resizeEvent(QResizeEvent *anEvent);
  58. void mousePressEvent(QMouseEvent *anEvent);
  59. void wheelEvent(QWheelEvent *anEvent);
  60. void keyPressEvent(QKeyEvent *anEvent);
  61. void keyReleaseEvent(QKeyEvent *anEvent);
  62. void closeEvent(QCloseEvent *anEvent);
  63. bool readSettings(QSettings *aSettings);
  64. bool writeSettings(QSettings *aSettings);
  65. void getImagesFromDir(const QDir &dir);
  66. void showWarning(const QString &text);
  67. bool askForUnsavedData();
  68. void loadLegendFromNode(QDomElement *anElement);
  69. void addLabel(
  70. int aLabelID,
  71. bool isMain,
  72. QString aLabel
  73. );
  74. void addPoly(Polygon *poly);
  75. void addBBox(BoundingBox *bbox);
  76. void addPolyFromData(
  77. QString *aPolyData,
  78. int *labelID
  79. );
  80. void addBBoxFromData(
  81. QString *aBBoxData,
  82. int *labelID
  83. );
  84. Polygon polyFromData(
  85. QString *aPolyData
  86. );
  87. Polygon polyFromListItemText(
  88. QString *aString,
  89. int *oldID
  90. );
  91. bool BBoxFromData(const QString *aBBoxData, BoundingBox &p_BBox);
  92. BoundingBox BBoxFromListItemText(
  93. QString *aString,
  94. int *oldID
  95. );
  96. void addBBoxArea(
  97. int anID,
  98. BoundingBox aBBox,
  99. int itemID = -1
  100. );
  101. void addPolyArea(
  102. int aPolyID,
  103. Polygon aPoly,
  104. int itemID = -1
  105. );
  106. bool deleteArea(QListWidgetItem *anItem);
  107. bool toggleLabelPriority(QListWidgetItem *anItem);
  108. void enableTools();
  109. void disableTools();
  110. void legendToXml(QDomDocument *aDoc, QDomElement *aRoot);
  111. void objectsToXml(QDomDocument *aDoc, QDomElement *aRoot);
  112. void addImage(Image *anImage);
  113. bool loadInfo(QString filename);
  114. bool loadPascalFile(QString aFilename, QString aPath = QString());
  115. bool loadPascalPolys(QString aFilename);
  116. bool selectImage(int anImageID);
  117. void setLabelColor(int anID, QColor aColor);
  118. public:
  119. ImageLabeler(QWidget *aParent = 0, QString aSettingsPath = QString());
  120. virtual ~ImageLabeler();
  121. public slots:
  122. void addLabel();
  123. void editLabel(QListWidgetItem *anItem);
  124. void removeLabel();
  125. void removeLabel(int aLabelID);
  126. void setLabelID(QListWidgetItem *anItem);
  127. void toggleLabelPriority();
  128. void focusOnArea();
  129. void editArea();
  130. void deleteArea();
  131. void saveAllInfo();
  132. void saveSegmentedPicture();
  133. void saveLegend();
  134. void loadImage();
  135. void loadImages();
  136. void loadInfo();
  137. void loadPascalFile();
  138. void loadPascalPolys();
  139. void loadLegendFromFile();
  140. /*! \brief Presents the next image of the list of images.
  141. *
  142. * Depending on the step the next image (default) or an arbituary next image is shown.
  143. * Example: using keyboard short cut Ctrl + LeftKey presents the next (+1) image,
  144. * and keyboard short cut Ctrl + UpKey presents the fifth next (+5) image.
  145. *
  146. * \param iImageStep incremental step for the next image to show (default is 1)
  147. *
  148. */
  149. void nextImage(int iImageStep = 1);
  150. /*! \brief Presents the previous image of the list of images.
  151. *
  152. * Depending on the step the previous image (default) or an arbituary previous image is shown.
  153. * Example: using keyboard short cut Ctrl + RightKey presents the previous (-1) image,
  154. * and keyboard short cut Ctrl + DownKey presents the fifth previous (-5) image.
  155. *
  156. * \param iImageStep incremental step for the previous image to show (default is 1)
  157. *
  158. */
  159. void prevImage(int iImageStep = 1);
  160. void setBoundingBoxTool(bool aButtonPressed);
  161. void setPolygonTool(bool aButtonPressed);
  162. void generateColors();
  163. void confirmSelection();
  164. void clearAll();
  165. void clearAllTool();
  166. void clearLabelList();
  167. void clearLabelColorList();
  168. void areaListPopupMenu(const QPoint &aPos);
  169. void labelListPopupMenu(const QPoint &aPos);
  170. void imageListPopupMenu(const QPoint &);
  171. void setDataFromForm(QString aData);
  172. void onSelectionStarted();
  173. void onAreaItemChange(QListWidgetItem *);
  174. void onAreaEdit();
  175. void setPureData();
  176. void setLabelColor();
  177. void viewNormal();
  178. void viewSegmented();
  179. void interruptSearch();
  180. void selectImage(QListWidgetItem *);
  181. void removeImage();
  182. void writeSettings();
  183. void readSettings();
  184. private:
  185. /* popup menu */
  186. //! \brief popup menu for area_list_
  187. //! \see list_areas_
  188. QMenu *popup_area_list_;
  189. //! \see deleteArea()
  190. QAction *action_delete_area_;
  191. //! \see editArea()
  192. QAction *action_edit_area_;
  193. //! \brief popup menu for list_label_
  194. //! \see list_label_
  195. QMenu *popup_label_list_;
  196. //! \see toggleLabelPriority()
  197. QAction *action_toggle_priority_;
  198. //! \see setLabelColor(int anID, QColor aColor)
  199. QAction *action_set_color_;
  200. //! \see removeLabel()
  201. QAction *action_delete_label_;
  202. //! \brief popup menu for list_images_
  203. //! \see list_images
  204. QMenu *popup_images_list_;
  205. //! \see removeImage()
  206. QAction *action_remove_image_;
  207. //! \brief object containing current loaded image
  208. //! \see image_holder_
  209. QPixmap *image_;
  210. //! widget containing the image_(inherited from QLabel)
  211. ImageHolder *image_holder_;
  212. //! \brief widget for editing tags and image description
  213. //! \see tags_
  214. //! \see image_description_
  215. LineEditForm line_edit_form_;
  216. //! widget containing possible options
  217. OptionsForm options_form_;
  218. /* variables */
  219. //! keeps current keyboard modifier(like ctrl, shift, alt etc)
  220. Qt::KeyboardModifiers keyboard_modifier_;
  221. //! number of the main label
  222. int main_label_;
  223. //! \brief 2d array for the segmented image
  224. //! \see setPureData()
  225. int **pure_data_;
  226. //! \brief number of selected label in the list_label_
  227. //! \see list_label_
  228. int label_ID_;
  229. //! \brief number of current image in the list_images_
  230. //! \see list_images_
  231. int image_ID_;
  232. //! contains current image description
  233. QString image_description_;
  234. //! contains tags for the current image
  235. QString tags_;
  236. //! "root" path to the directory where all the PASCAL data is located
  237. QString PASCALpath_;
  238. //! path to the currenlty loaded image
  239. QString current_image_;
  240. //! path to the segmented image
  241. QString segmented_image_;
  242. //! \brief list of all selected and confirmed rectangular areas
  243. //! \see addBBox(BoundingBox *bbox)
  244. QList< BoundingBox * > list_bounding_box_;
  245. //! \brief list of all selected and confirmed polygon areas
  246. //! \see addPoly(Polygon *poly)
  247. QList< Polygon * > list_polygon_;
  248. //! contains the paths for all loaded images
  249. QList< Image > *list_images_;
  250. //! list of label colors
  251. QList< uint > list_label_colors_;
  252. //! \brief buffer for manual list_areas_ items editing
  253. //! \see onAreaItemChange(QListWidgetItem *anItem)
  254. QString old_area_string_;
  255. /* options */
  256. //! enables/disables automatic color generation before image segmenting
  257. bool auto_color_generation_;
  258. /* flags */
  259. //! \brief flag used to interrupt recursive search of the images
  260. //! \see interruptSearch()
  261. //! \see loadImages()
  262. //! \see getImagesFromDir(const QDir &dir)
  263. bool interrupt_search_;
  264. //! flag indicating whether there is an unsaved data or not
  265. bool unsaved_data_;
  266. //! pointer to object used to read and write application settings
  267. QSettings *settings_;
  268. };
  269. #endif /* __IMAGELABELER_H__ */
  270. /*
  271. *
  272. */