ImageLabeler.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. bool askForKeepingAnnotations();
  69. void loadLegendFromNode(QDomElement *anElement);
  70. void addLabel(
  71. int aLabelID,
  72. bool isMain,
  73. QString aLabel
  74. );
  75. void addPoly(Polygon *poly);
  76. void addBBox(BoundingBox *bbox);
  77. void addPolyFromData(
  78. QString *aPolyData,
  79. int *labelID
  80. );
  81. void addBBoxFromData(
  82. QString *aBBoxData,
  83. int *labelID
  84. );
  85. Polygon polyFromData(
  86. QString *aPolyData
  87. );
  88. Polygon polyFromListItemText(
  89. QString *aString,
  90. int *oldID
  91. );
  92. bool BBoxFromData(const QString *aBBoxData, BoundingBox &p_BBox);
  93. BoundingBox BBoxFromListItemText(
  94. QString *aString,
  95. int *oldID
  96. );
  97. void addBBoxArea(
  98. int anID,
  99. BoundingBox aBBox,
  100. int itemID = -1
  101. );
  102. void addPolyArea(
  103. int aPolyID,
  104. Polygon aPoly,
  105. int itemID = -1
  106. );
  107. bool deleteArea(QListWidgetItem *anItem);
  108. bool toggleLabelPriority(QListWidgetItem *anItem);
  109. void enableTools();
  110. void disableTools();
  111. void legendToXml(QDomDocument *aDoc, QDomElement *aRoot);
  112. void objectsToXml(QDomDocument *aDoc, QDomElement *aRoot);
  113. void addImage(Image *anImage);
  114. bool loadInfo(QString filename);
  115. bool loadPascalFile(QString aFilename, QString aPath = QString());
  116. bool loadPascalPolys(QString aFilename);
  117. bool selectImage(int anImageID);
  118. void setLabelColor(int anID, QColor aColor);
  119. public:
  120. ImageLabeler(QWidget *aParent = 0, QString aSettingsPath = QString());
  121. virtual ~ImageLabeler();
  122. public slots:
  123. void addLabel();
  124. void editLabel(QListWidgetItem *anItem);
  125. void removeLabel();
  126. void removeLabel(int aLabelID);
  127. void setLabelID(QListWidgetItem *anItem);
  128. void toggleLabelPriority();
  129. void focusOnArea();
  130. void editArea();
  131. void deleteArea();
  132. void saveAllInfo();
  133. void saveSegmentedPicture();
  134. void saveLegend();
  135. void loadImage();
  136. void loadImages();
  137. void loadInfo();
  138. void loadPascalFile();
  139. void loadPascalPolys();
  140. void loadLegendFromFile();
  141. /*! \brief Presents the next image of the list of images.
  142. *
  143. * Depending on the step the next image (default) or an arbituary next image is shown.
  144. * Example: using keyboard short cut Ctrl + LeftKey presents the next (+1) image,
  145. * and keyboard short cut Ctrl + UpKey presents the fifth next (+5) image.
  146. *
  147. * \param iImageStep incremental step for the next image to show (default is 1)
  148. *
  149. */
  150. void nextImage(int iImageStep = 1);
  151. /*! \brief Presents the previous image of the list of images.
  152. *
  153. * Depending on the step the previous image (default) or an arbituary previous image is shown.
  154. * Example: using keyboard short cut Ctrl + RightKey presents the previous (-1) image,
  155. * and keyboard short cut Ctrl + DownKey presents the fifth previous (-5) image.
  156. *
  157. * \param iImageStep incremental step for the previous image to show (default is 1)
  158. *
  159. */
  160. void prevImage(int iImageStep = 1);
  161. void setBoundingBoxTool(bool aButtonPressed);
  162. void setPolygonTool(bool aButtonPressed);
  163. void generateColors();
  164. void confirmSelection();
  165. void clearAll();
  166. void clearAllTool();
  167. void clearLabelList();
  168. void clearLabelColorList();
  169. void areaListPopupMenu(const QPoint &aPos);
  170. void labelListPopupMenu(const QPoint &aPos);
  171. void imageListPopupMenu(const QPoint &);
  172. void setDataFromForm(QString aData);
  173. void onSelectionStarted();
  174. void onAreaItemChange(QListWidgetItem *);
  175. void onAreaEdit();
  176. void setPureData();
  177. void setLabelColor();
  178. void viewNormal();
  179. void viewSegmented();
  180. void interruptSearch();
  181. void selectImage(QListWidgetItem *);
  182. void removeImage();
  183. void writeSettings();
  184. void readSettings();
  185. private:
  186. /* popup menu */
  187. //! \brief popup menu for area_list_
  188. //! \see list_areas_
  189. QMenu *popup_area_list_;
  190. //! \see deleteArea()
  191. QAction *action_delete_area_;
  192. //! \see editArea()
  193. QAction *action_edit_area_;
  194. //! \brief popup menu for list_label_
  195. //! \see list_label_
  196. QMenu *popup_label_list_;
  197. //! \see toggleLabelPriority()
  198. QAction *action_toggle_priority_;
  199. //! \see setLabelColor(int anID, QColor aColor)
  200. QAction *action_set_color_;
  201. //! \see removeLabel()
  202. QAction *action_delete_label_;
  203. //! \brief popup menu for list_images_
  204. //! \see list_images
  205. QMenu *popup_images_list_;
  206. //! \see removeImage()
  207. QAction *action_remove_image_;
  208. //! \brief object containing current loaded image
  209. //! \see image_holder_
  210. QPixmap *image_;
  211. //! widget containing the image_(inherited from QLabel)
  212. ImageHolder *image_holder_;
  213. //! \brief widget for editing tags and image description
  214. //! \see tags_
  215. //! \see image_description_
  216. LineEditForm line_edit_form_;
  217. //! widget containing possible options
  218. OptionsForm options_form_;
  219. /* variables */
  220. //! keeps current keyboard modifier(like ctrl, shift, alt etc)
  221. Qt::KeyboardModifiers keyboard_modifier_;
  222. //! number of the main label
  223. int main_label_;
  224. //! \brief 2d array for the segmented image
  225. //! \see setPureData()
  226. int **pure_data_;
  227. //! \brief number of selected label in the list_label_
  228. //! \see list_label_
  229. int label_ID_;
  230. //! \brief number of current image in the list_images_
  231. //! \see list_images_
  232. int image_ID_;
  233. //! contains current image description
  234. QString image_description_;
  235. //! contains tags for the current image
  236. QString tags_;
  237. //! "root" path to the directory where all the PASCAL data is located
  238. QString PASCALpath_;
  239. //! path to the currenlty loaded image
  240. QString current_image_;
  241. //! path to the segmented image
  242. QString segmented_image_;
  243. //! \brief list of all selected and confirmed rectangular areas
  244. //! \see addBBox(BoundingBox *bbox)
  245. QList< BoundingBox * > list_bounding_box_;
  246. //! \brief list of all selected and confirmed polygon areas
  247. //! \see addPoly(Polygon *poly)
  248. QList< Polygon * > list_polygon_;
  249. //! contains the paths for all loaded images
  250. QList< Image > *list_images_;
  251. //! list of label colors
  252. QList< uint > list_label_colors_;
  253. //! \brief buffer for manual list_areas_ items editing
  254. //! \see onAreaItemChange(QListWidgetItem *anItem)
  255. QString old_area_string_;
  256. /* options */
  257. //! enables/disables automatic color generation before image segmenting
  258. bool auto_color_generation_;
  259. /* flags */
  260. //! \brief flag used to interrupt recursive search of the images
  261. //! \see interruptSearch()
  262. //! \see loadImages()
  263. //! \see getImagesFromDir(const QDir &dir)
  264. bool interrupt_search_;
  265. //! flag indicating whether there is an unsaved data or not
  266. bool unsaved_data_;
  267. //! pointer to object used to read and write application settings
  268. QSettings *settings_;
  269. };
  270. #endif /* __IMAGELABELER_H__ */
  271. /*
  272. *
  273. */