ImageLabeler.cpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666
  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. QString winTitle;
  1019. winTitle.append("ImageLabeler - ");
  1020. winTitle.append(*current_image_);
  1021. setWindowTitle(winTitle);
  1022. image_->load(*current_image_);
  1023. image_holder_->resize(image_->size());
  1024. image_holder_->setPixmap(*image_);
  1025. list_bounding_box_.clear();
  1026. list_polygon_.clear();
  1027. list_areas_->clear();
  1028. image_holder_->clearAll();
  1029. //setWindowTitle(tr("ImageLabeler"));
  1030. }
  1031. void
  1032. ImageLabeler::prevImage()
  1033. {
  1034. if (list_images_->isEmpty()) {
  1035. return;
  1036. /* NOTREACHED */
  1037. }
  1038. if (askForUnsavedData()) {
  1039. return;
  1040. /* NOTREACHED */
  1041. }
  1042. if (list_images_->begin() == current_image_)
  1043. current_image_ = list_images_->end() - 1;
  1044. else
  1045. current_image_--;
  1046. QString winTitle;
  1047. winTitle.append("ImageLabeler - ");
  1048. winTitle.append(*current_image_);
  1049. setWindowTitle(winTitle);
  1050. image_->load(*current_image_);
  1051. image_holder_->resize(image_->size());
  1052. image_holder_->setPixmap(*image_);
  1053. list_bounding_box_.clear();
  1054. list_polygon_.clear();
  1055. list_areas_->clear();
  1056. image_holder_->clearAll();
  1057. //setWindowTitle(tr("ImageLabeler"));
  1058. }
  1059. void
  1060. ImageLabeler::saveAllInfo()
  1061. {
  1062. if (list_images_->isEmpty()) {
  1063. showWarning("You have not opened any image yet");
  1064. return;
  1065. /* NOTREACHED */
  1066. }
  1067. /* ------------------------------------------------------------------------
  1068. * XML part
  1069. */
  1070. QDomDocument doc(tr("ImageLabeler"));
  1071. QDomElement root = doc.createElement(tr("pixelwise_labeling"));
  1072. doc.appendChild(root);
  1073. QDomElement image = doc.createElement(tr("image"));
  1074. root.appendChild(image);
  1075. QDomText pathToImage = doc.createTextNode(*current_image_);
  1076. image.appendChild(pathToImage);
  1077. if (!segmented_image_.isEmpty()) {
  1078. QDomElement segmentedImage = doc.createElement(tr("segmented"));
  1079. root.appendChild(segmentedImage);
  1080. QDomText pathToSegmented = doc.createTextNode(segmented_image_);
  1081. segmentedImage.appendChild(pathToSegmented);
  1082. }
  1083. QDomElement description = doc.createElement(tr("description"));
  1084. root.appendChild(description);
  1085. QDomText descriptionText = doc.createTextNode(image_description_);
  1086. description.appendChild(descriptionText);
  1087. legendToXml(&doc, &root);
  1088. objectsToXml(&doc, &root);
  1089. setPureData();
  1090. QDomElement pureData = doc.createElement(tr("pure_data"));
  1091. QSize imageSize = image_->size();
  1092. QString pixelValues;
  1093. for (int i = 0; i < imageSize.height(); i++) {
  1094. for (int j = 0; j < imageSize.width(); j++) {
  1095. pixelValues.append(QString("%1").arg(pure_data_[i][j]));
  1096. //pixelValues.append(QString(""));
  1097. }
  1098. pixelValues.append("\n");
  1099. }
  1100. QDomText pureDataText = doc.createTextNode(pixelValues);
  1101. pureData.appendChild(pureDataText);
  1102. root.appendChild(pureData);
  1103. QString xml = doc.toString();
  1104. /* ------------------------------------------------------------------------
  1105. * XML part ends
  1106. */
  1107. QFileDialog fileDialog(0, tr("Save all info"));
  1108. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1109. fileDialog.setDefaultSuffix("dat");
  1110. fileDialog.setFileMode(QFileDialog::AnyFile);
  1111. QString dir = getDirFromPath(&(*current_image_));
  1112. /* altering the name of a new file */
  1113. QString newFileName = alterFileName(*current_image_, "_labeled");
  1114. fileDialog.selectFile(newFileName);
  1115. fileDialog.setDirectory(dir);
  1116. QString filename;
  1117. if (fileDialog.exec()) {
  1118. filename = fileDialog.selectedFiles().last();
  1119. }
  1120. else {
  1121. //showWarning(tr("Can not open file dialog"));
  1122. return;
  1123. /* NOTREACHED */
  1124. }
  1125. if (filename.isEmpty()) {
  1126. return;
  1127. /* NOTREACHED */
  1128. }
  1129. QFile file(filename);
  1130. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1131. showWarning(tr("Can not open file for writing"));
  1132. return;
  1133. /* NOTREACHED */
  1134. }
  1135. file.write(xml.toLocal8Bit());
  1136. file.close();
  1137. unsaved_data_ = 0;
  1138. }
  1139. void
  1140. ImageLabeler::saveSegmentedPicture()
  1141. {
  1142. if (list_bounding_box_.isEmpty() && list_polygon_.isEmpty()) {
  1143. return;
  1144. /* NOTREACHED */
  1145. }
  1146. setPureData();
  1147. // QString suffix = *current_image_;
  1148. // int dotPos = suffix.lastIndexOf(".");
  1149. // suffix.mid(dotPos + 1, suffix.size() - dotPos - 1);
  1150. QFileDialog fileDialog(0, tr("Save segmented picture"));
  1151. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1152. fileDialog.setDefaultSuffix("png");
  1153. fileDialog.setFileMode(QFileDialog::AnyFile);
  1154. QString dir = getDirFromPath(&(*current_image_));
  1155. /* altering the name of a new file */
  1156. QString newFileName = alterFileName(*current_image_, "_segmented");
  1157. fileDialog.selectFile(newFileName);
  1158. fileDialog.setDirectory(dir);
  1159. QString filename;
  1160. if (fileDialog.exec()) {
  1161. filename = fileDialog.selectedFiles().last();
  1162. }
  1163. else {
  1164. return;
  1165. /* NOTREACHED */
  1166. }
  1167. QSize imageSize = image_holder_->pixmap()->size();
  1168. QImage newImage(imageSize, QImage::Format_RGB32);
  1169. bool generateColorsFlag = auto_color_generation_;
  1170. bool flag = 0;
  1171. /* checking if all the colors are are different from white(default) */
  1172. if (!generateColorsFlag) {
  1173. for (int i = 1; i < list_label_->count(); i++) {
  1174. if (list_label_colors_.at(i) == 0xffffffff && !flag)
  1175. flag = 1;
  1176. else if (list_label_colors_.at(i) == 0xffffffff && flag) {
  1177. QMessageBox msgBox;
  1178. msgBox.setText(tr("There are few labels with default white color."));
  1179. msgBox.setInformativeText(tr("Do you want to generate all colors automatically? Otherwise you'll have to do it manually."));
  1180. msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  1181. msgBox.setDefaultButton(QMessageBox::Yes);
  1182. msgBox.setIcon(QMessageBox::Question);
  1183. int ret = msgBox.exec();
  1184. if (QMessageBox::Yes == ret) {
  1185. generateColorsFlag = 1;
  1186. break;
  1187. }
  1188. else {
  1189. return;
  1190. /* NOTREACHED */
  1191. }
  1192. }
  1193. }
  1194. }
  1195. /* generating colors for labels */
  1196. if (list_label_colors_.count() < list_label_->count() ||
  1197. generateColorsFlag) {
  1198. generateColors();
  1199. }
  1200. for (int i = 0; i < imageSize.height(); i++)
  1201. for (int j = 0; j < imageSize.width(); j++) {
  1202. newImage.setPixel(j, i, list_label_colors_.at(pure_data_[i][j]));
  1203. }
  1204. if (!newImage.save(filename, "png", 100)) {
  1205. showWarning(tr("An error occurred while saving the segmented image"));
  1206. return;
  1207. /* NOTREACHED */
  1208. }
  1209. segmented_image_ = filename;
  1210. action_view_segmented_->setEnabled(true);
  1211. }
  1212. void
  1213. ImageLabeler::saveLegend()
  1214. {
  1215. if (!list_label_->count()) {
  1216. showWarning("You have not added any label yet");
  1217. return;
  1218. /* NOTREACHED */
  1219. }
  1220. /* ------------------------------------------------------------------------
  1221. * XML part
  1222. */
  1223. QDomDocument doc(tr("ImageLabeler"));
  1224. QDomElement root = doc.createElement(tr("root"));
  1225. doc.appendChild(root);
  1226. legendToXml(&doc, &root);
  1227. QString xml = doc.toString();
  1228. /* ------------------------------------------------------------------------
  1229. * XML part ends
  1230. */
  1231. QFileDialog fileDialog(0, tr("Save legend"));
  1232. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1233. fileDialog.setDefaultSuffix("dat");
  1234. fileDialog.setFileMode(QFileDialog::AnyFile);
  1235. /* altering the name of a new file */
  1236. QString newFileName = alterFileName(*current_image_, "_legend");
  1237. fileDialog.selectFile(newFileName);
  1238. QString filename;
  1239. if (fileDialog.exec()) {
  1240. filename = fileDialog.selectedFiles().last();
  1241. }
  1242. else {
  1243. //showWarning(tr("Can not open file dialog"));
  1244. return;
  1245. /* NOTREACHED */
  1246. }
  1247. if (filename.isEmpty()) {
  1248. return;
  1249. /* NOTREACHED */
  1250. }
  1251. QFile file(filename);
  1252. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1253. showWarning(tr("Can not open file for writing"));
  1254. return;
  1255. /* NOTREACHED */
  1256. }
  1257. file.write(xml.toLocal8Bit());
  1258. file.close();
  1259. }
  1260. void
  1261. ImageLabeler::loadInfo()
  1262. {
  1263. if (askForUnsavedData()) {
  1264. return;
  1265. /* NOTREACHED */
  1266. }
  1267. QFileDialog fileDialog(0, tr("Load file with info"));
  1268. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1269. fileDialog.setDefaultSuffix("dat");
  1270. fileDialog.setFileMode(QFileDialog::AnyFile);
  1271. QString filename;
  1272. if (fileDialog.exec()) {
  1273. filename = fileDialog.selectedFiles().last();
  1274. }
  1275. else {
  1276. //showWarning(tr("Can not open file dialog"));
  1277. return;
  1278. /* NOTREACHED */
  1279. }
  1280. QDomDocument doc("Image Labeler");
  1281. QFile file(filename);
  1282. if (!file.open(QIODevice::ReadOnly)) {
  1283. showWarning(tr("Can not open such file"));
  1284. return;
  1285. /* NOTREACHED */
  1286. }
  1287. QString errMsg;
  1288. if (!doc.setContent(&file, &errMsg)) {
  1289. showWarning(errMsg);
  1290. file.close();
  1291. return;
  1292. /* NOTREACHED */
  1293. }
  1294. file.close();
  1295. clearAll();
  1296. list_label_->clear();
  1297. enableTools();
  1298. /* getting all info */
  1299. QDomElement elements = doc.documentElement();
  1300. QDomNode rootNode = elements.firstChild();
  1301. QString string;
  1302. while(!rootNode.isNull()) {
  1303. QDomElement element = rootNode.toElement();
  1304. if(!element.isNull()) {
  1305. /* path to the image */
  1306. if (element.tagName() == "image") {
  1307. string = element.text();
  1308. if (string.isEmpty()) {
  1309. showWarning(
  1310. tr(
  1311. "The file with data doesn't contain path to the image"
  1312. )
  1313. );
  1314. return;
  1315. /* NOTREACHED */
  1316. }
  1317. list_images_->append(string);
  1318. current_image_ = list_images_->end();
  1319. current_image_--;
  1320. QString winTitle;
  1321. winTitle.append("ImageLabeler - ");
  1322. winTitle.append(*current_image_);
  1323. setWindowTitle(winTitle);
  1324. image_->load(*current_image_);
  1325. image_holder_->resize(image_->size());
  1326. image_holder_->setPixmap(*image_);
  1327. }
  1328. /* path to the segmented image */
  1329. if (element.tagName() == "segmented") {
  1330. string = element.text();
  1331. if (string.isEmpty()) {
  1332. continue;
  1333. }
  1334. segmented_image_ = string;
  1335. action_view_segmented_->setEnabled(true);
  1336. }
  1337. /* image description */
  1338. else if (element.tagName() == "description" &&
  1339. !element.text().isEmpty()) {
  1340. image_description_ = element.text();
  1341. }
  1342. /* legend */
  1343. else if (element.tagName() == "legend") {
  1344. loadLegendFromNode(&element);
  1345. }
  1346. /* objects */
  1347. else if (element.tagName() == "objects") {
  1348. QDomNode subNode = element.firstChild();
  1349. QDomElement subElement;
  1350. while(!subNode.isNull()) {
  1351. subElement = subNode.toElement();
  1352. if (subElement.isNull() || subElement.text().isEmpty()) {
  1353. subNode = subNode.nextSibling();
  1354. continue;
  1355. }
  1356. string = subElement.attribute("id");
  1357. bool ok = 1;
  1358. int id = string.toInt(&ok, 10);
  1359. if (!ok) {
  1360. qDebug() <<
  1361. "while getting poly: "
  1362. "poly id format is corrupted";
  1363. subNode = subNode.nextSibling();
  1364. continue;
  1365. }
  1366. string = subElement.text();
  1367. if (subElement.tagName() == "bbox") {
  1368. setBBoxFromData(&string, &id);
  1369. }
  1370. if (subElement.tagName() == "poly") {
  1371. setPolyFromData(&string, &id);
  1372. }
  1373. subNode = subNode.nextSibling();
  1374. }
  1375. }
  1376. }
  1377. rootNode = rootNode.nextSibling();
  1378. }
  1379. }
  1380. void
  1381. ImageLabeler::loadImages()
  1382. {
  1383. if (askForUnsavedData()) {
  1384. return;
  1385. /* NOTREACHED */
  1386. }
  1387. QFileDialog fileDialog(0, tr("Load images"));
  1388. fileDialog.setFileMode(QFileDialog::Directory);
  1389. QString dirName("");
  1390. if (fileDialog.exec()) {
  1391. /* TODO: make it possible to select multiple folders */
  1392. dirName = fileDialog.selectedFiles().last();
  1393. }
  1394. else {
  1395. //showWarning(tr("Could not open file dialog"));
  1396. return;
  1397. /* NOTREACHED */
  1398. }
  1399. clearAllTool();
  1400. QWidget *searchInProgress = new QWidget(0);
  1401. searchInProgress->setWindowTitle(tr("Loading images"));
  1402. QLabel *info = new QLabel(searchInProgress);
  1403. info->setText(
  1404. tr("Program is looking for all image files in your directory recursively."));
  1405. QPushButton *cancel = new QPushButton(tr("Cancel"), searchInProgress);
  1406. connect(cancel, SIGNAL(clicked()), this, SLOT(interruptSearch()));
  1407. QVBoxLayout *layout = new QVBoxLayout(searchInProgress);
  1408. searchInProgress->setLayout(layout);
  1409. layout->addWidget(info);
  1410. layout->addWidget(cancel);
  1411. searchInProgress->adjustSize();
  1412. searchInProgress->move(QApplication::desktop()->screen()->rect().center() - rect().center());
  1413. searchInProgress->show();
  1414. getImagesFromDir(QDir(dirName));
  1415. cancel->disconnect();
  1416. searchInProgress->hide();
  1417. delete info;
  1418. delete cancel;
  1419. delete layout;
  1420. delete searchInProgress;
  1421. if (interrupt_search_) {
  1422. interrupt_search_ = 0;
  1423. return;
  1424. /* NOTREACHED */
  1425. }
  1426. if (list_images_->isEmpty()) {
  1427. showWarning(tr("The folder you selected contains no images"));
  1428. return;
  1429. /* NOTREACHED */
  1430. }
  1431. current_image_ = list_images_->begin();
  1432. QString winTitle;
  1433. winTitle.append("ImageLabeler - ");
  1434. winTitle.append(*current_image_);
  1435. setWindowTitle(winTitle);
  1436. image_->load(*current_image_);
  1437. image_holder_->resize(image_->size());
  1438. image_holder_->setPixmap(*image_);
  1439. enableTools();
  1440. }
  1441. void
  1442. ImageLabeler::loadLegendFromFile()
  1443. {
  1444. QFileDialog fileDialog(0, tr("Load legend"));
  1445. fileDialog.setFileMode(QFileDialog::AnyFile);
  1446. QString filename;
  1447. if (fileDialog.exec()) {
  1448. filename = fileDialog.selectedFiles().last();
  1449. }
  1450. else {
  1451. //showWarning(tr("Could not open file dialog"));
  1452. return;
  1453. /* NOTREACHED */
  1454. }
  1455. QDomDocument doc("Image Labeler");
  1456. QFile file(filename);
  1457. if (!file.open(QIODevice::ReadOnly)) {
  1458. showWarning(tr("Can not open such file"));
  1459. return;
  1460. /* NOTREACHED */
  1461. }
  1462. QString errMsg;
  1463. if (!doc.setContent(&file, &errMsg)) {
  1464. showWarning(errMsg);
  1465. file.close();
  1466. return;
  1467. /* NOTREACHED */
  1468. }
  1469. file.close();
  1470. list_label_->clear();
  1471. /* getting legend */
  1472. QDomElement elements = doc.documentElement();
  1473. QDomNode rootNode = elements.firstChild();
  1474. QString string;
  1475. while(!rootNode.isNull()) {
  1476. QDomElement element = rootNode.toElement();
  1477. if(!element.isNull()) {
  1478. if (element.tagName() == "legend") {
  1479. loadLegendFromNode(&element);
  1480. }
  1481. }
  1482. rootNode = rootNode.nextSibling();
  1483. }
  1484. }
  1485. void
  1486. ImageLabeler::loadLegendFromNode(QDomElement *anElement)
  1487. {
  1488. if (!anElement) {
  1489. return;
  1490. /* NOTREACHED */
  1491. }
  1492. QDomNode subNode = anElement->firstChild();
  1493. QDomElement subElement;
  1494. QString string;
  1495. int id = -1;
  1496. bool isMain;
  1497. uint color = 0xff000000;
  1498. while(!subNode.isNull()) {
  1499. subElement = subNode.toElement();
  1500. if (!subElement.isNull() && !subElement.text().isEmpty()) {
  1501. string = subElement.attribute("id");
  1502. bool ok = 0;
  1503. id = string.toInt(&ok, 10);
  1504. if (!ok) {
  1505. qDebug() <<
  1506. "while getting legend: "
  1507. "label id format is corrupted";
  1508. subNode = subNode.nextSibling();
  1509. continue;
  1510. }
  1511. string = subElement.attribute("isMain");
  1512. isMain = string.toInt(&ok, 2);
  1513. if (!ok) {
  1514. qDebug() <<
  1515. "while getting legend: "
  1516. "label isMain flag format is corrupted";
  1517. subNode = subNode.nextSibling();
  1518. continue;
  1519. }
  1520. string = subElement.attribute("color");
  1521. color = string.toUInt(&ok, 16);
  1522. if (!ok) {
  1523. qDebug() <<
  1524. "while getting legend: "
  1525. "label color format is corrupted";
  1526. subNode = subNode.nextSibling();
  1527. continue;
  1528. }
  1529. string = subElement.text();
  1530. addLabel(id, isMain, string);
  1531. setLabelColor(id, color);
  1532. }
  1533. subNode = subNode.nextSibling();
  1534. }
  1535. }
  1536. void
  1537. ImageLabeler::legendToXml(QDomDocument *aDoc, QDomElement *aRoot)
  1538. {
  1539. QDomElement legend = aDoc->createElement(tr("legend"));
  1540. aRoot->appendChild(legend);
  1541. /* storing all labels made by user */
  1542. int labelCount = list_label_->count();
  1543. for (int i = 0; i < labelCount; i++) {
  1544. QDomElement label = aDoc->createElement(tr("label"));
  1545. label.setAttribute("color", QString("%1").arg(list_label_colors_.at(i), 0, 16));
  1546. label.setAttribute("id", i);
  1547. QString priority;
  1548. if (main_label_ == i)
  1549. priority.append("1");
  1550. else
  1551. priority.append("0");
  1552. label.setAttribute("isMain", priority);
  1553. QString labelText = list_label_->item(i)->text();
  1554. /* removing the number prefix of label */
  1555. if (-1 != labelText.indexOf(QString("%1: ").arg(i))) {
  1556. labelText = labelText.mid(3, labelText.size() - 3);
  1557. }
  1558. QDomText labelName = aDoc->createTextNode(labelText);
  1559. label.appendChild(labelName);
  1560. legend.appendChild(label);
  1561. }
  1562. /* in case we have no labels */
  1563. if (0 == labelCount) {
  1564. QDomElement label = aDoc->createElement(tr("label"));
  1565. label.setAttribute(tr("id"), -1);
  1566. legend.appendChild(label);
  1567. }
  1568. }
  1569. void
  1570. ImageLabeler::objectsToXml(QDomDocument *aDoc, QDomElement *aRoot)
  1571. {
  1572. QDomElement objects = aDoc->createElement(tr("objects"));
  1573. aRoot->appendChild(objects);
  1574. /* rects first */
  1575. for (int i = 0; i < list_bounding_box_.size(); i++) {
  1576. QDomElement rectData = aDoc->createElement(tr("bbox"));
  1577. rectData.setAttribute("id", list_bounding_box_.at(i)->label_ID_);
  1578. QRect rect = list_bounding_box_.at(i)->rect.normalized();
  1579. QString rectDataString =
  1580. QString("%1;%2;%3;%4;").
  1581. arg(rect.x()).
  1582. arg(rect.y()).
  1583. arg(rect.width()).
  1584. arg(rect.height());
  1585. QDomText rectDataText = aDoc->createTextNode(rectDataString);
  1586. rectData.appendChild(rectDataText);
  1587. objects.appendChild(rectData);
  1588. }
  1589. /* polys next */
  1590. for (int i = 0; i < list_polygon_.size(); i++) {
  1591. QDomElement polyData = aDoc->createElement(tr("poly"));
  1592. polyData.setAttribute("id", list_polygon_.at(i)->label_ID_);
  1593. QPolygon poly = list_polygon_.at(i)->poly;
  1594. QString polyDataString;
  1595. for (int j = 0; j < poly.count(); j++)
  1596. polyDataString.append(
  1597. QString("%1;%2;").
  1598. arg(poly.point(j).x()).
  1599. arg(poly.point(j).y())
  1600. );
  1601. QDomText polyDataText = aDoc->createTextNode(polyDataString);
  1602. polyData.appendChild(polyDataText);
  1603. objects.appendChild(polyData);
  1604. }
  1605. }
  1606. void
  1607. ImageLabeler::generateColors()
  1608. {
  1609. int labelCount = list_label_->count();
  1610. if (!labelCount) {
  1611. showWarning(tr("you have not added any labels yet"));
  1612. return;
  1613. /* NOTREACHED */
  1614. }
  1615. int coeff = (0xff / labelCount) * 3;
  1616. //QList< uint > colors;
  1617. list_label_colors_.clear();
  1618. list_label_colors_.append(0);
  1619. uchar red = 0xff;
  1620. uchar green = 0xff;
  1621. uchar blue = 0xff;
  1622. uchar iterationColor = coeff;
  1623. uint color = 0xffffffff;
  1624. int j = 1;
  1625. for (int i = 1; i < labelCount; i++) {
  1626. if (6 == j) {
  1627. iterationColor += coeff;
  1628. j = 1;
  1629. }
  1630. if (5 == j) {
  1631. red = 0xff - iterationColor;
  1632. green = 0xff - iterationColor;
  1633. blue = 0xff;
  1634. }
  1635. else if (4 == j) {
  1636. red = 0xff - iterationColor;
  1637. green = 0xff;
  1638. blue = 0xff - iterationColor;
  1639. }
  1640. else if (3 == j) {
  1641. red = 0xff - iterationColor;
  1642. green = 0xff;
  1643. blue = 0xff;
  1644. }
  1645. else if (2 == j) {
  1646. red = 0xff;
  1647. green = 0xff - iterationColor;
  1648. blue = 0xff;
  1649. }
  1650. else if (1 == j){
  1651. red = 0xff;
  1652. green = 0xff;
  1653. blue = 0xff - iterationColor;
  1654. }
  1655. j++;
  1656. color = red + (green * 0x100) + (blue * 0x10000) + 0xff000000;
  1657. int itemNo = list_label_colors_.count();
  1658. QPixmap iconPix = QPixmap(20, 20);
  1659. iconPix.fill(color);
  1660. QIcon icon(iconPix);
  1661. list_label_->item(itemNo)->setIcon(icon);
  1662. list_label_colors_.append(color);
  1663. }
  1664. }
  1665. void
  1666. ImageLabeler::setBBoxFromData(
  1667. QString *aBBoxData,
  1668. int *ID
  1669. )
  1670. {
  1671. BoundingBox *bbox = new BoundingBox;
  1672. *bbox = BBoxFromData(aBBoxData);
  1673. if (!bbox->rect.isValid() || !ID) {
  1674. return;
  1675. /* NOTREACHED */
  1676. }
  1677. bbox->label_ID_ = *ID;
  1678. list_bounding_box_.append(bbox);
  1679. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  1680. }
  1681. BoundingBox
  1682. ImageLabeler::BBoxFromData(
  1683. QString *aBBoxData
  1684. )
  1685. {
  1686. BoundingBox bbox;
  1687. QString buffer;
  1688. bbox.rect.setRect(-1, -1, -1, -1);
  1689. int startPos = 0;
  1690. bool ok = 1;
  1691. for (int i = 0; i < aBBoxData->size(); i++) {
  1692. if (';' != aBBoxData->at(i))
  1693. continue;
  1694. buffer = aBBoxData->mid(startPos, i - startPos);
  1695. int bboxData = buffer.toInt(&ok, 10);
  1696. if (!ok) {
  1697. qDebug() <<
  1698. "while getting objects: "
  1699. "poly format is corrupted";
  1700. break;
  1701. }
  1702. if (-1 == bbox.rect.x()) {
  1703. bbox.rect.setX(bboxData);
  1704. bbox.rect.setWidth(-1);
  1705. }
  1706. else if (-1 == bbox.rect.y()) {
  1707. bbox.rect.setY(bboxData);
  1708. bbox.rect.setHeight(-1);
  1709. }
  1710. else if (-1 == bbox.rect.width()) {
  1711. bbox.rect.setWidth(bboxData);
  1712. }
  1713. else if (-1 == bbox.rect.height()) {
  1714. bbox.rect.setHeight(bboxData);
  1715. }
  1716. startPos = i + 1;
  1717. }
  1718. if (!bbox.rect.isValid()) {
  1719. qDebug() <<
  1720. "BBoxFromData: "
  1721. "bbox format is corrupted";
  1722. bbox.rect.setRect(-1, -1, -1, -1);
  1723. }
  1724. else if (!ok) {
  1725. bbox.rect.setRect(-1, -1, -1, -1);
  1726. }
  1727. return bbox;
  1728. }
  1729. BoundingBox
  1730. ImageLabeler::BBoxFromString(
  1731. QString *aString,
  1732. int *oldID
  1733. )
  1734. {
  1735. BoundingBox bbox;
  1736. bbox.label_ID_ = -1;
  1737. *oldID = -1;
  1738. if (!aString) {
  1739. return bbox;
  1740. /* NOTREACHED */
  1741. }
  1742. if (-1 == aString->indexOf("BBox")) {
  1743. return bbox;
  1744. /* NOTREACHED */
  1745. }
  1746. /* getting bbox id in the list(it cannot be changed) */
  1747. bool ok = 0;
  1748. int bboxID = getNumFromString(aString, "BBox #", ";", &ok);
  1749. if (!ok || bboxID <= -1) {
  1750. qDebug() <<
  1751. "BBoxFromString: poly ID is corrupted";
  1752. return bbox;
  1753. /* NOTREACHED */
  1754. }
  1755. /* getting new label id */
  1756. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  1757. if (!ok || labelID <= -1) {
  1758. showWarning(
  1759. tr("new LabelID is wrong, area can not be changed")
  1760. );
  1761. return bbox;
  1762. /* NOTREACHED */
  1763. }
  1764. /* getting new points */
  1765. int pointsPos = aString->indexOf("points:") + 7;
  1766. int pointsLen = aString->size() - pointsPos;
  1767. if (pointsLen <= 0) {
  1768. showWarning(
  1769. tr("new points data is wrong, area can not be changed")
  1770. );
  1771. return bbox;
  1772. /* NOTREACHED */
  1773. }
  1774. QString pointsData = aString->mid(pointsPos, pointsLen);
  1775. bbox = BBoxFromData(&pointsData);
  1776. bbox.label_ID_ = labelID;
  1777. *oldID = bboxID;
  1778. return bbox;
  1779. }
  1780. void
  1781. ImageLabeler::setPolyFromData(
  1782. QString *aPolyData,
  1783. int *ID
  1784. )
  1785. {
  1786. Polygon *poly = new Polygon;
  1787. *poly = polyFromData(aPolyData);
  1788. if (poly->poly.isEmpty() || !ID) {
  1789. return;
  1790. /* NOTREACHED */
  1791. }
  1792. poly->label_ID_ = *ID;
  1793. list_polygon_.append(poly);
  1794. addPolyArea(list_polygon_.count() - 1, *poly);
  1795. }
  1796. Polygon
  1797. ImageLabeler::polyFromData(
  1798. QString *aPolyData
  1799. )
  1800. {
  1801. Polygon poly;
  1802. poly.label_ID_ = -1;
  1803. QPoint point;
  1804. QString buffer;
  1805. int startPos = 0;
  1806. bool ok = 1;
  1807. bool evenFlag = 0;
  1808. for (int i = 0; i < aPolyData->size(); i++) {
  1809. if (';' != aPolyData->at(i))
  1810. continue;
  1811. buffer = aPolyData->mid(startPos, i - startPos);
  1812. int polyCoor = buffer.toInt(&ok, 10);
  1813. if (!ok) {
  1814. qDebug() <<
  1815. "while getting objects: "
  1816. "poly format is corrupted";
  1817. break;
  1818. }
  1819. if (!evenFlag) {
  1820. point.setX(polyCoor);
  1821. evenFlag = 1;
  1822. }
  1823. else {
  1824. point.setY(polyCoor);
  1825. poly.poly.append(point);
  1826. evenFlag = 0;
  1827. }
  1828. startPos = i + 1;
  1829. }
  1830. if (evenFlag) {
  1831. qDebug() <<
  1832. "polyFromData: "
  1833. "poly format is corrupted";
  1834. poly.poly.clear();
  1835. }
  1836. else if (!ok) {
  1837. poly.poly.clear();
  1838. }
  1839. return poly;
  1840. }
  1841. Polygon
  1842. ImageLabeler::polyFromString(
  1843. QString *aString,
  1844. int *oldID
  1845. )
  1846. {
  1847. Polygon poly;
  1848. poly.label_ID_ = -1;
  1849. *oldID = -1;
  1850. if (!aString) {
  1851. return poly;
  1852. /* NOTREACHED */
  1853. }
  1854. if (-1 == aString->indexOf("Poly")) {
  1855. return poly;
  1856. /* NOTREACHED */
  1857. }
  1858. /* getting poly id in the list(it cannot be changed) */
  1859. bool ok = 0;
  1860. int polyID = getNumFromString(aString, "Poly #", ";", &ok);
  1861. if (!ok || polyID <= -1) {
  1862. qDebug() <<
  1863. "polyFromString: poly ID is corrupted";
  1864. return poly;
  1865. /* NOTREACHED */
  1866. }
  1867. /* getting new label id */
  1868. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  1869. if (!ok || labelID <= -1) {
  1870. showWarning(
  1871. tr("new LabelID is wrong, area can not be changed")
  1872. );
  1873. return poly;
  1874. /* NOTREACHED */
  1875. }
  1876. /* getting new points */
  1877. int pointsPos = aString->indexOf("points:") + 7;
  1878. int pointsLen = aString->size() - pointsPos;
  1879. if (pointsLen <= 0) {
  1880. showWarning(
  1881. tr("new points data is wrong, area can not be changed")
  1882. );
  1883. return poly;
  1884. /* NOTREACHED */
  1885. }
  1886. QString pointsData = aString->mid(pointsPos, pointsLen);
  1887. poly = polyFromData(&pointsData);
  1888. poly.label_ID_ = labelID;
  1889. *oldID = polyID;
  1890. return poly;
  1891. }
  1892. void
  1893. ImageLabeler::showWarning(
  1894. const QString text
  1895. )
  1896. {
  1897. if (text.isEmpty()) {
  1898. return;
  1899. /* NOTREACHED */
  1900. }
  1901. QMessageBox msgBox;
  1902. msgBox.setText(text);
  1903. msgBox.setIcon(QMessageBox::Warning);
  1904. msgBox.exec();
  1905. }
  1906. bool
  1907. ImageLabeler::askForUnsavedData()
  1908. {
  1909. if ((!list_bounding_box_.isEmpty() ||
  1910. !list_polygon_.isEmpty()) &&
  1911. unsaved_data_)
  1912. {
  1913. QMessageBox msgBox;
  1914. msgBox.setText(tr("There is some unsaved data"));
  1915. msgBox.setInformativeText(tr("Do you want to save your progress?"));
  1916. msgBox.setStandardButtons(
  1917. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  1918. msgBox.setDefaultButton(QMessageBox::Save);
  1919. msgBox.setIcon(QMessageBox::Question);
  1920. int ret = msgBox.exec();
  1921. if (QMessageBox::Save == ret)
  1922. saveAllInfo();
  1923. else if (QMessageBox::Cancel == ret)
  1924. return true;
  1925. }
  1926. return false;
  1927. }
  1928. void
  1929. ImageLabeler::setBoundingBoxTool(bool aButtonPressed)
  1930. {
  1931. if (aButtonPressed)
  1932. image_holder_->setTool(ImageHolder::BoundingBoxTool);
  1933. else {
  1934. image_holder_->setTool(ImageHolder::NoTool);
  1935. image_holder_->clearLast();
  1936. }
  1937. }
  1938. void
  1939. ImageLabeler::setPolygonTool(bool aButtonPressed)
  1940. {
  1941. if (aButtonPressed) {
  1942. image_holder_->setTool(ImageHolder::PolygonTool);
  1943. image_holder_->setFocus();
  1944. }
  1945. else {
  1946. image_holder_->setTool(ImageHolder::NoTool);
  1947. image_holder_->clearLast();
  1948. }
  1949. }
  1950. void
  1951. ImageLabeler::onSelectionStarted()
  1952. {
  1953. button_confirm_selection_->setEnabled(true);
  1954. }
  1955. void
  1956. ImageLabeler::onImageScaled()
  1957. {
  1958. }
  1959. void
  1960. ImageLabeler::confirmSelection()
  1961. {
  1962. if (!list_label_->count()) {
  1963. showWarning(tr("You haven't added any label"));
  1964. return;
  1965. /* NOTREACHED */
  1966. }
  1967. image_holder_->confirmSelection();
  1968. if (label_ID_ < 0)
  1969. label_ID_ = 0;
  1970. ImageHolder::Tool tool = image_holder_->tool();
  1971. switch (tool) {
  1972. case ImageHolder::BoundingBoxTool:
  1973. list_bounding_box_.last()->label_ID_ = label_ID_;
  1974. addBBoxArea(
  1975. list_bounding_box_.count() - 1,
  1976. *(list_bounding_box_.last())
  1977. );
  1978. break;
  1979. case ImageHolder::PolygonTool:
  1980. list_polygon_.last()->label_ID_ = label_ID_;
  1981. addPolyArea(
  1982. list_polygon_.count() - 1,
  1983. *(list_polygon_.last())
  1984. );
  1985. break;
  1986. default:
  1987. break;
  1988. }
  1989. button_confirm_selection_->setEnabled(false);
  1990. unsaved_data_ = 1;
  1991. }
  1992. void
  1993. ImageLabeler::clearAll()
  1994. {
  1995. clearLabelList();
  1996. list_areas_->clear();
  1997. list_bounding_box_.clear();
  1998. list_polygon_.clear();
  1999. list_images_->clear();
  2000. main_label_ = -1;
  2001. image_holder_->clearAll();
  2002. segmented_image_.clear();
  2003. action_view_normal_->setEnabled(false);
  2004. action_view_segmented_->setEnabled(false);
  2005. }
  2006. void
  2007. ImageLabeler::clearAllTool()
  2008. {
  2009. list_areas_->clear();
  2010. list_bounding_box_.clear();
  2011. list_polygon_.clear();
  2012. main_label_ = -1;
  2013. image_holder_->clearAll();
  2014. }
  2015. void ImageLabeler::clearLabelList()
  2016. {
  2017. list_label_->clear();
  2018. addLabel(0, false, "BACKGROUND");
  2019. list_label_->item(0)->setFlags(
  2020. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  2021. Qt::ItemIsEnabled
  2022. );
  2023. }
  2024. void
  2025. ImageLabeler::enableTools()
  2026. {
  2027. action_save_labels_->setEnabled(true);
  2028. action_save_segmented_->setEnabled(true);
  2029. action_save_legend_->setEnabled(true);
  2030. action_bound_box_tool_->setEnabled(true);
  2031. action_polygon_tool_->setEnabled(true);
  2032. action_add_description_->setEnabled(true);
  2033. action_undo_->setEnabled(true);
  2034. action_redo_->setEnabled(true);
  2035. button_bound_box_tool_->setEnabled(true);
  2036. button_polygon_tool_->setEnabled(true);
  2037. button_add_label_->setEnabled(true);
  2038. button_remove_label_->setEnabled(true);
  2039. button_prev_image_->setEnabled(true);
  2040. button_next_image_->setEnabled(true);
  2041. button_clear_selection_tool_->setEnabled(true);
  2042. button_generate_colors_->setEnabled(true);
  2043. button_delete_all_labels_->setEnabled(true);
  2044. }
  2045. void
  2046. ImageLabeler::disableTools()
  2047. {
  2048. action_save_labels_->setEnabled(false);
  2049. action_save_segmented_->setEnabled(false);
  2050. action_save_legend_->setEnabled(false);
  2051. action_bound_box_tool_->setEnabled(false);
  2052. action_polygon_tool_->setEnabled(false);
  2053. action_add_description_->setEnabled(false);
  2054. action_undo_->setEnabled(false);
  2055. action_redo_->setEnabled(false);
  2056. button_bound_box_tool_->setEnabled(false);
  2057. button_polygon_tool_->setEnabled(false);
  2058. button_add_label_->setEnabled(false);
  2059. button_remove_label_->setEnabled(false);
  2060. button_prev_image_->setEnabled(false);
  2061. button_next_image_->setEnabled(false);
  2062. button_clear_selection_tool_->setEnabled(false);
  2063. button_generate_colors_->setEnabled(false);
  2064. button_delete_all_labels_->setEnabled(false);
  2065. }
  2066. void
  2067. ImageLabeler::areaListPopupMenu(const QPoint &aPos)
  2068. {
  2069. QPoint globalPos = list_areas_->mapToGlobal(aPos);
  2070. QModelIndex index = list_areas_->indexAt(aPos);
  2071. if (-1 == index.row()) {
  2072. return;
  2073. /* NOTREACHED */
  2074. }
  2075. list_areas_->item(index.row())->setSelected(true);
  2076. popup_area_list_->exec(globalPos);
  2077. }
  2078. void
  2079. ImageLabeler::labelListPopupMenu(const QPoint &aPos)
  2080. {
  2081. QPoint globalPos = list_label_->mapToGlobal(aPos);
  2082. QModelIndex index = list_label_->indexAt(aPos);
  2083. if (-1 == index.row() || !index.row()) {
  2084. return;
  2085. /* NOTREACHED */
  2086. }
  2087. list_label_->item(index.row())->setSelected(true);
  2088. popup_label_list_->exec(globalPos);
  2089. }
  2090. void
  2091. ImageLabeler::setDescription(QString aDescription)
  2092. {
  2093. image_description_ = aDescription;
  2094. setWindowTitle(image_description_);
  2095. }
  2096. void
  2097. ImageLabeler::setPureData()
  2098. {
  2099. /* initializing array */
  2100. if (pure_data_) {
  2101. delete[] *pure_data_;
  2102. delete pure_data_;
  2103. }
  2104. QSize imageSize = image_->size();
  2105. pure_data_ = new int *[imageSize.height()];
  2106. if (!pure_data_) {
  2107. return;
  2108. /* NOTREACHED */
  2109. }
  2110. for (int i = 0; i < imageSize.height(); i++) {
  2111. pure_data_[i] = new int[imageSize.width()];
  2112. if (!pure_data_[i]) {
  2113. return;
  2114. /* NOTREACHED */
  2115. }
  2116. }
  2117. int bboxCnt = list_bounding_box_.count();
  2118. int polyCnt = list_polygon_.count();
  2119. for (int i = 0; i < imageSize.height(); i++)
  2120. for (int j = 0; j < imageSize.width(); j++) {
  2121. pure_data_[i][j] = 0;
  2122. /* bboxes first */
  2123. for (int cnt = 0; cnt < bboxCnt; cnt++) {
  2124. BoundingBox *bbox = list_bounding_box_.at(cnt);
  2125. if (bbox->rect.contains(j, i)) {
  2126. pure_data_[i][j] = bbox->label_ID_;
  2127. }
  2128. }
  2129. /* polys next */
  2130. for (int cnt = 0; cnt < polyCnt; cnt++) {
  2131. Polygon *poly = list_polygon_.at(cnt);
  2132. if (poly->poly.containsPoint(QPoint(j, i), Qt::OddEvenFill)) {
  2133. pure_data_[i][j] = poly->label_ID_;
  2134. }
  2135. }
  2136. }
  2137. }
  2138. void
  2139. ImageLabeler::setLabelColor()
  2140. {
  2141. if (list_label_colors_.count() < list_label_->count()) {
  2142. generateColors();
  2143. }
  2144. QListWidgetItem *current = list_label_->currentItem();
  2145. int labelID = list_label_->row(current);
  2146. QColor defaultColor;
  2147. defaultColor.setRgb(list_label_colors_.at(labelID));
  2148. QColor newColor = QColorDialog::getColor(
  2149. defaultColor,
  2150. list_label_
  2151. );
  2152. if (!newColor.isValid()) {
  2153. return;
  2154. /* NOTREACHED */
  2155. }
  2156. list_label_colors_.takeAt(labelID);
  2157. list_label_colors_.insert(labelID, newColor.rgb());
  2158. QPixmap iconPix = QPixmap(20, 20);
  2159. iconPix.fill(newColor);
  2160. QIcon icon(iconPix);
  2161. current->setIcon(icon);
  2162. }
  2163. void
  2164. ImageLabeler::setLabelColor(int anID, QColor aColor)
  2165. {
  2166. if (anID < 0) {
  2167. return;
  2168. /* NOTREACHED */
  2169. }
  2170. if (list_label_colors_.count() < list_label_->count()) {
  2171. generateColors();
  2172. }
  2173. QListWidgetItem *item = list_label_->item(anID);
  2174. list_label_colors_.takeAt(anID);
  2175. list_label_colors_.insert(anID, aColor.rgb());
  2176. QPixmap iconPix = QPixmap(20, 20);
  2177. iconPix.fill(aColor);
  2178. QIcon icon(iconPix);
  2179. item->setIcon(icon);
  2180. }
  2181. void
  2182. ImageLabeler::onOptionsSet()
  2183. {
  2184. auto_color_generation_ = options_form_.autoColorGeneration();
  2185. }
  2186. void
  2187. ImageLabeler::viewNormal()
  2188. {
  2189. if ((*current_image_).isEmpty()) {
  2190. return;
  2191. /* NOTREACHED */
  2192. }
  2193. image_->load(*current_image_);
  2194. image_holder_->setPixmap(*image_);
  2195. action_view_segmented_->setEnabled(true);
  2196. action_view_normal_->setEnabled(false);
  2197. }
  2198. void
  2199. ImageLabeler::viewSegmented()
  2200. {
  2201. if (segmented_image_.isEmpty()) {
  2202. return;
  2203. /* NOTREACHED */
  2204. }
  2205. image_->load(segmented_image_);
  2206. image_holder_->setPixmap(*image_);
  2207. action_view_segmented_->setEnabled(false);
  2208. action_view_normal_->setEnabled(true);
  2209. }
  2210. void
  2211. ImageLabeler::interruptSearch()
  2212. {
  2213. interrupt_search_ = 1;
  2214. }
  2215. void
  2216. ImageLabeler::resizeEvent (QResizeEvent *anEvent)
  2217. {
  2218. QWidget::resizeEvent(anEvent);
  2219. }
  2220. void
  2221. ImageLabeler::mousePressEvent(QMouseEvent *anEvent)
  2222. {
  2223. QWidget::mousePressEvent(anEvent);
  2224. }
  2225. void
  2226. ImageLabeler::keyPressEvent(QKeyEvent *anEvent)
  2227. {
  2228. if (Qt::Key_Left == anEvent->key() &&
  2229. Qt::NoModifier == anEvent->modifiers()) {
  2230. prevImage();
  2231. }
  2232. if (Qt::Key_Right == anEvent->key() &&
  2233. Qt::NoModifier == anEvent->modifiers()) {
  2234. nextImage();
  2235. }
  2236. if ((Qt::Key_Enter == anEvent->key() ||
  2237. Qt::Key_Return == anEvent->key()) &&
  2238. // Qt::NoModifier == anEvent->modifiers() &&
  2239. ImageHolder::NewSelection == image_holder_->state()) {
  2240. confirmSelection();
  2241. }
  2242. if ((Qt::Key_Enter == anEvent->key() ||
  2243. Qt::Key_Return == anEvent->key()) &&
  2244. // Qt::NoModifier == anEvent->modifiers() &&
  2245. -1 != image_holder_->focusedSelection()) {
  2246. image_holder_->clearFocusOnArea();
  2247. image_holder_->clearEdition();
  2248. image_holder_->update();
  2249. }
  2250. if (Qt::Key_Escape == anEvent->key()) {
  2251. image_holder_->clearLast();
  2252. image_holder_->clearFocusOnArea();
  2253. image_holder_->clearEdition();
  2254. image_holder_->update();
  2255. }
  2256. if (Qt::Key_Z == anEvent->key() &&
  2257. Qt::ControlModifier == anEvent->modifiers()) {
  2258. image_holder_->undo();
  2259. }
  2260. if (Qt::Key_Y == anEvent->key() &&
  2261. Qt::ControlModifier == anEvent->modifiers()) {
  2262. image_holder_->redo();
  2263. }
  2264. QWidget::keyPressEvent(anEvent);
  2265. }
  2266. void
  2267. ImageLabeler::wheelEvent(QWheelEvent *anEvent)
  2268. {
  2269. /* zoomin */
  2270. if (0 < anEvent->delta()) {
  2271. image_holder_->scaleImage(ZoomIn, 1.1);
  2272. }
  2273. /* zoomout */
  2274. else if (anEvent->delta() < 0) {
  2275. image_holder_->scaleImage(ZoomOut, 1.1);
  2276. }
  2277. }
  2278. /*
  2279. *
  2280. */