ImageLabeler.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. /* forward declarations */
  15. class QMenuBar;
  16. class QMenu;
  17. class QAction;
  18. class QHBoxLayout;
  19. class QVBoxLayout;
  20. class QGridLayout;
  21. class QPushButton;
  22. class QPixmap;
  23. class QLabel;
  24. class QScrollArea;
  25. class QFrame;
  26. class QListWidget;
  27. class QListWidgetItem;
  28. class QButtonGroup;
  29. class QDomDocument;
  30. class QDomElement;
  31. class QSettings;
  32. //! Structure keeps path to the image and it's flags
  33. /*
  34. * \see loadInfo(QString filename)
  35. * \see loadPascalFile(QString aFilename, QString aPath)
  36. *
  37. * Image could be labeled before so you can load all the info about it
  38. * using loadInfo(QString filename)
  39. * pas_ means it was loaded from the file with PASCAL format
  40. */
  41. struct Image {
  42. QString image_;
  43. bool labeled_;
  44. bool pas_;
  45. };
  46. //! \brief Main widget which contains all GUI elements
  47. //! and connect them with each other.
  48. /*
  49. * \\see ImageHolder
  50. */
  51. class ImageLabeler : public QMainWindow
  52. {
  53. Q_OBJECT
  54. protected:
  55. /* events */
  56. void resizeEvent(QResizeEvent *anEvent);
  57. void mousePressEvent(QMouseEvent *anEvent);
  58. void wheelEvent(QWheelEvent *anEvent);
  59. void keyPressEvent(QKeyEvent *anEvent);
  60. void keyReleaseEvent(QKeyEvent *anEvent);
  61. void closeEvent(QCloseEvent *anEvent);
  62. bool readSettings(QSettings *aSettings);
  63. bool writeSettings(QSettings *aSettings);
  64. void getImagesFromDir(const QDir &dir);
  65. void showWarning(const QString &text);
  66. bool askForUnsavedData();
  67. void loadLegendFromNode(QDomElement *anElement);
  68. void addLabel(
  69. int aLabelID,
  70. bool isMain,
  71. QString aLabel
  72. );
  73. void addPoly(Polygon *poly);
  74. void addBBox(BoundingBox *bbox);
  75. void addPolyFromData(
  76. QString *aPolyData,
  77. int *labelID
  78. );
  79. void addBBoxFromData(
  80. QString *aBBoxData,
  81. int *labelID
  82. );
  83. Polygon polyFromData(
  84. QString *aPolyData
  85. );
  86. Polygon polyFromListItemText(
  87. QString *aString,
  88. int *oldID
  89. );
  90. BoundingBox BBoxFromData(
  91. QString *aBBoxData
  92. );
  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 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. void nextImage();
  141. void prevImage();
  142. void setBoundingBoxTool(bool aButtonPressed);
  143. void setPolygonTool(bool aButtonPressed);
  144. void generateColors();
  145. void confirmSelection();
  146. void clearAll();
  147. void clearAllTool();
  148. void clearLabelList();
  149. void clearLabelColorList();
  150. void areaListPopupMenu(const QPoint &aPos);
  151. void labelListPopupMenu(const QPoint &aPos);
  152. void imageListPopupMenu(const QPoint &);
  153. void setDataFromForm(QString aData);
  154. void onSelectionStarted();
  155. void onAreaItemChange(QListWidgetItem *);
  156. void onAreaEdit();
  157. void setPureData();
  158. void setLabelColor();
  159. void viewNormal();
  160. void viewSegmented();
  161. void interruptSearch();
  162. void selectImage(QListWidgetItem *);
  163. void removeImage();
  164. void writeSettings();
  165. void readSettings();
  166. private:
  167. /* menu */
  168. //! main menu bar contains all other menus
  169. QMenuBar *menu_bar_;
  170. //! \brief contains all actions connected with file loading and saving files.
  171. //! also contains menu_pascal_
  172. QMenu *menu_file_;
  173. //! contains actions for loading PASCAL xml file and PASCAL polygon
  174. QMenu *menu_pascal_;
  175. //! \brief contains action_view_normal_ and action_view_segmented_
  176. //! \see action_view_normal_ \see action_view_segmented_
  177. QMenu *menu_view_;
  178. /*! \brief contains tools, undo, redo, description adding and options
  179. * \see action_undo_
  180. * \see action_redo_
  181. * \see action_bound_box_tool_
  182. * \see action_polygon_tool_
  183. * \see action_tagging_tool_
  184. * \see action_add_description_
  185. * \see action_options_
  186. */
  187. QMenu *menu_edit_;
  188. //! empty yet
  189. QMenu *menu_help_;
  190. /* menu file */
  191. //! \see loadImages()
  192. QAction *action_open_images_;
  193. //! \see loadImage()
  194. QAction *action_open_image_;
  195. //! \see loadInfo()
  196. QAction *action_open_labeled_image_;
  197. //! \see loadLegendFromFile()
  198. QAction *action_load_legend_;
  199. //! \see saveAllInfo()
  200. QAction *action_save_all_;
  201. //! \see saveSegmentedPicture()
  202. QAction *action_save_segmented_;
  203. //! \see saveLegend()
  204. QAction *action_save_legend_;
  205. //! closes the application
  206. QAction *action_quit_;
  207. /* menu pascal */
  208. //! \see loadPascalFile();
  209. QAction *action_load_pascal_file_;
  210. //! \see loadPascalPolys();
  211. QAction *action_load_pascal_poly_;
  212. /* menu view */
  213. //! loads an image from current_image_ \see viewNormal()
  214. QAction *action_view_normal_;
  215. //! loads an image from segmented_image_ \see viewSegmented()
  216. QAction *action_view_segmented_;
  217. /* menu edit */
  218. //! \see ImageHolder::undo()
  219. QAction *action_undo_;
  220. //! \see ImageHolder::redo()
  221. QAction *action_redo_;
  222. //! \see setBoundingBoxTool(bool aButtonPressed)
  223. QAction *action_bound_box_tool_;
  224. //! \see setPolygonTool(bool aButtonPressed)
  225. QAction *action_polygon_tool_;
  226. //! \see setTags()
  227. QAction *action_tagging_tool_;
  228. /*! \see setDataFromForm(QString aData)
  229. * \see image_description_
  230. */
  231. QAction *action_add_description_;
  232. //! \see OptionsForm
  233. QAction *action_options_;
  234. //! empty yet
  235. QAction *action_about_;
  236. //! empty yet
  237. QAction *action_help_content_;
  238. /* popup menu */
  239. //! \brief popup menu for area_list_
  240. //! \see list_areas_
  241. QMenu *popup_area_list_;
  242. //! \see deleteArea()
  243. QAction *action_delete_area_;
  244. //! \see editArea()
  245. QAction *action_edit_area_;
  246. //! \brief popup menu for list_label_
  247. //! \see list_label_
  248. QMenu *popup_label_list_;
  249. //! \see toggleLabelPriority()
  250. QAction *action_toggle_priority_;
  251. //! \see setLabelColor(int anID, QColor aColor)
  252. QAction *action_set_color_;
  253. //! \see removeLabel()
  254. QAction *action_delete_label_;
  255. //! \brief popup menu for list_images_
  256. //! \see list_images
  257. QMenu *popup_images_list_;
  258. //! \see removeImage()
  259. QAction *action_remove_image_;
  260. /* layouts */
  261. QHBoxLayout *layout_main_;
  262. QVBoxLayout *layout_left_;
  263. QVBoxLayout *layout_toolbox_;
  264. QVBoxLayout *layout_center_;
  265. QVBoxLayout *layout_frame_image_;
  266. QGridLayout *layout_image_widget_;
  267. QHBoxLayout *layout_center_buttons_;
  268. QVBoxLayout *layout_right_;
  269. QVBoxLayout *layout_labelbox_;
  270. QHBoxLayout *layout_labelbox_buttons_;
  271. /* widgets */
  272. //! parent for all widgets
  273. QWidget *central_widget_;
  274. //! \brief container(parent) of the image_holder_
  275. //! \see iamge_holder_
  276. QScrollArea *frame_image_;
  277. //! frame containing all the widgets in the center
  278. QFrame *frame_center_;
  279. //! frame containing all the left part
  280. QFrame *frame_toolbox_;
  281. //! frame containing all the right part
  282. QFrame *frame_labelbox_;
  283. //! \brief object containing current loaded image
  284. //! \see image_holder_
  285. QPixmap *image_;
  286. //! widget containing the image_(inherited from QLabel)
  287. ImageHolder *image_holder_;
  288. //! just an information label
  289. QLabel *label_toolbox_;
  290. //! just an information label
  291. QLabel *label_list_areas_;
  292. //! just an information label
  293. QLabel *label_list_images_;
  294. //! list widget containing labels
  295. QListWidget *list_label_;
  296. //! list widget containing selected areas data
  297. QListWidget *list_areas_;
  298. //! \brief list widget containing loaded images names
  299. //! \see list_images_
  300. QListWidget *list_images_widget_;
  301. //! \brief widget for editing tags and image description
  302. //! \see tags_
  303. //! \see image_description_
  304. LineEditForm line_edit_form_;
  305. //! widget containing possible options
  306. OptionsForm options_form_;
  307. //! \brief button switching to the bounding box tool
  308. //! \see BoundingBox
  309. //! \see setBoundingBoxTool(bool aButtonPressed)
  310. QPushButton *button_bound_box_tool_;
  311. //! \brief button switching to the bounding box tool
  312. //! \see Polygon
  313. //! \see setPolygonTool(bool aButtonPressed)
  314. QPushButton *button_polygon_tool_;
  315. //! \brief button calling LineEditForm to edit tags
  316. //! \see tags_
  317. //! \see LineEditForm
  318. QPushButton *button_tagging_tool_;
  319. //! \brief button clearing all selection
  320. //! \see clearAllTool()
  321. QPushButton *button_clear_selection_tool_;
  322. //! \brief button deleting all labels(except BACKGROUND)
  323. //! \see clearLabelList()
  324. QPushButton *button_delete_all_labels_;
  325. //! \brief button generating colors for all labels(except BACKGROUND)
  326. //! \see generateColors()
  327. QPushButton *button_generate_colors_;
  328. //! \brief button adding new label
  329. //! \see addLabel()
  330. QPushButton *button_add_label_;
  331. //! \brief button removing current label
  332. //! \see removeLabel()
  333. QPushButton *button_remove_label_;
  334. //! \brief button switching to the next image
  335. //! \see nextImage()
  336. QPushButton *button_prev_image_;
  337. //! \brief button switching to the previous image
  338. //! \see prevImage()
  339. QPushButton *button_next_image_;
  340. //! \brief button confirming current selection(selected area)
  341. //! \see configrmSelection()
  342. QPushButton *button_confirm_selection_;
  343. //! abstract group manages tool buttons state
  344. QButtonGroup *group_tools_;
  345. /* variables */
  346. //! keeps current keyboard modifier(like ctrl, shift, alt etc)
  347. Qt::KeyboardModifiers keyboard_modifier_;
  348. //! number of the main label
  349. int main_label_;
  350. //! \brief 2d array for the segmented image
  351. //! \see setPureData()
  352. int **pure_data_;
  353. //! \brief number of selected label in the list_label_
  354. //! \see list_label_
  355. int label_ID_;
  356. //! \brief number of current image in the list_images_
  357. //! \see list_images_
  358. int image_ID_;
  359. //! contains current image description
  360. QString image_description_;
  361. //! contains tags for the current image
  362. QString tags_;
  363. //! "root" path to the directory where all the PASCAL data is located
  364. QString PASCALpath_;
  365. //! path to the currenlty loaded image
  366. QString current_image_;
  367. //! path to the segmented image
  368. QString segmented_image_;
  369. //! \brief list of all selected and confirmed rectangular areas
  370. //! \see addBBox(BoundingBox *bbox)
  371. QList< BoundingBox * > list_bounding_box_;
  372. //! \brief list of all selected and confirmed polygon areas
  373. //! \see addPoly(Polygon *poly)
  374. QList< Polygon * > list_polygon_;
  375. //! contains the paths for all loaded images
  376. QList< Image > *list_images_;
  377. //! list of label colors
  378. QList< uint > list_label_colors_;
  379. //! \brief buffer for manual list_areas_ items editing
  380. //! \see onAreaItemChange(QListWidgetItem *anItem)
  381. QString old_area_string_;
  382. /* options */
  383. //! enables/disables automatic color generation before image segmenting
  384. bool auto_color_generation_;
  385. /* flags */
  386. //! \brief flag used to interrupt recursive search of the images
  387. //! \see interruptSearch()
  388. //! \see loadImages()
  389. //! \see getImagesFromDir(const QDir &dir)
  390. bool interrupt_search_;
  391. //! flag indicating whether there is an unsaved data or not
  392. bool unsaved_data_;
  393. //! pointer to object used to read and write application settings
  394. QSettings *settings_;
  395. };
  396. #endif /* __IMAGELABELER_H__ */
  397. /*
  398. *
  399. */