ImageLabeler.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 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. void nextImage();
  142. void prevImage();
  143. void setBoundingBoxTool(bool aButtonPressed);
  144. void setPolygonTool(bool aButtonPressed);
  145. void generateColors();
  146. void confirmSelection();
  147. void clearAll();
  148. void clearAllTool();
  149. void clearLabelList();
  150. void clearLabelColorList();
  151. void areaListPopupMenu(const QPoint &aPos);
  152. void labelListPopupMenu(const QPoint &aPos);
  153. void imageListPopupMenu(const QPoint &);
  154. void setDataFromForm(QString aData);
  155. void onSelectionStarted();
  156. void onAreaItemChange(QListWidgetItem *);
  157. void onAreaEdit();
  158. void setPureData();
  159. void setLabelColor();
  160. void viewNormal();
  161. void viewSegmented();
  162. void interruptSearch();
  163. void selectImage(QListWidgetItem *);
  164. void removeImage();
  165. void writeSettings();
  166. void readSettings();
  167. private:
  168. /*
  169. * menu
  170. */
  171. //! main menu bar contains all other menus
  172. QMenuBar *menu_bar_;
  173. //! \brief contains all actions connected with file loading and saving.
  174. //! also contains menu_pascal_
  175. QMenu *menu_file_;
  176. //! \brief contains all actions connected with file saving.
  177. QMenu *menu_save_;
  178. //! \brief contains all actions connected with file saving.
  179. QMenu *menu_load_;
  180. //! contains actions for loading PASCAL xml file and PASCAL polygon
  181. QMenu *menu_pascal_;
  182. //! \brief contains action_view_normal_ and action_view_segmented_
  183. //! \see action_view_normal_ \see action_view_segmented_
  184. QMenu *menu_view_;
  185. /*! \brief contains tools, undo, redo, description adding and options
  186. * \see action_undo_
  187. * \see action_redo_
  188. * \see action_bound_box_tool_
  189. * \see action_polygon_tool_
  190. * \see action_tagging_tool_
  191. * \see action_add_description_
  192. * \see action_options_
  193. */
  194. QMenu *menu_edit_;
  195. //! empty yet
  196. QMenu *menu_help_;
  197. /* menu file */
  198. //! \see loadImages()
  199. QAction *action_open_images_;
  200. //! \see loadImage()
  201. QAction *action_open_image_;
  202. //! \see loadInfo()
  203. QAction *action_open_labeled_image_;
  204. //! \see loadLegendFromFile()
  205. QAction *action_load_legend_;
  206. //! \see saveAllInfo()
  207. QAction *action_save_all_;
  208. //! \see saveSegmentedPicture()
  209. QAction *action_save_segmented_;
  210. //! \see saveLegend()
  211. QAction *action_save_legend_;
  212. //! closes the application
  213. QAction *action_quit_;
  214. /* menu pascal */
  215. //! \see loadPascalFile();
  216. QAction *action_load_pascal_file_;
  217. //! \see loadPascalPolys();
  218. QAction *action_load_pascal_poly_;
  219. /* menu view */
  220. //! loads an image from current_image_ \see viewNormal()
  221. QAction *action_view_normal_;
  222. //! loads an image from segmented_image_ \see viewSegmented()
  223. QAction *action_view_segmented_;
  224. /* menu edit */
  225. //! \see ImageHolder::undo()
  226. QAction *action_undo_;
  227. //! \see ImageHolder::redo()
  228. QAction *action_redo_;
  229. //! \see setBoundingBoxTool(bool aButtonPressed)
  230. QAction *action_bound_box_tool_;
  231. //! \see setPolygonTool(bool aButtonPressed)
  232. QAction *action_polygon_tool_;
  233. //! \see setTags()
  234. QAction *action_tagging_tool_;
  235. /*! \see setDataFromForm(QString aData)
  236. * \see image_description_
  237. */
  238. QAction *action_add_description_;
  239. //! \see OptionsForm
  240. QAction *action_options_;
  241. //! empty yet
  242. QAction *action_about_;
  243. //! empty yet
  244. QAction *action_help_content_;
  245. /* popup menu */
  246. //! \brief popup menu for area_list_
  247. //! \see list_areas_
  248. QMenu *popup_area_list_;
  249. //! \see deleteArea()
  250. QAction *action_delete_area_;
  251. //! \see editArea()
  252. QAction *action_edit_area_;
  253. //! \brief popup menu for list_label_
  254. //! \see list_label_
  255. QMenu *popup_label_list_;
  256. //! \see toggleLabelPriority()
  257. QAction *action_toggle_priority_;
  258. //! \see setLabelColor(int anID, QColor aColor)
  259. QAction *action_set_color_;
  260. //! \see removeLabel()
  261. QAction *action_delete_label_;
  262. //! \brief popup menu for list_images_
  263. //! \see list_images
  264. QMenu *popup_images_list_;
  265. //! \see removeImage()
  266. QAction *action_remove_image_;
  267. /* layouts */
  268. QHBoxLayout *layout_main_;
  269. QVBoxLayout *layout_left_;
  270. QVBoxLayout *layout_toolbox_;
  271. QHBoxLayout *layout_imagelist_buttons_;
  272. QVBoxLayout *layout_center_;
  273. QVBoxLayout *layout_frame_image_;
  274. QGridLayout *layout_image_widget_;
  275. QHBoxLayout *layout_center_buttons_;
  276. QVBoxLayout *layout_right_;
  277. QVBoxLayout *layout_labelbox_;
  278. QHBoxLayout *layout_labelbox_buttons_;
  279. QVBoxLayout *layout_areabox_buttons_;
  280. /* widgets */
  281. //! parent for all widgets
  282. QWidget *central_widget_;
  283. //! \brief container(parent) of the image_holder_
  284. //! \see iamge_holder_
  285. QScrollArea *frame_image_;
  286. //! frame containing all the widgets in the center
  287. QFrame *frame_center_;
  288. //! frame containing all the left part
  289. QFrame *frame_toolbox_;
  290. //! frame containing all the right part
  291. QFrame *frame_labelbox_;
  292. //! \brief object containing current loaded image
  293. //! \see image_holder_
  294. QPixmap *image_;
  295. //! widget containing the image_(inherited from QLabel)
  296. ImageHolder *image_holder_;
  297. //! just an information label
  298. QLabel *label_toolbox_;
  299. //! just an information label
  300. QLabel *label_list_label_;
  301. //! just an information label
  302. QLabel *label_list_areas_;
  303. //! just an information label
  304. QLabel *label_list_images_;
  305. //! list widget containing labels
  306. QListWidget *list_label_;
  307. //! list widget containing selected areas data
  308. QListWidget *list_areas_;
  309. //! \brief list widget containing loaded images names
  310. //! \see list_images_
  311. QListWidget *list_images_widget_;
  312. //! \brief widget for editing tags and image description
  313. //! \see tags_
  314. //! \see image_description_
  315. LineEditForm line_edit_form_;
  316. //! widget containing possible options
  317. OptionsForm options_form_;
  318. //! \brief button switching to the bounding box tool
  319. //! \see BoundingBox
  320. //! \see setBoundingBoxTool(bool aButtonPressed)
  321. QPushButton *button_bound_box_tool_;
  322. //! \brief button switching to the bounding box tool
  323. //! \see Polygon
  324. //! \see setPolygonTool(bool aButtonPressed)
  325. QPushButton *button_polygon_tool_;
  326. //! \brief button calling LineEditForm to edit tags
  327. //! \see tags_
  328. //! \see LineEditForm
  329. QPushButton *button_tagging_tool_;
  330. //! \brief button clearing all selection
  331. //! \see clearAllTool()
  332. QPushButton *button_clear_selection_tool_;
  333. //! \brief button deleting all labels(except BACKGROUND)
  334. //! \see clearLabelList()
  335. QPushButton *button_delete_all_labels_;
  336. //! \brief button generating colors for all labels(except BACKGROUND)
  337. //! \see generateColors()
  338. QPushButton *button_generate_colors_;
  339. //! \brief button adding new label
  340. //! \see addLabel()
  341. QPushButton *button_add_label_;
  342. //! \brief button removing current label
  343. //! \see removeLabel()
  344. QPushButton *button_remove_label_;
  345. //! \brief button deleting current area
  346. //! \see deleteArea()
  347. QPushButton *button_delete_area_;
  348. //! \brief button allowing area text changing
  349. //! \see editArea()
  350. QPushButton *button_change_area_text_;
  351. //! \brief button allowing area modifying
  352. //! \see focusOnArea()
  353. QPushButton *button_change_area_;
  354. //! \brief button removing current image
  355. //! \see loadImage()
  356. QPushButton *button_add_image_;
  357. //! \brief button removing current image
  358. //! \see removeImage()
  359. QPushButton *button_remove_image_;
  360. //! \brief button switching to the next image
  361. //! \see nextImage()
  362. QPushButton *button_prev_image_;
  363. //! \brief button switching to the previous image
  364. //! \see prevImage()
  365. QPushButton *button_next_image_;
  366. //! \brief button confirming current selection(selected area)
  367. //! \see configrmSelection()
  368. QPushButton *button_confirm_selection_;
  369. //! abstract group manages tool buttons state
  370. QButtonGroup *group_tools_;
  371. /* variables */
  372. //! keeps current keyboard modifier(like ctrl, shift, alt etc)
  373. Qt::KeyboardModifiers keyboard_modifier_;
  374. //! number of the main label
  375. int main_label_;
  376. //! \brief 2d array for the segmented image
  377. //! \see setPureData()
  378. int **pure_data_;
  379. //! \brief number of selected label in the list_label_
  380. //! \see list_label_
  381. int label_ID_;
  382. //! \brief number of current image in the list_images_
  383. //! \see list_images_
  384. int image_ID_;
  385. //! contains current image description
  386. QString image_description_;
  387. //! contains tags for the current image
  388. QString tags_;
  389. //! "root" path to the directory where all the PASCAL data is located
  390. QString PASCALpath_;
  391. //! path to the currenlty loaded image
  392. QString current_image_;
  393. //! path to the segmented image
  394. QString segmented_image_;
  395. //! \brief list of all selected and confirmed rectangular areas
  396. //! \see addBBox(BoundingBox *bbox)
  397. QList< BoundingBox * > list_bounding_box_;
  398. //! \brief list of all selected and confirmed polygon areas
  399. //! \see addPoly(Polygon *poly)
  400. QList< Polygon * > list_polygon_;
  401. //! contains the paths for all loaded images
  402. QList< Image > *list_images_;
  403. //! list of label colors
  404. QList< uint > list_label_colors_;
  405. //! \brief buffer for manual list_areas_ items editing
  406. //! \see onAreaItemChange(QListWidgetItem *anItem)
  407. QString old_area_string_;
  408. /* options */
  409. //! enables/disables automatic color generation before image segmenting
  410. bool auto_color_generation_;
  411. /* flags */
  412. //! \brief flag used to interrupt recursive search of the images
  413. //! \see interruptSearch()
  414. //! \see loadImages()
  415. //! \see getImagesFromDir(const QDir &dir)
  416. bool interrupt_search_;
  417. //! flag indicating whether there is an unsaved data or not
  418. bool unsaved_data_;
  419. //! pointer to object used to read and write application settings
  420. QSettings *settings_;
  421. };
  422. #endif /* __IMAGELABELER_H__ */
  423. /*
  424. *
  425. */