ImageLabeler.h 13 KB

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