ImageLabeler.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643
  1. /*
  2. * ImageLabeler.cpp
  3. *
  4. * Created on: Oct 4, 2011
  5. * Author: Gapchich Vladislav
  6. */
  7. #include "ImageLabeler.h"
  8. #include "functions.h"
  9. #include <QApplication>
  10. #include <QFrame>
  11. #include <QMenuBar>
  12. #include <QMenu>
  13. #include <QAction>
  14. #include <QBoxLayout>
  15. #include <QGridLayout>
  16. #include <QPixmap>
  17. #include <QLabel>
  18. #include <QScrollArea>
  19. #include <QPushButton>
  20. #include <QButtonGroup>
  21. #include <QListWidget>
  22. #include <QListWidgetItem>
  23. #include <QDesktopWidget>
  24. #include <QFileDialog>
  25. #include <QColorDialog>
  26. #include <QDir>
  27. #include <QMessageBox>
  28. #include <QListIterator>
  29. #include <QDomDocument>
  30. #include <QFile>
  31. #include <QKeyEvent>
  32. #include <QDebug>
  33. ImageLabeler::ImageLabeler(QWidget *aParent) :
  34. QMainWindow(aParent)
  35. {
  36. /*
  37. * Variables
  38. */
  39. list_images_ = new QStringList();
  40. main_label_ = -1;
  41. pure_data_ = 0;
  42. //label_ID_ = -1;
  43. /* options */
  44. auto_color_generation_ = 0;
  45. /* flags */
  46. interrupt_search_ = 0;
  47. unsaved_data_ = 0;
  48. /*
  49. * menu bar part begins
  50. */
  51. menu_bar_ = new QMenuBar(this);
  52. setMenuBar(menu_bar_);
  53. menu_file_ = new QMenu(menu_bar_);
  54. menu_file_->setTitle(tr("&File"));
  55. menu_view_ = new QMenu(menu_bar_);
  56. menu_view_->setTitle(tr("&View"));
  57. menu_edit_ = new QMenu(menu_bar_);
  58. menu_edit_->setTitle(tr("&Edit"));
  59. menu_help_ = new QMenu(menu_bar_);
  60. menu_help_->setTitle(tr("&Help"));
  61. /* menu file */
  62. action_open_images_ = new QAction(this);
  63. action_open_images_->setText(tr("&Load images"));
  64. action_open_labeled_image_ = new QAction(this);
  65. action_open_labeled_image_->setText(tr("&Open labeled image"));
  66. action_load_legend_ = new QAction(this);
  67. action_load_legend_->setText(tr("Load &legend"));
  68. action_save_labels_ = new QAction(this);
  69. action_save_labels_->setText(tr("&Save all info"));
  70. action_save_labels_->setEnabled(false);
  71. action_save_segmented_ = new QAction(this);
  72. action_save_segmented_->setText(tr("Save segmented &picture"));
  73. action_save_segmented_->setEnabled(false);
  74. action_save_legend_ = new QAction(this);
  75. action_save_legend_->setText(tr("Save &legend"));
  76. action_save_legend_->setEnabled(false);
  77. action_quit_ = new QAction(this);
  78. action_quit_->setText(tr("&Quit"));
  79. /* menu view */
  80. action_view_normal_ = new QAction(this);
  81. action_view_normal_->setText(tr("&Normal"));
  82. action_view_normal_->setEnabled(false);
  83. action_view_segmented_ = new QAction(this);
  84. action_view_segmented_->setText(tr("&Segmented"));
  85. action_view_segmented_->setEnabled(false);
  86. /* menu edit */
  87. action_undo_ = new QAction(this);
  88. action_undo_->setText(tr("&Undo"));
  89. action_undo_->setEnabled(false);
  90. action_redo_ = new QAction(this);
  91. action_redo_->setText(tr("&Redo"));
  92. action_redo_->setEnabled(false);
  93. action_bound_box_tool_ = new QAction(this);
  94. action_bound_box_tool_->setText(tr("Bounding box tool"));
  95. action_bound_box_tool_->setEnabled(false);
  96. action_polygon_tool_ = new QAction(this);
  97. action_polygon_tool_->setText(tr("&Polygon tool"));
  98. action_polygon_tool_->setEnabled(false);
  99. action_tagging_tool_ = new QAction(this);
  100. action_tagging_tool_->setText(tr("&Tagging tool"));
  101. action_tagging_tool_->setEnabled(false);
  102. action_add_description_ = new QAction(this);
  103. action_add_description_->setText(tr("&Add image description"));
  104. action_add_description_->setEnabled(false);
  105. action_options_ = new QAction(this);
  106. action_options_->setText(tr("&Options"));
  107. /* menu help */
  108. action_help_content_ = new QAction(this);
  109. action_help_content_->setText(tr("&Help content"));
  110. action_help_content_->setEnabled(false);
  111. action_about_ = new QAction(this);
  112. action_about_->setText(tr("&About"));
  113. action_about_->setEnabled(false);
  114. /* ------------------ */
  115. menu_file_->addAction(action_open_images_);
  116. menu_file_->addAction(action_open_labeled_image_);
  117. menu_file_->addAction(action_load_legend_);
  118. menu_file_->addAction(action_save_segmented_);
  119. menu_file_->addAction(action_save_legend_);
  120. menu_file_->addAction(action_save_labels_);
  121. menu_file_->addAction(action_quit_);
  122. menu_view_->addAction(action_view_normal_);
  123. menu_view_->addAction(action_view_segmented_);
  124. menu_edit_->addAction(action_undo_);
  125. menu_edit_->addAction(action_redo_);
  126. menu_edit_->addAction(action_bound_box_tool_);
  127. menu_edit_->addAction(action_polygon_tool_);
  128. menu_edit_->addAction(action_tagging_tool_);
  129. menu_edit_->addAction(action_add_description_);
  130. menu_edit_->addAction(action_options_);
  131. menu_help_->addAction(action_help_content_);
  132. menu_help_->addAction(action_about_);
  133. menu_bar_->addAction(menu_file_->menuAction());
  134. menu_bar_->addAction(menu_view_->menuAction());
  135. menu_bar_->addAction(menu_edit_->menuAction());
  136. menu_bar_->addAction(menu_help_->menuAction());
  137. /*
  138. * popup menu part begins
  139. */
  140. popup_area_list_ = new QMenu;
  141. action_delete_area_ = new QAction(this);
  142. action_delete_area_->setText(tr("&Delete area"));
  143. action_edit_area_ = new QAction(this);
  144. action_edit_area_->setText(tr("&Change area"));
  145. popup_area_list_->addAction(action_delete_area_);
  146. popup_area_list_->addAction(action_edit_area_);
  147. popup_label_list_ = new QMenu;
  148. action_set_color_ = new QAction(this);
  149. action_set_color_->setText(tr("Set &color"));
  150. action_toggle_priority_ = new QAction(this);
  151. action_toggle_priority_->setText(tr("Toggle &priority"));
  152. action_delete_label_ = new QAction(this);
  153. action_delete_label_->setText(tr("&Delete"));
  154. popup_label_list_->addAction(action_set_color_);
  155. popup_label_list_->addAction(action_toggle_priority_);
  156. popup_label_list_->addAction(action_delete_label_);
  157. /*
  158. * widgets part begins
  159. */
  160. central_widget_ = new QWidget(this);
  161. setCentralWidget(central_widget_);
  162. frame_toolbox_ = new QFrame(central_widget_);
  163. frame_toolbox_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  164. frame_toolbox_->setLineWidth(0);
  165. frame_toolbox_->setMidLineWidth(0);
  166. frame_center_ = new QFrame(central_widget_);
  167. //frame_image_->setFrameStyle(QFrame::Box | QFrame::Plain);
  168. //frame_image_->setLineWidth(1);
  169. frame_image_ = new QScrollArea(frame_center_);
  170. //frame_image_->setStyleSheet("QWidget {background: #888888;}");
  171. frame_image_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  172. frame_image_->setLineWidth(0);
  173. frame_image_->setMidLineWidth(0);
  174. frame_image_->setWidgetResizable(false);
  175. frame_image_->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
  176. frame_image_->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
  177. frame_labelbox_ = new QFrame(central_widget_);
  178. frame_labelbox_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  179. frame_labelbox_->setLineWidth(0);
  180. frame_labelbox_->setMidLineWidth(0);
  181. image_ = new QPixmap(500, 500);
  182. image_->fill(QColor(Qt::white));
  183. //image_holder_ = new ImageHolder(frame_image_);
  184. image_holder_ = new ImageHolder;//(frame_image_);
  185. image_holder_->setPixmap(*image_);
  186. //image_holder_->setStyleSheet("QLabel {background: #ffffff;}");
  187. image_holder_->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
  188. image_holder_->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
  189. image_holder_->setFocusPolicy(Qt::StrongFocus);
  190. image_holder_->setFocus();
  191. image_holder_->setScaledContents(true);
  192. image_holder_->setFrameStyle(QFrame::Box | QFrame::Plain);
  193. image_holder_->setLineWidth(0);
  194. frame_image_->setWidget(image_holder_);
  195. list_label_ = new QListWidget(central_widget_);
  196. list_label_->setContextMenuPolicy(Qt::CustomContextMenu);
  197. list_areas_ = new QListWidget(central_widget_);
  198. list_areas_->setContextMenuPolicy(Qt::CustomContextMenu);
  199. // addLabel(0, false, "BACKGROUND");
  200. // list_label_->item(0)->setFlags(Qt::ItemIsSelectable);
  201. //QListWidgetItem *background = list_label_->item(0);
  202. //qDebug() << background;
  203. //background->setFlags(Qt::ItemIsSelectable);
  204. label_toolbox_ = new QLabel(tr("Tool box"), frame_toolbox_);
  205. label_list_areas_ = new QLabel(tr("Selected areas"), central_widget_);
  206. /* buttons */
  207. button_bound_box_tool_ = new QPushButton(frame_toolbox_);
  208. button_bound_box_tool_->setText(tr("bbox"));
  209. button_bound_box_tool_->setEnabled(false);
  210. button_bound_box_tool_->setCheckable(true);
  211. button_polygon_tool_ = new QPushButton(frame_toolbox_);
  212. button_polygon_tool_->setText(tr("poly tool"));
  213. button_polygon_tool_->setEnabled(false);
  214. button_polygon_tool_->setCheckable(true);
  215. button_tagging_tool_ = new QPushButton(frame_toolbox_);
  216. button_tagging_tool_->setText(tr("tagging"));
  217. button_tagging_tool_->setEnabled(false);
  218. button_tagging_tool_->setCheckable(true);
  219. button_clear_selection_tool_ = new QPushButton(frame_toolbox_);
  220. button_clear_selection_tool_->setText(tr("clear selection"));
  221. button_clear_selection_tool_->setEnabled(false);
  222. button_generate_colors_ = new QPushButton(frame_toolbox_);
  223. button_generate_colors_->setText(tr("generate label colors"));
  224. button_generate_colors_->setEnabled(false);
  225. button_delete_all_labels_ = new QPushButton(frame_toolbox_);
  226. button_delete_all_labels_->setText(tr("delete all labels"));
  227. button_delete_all_labels_->setEnabled(false);
  228. group_tools_ = new QButtonGroup;
  229. group_tools_->addButton(button_bound_box_tool_);
  230. group_tools_->addButton(button_polygon_tool_);
  231. group_tools_->addButton(button_tagging_tool_);
  232. button_confirm_selection_ = new QPushButton(central_widget_);
  233. button_confirm_selection_->setText(tr("Confirm selection"));
  234. button_confirm_selection_->setEnabled(false);
  235. button_add_label_ = new QPushButton(frame_labelbox_);
  236. button_add_label_->setText(tr("Add label"));
  237. button_add_label_->setEnabled(false);
  238. button_remove_label_ = new QPushButton(frame_labelbox_);
  239. button_remove_label_->setText(tr("Remove label"));
  240. button_remove_label_->setEnabled(false);
  241. button_prev_image_ = new QPushButton(central_widget_);
  242. button_prev_image_->setText("←");
  243. button_next_image_ = new QPushButton(central_widget_);
  244. button_next_image_->setText("→");
  245. /*
  246. * layouts part begins
  247. */
  248. layout_main_ = new QHBoxLayout(central_widget_);
  249. layout_left_ = new QVBoxLayout();
  250. layout_toolbox_ = new QVBoxLayout();
  251. layout_center_ = new QVBoxLayout();
  252. layout_frame_image_ = new QVBoxLayout();
  253. layout_image_widget_ = new QGridLayout();
  254. layout_center_buttons_ = new QHBoxLayout();
  255. layout_right_ = new QVBoxLayout();
  256. layout_labelbox_ = new QVBoxLayout();
  257. layout_labelbox_buttons_ = new QHBoxLayout();
  258. layout_main_->addLayout(layout_left_);
  259. layout_main_->addLayout(layout_center_);
  260. layout_main_->addLayout(layout_right_);
  261. /* making the center part stretchable */
  262. layout_main_->setStretch(1, 1);
  263. /* left part */
  264. layout_left_->addWidget(frame_toolbox_);
  265. frame_toolbox_->setLayout(layout_toolbox_);
  266. layout_toolbox_->addWidget(label_toolbox_);
  267. layout_toolbox_->addWidget(button_bound_box_tool_);
  268. layout_toolbox_->addWidget(button_polygon_tool_);
  269. layout_toolbox_->addWidget(button_tagging_tool_);
  270. layout_toolbox_->addSpacing(10);
  271. layout_toolbox_->addWidget(button_clear_selection_tool_);
  272. layout_toolbox_->addWidget(button_delete_all_labels_);
  273. layout_toolbox_->addSpacing(10);
  274. layout_toolbox_->addWidget(button_generate_colors_);
  275. /* stretch is for making toolbox as small as it can be */
  276. layout_left_->addStretch(1);
  277. layout_left_->addWidget(button_confirm_selection_);
  278. /* central part */
  279. layout_center_->addWidget(frame_center_);
  280. frame_center_->setLayout(layout_frame_image_);
  281. //layout_frame_image_->addWidget(image_holder_);
  282. layout_frame_image_->setContentsMargins(0, 0, 0, 0);
  283. layout_frame_image_->addWidget(frame_image_);
  284. //frame_image_->setLayout(layout_image_widget_);
  285. // layout_image_widget_->setRowStretch(0, 1);
  286. // layout_image_widget_->setColumnStretch(0, 1);
  287. // layout_image_widget_->addWidget(image_holder_, 1, 1);
  288. // layout_image_widget_->setRowStretch(2, 1);
  289. // layout_image_widget_->setColumnStretch(2, 1);
  290. //layout_frame_image_->addStretch(1);
  291. layout_frame_image_->addLayout(layout_center_buttons_);
  292. layout_center_buttons_->addWidget(button_prev_image_);
  293. layout_center_buttons_->addWidget(button_next_image_);
  294. /* right part */
  295. layout_right_->addWidget(frame_labelbox_);
  296. frame_labelbox_->setFixedWidth(300);
  297. frame_labelbox_->setLayout(layout_labelbox_);
  298. layout_labelbox_->addLayout(layout_labelbox_buttons_);
  299. layout_labelbox_buttons_->addWidget(button_add_label_);
  300. layout_labelbox_buttons_->addWidget(button_remove_label_);
  301. layout_labelbox_->addWidget(list_label_);
  302. layout_labelbox_->addWidget(label_list_areas_);
  303. layout_labelbox_->addWidget(list_areas_);
  304. connect(
  305. action_quit_,
  306. SIGNAL(triggered()),
  307. this,
  308. SLOT(close())
  309. );
  310. connect(
  311. action_open_images_,
  312. SIGNAL(triggered()),
  313. this,
  314. SLOT(loadImages())
  315. );
  316. connect(
  317. action_open_labeled_image_,
  318. SIGNAL(triggered()),
  319. this,
  320. SLOT(loadInfo())
  321. );
  322. connect(
  323. action_load_legend_,
  324. SIGNAL(triggered()),
  325. this,
  326. SLOT(loadLegendFromFile())
  327. );
  328. connect(
  329. action_save_legend_,
  330. SIGNAL(triggered()),
  331. this,
  332. SLOT(saveLegend())
  333. );
  334. connect(
  335. action_save_segmented_,
  336. SIGNAL(triggered()),
  337. this,
  338. SLOT(saveSegmentedPicture())
  339. );
  340. connect(
  341. action_save_labels_,
  342. SIGNAL(triggered()),
  343. this,
  344. SLOT(saveAllInfo())
  345. );
  346. connect(
  347. action_view_normal_,
  348. SIGNAL(triggered()),
  349. this,
  350. SLOT(viewNormal())
  351. );
  352. connect(
  353. action_view_segmented_,
  354. SIGNAL(triggered()),
  355. this,
  356. SLOT(viewSegmented())
  357. );
  358. connect(
  359. action_undo_,
  360. SIGNAL(triggered()),
  361. image_holder_,
  362. SLOT(undo())
  363. );
  364. connect(
  365. action_redo_,
  366. SIGNAL(triggered()),
  367. image_holder_,
  368. SLOT(redo())
  369. );
  370. connect(
  371. action_add_description_,
  372. SIGNAL(triggered()),
  373. &image_description_form_,
  374. SLOT(show())
  375. );
  376. connect(
  377. action_options_,
  378. SIGNAL(triggered()),
  379. &options_form_,
  380. SLOT(show())
  381. );
  382. connect(
  383. button_add_label_,
  384. SIGNAL(clicked()),
  385. this,
  386. SLOT(addLabel())
  387. );
  388. connect(
  389. button_remove_label_,
  390. SIGNAL(clicked()),
  391. this, SLOT(removeLabel())
  392. );
  393. connect(
  394. button_next_image_,
  395. SIGNAL(clicked()),
  396. this,
  397. SLOT(nextImage())
  398. );
  399. connect(
  400. button_prev_image_,
  401. SIGNAL(clicked()),
  402. this,
  403. SLOT(prevImage())
  404. );
  405. connect(
  406. button_bound_box_tool_,
  407. SIGNAL(toggled(bool)),
  408. this,
  409. SLOT(setBoundingBoxTool(bool))
  410. );
  411. connect(
  412. button_polygon_tool_,
  413. SIGNAL(toggled(bool)),
  414. this,
  415. SLOT(setPolygonTool(bool))
  416. );
  417. connect(
  418. button_clear_selection_tool_,
  419. SIGNAL(clicked()),
  420. this,
  421. SLOT(clearAllTool())
  422. );
  423. connect(
  424. button_generate_colors_,
  425. SIGNAL(clicked()),
  426. this,
  427. SLOT(generateColors())
  428. );
  429. connect(
  430. button_delete_all_labels_,
  431. SIGNAL(clicked()),
  432. this,
  433. SLOT(clearLabelList())
  434. );
  435. connect(
  436. button_confirm_selection_,
  437. SIGNAL(clicked()),
  438. this,
  439. SLOT(confirmSelection())
  440. );
  441. connect(
  442. list_label_,
  443. SIGNAL(itemChanged(QListWidgetItem *)),
  444. this,
  445. SLOT(editLabel(QListWidgetItem *))
  446. );
  447. connect(
  448. list_label_,
  449. SIGNAL(itemClicked(QListWidgetItem *)),
  450. this,
  451. SLOT(setLabelID(QListWidgetItem *))
  452. );
  453. connect(
  454. list_areas_,
  455. SIGNAL(itemDoubleClicked(QListWidgetItem *)),
  456. image_holder_,
  457. SLOT(focusOnArea(QListWidgetItem *))
  458. );
  459. connect(
  460. list_areas_,
  461. SIGNAL(customContextMenuRequested(const QPoint &)),
  462. this,
  463. SLOT(areaListPopupMenu(const QPoint &))
  464. );
  465. connect(
  466. list_areas_,
  467. SIGNAL(itemChanged(QListWidgetItem *)),
  468. this,
  469. SLOT(onAreaItemChange(QListWidgetItem *))
  470. );
  471. connect(
  472. list_label_,
  473. SIGNAL(customContextMenuRequested(const QPoint &)),
  474. this,
  475. SLOT(labelListPopupMenu(const QPoint &))
  476. );
  477. connect(
  478. action_delete_area_,
  479. SIGNAL(triggered()),
  480. this,
  481. SLOT(deleteArea())
  482. );
  483. connect(
  484. action_edit_area_,
  485. SIGNAL(triggered()),
  486. this,
  487. SLOT(editArea())
  488. );
  489. connect(
  490. action_set_color_,
  491. SIGNAL(triggered()),
  492. this,
  493. SLOT(setLabelColor())
  494. );
  495. connect(
  496. action_toggle_priority_,
  497. SIGNAL(triggered()),
  498. this,
  499. SLOT(toggleLabelPriority())
  500. );
  501. connect(
  502. action_delete_label_,
  503. SIGNAL(triggered()),
  504. this,
  505. SLOT(removeLabel())
  506. );
  507. connect(
  508. image_holder_,
  509. SIGNAL(selectionStarted()),
  510. this,
  511. SLOT(onSelectionStarted())
  512. );
  513. connect(
  514. &image_description_form_,
  515. SIGNAL(descriptionSet(QString)),
  516. this,
  517. SLOT(setDescription(QString))
  518. );
  519. connect(
  520. image_holder_,
  521. SIGNAL(imageScaled()),
  522. this,
  523. SLOT(onImageScaled())
  524. );
  525. connect(
  526. image_holder_,
  527. SIGNAL(areaEdited()),
  528. this,
  529. SLOT(onAreaEdit())
  530. );
  531. connect(
  532. &options_form_,
  533. SIGNAL(optionsSet()),
  534. this,
  535. SLOT(onOptionsSet())
  536. );
  537. image_holder_->setBoundingBoxList(&list_bounding_box_);
  538. image_holder_->setPolygonList(&list_polygon_);
  539. image_holder_->setLabelColorList(&list_label_colors_);
  540. image_holder_->setMainLabelNum(&main_label_);
  541. image_holder_->setImage(image_);
  542. }
  543. ImageLabeler::~ImageLabeler()
  544. {
  545. delete action_quit_;
  546. delete action_open_labeled_image_;
  547. delete action_open_images_;
  548. delete action_load_legend_;
  549. delete action_save_legend_;
  550. delete action_save_segmented_;
  551. delete action_save_labels_;
  552. delete action_view_normal_;
  553. delete action_view_segmented_;
  554. delete action_undo_;
  555. delete action_redo_;
  556. delete action_bound_box_tool_;
  557. delete action_polygon_tool_;
  558. delete action_tagging_tool_;
  559. delete action_add_description_;
  560. delete action_options_;
  561. delete action_about_;
  562. delete action_help_content_;
  563. delete menu_file_;
  564. delete menu_view_;
  565. delete menu_edit_;
  566. delete menu_help_;
  567. delete menu_bar_;
  568. delete action_delete_area_;
  569. delete action_edit_area_;
  570. delete popup_area_list_;
  571. delete action_toggle_priority_;
  572. delete action_set_color_;
  573. delete action_delete_label_;
  574. delete popup_label_list_;
  575. delete image_;
  576. delete image_holder_;
  577. delete button_add_label_;
  578. delete button_remove_label_;
  579. delete button_bound_box_tool_;
  580. delete button_polygon_tool_;
  581. delete button_tagging_tool_;
  582. delete button_clear_selection_tool_;
  583. delete button_generate_colors_;
  584. delete button_delete_all_labels_;
  585. delete button_next_image_;
  586. delete button_prev_image_;
  587. delete button_confirm_selection_;
  588. delete label_list_areas_;
  589. delete label_toolbox_;
  590. delete list_areas_;
  591. delete list_label_;
  592. delete layout_toolbox_;
  593. delete layout_right_;
  594. delete layout_center_buttons_;
  595. delete layout_frame_image_;
  596. delete layout_center_;
  597. delete layout_labelbox_buttons_;
  598. delete layout_labelbox_;
  599. delete layout_left_;
  600. delete layout_main_;
  601. delete frame_labelbox_;
  602. delete frame_toolbox_;
  603. delete frame_image_;
  604. delete frame_center_;
  605. delete central_widget_;
  606. delete list_images_;
  607. //delete current_image_;
  608. if (pure_data_) {
  609. delete[] *pure_data_;
  610. delete pure_data_;
  611. }
  612. }
  613. void
  614. ImageLabeler::addLabel()
  615. {
  616. QListWidgetItem *newItem = new QListWidgetItem;
  617. QString label;
  618. int itemNum = list_label_->count();
  619. label.append(QString("%1: ").arg(itemNum));
  620. if (itemNum) {
  621. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  622. label.append("New label");
  623. }
  624. else {
  625. newItem->setFlags(
  626. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  627. Qt::ItemIsEnabled
  628. );
  629. label.append("BACKGROUND");
  630. }
  631. newItem->setText(label);
  632. QPixmap iconPix = QPixmap(20, 20);
  633. QColor color;
  634. if (0 != itemNum)
  635. color = Qt::white;
  636. else
  637. color = Qt::black;
  638. iconPix.fill(color);
  639. QIcon icon(iconPix);
  640. list_label_colors_.append(color.rgb());
  641. newItem->setIcon(icon);
  642. list_label_->addItem(newItem);
  643. list_label_->setItemSelected(newItem, true);
  644. unsaved_data_ = 1;
  645. }
  646. void
  647. ImageLabeler::addLabel(
  648. int aLabelID,
  649. bool isMain,
  650. QString aLabel
  651. )
  652. {
  653. QListWidgetItem *newItem = new QListWidgetItem;
  654. if (0 == aLabelID) {
  655. aLabel = QString("BACKGROUND");
  656. newItem->setFlags(
  657. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  658. Qt::ItemIsEnabled
  659. );
  660. }
  661. QString label;
  662. label.append(QString("%1: %2").
  663. arg(aLabelID).
  664. arg(aLabel)
  665. );
  666. /* TODO: check if there is another main label and make it common */
  667. if (isMain) {
  668. main_label_ = aLabelID;
  669. }
  670. QPixmap iconPix = QPixmap(20, 20);
  671. QColor color;
  672. if (0 != aLabelID)
  673. color = Qt::white;
  674. else
  675. color = Qt::black;
  676. iconPix.fill(color);
  677. QIcon icon(iconPix);
  678. list_label_colors_.append(color.rgb());
  679. newItem->setIcon(icon);
  680. newItem->setText(label);
  681. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  682. list_label_->addItem(newItem);
  683. list_label_->setItemSelected(newItem, true);
  684. unsaved_data_ = 1;
  685. }
  686. void
  687. ImageLabeler::editLabel(QListWidgetItem *anItem)
  688. {
  689. QString label = anItem->text();
  690. int itemRow = list_label_->row(anItem);
  691. QString prefix = QString("%1: ").arg(itemRow);
  692. if (-1 != label.indexOf(prefix)) {
  693. return;
  694. /* NOTREACHED */
  695. }
  696. label.prepend(QString("%1: ").arg(itemRow));
  697. if (main_label_ == itemRow) {
  698. label.append(" #main");
  699. }
  700. list_label_->blockSignals(true);
  701. anItem->setText(label);
  702. list_label_->blockSignals(false);
  703. unsaved_data_ = 1;
  704. }
  705. void
  706. ImageLabeler::removeLabel()
  707. {
  708. if (0 == list_label_->count()) {
  709. return;
  710. /* NOTREACHED */
  711. }
  712. //QListWidgetItem *current = list_label_->currentItem();
  713. /* we need to keep BACKGROUND category */
  714. if (label_ID_ < 1) {
  715. return;
  716. /* NOTREACHED */
  717. }
  718. if (list_label_->count() <= label_ID_ ||
  719. list_label_colors_.count() <= label_ID_) {
  720. return;
  721. /* NOTREACHED */
  722. }
  723. list_label_->takeItem(label_ID_);
  724. list_label_colors_.takeAt(label_ID_);
  725. unsaved_data_ = 1;
  726. }
  727. void
  728. ImageLabeler::setLabelID(
  729. QListWidgetItem *anItem
  730. )
  731. {
  732. Q_UNUSED(anItem)
  733. if (!list_label_->count()) {
  734. return;
  735. /* NOTREACHED */
  736. }
  737. label_ID_ = list_label_->row(anItem);
  738. }
  739. void
  740. ImageLabeler::addBBoxArea(
  741. int anID,
  742. BoundingBox aBBox,
  743. int itemID
  744. )
  745. {
  746. QListWidgetItem *newItem = new QListWidgetItem;
  747. if (-1 == itemID)
  748. itemID = list_areas_->count();
  749. QString label;
  750. label.append(QString("%1: ").arg(itemID));
  751. label.append(QString("BBox #%1; ").arg(anID));
  752. label.append(QString("LabelID: %1; ").arg(aBBox.label_ID_));
  753. label.append(
  754. QString("points:%1;%2;%3;%4; ").
  755. arg(aBBox.rect.topLeft().x()).
  756. arg(aBBox.rect.topLeft().y()).
  757. arg(aBBox.rect.bottomRight().x()).
  758. arg(aBBox.rect.bottomRight().y())
  759. );
  760. newItem->setText(label);
  761. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  762. list_areas_->insertItem(itemID, newItem);
  763. list_areas_->setItemSelected(newItem, true);
  764. }
  765. void
  766. ImageLabeler::addPolyArea(
  767. int aPolyID,
  768. Polygon aPoly,
  769. int itemID
  770. )
  771. {
  772. QListWidgetItem *newItem = new QListWidgetItem;
  773. if (-1 == itemID)
  774. itemID = list_areas_->count();
  775. QString label;
  776. label.append(QString("%1: ").arg(itemID));
  777. label.append(QString("Poly #%1; ").arg(aPolyID));
  778. label.append(QString("LabelID: %1; ").arg(aPoly.label_ID_));
  779. label.append("points:");
  780. for (int i = 0; i < aPoly.poly.count(); i++) {
  781. label.append(
  782. QString("%1;%2;").
  783. arg(aPoly.poly.point(i).x()).
  784. arg(aPoly.poly.point(i).y())
  785. );
  786. }
  787. newItem->setText(label);
  788. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  789. list_areas_->insertItem(itemID, newItem);
  790. list_areas_->setItemSelected(newItem, true);
  791. }
  792. void
  793. ImageLabeler::editArea()
  794. {
  795. bool oldState = list_areas_->blockSignals(true);
  796. if (!list_areas_->currentItem() || !list_areas_->count()) {
  797. return;
  798. /* NOTREACHED */
  799. }
  800. QListWidgetItem *current = list_areas_->currentItem();
  801. old_area_string_ = current->text();
  802. current->setFlags(current->flags() | Qt::ItemIsEditable);
  803. list_areas_->editItem(current);
  804. list_areas_->blockSignals(oldState);
  805. }
  806. void
  807. ImageLabeler::onAreaItemChange(QListWidgetItem *anItem)
  808. {
  809. list_areas_->blockSignals(true);
  810. QString areaString = anItem->text();
  811. int oldID = -1;
  812. if (-1 != areaString.indexOf("Poly")) {
  813. Polygon *poly = new Polygon;
  814. *poly = polyFromString(&areaString, &oldID);
  815. if (-1 < poly->label_ID_ && !poly->poly.isEmpty() &&
  816. -1 < oldID) {
  817. list_polygon_.takeAt(oldID);
  818. list_polygon_.insert(oldID, poly);
  819. }
  820. else
  821. anItem->setText(old_area_string_);
  822. }
  823. else if (-1 != areaString.indexOf("BBox")) {
  824. BoundingBox *bbox = new BoundingBox;
  825. *bbox = BBoxFromString(&areaString, &oldID);
  826. if (-1 < bbox->label_ID_ && -1 < oldID) {
  827. list_bounding_box_.takeAt(oldID);
  828. list_bounding_box_.insert(oldID, bbox);
  829. }
  830. else
  831. anItem->setText(old_area_string_);
  832. }
  833. else {
  834. showWarning(tr("record format is corrupted, try again"));
  835. anItem->setText(old_area_string_);
  836. }
  837. anItem->setFlags(anItem->flags() ^ Qt::ItemIsEditable);
  838. list_areas_->blockSignals(false);
  839. image_holder_->update();
  840. unsaved_data_ = 1;
  841. }
  842. void
  843. ImageLabeler::onAreaEdit()
  844. {
  845. if (!list_areas_->count() ||
  846. -1 == image_holder_->focusedSelection()) {
  847. //showWarning(tr("You haven't added any label"));
  848. return;
  849. /* NOTREACHED */
  850. }
  851. int figureID = image_holder_->focusedSelection();
  852. Figure figure = image_holder_->focusedSelectionType();
  853. for (int i = 0; i < list_areas_->count(); i++) {
  854. QListWidgetItem *item = list_areas_->item(i);
  855. QString text = item->text();
  856. if (RectFigure == figure && -1 != text.indexOf("BBox")) {
  857. bool ok = 0;
  858. int num = getNumFromString(&text, "BBox #", ";", &ok);
  859. if (ok && num == figureID) {
  860. list_areas_->takeItem(i);
  861. addBBoxArea(num, *list_bounding_box_.at(num), i);
  862. }
  863. }
  864. else if (PolyFigure == figure && -1 != text.indexOf("Poly")) {
  865. bool ok = 0;
  866. int num = getNumFromString(&text, "Poly #", ";", &ok);
  867. if (ok && num == figureID) {
  868. list_areas_->takeItem(i);
  869. addPolyArea(num, *list_polygon_.at(num), i);
  870. }
  871. }
  872. }
  873. unsaved_data_ = 1;
  874. }
  875. void
  876. ImageLabeler::deleteArea()
  877. {
  878. if (!list_areas_->currentItem() || !list_areas_->count()) {
  879. return;
  880. /* NOTREACHED */
  881. }
  882. QString text = list_areas_->currentItem()->text();
  883. bool ok = 0;
  884. //int labelID = getNumFromString(&text, "LabelID:", ";", &ok);
  885. // if (!ok) {
  886. // return;
  887. // /* NOTREACHED */
  888. // }
  889. /*
  890. * 0 - bbox
  891. * 1 - poly
  892. */
  893. QString shape;
  894. if (-1 != text.indexOf("BBox"))
  895. shape = QString("BBox");
  896. else if (-1 != text.indexOf("Poly"))
  897. shape = QString("Poly");
  898. else {
  899. return;
  900. /* NOTREACHED */
  901. }
  902. int areaNum = getNumFromString(&text, "#", ";", &ok);
  903. if (!ok) {
  904. return;
  905. /* NOTREACHED */
  906. }
  907. int currentItemRow = list_areas_->row(list_areas_->currentItem());
  908. /* changing all shapes(depends on current) which are next in the list */
  909. for (int i = list_areas_->count() - 1; i > currentItemRow; i--) {
  910. QListWidgetItem item = *(list_areas_->item(i));
  911. QString newText = item.text();
  912. if (-1 == newText.indexOf(shape))
  913. continue;
  914. int num = getNumFromString(&newText, "#", ";", &ok);
  915. num--;
  916. QString numString = QString("%1").arg(num);
  917. int numPos = newText.indexOf("#") + 1;
  918. newText.replace(numPos, numString.size(), numString);
  919. list_areas_->takeItem(i);
  920. list_areas_->insertItem(i, newText);
  921. }
  922. list_areas_->takeItem(currentItemRow);
  923. if (shape == "BBox")
  924. list_bounding_box_.removeAt(areaNum);
  925. else
  926. list_polygon_.removeAt(areaNum);
  927. image_holder_->update();
  928. unsaved_data_ = 1;
  929. }
  930. void
  931. ImageLabeler::toggleLabelPriority()
  932. {
  933. if (!list_label_->count()) {
  934. return;
  935. /* NOTREACHED */
  936. }
  937. QListWidgetItem *item = list_label_->currentItem();
  938. /* because we need to keep BACKGROUND category */
  939. if (0 == list_label_->row(item)) {
  940. return;
  941. /* NOTREACHED */
  942. }
  943. int itemRow = list_label_->row(item);
  944. QString text = item->text();
  945. /* cleaning previous " #main" mark */
  946. if (-1 != main_label_) {
  947. QListWidgetItem *lastMain = list_label_->item(main_label_);
  948. QString lastMainText = lastMain->text();
  949. int mainPos = lastMainText.indexOf(" #main");
  950. lastMainText = lastMainText.mid(0, mainPos);
  951. list_label_->blockSignals(true);
  952. lastMain->setText(lastMainText);
  953. list_label_->blockSignals(false);
  954. }
  955. if (main_label_ == itemRow) {
  956. int mainPos = text.indexOf(" #main");
  957. text = text.mid(0, mainPos);
  958. main_label_ = -1;
  959. }
  960. else {
  961. text.append(" #main");
  962. main_label_ = list_label_->row(item);
  963. }
  964. list_label_->blockSignals(true);
  965. item->setText(text);
  966. list_label_->blockSignals(false);
  967. image_holder_->update();
  968. unsaved_data_ = 1;
  969. }
  970. void
  971. ImageLabeler::getImagesFromDir(const QDir &dir)
  972. {
  973. /* avoiding freezing during recursive search for files */
  974. QApplication::processEvents();
  975. if (interrupt_search_) {
  976. clearAll();
  977. return;
  978. }
  979. QStringList filenameFilter;
  980. filenameFilter <<
  981. "*.jpeg" <<
  982. "*.jpg" <<
  983. "*.gif" <<
  984. "*.png" <<
  985. "*.bmp" <<
  986. "*.tiff"
  987. ;
  988. QStringList listImages =
  989. dir.entryList(filenameFilter, QDir::Files);
  990. foreach (QString file, listImages)
  991. list_images_->append(dir.absoluteFilePath(file));
  992. QStringList listDir = dir.entryList(QDir::Dirs);
  993. foreach (QString subdir, listDir) {
  994. if ("." == subdir || ".." == subdir)
  995. continue;
  996. getImagesFromDir(QDir(dir.absoluteFilePath(subdir)));
  997. }
  998. }
  999. void
  1000. ImageLabeler::nextImage()
  1001. {
  1002. if (list_images_->isEmpty()) {
  1003. return;
  1004. /* NOTREACHED */
  1005. }
  1006. if (askForUnsavedData()) {
  1007. return;
  1008. /* NOTREACHED */
  1009. }
  1010. if (list_images_->end() == current_image_ + 1)
  1011. current_image_ = list_images_->begin();
  1012. else
  1013. current_image_++;
  1014. if ((*current_image_).isEmpty()) {
  1015. return;
  1016. /* NOTREACHED */
  1017. }
  1018. image_->load(*current_image_);
  1019. image_holder_->resize(image_->size());
  1020. image_holder_->setPixmap(*image_);
  1021. list_bounding_box_.clear();
  1022. list_polygon_.clear();
  1023. list_areas_->clear();
  1024. image_holder_->clearAll();
  1025. setWindowTitle(tr("ImageLabeler"));
  1026. }
  1027. void
  1028. ImageLabeler::prevImage()
  1029. {
  1030. if (list_images_->isEmpty()) {
  1031. return;
  1032. /* NOTREACHED */
  1033. }
  1034. if (askForUnsavedData()) {
  1035. return;
  1036. /* NOTREACHED */
  1037. }
  1038. if (list_images_->begin() == current_image_)
  1039. current_image_ = list_images_->end() - 1;
  1040. else
  1041. current_image_--;
  1042. image_->load(*current_image_);
  1043. image_holder_->resize(image_->size());
  1044. image_holder_->setPixmap(*image_);
  1045. list_bounding_box_.clear();
  1046. list_polygon_.clear();
  1047. list_areas_->clear();
  1048. image_holder_->clearAll();
  1049. setWindowTitle(tr("ImageLabeler"));
  1050. }
  1051. void
  1052. ImageLabeler::saveAllInfo()
  1053. {
  1054. if (list_images_->isEmpty()) {
  1055. showWarning("You have not opened any image yet");
  1056. return;
  1057. /* NOTREACHED */
  1058. }
  1059. /* ------------------------------------------------------------------------
  1060. * XML part
  1061. */
  1062. QDomDocument doc(tr("ImageLabeler"));
  1063. QDomElement root = doc.createElement(tr("pixelwise_labeling"));
  1064. doc.appendChild(root);
  1065. QDomElement image = doc.createElement(tr("image"));
  1066. root.appendChild(image);
  1067. QDomText pathToImage = doc.createTextNode(*current_image_);
  1068. image.appendChild(pathToImage);
  1069. if (!segmented_image_.isEmpty()) {
  1070. QDomElement segmentedImage = doc.createElement(tr("segmented"));
  1071. root.appendChild(segmentedImage);
  1072. QDomText pathToSegmented = doc.createTextNode(segmented_image_);
  1073. image.appendChild(pathToSegmented);
  1074. }
  1075. QDomElement description = doc.createElement(tr("description"));
  1076. root.appendChild(description);
  1077. QDomText descriptionText = doc.createTextNode(image_description_);
  1078. description.appendChild(descriptionText);
  1079. legendToXml(&doc, &root);
  1080. setPureData();
  1081. QDomElement pureData = doc.createElement(tr("pure_data"));
  1082. QSize imageSize = image_->size();
  1083. QString pixelValues;
  1084. for (int i = 0; i < imageSize.height(); i++) {
  1085. for (int j = 0; j < imageSize.width(); j++) {
  1086. pixelValues.append(QString("%1").arg(pure_data_[i][j]));
  1087. //pixelValues.append(QString(""));
  1088. }
  1089. pixelValues.append("\n");
  1090. }
  1091. QDomText pureDataText = doc.createTextNode(pixelValues);
  1092. pureData.appendChild(pureDataText);
  1093. root.appendChild(pureData);
  1094. QString xml = doc.toString();
  1095. /* ------------------------------------------------------------------------
  1096. * XML part ends
  1097. */
  1098. QFileDialog fileDialog(0, tr("Save all info"));
  1099. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1100. fileDialog.setDefaultSuffix("dat");
  1101. fileDialog.setFileMode(QFileDialog::AnyFile);
  1102. QString dir = getDirFromPath(&(*current_image_));
  1103. /* altering the name of a new file */
  1104. QString newFileName = alterFileName(*current_image_, "_labeled");
  1105. fileDialog.selectFile(newFileName);
  1106. fileDialog.setDirectory(dir);
  1107. QString filename;
  1108. if (fileDialog.exec()) {
  1109. filename = fileDialog.selectedFiles().last();
  1110. }
  1111. else {
  1112. //showWarning(tr("Can not open file dialog"));
  1113. return;
  1114. /* NOTREACHED */
  1115. }
  1116. if (filename.isEmpty()) {
  1117. return;
  1118. /* NOTREACHED */
  1119. }
  1120. QFile file(filename);
  1121. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1122. showWarning(tr("Can not open file for writing"));
  1123. return;
  1124. /* NOTREACHED */
  1125. }
  1126. file.write(xml.toLocal8Bit());
  1127. file.close();
  1128. unsaved_data_ = 0;
  1129. }
  1130. void
  1131. ImageLabeler::saveSegmentedPicture()
  1132. {
  1133. if (list_bounding_box_.isEmpty() && list_polygon_.isEmpty()) {
  1134. return;
  1135. /* NOTREACHED */
  1136. }
  1137. setPureData();
  1138. // QString suffix = *current_image_;
  1139. // int dotPos = suffix.lastIndexOf(".");
  1140. // suffix.mid(dotPos + 1, suffix.size() - dotPos - 1);
  1141. QFileDialog fileDialog(0, tr("Save segmented picture"));
  1142. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1143. fileDialog.setDefaultSuffix("png");
  1144. fileDialog.setFileMode(QFileDialog::AnyFile);
  1145. QString dir = getDirFromPath(&(*current_image_));
  1146. /* altering the name of a new file */
  1147. QString newFileName = alterFileName(*current_image_, "_segmented");
  1148. fileDialog.selectFile(newFileName);
  1149. fileDialog.setDirectory(dir);
  1150. QString filename;
  1151. if (fileDialog.exec()) {
  1152. filename = fileDialog.selectedFiles().last();
  1153. }
  1154. else {
  1155. return;
  1156. /* NOTREACHED */
  1157. }
  1158. QSize imageSize = image_holder_->pixmap()->size();
  1159. QImage newImage(imageSize, QImage::Format_RGB32);
  1160. bool generateColorsFlag = auto_color_generation_;
  1161. bool flag = 0;
  1162. /* checking if all the colors are are different from white(default) */
  1163. if (!generateColorsFlag) {
  1164. for (int i = 1; i < list_label_->count(); i++) {
  1165. if (list_label_colors_.at(i) == 0xffffffff && !flag)
  1166. flag = 1;
  1167. else if (list_label_colors_.at(i) == 0xffffffff && flag) {
  1168. QMessageBox msgBox;
  1169. msgBox.setText(tr("There are few labels with default white color."));
  1170. msgBox.setInformativeText(tr("Do you want to generate all colors automatically? Otherwise you'll have to do it manually."));
  1171. msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  1172. msgBox.setDefaultButton(QMessageBox::Yes);
  1173. msgBox.setIcon(QMessageBox::Question);
  1174. int ret = msgBox.exec();
  1175. if (QMessageBox::Yes == ret) {
  1176. generateColorsFlag = 1;
  1177. break;
  1178. }
  1179. else {
  1180. return;
  1181. /* NOTREACHED */
  1182. }
  1183. }
  1184. }
  1185. }
  1186. /* generating colors for labels */
  1187. if (list_label_colors_.count() < list_label_->count() ||
  1188. generateColorsFlag) {
  1189. generateColors();
  1190. }
  1191. for (int i = 0; i < imageSize.height(); i++)
  1192. for (int j = 0; j < imageSize.width(); j++) {
  1193. newImage.setPixel(j, i, list_label_colors_.at(pure_data_[i][j]));
  1194. }
  1195. if (!newImage.save(filename, "png", 100)) {
  1196. showWarning(tr("An error occurred while saving the segmented image"));
  1197. return;
  1198. /* NOTREACHED */
  1199. }
  1200. segmented_image_ = filename;
  1201. action_view_segmented_->setEnabled(true);
  1202. }
  1203. void
  1204. ImageLabeler::saveLegend()
  1205. {
  1206. if (!list_label_->count()) {
  1207. showWarning("You have not added any label yet");
  1208. return;
  1209. /* NOTREACHED */
  1210. }
  1211. /* ------------------------------------------------------------------------
  1212. * XML part
  1213. */
  1214. QDomDocument doc(tr("ImageLabeler"));
  1215. QDomElement root = doc.createElement(tr("root"));
  1216. doc.appendChild(root);
  1217. legendToXml(&doc, &root);
  1218. QString xml = doc.toString();
  1219. /* ------------------------------------------------------------------------
  1220. * XML part ends
  1221. */
  1222. QFileDialog fileDialog(0, tr("Save legend"));
  1223. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1224. fileDialog.setDefaultSuffix("dat");
  1225. fileDialog.setFileMode(QFileDialog::AnyFile);
  1226. /* altering the name of a new file */
  1227. QString newFileName = alterFileName(*current_image_, "_legend");
  1228. fileDialog.selectFile(newFileName);
  1229. QString filename;
  1230. if (fileDialog.exec()) {
  1231. filename = fileDialog.selectedFiles().last();
  1232. }
  1233. else {
  1234. //showWarning(tr("Can not open file dialog"));
  1235. return;
  1236. /* NOTREACHED */
  1237. }
  1238. if (filename.isEmpty()) {
  1239. return;
  1240. /* NOTREACHED */
  1241. }
  1242. QFile file(filename);
  1243. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1244. showWarning(tr("Can not open file for writing"));
  1245. return;
  1246. /* NOTREACHED */
  1247. }
  1248. file.write(xml.toLocal8Bit());
  1249. file.close();
  1250. }
  1251. void
  1252. ImageLabeler::loadInfo()
  1253. {
  1254. if (askForUnsavedData()) {
  1255. return;
  1256. /* NOTREACHED */
  1257. }
  1258. QFileDialog fileDialog(0, tr("Load file with info"));
  1259. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1260. fileDialog.setDefaultSuffix("dat");
  1261. fileDialog.setFileMode(QFileDialog::AnyFile);
  1262. QString filename;
  1263. if (fileDialog.exec()) {
  1264. filename = fileDialog.selectedFiles().last();
  1265. }
  1266. else {
  1267. //showWarning(tr("Can not open file dialog"));
  1268. return;
  1269. /* NOTREACHED */
  1270. }
  1271. QDomDocument doc("Image Labeler");
  1272. QFile file(filename);
  1273. if (!file.open(QIODevice::ReadOnly)) {
  1274. showWarning(tr("Can not open such file"));
  1275. return;
  1276. /* NOTREACHED */
  1277. }
  1278. QString errMsg;
  1279. if (!doc.setContent(&file, &errMsg)) {
  1280. showWarning(errMsg);
  1281. file.close();
  1282. return;
  1283. /* NOTREACHED */
  1284. }
  1285. file.close();
  1286. clearAll();
  1287. list_label_->clear();
  1288. enableTools();
  1289. /* getting all info */
  1290. QDomElement elements = doc.documentElement();
  1291. QDomNode rootNode = elements.firstChild();
  1292. QString string;
  1293. while(!rootNode.isNull()) {
  1294. QDomElement element = rootNode.toElement();
  1295. if(!element.isNull()) {
  1296. /* path to the image */
  1297. if (element.tagName() == "image") {
  1298. string = element.text();
  1299. if (string.isEmpty()) {
  1300. showWarning(
  1301. tr(
  1302. "The file with data doesn't contain path to the image"
  1303. )
  1304. );
  1305. return;
  1306. /* NOTREACHED */
  1307. }
  1308. list_images_->append(string);
  1309. current_image_ = list_images_->end();
  1310. current_image_--;
  1311. image_->load(*current_image_);
  1312. image_holder_->resize(image_->size());
  1313. image_holder_->setPixmap(*image_);
  1314. }
  1315. /* path to the segmented image */
  1316. if (element.tagName() == "segmented") {
  1317. string = element.text();
  1318. if (string.isEmpty()) {
  1319. continue;
  1320. }
  1321. segmented_image_ = string;
  1322. action_view_segmented_->setEnabled(true);
  1323. }
  1324. /* image description */
  1325. else if (element.tagName() == "description" &&
  1326. !element.text().isEmpty()) {
  1327. image_description_ = element.text();
  1328. }
  1329. /* legend */
  1330. else if (element.tagName() == "legend") {
  1331. loadLegendFromNode(&element);
  1332. }
  1333. /* objects */
  1334. else if (element.tagName() == "objects") {
  1335. QDomNode subNode = element.firstChild();
  1336. QDomElement subElement;
  1337. while(!subNode.isNull()) {
  1338. subElement = subNode.toElement();
  1339. if (subElement.isNull() || subElement.text().isEmpty()) {
  1340. subNode = subNode.nextSibling();
  1341. continue;
  1342. }
  1343. string = subElement.attribute("id");
  1344. bool ok = 1;
  1345. int id = string.toInt(&ok, 10);
  1346. if (!ok) {
  1347. qDebug() <<
  1348. "while getting poly: "
  1349. "poly id format is corrupted";
  1350. subNode = subNode.nextSibling();
  1351. continue;
  1352. }
  1353. string = subElement.text();
  1354. if (subElement.tagName() == "bbox") {
  1355. setBBoxFromData(&string, &id);
  1356. }
  1357. if (subElement.tagName() == "poly") {
  1358. setPolyFromData(&string, &id);
  1359. }
  1360. subNode = subNode.nextSibling();
  1361. }
  1362. }
  1363. }
  1364. rootNode = rootNode.nextSibling();
  1365. }
  1366. }
  1367. void
  1368. ImageLabeler::loadImages()
  1369. {
  1370. if (askForUnsavedData()) {
  1371. return;
  1372. /* NOTREACHED */
  1373. }
  1374. QFileDialog fileDialog(0, tr("Load images"));
  1375. fileDialog.setFileMode(QFileDialog::Directory);
  1376. QString dirName("");
  1377. if (fileDialog.exec()) {
  1378. /* TODO: make it possible to select multiple folders */
  1379. dirName = fileDialog.selectedFiles().last();
  1380. }
  1381. else {
  1382. //showWarning(tr("Could not open file dialog"));
  1383. return;
  1384. /* NOTREACHED */
  1385. }
  1386. clearAllTool();
  1387. QWidget *searchInProgress = new QWidget(0);
  1388. searchInProgress->setWindowTitle(tr("Loading images"));
  1389. QLabel *info = new QLabel(searchInProgress);
  1390. info->setText(
  1391. tr("Program is looking for all image files in your directory recursively."));
  1392. QPushButton *cancel = new QPushButton(tr("Cancel"), searchInProgress);
  1393. connect(cancel, SIGNAL(clicked()), this, SLOT(interruptSearch()));
  1394. QVBoxLayout *layout = new QVBoxLayout(searchInProgress);
  1395. searchInProgress->setLayout(layout);
  1396. layout->addWidget(info);
  1397. layout->addWidget(cancel);
  1398. searchInProgress->adjustSize();
  1399. searchInProgress->move(QApplication::desktop()->screen()->rect().center() - rect().center());
  1400. searchInProgress->show();
  1401. getImagesFromDir(QDir(dirName));
  1402. cancel->disconnect();
  1403. searchInProgress->hide();
  1404. delete info;
  1405. delete cancel;
  1406. delete layout;
  1407. delete searchInProgress;
  1408. if (interrupt_search_) {
  1409. interrupt_search_ = 0;
  1410. return;
  1411. /* NOTREACHED */
  1412. }
  1413. if (list_images_->isEmpty()) {
  1414. showWarning(tr("The folder you selected contains no images"));
  1415. return;
  1416. /* NOTREACHED */
  1417. }
  1418. current_image_ = list_images_->begin();
  1419. image_->load(*current_image_);
  1420. image_holder_->resize(image_->size());
  1421. image_holder_->setPixmap(*image_);
  1422. enableTools();
  1423. }
  1424. void
  1425. ImageLabeler::loadLegendFromFile()
  1426. {
  1427. QFileDialog fileDialog(0, tr("Load legend"));
  1428. fileDialog.setFileMode(QFileDialog::AnyFile);
  1429. QString filename;
  1430. if (fileDialog.exec()) {
  1431. filename = fileDialog.selectedFiles().last();
  1432. }
  1433. else {
  1434. //showWarning(tr("Could not open file dialog"));
  1435. return;
  1436. /* NOTREACHED */
  1437. }
  1438. QDomDocument doc("Image Labeler");
  1439. QFile file(filename);
  1440. if (!file.open(QIODevice::ReadOnly)) {
  1441. showWarning(tr("Can not open such file"));
  1442. return;
  1443. /* NOTREACHED */
  1444. }
  1445. QString errMsg;
  1446. if (!doc.setContent(&file, &errMsg)) {
  1447. showWarning(errMsg);
  1448. file.close();
  1449. return;
  1450. /* NOTREACHED */
  1451. }
  1452. file.close();
  1453. list_label_->clear();
  1454. /* getting legend */
  1455. QDomElement elements = doc.documentElement();
  1456. QDomNode rootNode = elements.firstChild();
  1457. QString string;
  1458. while(!rootNode.isNull()) {
  1459. QDomElement element = rootNode.toElement();
  1460. if(!element.isNull()) {
  1461. if (element.tagName() == "legend") {
  1462. loadLegendFromNode(&element);
  1463. }
  1464. }
  1465. rootNode = rootNode.nextSibling();
  1466. }
  1467. }
  1468. void
  1469. ImageLabeler::loadLegendFromNode(QDomElement *anElement)
  1470. {
  1471. if (!anElement) {
  1472. return;
  1473. /* NOTREACHED */
  1474. }
  1475. QDomNode subNode = anElement->firstChild();
  1476. QDomElement subElement;
  1477. QString string;
  1478. int id = -1;
  1479. bool isMain;
  1480. uint color = 0xff000000;
  1481. while(!subNode.isNull()) {
  1482. subElement = subNode.toElement();
  1483. if (!subElement.isNull() && !subElement.text().isEmpty()) {
  1484. string = subElement.attribute("id");
  1485. bool ok = 0;
  1486. id = string.toInt(&ok, 10);
  1487. if (!ok) {
  1488. qDebug() <<
  1489. "while getting legend: "
  1490. "label id format is corrupted";
  1491. subNode = subNode.nextSibling();
  1492. continue;
  1493. }
  1494. string = subElement.attribute("isMain");
  1495. isMain = string.toInt(&ok, 2);
  1496. if (!ok) {
  1497. qDebug() <<
  1498. "while getting legend: "
  1499. "label isMain flag format is corrupted";
  1500. subNode = subNode.nextSibling();
  1501. continue;
  1502. }
  1503. string = subElement.attribute("color");
  1504. color = string.toUInt(&ok, 16);
  1505. if (!ok) {
  1506. qDebug() <<
  1507. "while getting legend: "
  1508. "label color format is corrupted";
  1509. subNode = subNode.nextSibling();
  1510. continue;
  1511. }
  1512. string = subElement.text();
  1513. addLabel(id, isMain, string);
  1514. setLabelColor(id, color);
  1515. }
  1516. subNode = subNode.nextSibling();
  1517. }
  1518. }
  1519. void
  1520. ImageLabeler::legendToXml(QDomDocument *aDoc, QDomElement *aRoot)
  1521. {
  1522. QDomElement legend = aDoc->createElement(tr("legend"));
  1523. aRoot->appendChild(legend);
  1524. /* storing all labels made by user */
  1525. int labelCount = list_label_->count();
  1526. for (int i = 0; i < labelCount; i++) {
  1527. QDomElement label = aDoc->createElement(tr("label"));
  1528. label.setAttribute("color", QString("%1").arg(list_label_colors_.at(i), 0, 16));
  1529. label.setAttribute("id", i);
  1530. QString priority;
  1531. if (main_label_ == i)
  1532. priority.append("1");
  1533. else
  1534. priority.append("0");
  1535. label.setAttribute("isMain", priority);
  1536. QString labelText = list_label_->item(i)->text();
  1537. /* removing the number prefix of label */
  1538. if (-1 != labelText.indexOf(QString("%1: ").arg(i))) {
  1539. labelText = labelText.mid(3, labelText.size() - 3);
  1540. }
  1541. QDomText labelName = aDoc->createTextNode(labelText);
  1542. label.appendChild(labelName);
  1543. legend.appendChild(label);
  1544. }
  1545. /* in case we have no labels */
  1546. if (0 == labelCount) {
  1547. QDomElement label = aDoc->createElement(tr("label"));
  1548. label.setAttribute(tr("id"), -1);
  1549. legend.appendChild(label);
  1550. }
  1551. }
  1552. void
  1553. ImageLabeler::objectsToXml(QDomDocument *aDoc, QDomElement *aRoot)
  1554. {
  1555. QDomElement objects = aDoc->createElement(tr("objects"));
  1556. aRoot->appendChild(objects);
  1557. /* rects first */
  1558. for (int i = 0; i < list_bounding_box_.size(); i++) {
  1559. QDomElement rectData = aDoc->createElement(tr("bbox"));
  1560. rectData.setAttribute("id", list_bounding_box_.at(i)->label_ID_);
  1561. QRect rect = list_bounding_box_.at(i)->rect.normalized();
  1562. QString rectDataString =
  1563. QString("%1;%2;%3;%4;").
  1564. arg(rect.x()).
  1565. arg(rect.y()).
  1566. arg(rect.width()).
  1567. arg(rect.height());
  1568. QDomText rectDataText = aDoc->createTextNode(rectDataString);
  1569. rectData.appendChild(rectDataText);
  1570. objects.appendChild(rectData);
  1571. }
  1572. /* polys next */
  1573. for (int i = 0; i < list_polygon_.size(); i++) {
  1574. QDomElement polyData = aDoc->createElement(tr("poly"));
  1575. polyData.setAttribute("id", list_polygon_.at(i)->label_ID_);
  1576. QPolygon poly = list_polygon_.at(i)->poly;
  1577. QString polyDataString;
  1578. for (int j = 0; j < poly.count(); j++)
  1579. polyDataString.append(
  1580. QString("%1;%2;").
  1581. arg(poly.point(j).x()).
  1582. arg(poly.point(j).y())
  1583. );
  1584. QDomText polyDataText = aDoc->createTextNode(polyDataString);
  1585. polyData.appendChild(polyDataText);
  1586. objects.appendChild(polyData);
  1587. }
  1588. }
  1589. void
  1590. ImageLabeler::generateColors()
  1591. {
  1592. int labelCount = list_label_->count();
  1593. if (!labelCount) {
  1594. showWarning(tr("you have not added any labels yet"));
  1595. return;
  1596. /* NOTREACHED */
  1597. }
  1598. int coeff = (0xff / labelCount) * 3;
  1599. //QList< uint > colors;
  1600. list_label_colors_.clear();
  1601. list_label_colors_.append(0);
  1602. uchar red = 0xff;
  1603. uchar green = 0xff;
  1604. uchar blue = 0xff;
  1605. uchar iterationColor = coeff;
  1606. uint color = 0xffffffff;
  1607. int j = 1;
  1608. for (int i = 1; i < labelCount; i++) {
  1609. if (6 == j) {
  1610. iterationColor += coeff;
  1611. j = 1;
  1612. }
  1613. if (5 == j) {
  1614. red = 0xff - iterationColor;
  1615. green = 0xff - iterationColor;
  1616. blue = 0xff;
  1617. }
  1618. else if (4 == j) {
  1619. red = 0xff - iterationColor;
  1620. green = 0xff;
  1621. blue = 0xff - iterationColor;
  1622. }
  1623. else if (3 == j) {
  1624. red = 0xff - iterationColor;
  1625. green = 0xff;
  1626. blue = 0xff;
  1627. }
  1628. else if (2 == j) {
  1629. red = 0xff;
  1630. green = 0xff - iterationColor;
  1631. blue = 0xff;
  1632. }
  1633. else if (1 == j){
  1634. red = 0xff;
  1635. green = 0xff;
  1636. blue = 0xff - iterationColor;
  1637. }
  1638. j++;
  1639. color = red + (green * 0x100) + (blue * 0x10000) + 0xff000000;
  1640. int itemNo = list_label_colors_.count();
  1641. QPixmap iconPix = QPixmap(20, 20);
  1642. iconPix.fill(color);
  1643. QIcon icon(iconPix);
  1644. list_label_->item(itemNo)->setIcon(icon);
  1645. list_label_colors_.append(color);
  1646. }
  1647. }
  1648. void
  1649. ImageLabeler::setBBoxFromData(
  1650. QString *aBBoxData,
  1651. int *ID
  1652. )
  1653. {
  1654. BoundingBox *bbox = new BoundingBox;
  1655. *bbox = BBoxFromData(aBBoxData);
  1656. if (!bbox->rect.isValid() || !ID) {
  1657. return;
  1658. /* NOTREACHED */
  1659. }
  1660. bbox->label_ID_ = *ID;
  1661. list_bounding_box_.append(bbox);
  1662. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  1663. }
  1664. BoundingBox
  1665. ImageLabeler::BBoxFromData(
  1666. QString *aBBoxData
  1667. )
  1668. {
  1669. BoundingBox bbox;
  1670. QString buffer;
  1671. bbox.rect.setRect(-1, -1, -1, -1);
  1672. int startPos = 0;
  1673. bool ok = 1;
  1674. for (int i = 0; i < aBBoxData->size(); i++) {
  1675. if (';' != aBBoxData->at(i))
  1676. continue;
  1677. buffer = aBBoxData->mid(startPos, i - startPos);
  1678. int bboxData = buffer.toInt(&ok, 10);
  1679. if (!ok) {
  1680. qDebug() <<
  1681. "while getting objects: "
  1682. "poly format is corrupted";
  1683. break;
  1684. }
  1685. if (-1 == bbox.rect.x()) {
  1686. bbox.rect.setX(bboxData);
  1687. bbox.rect.setWidth(-1);
  1688. }
  1689. else if (-1 == bbox.rect.y()) {
  1690. bbox.rect.setY(bboxData);
  1691. bbox.rect.setHeight(-1);
  1692. }
  1693. else if (-1 == bbox.rect.width()) {
  1694. bbox.rect.setWidth(bboxData);
  1695. }
  1696. else if (-1 == bbox.rect.height()) {
  1697. bbox.rect.setHeight(bboxData);
  1698. }
  1699. startPos = i + 1;
  1700. }
  1701. if (!bbox.rect.isValid()) {
  1702. qDebug() <<
  1703. "BBoxFromData: "
  1704. "bbox format is corrupted";
  1705. bbox.rect.setRect(-1, -1, -1, -1);
  1706. }
  1707. else if (!ok) {
  1708. bbox.rect.setRect(-1, -1, -1, -1);
  1709. }
  1710. return bbox;
  1711. }
  1712. BoundingBox
  1713. ImageLabeler::BBoxFromString(
  1714. QString *aString,
  1715. int *oldID
  1716. )
  1717. {
  1718. BoundingBox bbox;
  1719. bbox.label_ID_ = -1;
  1720. *oldID = -1;
  1721. if (!aString) {
  1722. return bbox;
  1723. /* NOTREACHED */
  1724. }
  1725. if (-1 == aString->indexOf("BBox")) {
  1726. return bbox;
  1727. /* NOTREACHED */
  1728. }
  1729. /* getting bbox id in the list(it cannot be changed) */
  1730. bool ok = 0;
  1731. int bboxID = getNumFromString(aString, "BBox #", ";", &ok);
  1732. if (!ok || bboxID <= -1) {
  1733. qDebug() <<
  1734. "BBoxFromString: poly ID is corrupted";
  1735. return bbox;
  1736. /* NOTREACHED */
  1737. }
  1738. /* getting new label id */
  1739. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  1740. if (!ok || labelID <= -1) {
  1741. showWarning(
  1742. tr("new LabelID is wrong, area can not be changed")
  1743. );
  1744. return bbox;
  1745. /* NOTREACHED */
  1746. }
  1747. /* getting new points */
  1748. int pointsPos = aString->indexOf("points:") + 7;
  1749. int pointsLen = aString->size() - pointsPos;
  1750. if (pointsLen <= 0) {
  1751. showWarning(
  1752. tr("new points data is wrong, area can not be changed")
  1753. );
  1754. return bbox;
  1755. /* NOTREACHED */
  1756. }
  1757. QString pointsData = aString->mid(pointsPos, pointsLen);
  1758. bbox = BBoxFromData(&pointsData);
  1759. bbox.label_ID_ = labelID;
  1760. *oldID = bboxID;
  1761. return bbox;
  1762. }
  1763. void
  1764. ImageLabeler::setPolyFromData(
  1765. QString *aPolyData,
  1766. int *ID
  1767. )
  1768. {
  1769. Polygon *poly = new Polygon;
  1770. *poly = polyFromData(aPolyData);
  1771. if (poly->poly.isEmpty() || !ID) {
  1772. return;
  1773. /* NOTREACHED */
  1774. }
  1775. poly->label_ID_ = *ID;
  1776. list_polygon_.append(poly);
  1777. addPolyArea(list_polygon_.count() - 1, *poly);
  1778. }
  1779. Polygon
  1780. ImageLabeler::polyFromData(
  1781. QString *aPolyData
  1782. )
  1783. {
  1784. Polygon poly;
  1785. poly.label_ID_ = -1;
  1786. QPoint point;
  1787. QString buffer;
  1788. int startPos = 0;
  1789. bool ok = 1;
  1790. bool evenFlag = 0;
  1791. for (int i = 0; i < aPolyData->size(); i++) {
  1792. if (';' != aPolyData->at(i))
  1793. continue;
  1794. buffer = aPolyData->mid(startPos, i - startPos);
  1795. int polyCoor = buffer.toInt(&ok, 10);
  1796. if (!ok) {
  1797. qDebug() <<
  1798. "while getting objects: "
  1799. "poly format is corrupted";
  1800. break;
  1801. }
  1802. if (!evenFlag) {
  1803. point.setX(polyCoor);
  1804. evenFlag = 1;
  1805. }
  1806. else {
  1807. point.setY(polyCoor);
  1808. poly.poly.append(point);
  1809. evenFlag = 0;
  1810. }
  1811. startPos = i + 1;
  1812. }
  1813. if (evenFlag) {
  1814. qDebug() <<
  1815. "polyFromData: "
  1816. "poly format is corrupted";
  1817. poly.poly.clear();
  1818. }
  1819. else if (!ok) {
  1820. poly.poly.clear();
  1821. }
  1822. return poly;
  1823. }
  1824. Polygon
  1825. ImageLabeler::polyFromString(
  1826. QString *aString,
  1827. int *oldID
  1828. )
  1829. {
  1830. Polygon poly;
  1831. poly.label_ID_ = -1;
  1832. *oldID = -1;
  1833. if (!aString) {
  1834. return poly;
  1835. /* NOTREACHED */
  1836. }
  1837. if (-1 == aString->indexOf("Poly")) {
  1838. return poly;
  1839. /* NOTREACHED */
  1840. }
  1841. /* getting poly id in the list(it cannot be changed) */
  1842. bool ok = 0;
  1843. int polyID = getNumFromString(aString, "Poly #", ";", &ok);
  1844. if (!ok || polyID <= -1) {
  1845. qDebug() <<
  1846. "polyFromString: poly ID is corrupted";
  1847. return poly;
  1848. /* NOTREACHED */
  1849. }
  1850. /* getting new label id */
  1851. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  1852. if (!ok || labelID <= -1) {
  1853. showWarning(
  1854. tr("new LabelID is wrong, area can not be changed")
  1855. );
  1856. return poly;
  1857. /* NOTREACHED */
  1858. }
  1859. /* getting new points */
  1860. int pointsPos = aString->indexOf("points:") + 7;
  1861. int pointsLen = aString->size() - pointsPos;
  1862. if (pointsLen <= 0) {
  1863. showWarning(
  1864. tr("new points data is wrong, area can not be changed")
  1865. );
  1866. return poly;
  1867. /* NOTREACHED */
  1868. }
  1869. QString pointsData = aString->mid(pointsPos, pointsLen);
  1870. poly = polyFromData(&pointsData);
  1871. poly.label_ID_ = labelID;
  1872. *oldID = polyID;
  1873. return poly;
  1874. }
  1875. void
  1876. ImageLabeler::showWarning(
  1877. const QString text
  1878. )
  1879. {
  1880. if (text.isEmpty()) {
  1881. return;
  1882. /* NOTREACHED */
  1883. }
  1884. QMessageBox msgBox;
  1885. msgBox.setText(text);
  1886. msgBox.setIcon(QMessageBox::Warning);
  1887. msgBox.exec();
  1888. }
  1889. bool
  1890. ImageLabeler::askForUnsavedData()
  1891. {
  1892. if ((!list_bounding_box_.isEmpty() ||
  1893. !list_polygon_.isEmpty()) &&
  1894. unsaved_data_)
  1895. {
  1896. QMessageBox msgBox;
  1897. msgBox.setText(tr("There is some unsaved data"));
  1898. msgBox.setInformativeText(tr("Do you want to save your progress?"));
  1899. msgBox.setStandardButtons(
  1900. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  1901. msgBox.setDefaultButton(QMessageBox::Save);
  1902. msgBox.setIcon(QMessageBox::Question);
  1903. int ret = msgBox.exec();
  1904. if (QMessageBox::Save == ret)
  1905. saveAllInfo();
  1906. else if (QMessageBox::Cancel == ret)
  1907. return true;
  1908. }
  1909. return false;
  1910. }
  1911. void
  1912. ImageLabeler::setBoundingBoxTool(bool aButtonPressed)
  1913. {
  1914. if (aButtonPressed)
  1915. image_holder_->setTool(ImageHolder::BoundingBoxTool);
  1916. else {
  1917. image_holder_->setTool(ImageHolder::NoTool);
  1918. image_holder_->clearLast();
  1919. }
  1920. }
  1921. void
  1922. ImageLabeler::setPolygonTool(bool aButtonPressed)
  1923. {
  1924. if (aButtonPressed) {
  1925. image_holder_->setTool(ImageHolder::PolygonTool);
  1926. image_holder_->setFocus();
  1927. }
  1928. else {
  1929. image_holder_->setTool(ImageHolder::NoTool);
  1930. image_holder_->clearLast();
  1931. }
  1932. }
  1933. void
  1934. ImageLabeler::onSelectionStarted()
  1935. {
  1936. button_confirm_selection_->setEnabled(true);
  1937. }
  1938. void
  1939. ImageLabeler::onImageScaled()
  1940. {
  1941. }
  1942. void
  1943. ImageLabeler::confirmSelection()
  1944. {
  1945. if (!list_label_->count()) {
  1946. showWarning(tr("You haven't added any label"));
  1947. return;
  1948. /* NOTREACHED */
  1949. }
  1950. image_holder_->confirmSelection();
  1951. if (label_ID_ < 0)
  1952. label_ID_ = 0;
  1953. ImageHolder::Tool tool = image_holder_->tool();
  1954. switch (tool) {
  1955. case ImageHolder::BoundingBoxTool:
  1956. list_bounding_box_.last()->label_ID_ = label_ID_;
  1957. addBBoxArea(
  1958. list_bounding_box_.count() - 1,
  1959. *(list_bounding_box_.last())
  1960. );
  1961. break;
  1962. case ImageHolder::PolygonTool:
  1963. list_polygon_.last()->label_ID_ = label_ID_;
  1964. addPolyArea(
  1965. list_polygon_.count() - 1,
  1966. *(list_polygon_.last())
  1967. );
  1968. break;
  1969. default:
  1970. break;
  1971. }
  1972. button_confirm_selection_->setEnabled(false);
  1973. unsaved_data_ = 1;
  1974. }
  1975. void
  1976. ImageLabeler::clearAll()
  1977. {
  1978. clearLabelList();
  1979. list_areas_->clear();
  1980. list_bounding_box_.clear();
  1981. list_polygon_.clear();
  1982. list_images_->clear();
  1983. main_label_ = -1;
  1984. image_holder_->clearAll();
  1985. segmented_image_.clear();
  1986. action_view_normal_->setEnabled(false);
  1987. action_view_segmented_->setEnabled(false);
  1988. }
  1989. void
  1990. ImageLabeler::clearAllTool()
  1991. {
  1992. list_areas_->clear();
  1993. list_bounding_box_.clear();
  1994. list_polygon_.clear();
  1995. main_label_ = -1;
  1996. image_holder_->clearAll();
  1997. }
  1998. void ImageLabeler::clearLabelList()
  1999. {
  2000. list_label_->clear();
  2001. addLabel(0, false, "BACKGROUND");
  2002. list_label_->item(0)->setFlags(
  2003. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  2004. Qt::ItemIsEnabled
  2005. );
  2006. }
  2007. void
  2008. ImageLabeler::enableTools()
  2009. {
  2010. action_save_labels_->setEnabled(true);
  2011. action_save_segmented_->setEnabled(true);
  2012. action_save_legend_->setEnabled(true);
  2013. action_bound_box_tool_->setEnabled(true);
  2014. action_polygon_tool_->setEnabled(true);
  2015. action_add_description_->setEnabled(true);
  2016. action_undo_->setEnabled(true);
  2017. action_redo_->setEnabled(true);
  2018. button_bound_box_tool_->setEnabled(true);
  2019. button_polygon_tool_->setEnabled(true);
  2020. button_add_label_->setEnabled(true);
  2021. button_remove_label_->setEnabled(true);
  2022. button_prev_image_->setEnabled(true);
  2023. button_next_image_->setEnabled(true);
  2024. button_clear_selection_tool_->setEnabled(true);
  2025. button_generate_colors_->setEnabled(true);
  2026. button_delete_all_labels_->setEnabled(true);
  2027. }
  2028. void
  2029. ImageLabeler::disableTools()
  2030. {
  2031. action_save_labels_->setEnabled(false);
  2032. action_save_segmented_->setEnabled(false);
  2033. action_save_legend_->setEnabled(false);
  2034. action_bound_box_tool_->setEnabled(false);
  2035. action_polygon_tool_->setEnabled(false);
  2036. action_add_description_->setEnabled(false);
  2037. action_undo_->setEnabled(false);
  2038. action_redo_->setEnabled(false);
  2039. button_bound_box_tool_->setEnabled(false);
  2040. button_polygon_tool_->setEnabled(false);
  2041. button_add_label_->setEnabled(false);
  2042. button_remove_label_->setEnabled(false);
  2043. button_prev_image_->setEnabled(false);
  2044. button_next_image_->setEnabled(false);
  2045. button_clear_selection_tool_->setEnabled(false);
  2046. button_generate_colors_->setEnabled(false);
  2047. button_delete_all_labels_->setEnabled(false);
  2048. }
  2049. void
  2050. ImageLabeler::areaListPopupMenu(const QPoint &aPos)
  2051. {
  2052. QPoint globalPos = list_areas_->mapToGlobal(aPos);
  2053. QModelIndex index = list_areas_->indexAt(aPos);
  2054. if (-1 == index.row()) {
  2055. return;
  2056. /* NOTREACHED */
  2057. }
  2058. list_areas_->item(index.row())->setSelected(true);
  2059. popup_area_list_->exec(globalPos);
  2060. }
  2061. void
  2062. ImageLabeler::labelListPopupMenu(const QPoint &aPos)
  2063. {
  2064. QPoint globalPos = list_label_->mapToGlobal(aPos);
  2065. QModelIndex index = list_label_->indexAt(aPos);
  2066. if (-1 == index.row() || !index.row()) {
  2067. return;
  2068. /* NOTREACHED */
  2069. }
  2070. list_label_->item(index.row())->setSelected(true);
  2071. popup_label_list_->exec(globalPos);
  2072. }
  2073. void
  2074. ImageLabeler::setDescription(QString aDescription)
  2075. {
  2076. image_description_ = aDescription;
  2077. setWindowTitle(image_description_);
  2078. }
  2079. void
  2080. ImageLabeler::setPureData()
  2081. {
  2082. /* initializing array */
  2083. if (pure_data_) {
  2084. delete[] *pure_data_;
  2085. delete pure_data_;
  2086. }
  2087. QSize imageSize = image_->size();
  2088. pure_data_ = new int *[imageSize.height()];
  2089. if (!pure_data_) {
  2090. return;
  2091. /* NOTREACHED */
  2092. }
  2093. for (int i = 0; i < imageSize.height(); i++) {
  2094. pure_data_[i] = new int[imageSize.width()];
  2095. if (!pure_data_[i]) {
  2096. return;
  2097. /* NOTREACHED */
  2098. }
  2099. }
  2100. int bboxCnt = list_bounding_box_.count();
  2101. int polyCnt = list_polygon_.count();
  2102. for (int i = 0; i < imageSize.height(); i++)
  2103. for (int j = 0; j < imageSize.width(); j++) {
  2104. pure_data_[i][j] = 0;
  2105. /* bboxes first */
  2106. for (int cnt = 0; cnt < bboxCnt; cnt++) {
  2107. BoundingBox *bbox = list_bounding_box_.at(cnt);
  2108. if (bbox->rect.contains(j, i)) {
  2109. pure_data_[i][j] = bbox->label_ID_;
  2110. }
  2111. }
  2112. /* polys next */
  2113. for (int cnt = 0; cnt < polyCnt; cnt++) {
  2114. Polygon *poly = list_polygon_.at(cnt);
  2115. if (poly->poly.containsPoint(QPoint(j, i), Qt::OddEvenFill)) {
  2116. pure_data_[i][j] = poly->label_ID_;
  2117. }
  2118. }
  2119. }
  2120. }
  2121. void
  2122. ImageLabeler::setLabelColor()
  2123. {
  2124. if (list_label_colors_.count() < list_label_->count()) {
  2125. generateColors();
  2126. }
  2127. QListWidgetItem *current = list_label_->currentItem();
  2128. int labelID = list_label_->row(current);
  2129. QColor defaultColor;
  2130. defaultColor.setRgb(list_label_colors_.at(labelID));
  2131. QColor newColor = QColorDialog::getColor(
  2132. defaultColor,
  2133. list_label_
  2134. );
  2135. if (!newColor.isValid()) {
  2136. return;
  2137. /* NOTREACHED */
  2138. }
  2139. list_label_colors_.takeAt(labelID);
  2140. list_label_colors_.insert(labelID, newColor.rgb());
  2141. QPixmap iconPix = QPixmap(20, 20);
  2142. iconPix.fill(newColor);
  2143. QIcon icon(iconPix);
  2144. current->setIcon(icon);
  2145. }
  2146. void
  2147. ImageLabeler::setLabelColor(int anID, QColor aColor)
  2148. {
  2149. if (anID < 0) {
  2150. return;
  2151. /* NOTREACHED */
  2152. }
  2153. if (list_label_colors_.count() < list_label_->count()) {
  2154. generateColors();
  2155. }
  2156. QListWidgetItem *item = list_label_->item(anID);
  2157. list_label_colors_.takeAt(anID);
  2158. list_label_colors_.insert(anID, aColor.rgb());
  2159. QPixmap iconPix = QPixmap(20, 20);
  2160. iconPix.fill(aColor);
  2161. QIcon icon(iconPix);
  2162. item->setIcon(icon);
  2163. }
  2164. void
  2165. ImageLabeler::onOptionsSet()
  2166. {
  2167. auto_color_generation_ = options_form_.autoColorGeneration();
  2168. }
  2169. void
  2170. ImageLabeler::viewNormal()
  2171. {
  2172. if ((*current_image_).isEmpty()) {
  2173. return;
  2174. /* NOTREACHED */
  2175. }
  2176. image_->load(*current_image_);
  2177. image_holder_->setPixmap(*image_);
  2178. action_view_segmented_->setEnabled(true);
  2179. action_view_normal_->setEnabled(false);
  2180. }
  2181. void
  2182. ImageLabeler::viewSegmented()
  2183. {
  2184. if (segmented_image_.isEmpty()) {
  2185. return;
  2186. /* NOTREACHED */
  2187. }
  2188. image_->load(segmented_image_);
  2189. image_holder_->setPixmap(*image_);
  2190. action_view_segmented_->setEnabled(false);
  2191. action_view_normal_->setEnabled(true);
  2192. }
  2193. void
  2194. ImageLabeler::interruptSearch()
  2195. {
  2196. interrupt_search_ = 1;
  2197. }
  2198. void
  2199. ImageLabeler::resizeEvent (QResizeEvent *anEvent)
  2200. {
  2201. QWidget::resizeEvent(anEvent);
  2202. }
  2203. void
  2204. ImageLabeler::mousePressEvent(QMouseEvent *anEvent)
  2205. {
  2206. QWidget::mousePressEvent(anEvent);
  2207. }
  2208. void
  2209. ImageLabeler::keyPressEvent(QKeyEvent *anEvent)
  2210. {
  2211. if (Qt::Key_Left == anEvent->key() &&
  2212. Qt::NoModifier == anEvent->modifiers()) {
  2213. prevImage();
  2214. }
  2215. if (Qt::Key_Right == anEvent->key() &&
  2216. Qt::NoModifier == anEvent->modifiers()) {
  2217. nextImage();
  2218. }
  2219. if ((Qt::Key_Enter == anEvent->key() ||
  2220. Qt::Key_Return == anEvent->key()) &&
  2221. // Qt::NoModifier == anEvent->modifiers() &&
  2222. ImageHolder::NewSelection == image_holder_->state()) {
  2223. confirmSelection();
  2224. }
  2225. if ((Qt::Key_Enter == anEvent->key() ||
  2226. Qt::Key_Return == anEvent->key()) &&
  2227. // Qt::NoModifier == anEvent->modifiers() &&
  2228. -1 != image_holder_->focusedSelection()) {
  2229. image_holder_->clearFocusOnArea();
  2230. image_holder_->clearEdition();
  2231. image_holder_->update();
  2232. }
  2233. if (Qt::Key_Escape == anEvent->key()) {
  2234. image_holder_->clearLast();
  2235. image_holder_->clearFocusOnArea();
  2236. image_holder_->clearEdition();
  2237. image_holder_->update();
  2238. }
  2239. if (Qt::Key_Z == anEvent->key() &&
  2240. Qt::ControlModifier == anEvent->modifiers()) {
  2241. image_holder_->undo();
  2242. }
  2243. if (Qt::Key_Y == anEvent->key() &&
  2244. Qt::ControlModifier == anEvent->modifiers()) {
  2245. image_holder_->redo();
  2246. }
  2247. QWidget::keyPressEvent(anEvent);
  2248. }
  2249. void
  2250. ImageLabeler::wheelEvent(QWheelEvent *anEvent)
  2251. {
  2252. /* zoomin */
  2253. if (0 < anEvent->delta()) {
  2254. image_holder_->scaleImage(ZoomIn, 1.1);
  2255. }
  2256. /* zoomout */
  2257. else if (anEvent->delta() < 0) {
  2258. image_holder_->scaleImage(ZoomOut, 1.1);
  2259. }
  2260. }
  2261. /*
  2262. *
  2263. */