ImageLabeler.cpp 57 KB

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