ImageLabeler.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612
  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(onAreaItemChange(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. image_holder_,
  526. SIGNAL(areaEdited()),
  527. this,
  528. SLOT(onAreaEdit())
  529. );
  530. connect(
  531. &options_form_,
  532. SIGNAL(optionsSet()),
  533. this,
  534. SLOT(onOptionsSet())
  535. );
  536. image_holder_->setBoundingBoxList(&list_bounding_box_);
  537. image_holder_->setPolygonList(&list_polygon_);
  538. image_holder_->setLabelColorList(&list_label_colors_);
  539. image_holder_->setMainLabelNum(&main_label_);
  540. image_holder_->setImage(image_);
  541. }
  542. ImageLabeler::~ImageLabeler()
  543. {
  544. delete action_quit_;
  545. delete action_open_labeled_image_;
  546. delete action_open_images_;
  547. delete action_load_legend_;
  548. delete action_save_legend_;
  549. delete action_save_segmented_;
  550. delete action_save_labels_;
  551. delete action_view_normal_;
  552. delete action_view_segmented_;
  553. delete action_undo_;
  554. delete action_redo_;
  555. delete action_bound_box_tool_;
  556. delete action_polygon_tool_;
  557. delete action_tagging_tool_;
  558. delete action_add_description_;
  559. delete action_options_;
  560. delete action_about_;
  561. delete action_help_content_;
  562. delete menu_file_;
  563. delete menu_view_;
  564. delete menu_edit_;
  565. delete menu_help_;
  566. delete menu_bar_;
  567. delete action_delete_area_;
  568. delete action_edit_area_;
  569. delete popup_area_list_;
  570. delete action_toggle_priority_;
  571. delete action_set_color_;
  572. delete action_delete_label_;
  573. delete popup_label_list_;
  574. delete image_;
  575. delete image_holder_;
  576. delete button_add_label_;
  577. delete button_remove_label_;
  578. delete button_bound_box_tool_;
  579. delete button_polygon_tool_;
  580. delete button_tagging_tool_;
  581. delete button_clear_selection_tool_;
  582. delete button_generate_colors_;
  583. delete button_delete_all_labels_;
  584. delete button_next_image_;
  585. delete button_prev_image_;
  586. delete button_confirm_selection_;
  587. delete label_list_areas_;
  588. delete label_toolbox_;
  589. delete list_areas_;
  590. delete list_label_;
  591. delete layout_toolbox_;
  592. delete layout_right_;
  593. delete layout_center_buttons_;
  594. delete layout_frame_image_;
  595. delete layout_center_;
  596. delete layout_labelbox_buttons_;
  597. delete layout_labelbox_;
  598. delete layout_left_;
  599. delete layout_main_;
  600. delete frame_labelbox_;
  601. delete frame_toolbox_;
  602. delete frame_image_;
  603. delete frame_center_;
  604. delete central_widget_;
  605. delete list_images_;
  606. //delete current_image_;
  607. if (pure_data_) {
  608. delete[] *pure_data_;
  609. delete pure_data_;
  610. }
  611. }
  612. void
  613. ImageLabeler::addLabel()
  614. {
  615. QListWidgetItem *newItem = new QListWidgetItem;
  616. QString label;
  617. int itemNum = list_label_->count();
  618. label.append(QString("%1: ").arg(itemNum));
  619. if (itemNum) {
  620. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  621. label.append("New label");
  622. }
  623. else {
  624. newItem->setFlags(
  625. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  626. Qt::ItemIsEnabled
  627. );
  628. label.append("BACKGROUND");
  629. }
  630. newItem->setText(label);
  631. QPixmap iconPix = QPixmap(20, 20);
  632. QColor color;
  633. if (0 != itemNum)
  634. color = Qt::white;
  635. else
  636. color = Qt::black;
  637. iconPix.fill(color);
  638. QIcon icon(iconPix);
  639. list_label_colors_.append(color.rgb());
  640. newItem->setIcon(icon);
  641. list_label_->addItem(newItem);
  642. list_label_->setItemSelected(newItem, true);
  643. }
  644. void
  645. ImageLabeler::addLabel(
  646. int aLabelID,
  647. bool isMain,
  648. QString aLabel
  649. )
  650. {
  651. QListWidgetItem *newItem = new QListWidgetItem;
  652. if (0 == aLabelID) {
  653. aLabel = QString("BACKGROUND");
  654. newItem->setFlags(
  655. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  656. Qt::ItemIsEnabled
  657. );
  658. }
  659. QString label;
  660. label.append(QString("%1: %2").
  661. arg(aLabelID).
  662. arg(aLabel)
  663. );
  664. /* TODO: check if there is another main label and make it common */
  665. if (isMain) {
  666. main_label_ = aLabelID;
  667. }
  668. QPixmap iconPix = QPixmap(20, 20);
  669. QColor color;
  670. if (0 != aLabelID)
  671. color = Qt::white;
  672. else
  673. color = Qt::black;
  674. iconPix.fill(color);
  675. QIcon icon(iconPix);
  676. list_label_colors_.append(color.rgb());
  677. newItem->setIcon(icon);
  678. newItem->setText(label);
  679. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  680. list_label_->addItem(newItem);
  681. list_label_->setItemSelected(newItem, true);
  682. }
  683. void
  684. ImageLabeler::editLabel(QListWidgetItem *anItem)
  685. {
  686. QString label = anItem->text();
  687. int itemRow = list_label_->row(anItem);
  688. QString prefix = QString("%1: ").arg(itemRow);
  689. if (-1 != label.indexOf(prefix)) {
  690. return;
  691. /* NOTREACHED */
  692. }
  693. label.prepend(QString("%1: ").arg(itemRow));
  694. if (main_label_ == itemRow) {
  695. label.append(" #main");
  696. }
  697. list_label_->blockSignals(true);
  698. anItem->setText(label);
  699. list_label_->blockSignals(false);
  700. }
  701. void
  702. ImageLabeler::removeLabel()
  703. {
  704. if (0 == list_label_->count()) {
  705. return;
  706. /* NOTREACHED */
  707. }
  708. //QListWidgetItem *current = list_label_->currentItem();
  709. /* we need to keep BACKGROUND category */
  710. if (label_ID_ < 1) {
  711. return;
  712. /* NOTREACHED */
  713. }
  714. if (list_label_->count() <= label_ID_ ||
  715. list_label_colors_.count() <= label_ID_) {
  716. return;
  717. /* NOTREACHED */
  718. }
  719. list_label_->takeItem(label_ID_);
  720. list_label_colors_.takeAt(label_ID_);
  721. }
  722. void
  723. ImageLabeler::setLabelID(
  724. QListWidgetItem *anItem
  725. )
  726. {
  727. Q_UNUSED(anItem)
  728. if (!list_label_->count()) {
  729. return;
  730. /* NOTREACHED */
  731. }
  732. label_ID_ = list_label_->row(anItem);
  733. }
  734. void
  735. ImageLabeler::addBBoxArea(
  736. int anID,
  737. BoundingBox aBBox,
  738. int itemID
  739. )
  740. {
  741. QListWidgetItem *newItem = new QListWidgetItem;
  742. if (-1 == itemID)
  743. itemID = list_areas_->count();
  744. QString label;
  745. label.append(QString("%1: ").arg(itemID));
  746. label.append(QString("BBox #%1; ").arg(anID));
  747. label.append(QString("LabelID: %1; ").arg(aBBox.label_ID_));
  748. label.append(
  749. QString("points:%1;%2;%3;%4; ").
  750. arg(aBBox.rect.topLeft().x()).
  751. arg(aBBox.rect.topLeft().y()).
  752. arg(aBBox.rect.bottomRight().x()).
  753. arg(aBBox.rect.bottomRight().y())
  754. );
  755. newItem->setText(label);
  756. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  757. list_areas_->insertItem(itemID, newItem);
  758. list_areas_->setItemSelected(newItem, true);
  759. }
  760. void
  761. ImageLabeler::addPolyArea(
  762. int aPolyID,
  763. Polygon aPoly,
  764. int itemID
  765. )
  766. {
  767. QListWidgetItem *newItem = new QListWidgetItem;
  768. if (-1 == itemID)
  769. itemID = list_areas_->count();
  770. QString label;
  771. label.append(QString("%1: ").arg(itemID));
  772. label.append(QString("Poly #%1; ").arg(aPolyID));
  773. label.append(QString("LabelID: %1; ").arg(aPoly.label_ID_));
  774. label.append("points:");
  775. for (int i = 0; i < aPoly.poly.count(); i++) {
  776. label.append(
  777. QString("%1;%2;").
  778. arg(aPoly.poly.point(i).x()).
  779. arg(aPoly.poly.point(i).y())
  780. );
  781. }
  782. newItem->setText(label);
  783. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  784. list_areas_->insertItem(itemID, newItem);
  785. list_areas_->setItemSelected(newItem, true);
  786. }
  787. /* TODO: finish area editing */
  788. void
  789. ImageLabeler::editArea()
  790. {
  791. bool oldState = list_areas_->blockSignals(true);
  792. if (!list_areas_->currentItem() || !list_areas_->count()) {
  793. return;
  794. /* NOTREACHED */
  795. }
  796. QListWidgetItem *current = list_areas_->currentItem();
  797. old_area_string_ = current->text();
  798. current->setFlags(current->flags() | Qt::ItemIsEditable);
  799. list_areas_->editItem(current);
  800. list_areas_->blockSignals(oldState);
  801. }
  802. void
  803. ImageLabeler::onAreaItemChange(QListWidgetItem *anItem)
  804. {
  805. list_areas_->blockSignals(true);
  806. QString areaString = anItem->text();
  807. int oldID = -1;
  808. if (-1 != areaString.indexOf("Poly")) {
  809. Polygon *poly = new Polygon;
  810. *poly = polyFromString(&areaString, &oldID);
  811. if (-1 < poly->label_ID_ && !poly->poly.isEmpty() &&
  812. -1 < oldID) {
  813. list_polygon_.takeAt(oldID);
  814. list_polygon_.insert(oldID, poly);
  815. }
  816. else
  817. anItem->setText(old_area_string_);
  818. }
  819. else if (-1 != areaString.indexOf("BBox")) {
  820. BoundingBox *bbox = new BoundingBox;
  821. *bbox = BBoxFromString(&areaString, &oldID);
  822. if (-1 < bbox->label_ID_ && -1 < oldID) {
  823. list_bounding_box_.takeAt(oldID);
  824. list_bounding_box_.insert(oldID, bbox);
  825. }
  826. else
  827. anItem->setText(old_area_string_);
  828. }
  829. else {
  830. showWarning(tr("record format is corrupted, try again"));
  831. anItem->setText(old_area_string_);
  832. }
  833. anItem->setFlags(anItem->flags() ^ Qt::ItemIsEditable);
  834. list_areas_->blockSignals(false);
  835. image_holder_->update();
  836. }
  837. void
  838. ImageLabeler::onAreaEdit()
  839. {
  840. if (!list_areas_->count() ||
  841. -1 == image_holder_->focusedSelection()) {
  842. //showWarning(tr("You haven't added any label"));
  843. return;
  844. /* NOTREACHED */
  845. }
  846. int figureID = image_holder_->focusedSelection();
  847. Figure figure = image_holder_->focusedSelectionType();
  848. for (int i = 0; i < list_areas_->count(); i++) {
  849. QListWidgetItem *item = list_areas_->item(i);
  850. QString text = item->text();
  851. if (RectFigure == figure && -1 != text.indexOf("BBox")) {
  852. bool ok = 0;
  853. int num = getNumFromString(&text, "BBox #", ";", &ok);
  854. if (ok && num == figureID) {
  855. list_areas_->takeItem(i);
  856. addBBoxArea(num, *list_bounding_box_.at(num), i);
  857. }
  858. }
  859. else if (PolyFigure == figure && -1 != text.indexOf("Poly")) {
  860. bool ok = 0;
  861. int num = getNumFromString(&text, "Poly #", ";", &ok);
  862. if (ok && num == figureID) {
  863. list_areas_->takeItem(i);
  864. addPolyArea(num, *list_polygon_.at(num), i);
  865. }
  866. }
  867. }
  868. }
  869. void
  870. ImageLabeler::deleteArea()
  871. {
  872. if (!list_areas_->currentItem() || !list_areas_->count()) {
  873. return;
  874. /* NOTREACHED */
  875. }
  876. QString text = list_areas_->currentItem()->text();
  877. bool ok = 0;
  878. //int labelID = getNumFromString(&text, "LabelID:", ";", &ok);
  879. // if (!ok) {
  880. // return;
  881. // /* NOTREACHED */
  882. // }
  883. /*
  884. * 0 - bbox
  885. * 1 - poly
  886. */
  887. QString shape;
  888. if (-1 != text.indexOf("BBox"))
  889. shape = QString("BBox");
  890. else if (-1 != text.indexOf("Poly"))
  891. shape = QString("Poly");
  892. else {
  893. return;
  894. /* NOTREACHED */
  895. }
  896. int areaNum = getNumFromString(&text, "#", ";", &ok);
  897. if (!ok) {
  898. return;
  899. /* NOTREACHED */
  900. }
  901. int currentItemRow = list_areas_->row(list_areas_->currentItem());
  902. /* changing all shapes(depends on current) which are next in the list */
  903. for (int i = list_areas_->count() - 1; i > currentItemRow; i--) {
  904. QListWidgetItem item = *(list_areas_->item(i));
  905. QString newText = item.text();
  906. if (-1 == newText.indexOf(shape))
  907. continue;
  908. int num = getNumFromString(&newText, "#", ";", &ok);
  909. num--;
  910. QString numString = QString("%1").arg(num);
  911. int numPos = newText.indexOf("#") + 1;
  912. newText.replace(numPos, numString.size(), numString);
  913. list_areas_->takeItem(i);
  914. list_areas_->insertItem(i, newText);
  915. }
  916. list_areas_->takeItem(currentItemRow);
  917. if (shape == "BBox")
  918. list_bounding_box_.removeAt(areaNum);
  919. else
  920. list_polygon_.removeAt(areaNum);
  921. image_holder_->update();
  922. }
  923. void
  924. ImageLabeler::toggleLabelPriority()
  925. {
  926. if (!list_label_->count()) {
  927. return;
  928. /* NOTREACHED */
  929. }
  930. QListWidgetItem *item = list_label_->currentItem();
  931. /* because we need to keep BACKGROUND category */
  932. if (0 == list_label_->row(item)) {
  933. return;
  934. /* NOTREACHED */
  935. }
  936. int itemRow = list_label_->row(item);
  937. QString text = item->text();
  938. /* cleaning previous " #main" mark */
  939. if (-1 != main_label_) {
  940. QListWidgetItem *lastMain = list_label_->item(main_label_);
  941. QString lastMainText = lastMain->text();
  942. int mainPos = lastMainText.indexOf(" #main");
  943. lastMainText = lastMainText.mid(0, mainPos);
  944. list_label_->blockSignals(true);
  945. lastMain->setText(lastMainText);
  946. list_label_->blockSignals(false);
  947. }
  948. if (main_label_ == itemRow) {
  949. int mainPos = text.indexOf(" #main");
  950. text = text.mid(0, mainPos);
  951. main_label_ = -1;
  952. }
  953. else {
  954. text.append(" #main");
  955. main_label_ = list_label_->row(item);
  956. }
  957. list_label_->blockSignals(true);
  958. item->setText(text);
  959. list_label_->blockSignals(false);
  960. image_holder_->update();
  961. }
  962. void
  963. ImageLabeler::getImagesFromDir(const QDir &dir)
  964. {
  965. /* avoiding freezing during recursive search for files */
  966. QApplication::processEvents();
  967. if (interrupt_search_) {
  968. clearAll();
  969. return;
  970. }
  971. QStringList filenameFilter;
  972. filenameFilter <<
  973. "*.jpeg" <<
  974. "*.jpg" <<
  975. "*.gif" <<
  976. "*.png" <<
  977. "*.bmp" <<
  978. "*.tiff"
  979. ;
  980. QStringList listImages =
  981. dir.entryList(filenameFilter, QDir::Files);
  982. foreach (QString file, listImages)
  983. list_images_->append(dir.absoluteFilePath(file));
  984. QStringList listDir = dir.entryList(QDir::Dirs);
  985. foreach (QString subdir, listDir) {
  986. if ("." == subdir || ".." == subdir)
  987. continue;
  988. getImagesFromDir(QDir(dir.absoluteFilePath(subdir)));
  989. }
  990. }
  991. void
  992. ImageLabeler::nextImage()
  993. {
  994. if (list_images_->isEmpty()) {
  995. return;
  996. /* NOTREACHED */
  997. }
  998. if (askForUnsavedData()) {
  999. return;
  1000. /* NOTREACHED */
  1001. }
  1002. if (list_images_->end() == current_image_ + 1)
  1003. current_image_ = list_images_->begin();
  1004. else
  1005. current_image_++;
  1006. if ((*current_image_).isEmpty()) {
  1007. return;
  1008. /* NOTREACHED */
  1009. }
  1010. image_->load(*current_image_);
  1011. image_holder_->resize(image_->size());
  1012. image_holder_->setPixmap(*image_);
  1013. list_bounding_box_.clear();
  1014. list_polygon_.clear();
  1015. list_areas_->clear();
  1016. image_holder_->clearAll();
  1017. setWindowTitle(tr("ImageLabeler"));
  1018. }
  1019. void
  1020. ImageLabeler::prevImage()
  1021. {
  1022. if (list_images_->isEmpty()) {
  1023. return;
  1024. /* NOTREACHED */
  1025. }
  1026. if (askForUnsavedData()) {
  1027. return;
  1028. /* NOTREACHED */
  1029. }
  1030. if (list_images_->begin() == current_image_)
  1031. current_image_ = list_images_->end() - 1;
  1032. else
  1033. current_image_--;
  1034. image_->load(*current_image_);
  1035. image_holder_->resize(image_->size());
  1036. image_holder_->setPixmap(*image_);
  1037. list_bounding_box_.clear();
  1038. list_polygon_.clear();
  1039. list_areas_->clear();
  1040. image_holder_->clearAll();
  1041. setWindowTitle(tr("ImageLabeler"));
  1042. }
  1043. void
  1044. ImageLabeler::saveAllInfo()
  1045. {
  1046. if (list_images_->isEmpty()) {
  1047. showWarning("You have not opened any image yet");
  1048. return;
  1049. /* NOTREACHED */
  1050. }
  1051. /* ------------------------------------------------------------------------
  1052. * XML part
  1053. */
  1054. QDomDocument doc(tr("ImageLabeler"));
  1055. QDomElement root = doc.createElement(tr("pixelwise_labeling"));
  1056. doc.appendChild(root);
  1057. QDomElement image = doc.createElement(tr("image"));
  1058. root.appendChild(image);
  1059. QDomText pathToImage = doc.createTextNode(*current_image_);
  1060. image.appendChild(pathToImage);
  1061. if (!segmented_image_.isEmpty()) {
  1062. QDomElement segmentedImage = doc.createElement(tr("segmented"));
  1063. root.appendChild(segmentedImage);
  1064. QDomText pathToSegmented = doc.createTextNode(segmented_image_);
  1065. image.appendChild(pathToSegmented);
  1066. }
  1067. QDomElement description = doc.createElement(tr("description"));
  1068. root.appendChild(description);
  1069. QDomText descriptionText = doc.createTextNode(image_description_);
  1070. description.appendChild(descriptionText);
  1071. legendToXml(&doc, &root);
  1072. setPureData();
  1073. QDomElement pureData = doc.createElement(tr("pure_data"));
  1074. QSize imageSize = image_->size();
  1075. QString pixelValues;
  1076. for (int i = 0; i < imageSize.height(); i++) {
  1077. for (int j = 0; j < imageSize.width(); j++) {
  1078. pixelValues.append(QString("%1").arg(pure_data_[i][j]));
  1079. //pixelValues.append(QString(""));
  1080. }
  1081. pixelValues.append("\n");
  1082. }
  1083. QDomText pureDataText = doc.createTextNode(pixelValues);
  1084. pureData.appendChild(pureDataText);
  1085. root.appendChild(pureData);
  1086. QString xml = doc.toString();
  1087. /* ------------------------------------------------------------------------
  1088. * XML part ends
  1089. */
  1090. QFileDialog fileDialog(0, tr("Save all info"));
  1091. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1092. fileDialog.setDefaultSuffix("dat");
  1093. fileDialog.setFileMode(QFileDialog::AnyFile);
  1094. /* altering the name of a new file */
  1095. QString newFileName = alterFileName(*current_image_, "_labeled");
  1096. fileDialog.selectFile(newFileName);
  1097. QString filename;
  1098. if (fileDialog.exec()) {
  1099. filename = fileDialog.selectedFiles().last();
  1100. }
  1101. else {
  1102. //showWarning(tr("Can not open file dialog"));
  1103. return;
  1104. /* NOTREACHED */
  1105. }
  1106. if (filename.isEmpty()) {
  1107. return;
  1108. /* NOTREACHED */
  1109. }
  1110. QFile file(filename);
  1111. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1112. showWarning(tr("Can not open file for writing"));
  1113. return;
  1114. /* NOTREACHED */
  1115. }
  1116. file.write(xml.toLocal8Bit());
  1117. file.close();
  1118. }
  1119. void
  1120. ImageLabeler::saveSegmentedPicture()
  1121. {
  1122. if (list_bounding_box_.isEmpty() && list_polygon_.isEmpty()) {
  1123. return;
  1124. /* NOTREACHED */
  1125. }
  1126. setPureData();
  1127. // QString suffix = *current_image_;
  1128. // int dotPos = suffix.lastIndexOf(".");
  1129. // suffix.mid(dotPos + 1, suffix.size() - dotPos - 1);
  1130. QFileDialog fileDialog(0, tr("Save segmented picture"));
  1131. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1132. fileDialog.setDefaultSuffix("png");
  1133. fileDialog.setFileMode(QFileDialog::AnyFile);
  1134. /* altering the name of a new file */
  1135. QString newFileName = alterFileName(*current_image_, "_segmented");
  1136. fileDialog.selectFile(newFileName);
  1137. QString filename;
  1138. if (fileDialog.exec()) {
  1139. filename = fileDialog.selectedFiles().last();
  1140. }
  1141. else {
  1142. return;
  1143. /* NOTREACHED */
  1144. }
  1145. QSize imageSize = image_holder_->pixmap()->size();
  1146. QImage newImage(imageSize, QImage::Format_RGB32);
  1147. bool generateColorsFlag = auto_color_generation_;
  1148. bool flag = 0;
  1149. /* checking if all the colors are are different from white(default) */
  1150. if (!generateColorsFlag) {
  1151. for (int i = 1; i < list_label_->count(); i++) {
  1152. if (list_label_colors_.at(i) == 0xffffffff && !flag)
  1153. flag = 1;
  1154. else if (list_label_colors_.at(i) == 0xffffffff && flag) {
  1155. QMessageBox msgBox;
  1156. msgBox.setText(tr("There are few labels with default white color."));
  1157. msgBox.setInformativeText(tr("Do you want to generate all colors automatically? Otherwise you'll have to do it manually."));
  1158. msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  1159. msgBox.setDefaultButton(QMessageBox::Yes);
  1160. msgBox.setIcon(QMessageBox::Question);
  1161. int ret = msgBox.exec();
  1162. if (QMessageBox::Yes == ret) {
  1163. generateColorsFlag = 1;
  1164. break;
  1165. }
  1166. else {
  1167. return;
  1168. /* NOTREACHED */
  1169. }
  1170. }
  1171. }
  1172. }
  1173. /* generating colors for labels */
  1174. if (list_label_colors_.count() < list_label_->count() ||
  1175. generateColorsFlag) {
  1176. generateColors();
  1177. }
  1178. for (int i = 0; i < imageSize.height(); i++)
  1179. for (int j = 0; j < imageSize.width(); j++) {
  1180. newImage.setPixel(j, i, list_label_colors_.at(pure_data_[i][j]));
  1181. }
  1182. if (!newImage.save(filename, "png", 100)) {
  1183. showWarning(tr("An error occurred while saving the segmented image"));
  1184. return;
  1185. /* NOTREACHED */
  1186. }
  1187. segmented_image_ = filename;
  1188. action_view_segmented_->setEnabled(true);
  1189. }
  1190. void
  1191. ImageLabeler::saveLegend()
  1192. {
  1193. if (!list_label_->count()) {
  1194. showWarning("You have not added any label yet");
  1195. return;
  1196. /* NOTREACHED */
  1197. }
  1198. /* ------------------------------------------------------------------------
  1199. * XML part
  1200. */
  1201. QDomDocument doc(tr("ImageLabeler"));
  1202. QDomElement root = doc.createElement(tr("root"));
  1203. doc.appendChild(root);
  1204. legendToXml(&doc, &root);
  1205. QString xml = doc.toString();
  1206. /* ------------------------------------------------------------------------
  1207. * XML part ends
  1208. */
  1209. QFileDialog fileDialog(0, tr("Save legend"));
  1210. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1211. fileDialog.setDefaultSuffix("dat");
  1212. fileDialog.setFileMode(QFileDialog::AnyFile);
  1213. /* altering the name of a new file */
  1214. QString newFileName = alterFileName(*current_image_, "_legend");
  1215. fileDialog.selectFile(newFileName);
  1216. QString filename;
  1217. if (fileDialog.exec()) {
  1218. filename = fileDialog.selectedFiles().last();
  1219. }
  1220. else {
  1221. //showWarning(tr("Can not open file dialog"));
  1222. return;
  1223. /* NOTREACHED */
  1224. }
  1225. if (filename.isEmpty()) {
  1226. return;
  1227. /* NOTREACHED */
  1228. }
  1229. QFile file(filename);
  1230. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1231. showWarning(tr("Can not open file for writing"));
  1232. return;
  1233. /* NOTREACHED */
  1234. }
  1235. file.write(xml.toLocal8Bit());
  1236. file.close();
  1237. }
  1238. void
  1239. ImageLabeler::loadInfo()
  1240. {
  1241. if (askForUnsavedData()) {
  1242. return;
  1243. /* NOTREACHED */
  1244. }
  1245. QFileDialog fileDialog(0, tr("Load file with info"));
  1246. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1247. fileDialog.setDefaultSuffix("dat");
  1248. fileDialog.setFileMode(QFileDialog::AnyFile);
  1249. QString filename;
  1250. if (fileDialog.exec()) {
  1251. filename = fileDialog.selectedFiles().last();
  1252. }
  1253. else {
  1254. //showWarning(tr("Can not open file dialog"));
  1255. return;
  1256. /* NOTREACHED */
  1257. }
  1258. QDomDocument doc("Image Labeler");
  1259. QFile file(filename);
  1260. if (!file.open(QIODevice::ReadOnly)) {
  1261. showWarning(tr("Can not open such file"));
  1262. return;
  1263. /* NOTREACHED */
  1264. }
  1265. QString errMsg;
  1266. if (!doc.setContent(&file, &errMsg)) {
  1267. showWarning(errMsg);
  1268. file.close();
  1269. return;
  1270. /* NOTREACHED */
  1271. }
  1272. file.close();
  1273. clearAll();
  1274. list_label_->clear();
  1275. enableTools();
  1276. /* getting all info */
  1277. QDomElement elements = doc.documentElement();
  1278. QDomNode rootNode = elements.firstChild();
  1279. QString string;
  1280. while(!rootNode.isNull()) {
  1281. QDomElement element = rootNode.toElement();
  1282. if(!element.isNull()) {
  1283. /* path to the image */
  1284. if (element.tagName() == "image") {
  1285. string = element.text();
  1286. if (string.isEmpty()) {
  1287. showWarning(
  1288. tr(
  1289. "The file with data doesn't contain path to the image"
  1290. )
  1291. );
  1292. return;
  1293. /* NOTREACHED */
  1294. }
  1295. list_images_->append(string);
  1296. current_image_ = list_images_->end();
  1297. current_image_--;
  1298. image_->load(*current_image_);
  1299. image_holder_->resize(image_->size());
  1300. image_holder_->setPixmap(*image_);
  1301. }
  1302. /* path to the segmented image */
  1303. if (element.tagName() == "segmented") {
  1304. string = element.text();
  1305. if (string.isEmpty()) {
  1306. continue;
  1307. }
  1308. segmented_image_ = string;
  1309. action_view_segmented_->setEnabled(true);
  1310. }
  1311. /* image description */
  1312. else if (element.tagName() == "description" &&
  1313. !element.text().isEmpty()) {
  1314. image_description_ = element.text();
  1315. }
  1316. /* legend */
  1317. else if (element.tagName() == "legend") {
  1318. loadLegendFromNode(&element);
  1319. }
  1320. /* objects */
  1321. else if (element.tagName() == "objects") {
  1322. QDomNode subNode = element.firstChild();
  1323. QDomElement subElement;
  1324. while(!subNode.isNull()) {
  1325. subElement = subNode.toElement();
  1326. if (subElement.isNull() || subElement.text().isEmpty()) {
  1327. subNode = subNode.nextSibling();
  1328. continue;
  1329. }
  1330. string = subElement.attribute("id");
  1331. bool ok = 1;
  1332. int id = string.toInt(&ok, 10);
  1333. if (!ok) {
  1334. qDebug() <<
  1335. "while getting poly: "
  1336. "poly id format is corrupted";
  1337. subNode = subNode.nextSibling();
  1338. continue;
  1339. }
  1340. string = subElement.text();
  1341. if (subElement.tagName() == "bbox") {
  1342. setBBoxFromData(&string, &id);
  1343. }
  1344. if (subElement.tagName() == "poly") {
  1345. setPolyFromData(&string, &id);
  1346. }
  1347. subNode = subNode.nextSibling();
  1348. }
  1349. }
  1350. }
  1351. rootNode = rootNode.nextSibling();
  1352. }
  1353. }
  1354. void
  1355. ImageLabeler::loadImages()
  1356. {
  1357. if (askForUnsavedData()) {
  1358. return;
  1359. /* NOTREACHED */
  1360. }
  1361. QFileDialog fileDialog(0, tr("Load images"));
  1362. fileDialog.setFileMode(QFileDialog::Directory);
  1363. QString dirName("");
  1364. if (fileDialog.exec()) {
  1365. /* TODO: make it possible to select multiple folders */
  1366. dirName = fileDialog.selectedFiles().last();
  1367. }
  1368. else {
  1369. //showWarning(tr("Could not open file dialog"));
  1370. return;
  1371. /* NOTREACHED */
  1372. }
  1373. clearAllTool();
  1374. QWidget *searchInProgress = new QWidget(0);
  1375. searchInProgress->setWindowTitle(tr("Loading images"));
  1376. QLabel *info = new QLabel(searchInProgress);
  1377. info->setText(
  1378. tr("Program is looking for all image files in your directory recursively."));
  1379. QPushButton *cancel = new QPushButton(tr("Cancel"), searchInProgress);
  1380. connect(cancel, SIGNAL(clicked()), this, SLOT(interruptSearch()));
  1381. QVBoxLayout *layout = new QVBoxLayout(searchInProgress);
  1382. searchInProgress->setLayout(layout);
  1383. layout->addWidget(info);
  1384. layout->addWidget(cancel);
  1385. searchInProgress->adjustSize();
  1386. searchInProgress->move(QApplication::desktop()->screen()->rect().center() - rect().center());
  1387. searchInProgress->show();
  1388. getImagesFromDir(QDir(dirName));
  1389. cancel->disconnect();
  1390. searchInProgress->hide();
  1391. delete info;
  1392. delete cancel;
  1393. delete layout;
  1394. delete searchInProgress;
  1395. if (interrupt_search_) {
  1396. interrupt_search_ = 0;
  1397. return;
  1398. /* NOTREACHED */
  1399. }
  1400. if (list_images_->isEmpty()) {
  1401. showWarning(tr("The folder you selected contains no images"));
  1402. return;
  1403. /* NOTREACHED */
  1404. }
  1405. current_image_ = list_images_->begin();
  1406. image_->load(*current_image_);
  1407. image_holder_->resize(image_->size());
  1408. image_holder_->setPixmap(*image_);
  1409. enableTools();
  1410. }
  1411. void
  1412. ImageLabeler::loadLegendFromFile()
  1413. {
  1414. QFileDialog fileDialog(0, tr("Load legend"));
  1415. fileDialog.setFileMode(QFileDialog::AnyFile);
  1416. QString filename;
  1417. if (fileDialog.exec()) {
  1418. filename = fileDialog.selectedFiles().last();
  1419. }
  1420. else {
  1421. //showWarning(tr("Could not open file dialog"));
  1422. return;
  1423. /* NOTREACHED */
  1424. }
  1425. QDomDocument doc("Image Labeler");
  1426. QFile file(filename);
  1427. if (!file.open(QIODevice::ReadOnly)) {
  1428. showWarning(tr("Can not open such file"));
  1429. return;
  1430. /* NOTREACHED */
  1431. }
  1432. QString errMsg;
  1433. if (!doc.setContent(&file, &errMsg)) {
  1434. showWarning(errMsg);
  1435. file.close();
  1436. return;
  1437. /* NOTREACHED */
  1438. }
  1439. file.close();
  1440. list_label_->clear();
  1441. /* getting legend */
  1442. QDomElement elements = doc.documentElement();
  1443. QDomNode rootNode = elements.firstChild();
  1444. QString string;
  1445. while(!rootNode.isNull()) {
  1446. QDomElement element = rootNode.toElement();
  1447. if(!element.isNull()) {
  1448. if (element.tagName() == "legend") {
  1449. loadLegendFromNode(&element);
  1450. }
  1451. }
  1452. rootNode = rootNode.nextSibling();
  1453. }
  1454. }
  1455. void
  1456. ImageLabeler::loadLegendFromNode(QDomElement *anElement)
  1457. {
  1458. if (!anElement) {
  1459. return;
  1460. /* NOTREACHED */
  1461. }
  1462. QDomNode subNode = anElement->firstChild();
  1463. QDomElement subElement;
  1464. QString string;
  1465. int id = -1;
  1466. bool isMain;
  1467. uint color = 0xff000000;
  1468. while(!subNode.isNull()) {
  1469. subElement = subNode.toElement();
  1470. if (!subElement.isNull() && !subElement.text().isEmpty()) {
  1471. string = subElement.attribute("id");
  1472. bool ok = 0;
  1473. id = string.toInt(&ok, 10);
  1474. if (!ok) {
  1475. qDebug() <<
  1476. "while getting legend: "
  1477. "label id format is corrupted";
  1478. subNode = subNode.nextSibling();
  1479. continue;
  1480. }
  1481. string = subElement.attribute("isMain");
  1482. isMain = string.toInt(&ok, 2);
  1483. if (!ok) {
  1484. qDebug() <<
  1485. "while getting legend: "
  1486. "label isMain flag format is corrupted";
  1487. subNode = subNode.nextSibling();
  1488. continue;
  1489. }
  1490. string = subElement.attribute("color");
  1491. color = string.toUInt(&ok, 16);
  1492. if (!ok) {
  1493. qDebug() <<
  1494. "while getting legend: "
  1495. "label color format is corrupted";
  1496. subNode = subNode.nextSibling();
  1497. continue;
  1498. }
  1499. string = subElement.text();
  1500. addLabel(id, isMain, string);
  1501. setLabelColor(id, color);
  1502. }
  1503. subNode = subNode.nextSibling();
  1504. }
  1505. }
  1506. void
  1507. ImageLabeler::legendToXml(QDomDocument *aDoc, QDomElement *aRoot)
  1508. {
  1509. QDomElement legend = aDoc->createElement(tr("legend"));
  1510. aRoot->appendChild(legend);
  1511. /* storing all labels made by user */
  1512. int labelCount = list_label_->count();
  1513. for (int i = 0; i < labelCount; i++) {
  1514. QDomElement label = aDoc->createElement(tr("label"));
  1515. label.setAttribute("color", QString("%1").arg(list_label_colors_.at(i), 0, 16));
  1516. label.setAttribute("id", i);
  1517. QString priority;
  1518. if (main_label_ == i)
  1519. priority.append("1");
  1520. else
  1521. priority.append("0");
  1522. label.setAttribute("isMain", priority);
  1523. QString labelText = list_label_->item(i)->text();
  1524. /* removing the number prefix of label */
  1525. if (-1 != labelText.indexOf(QString("%1: ").arg(i))) {
  1526. labelText = labelText.mid(3, labelText.size() - 3);
  1527. }
  1528. QDomText labelName = aDoc->createTextNode(labelText);
  1529. label.appendChild(labelName);
  1530. legend.appendChild(label);
  1531. }
  1532. /* in case we have no labels */
  1533. if (0 == labelCount) {
  1534. QDomElement label = aDoc->createElement(tr("label"));
  1535. label.setAttribute(tr("id"), -1);
  1536. legend.appendChild(label);
  1537. }
  1538. }
  1539. void
  1540. ImageLabeler::objectsToXml(QDomDocument *aDoc, QDomElement *aRoot)
  1541. {
  1542. QDomElement objects = aDoc->createElement(tr("objects"));
  1543. aRoot->appendChild(objects);
  1544. /* rects first */
  1545. for (int i = 0; i < list_bounding_box_.size(); i++) {
  1546. QDomElement rectData = aDoc->createElement(tr("bbox"));
  1547. rectData.setAttribute("id", list_bounding_box_.at(i)->label_ID_);
  1548. QRect rect = list_bounding_box_.at(i)->rect.normalized();
  1549. QString rectDataString =
  1550. QString("%1;%2;%3;%4;").
  1551. arg(rect.x()).
  1552. arg(rect.y()).
  1553. arg(rect.width()).
  1554. arg(rect.height());
  1555. QDomText rectDataText = aDoc->createTextNode(rectDataString);
  1556. rectData.appendChild(rectDataText);
  1557. objects.appendChild(rectData);
  1558. }
  1559. /* polys next */
  1560. for (int i = 0; i < list_polygon_.size(); i++) {
  1561. QDomElement polyData = aDoc->createElement(tr("poly"));
  1562. polyData.setAttribute("id", list_polygon_.at(i)->label_ID_);
  1563. QPolygon poly = list_polygon_.at(i)->poly;
  1564. QString polyDataString;
  1565. for (int j = 0; j < poly.count(); j++)
  1566. polyDataString.append(
  1567. QString("%1;%2;").
  1568. arg(poly.point(j).x()).
  1569. arg(poly.point(j).y())
  1570. );
  1571. QDomText polyDataText = aDoc->createTextNode(polyDataString);
  1572. polyData.appendChild(polyDataText);
  1573. objects.appendChild(polyData);
  1574. }
  1575. }
  1576. void
  1577. ImageLabeler::generateColors()
  1578. {
  1579. int labelCount = list_label_->count();
  1580. if (!labelCount) {
  1581. showWarning(tr("you have not added any labels yet"));
  1582. return;
  1583. /* NOTREACHED */
  1584. }
  1585. int coeff = (0xff / labelCount) * 3;
  1586. //QList< uint > colors;
  1587. list_label_colors_.clear();
  1588. list_label_colors_.append(0);
  1589. uchar red = 0xff;
  1590. uchar green = 0xff;
  1591. uchar blue = 0xff;
  1592. uchar iterationColor = coeff;
  1593. uint color = 0xffffffff;
  1594. int j = 1;
  1595. for (int i = 1; i < labelCount; i++) {
  1596. if (6 == j) {
  1597. iterationColor += coeff;
  1598. j = 1;
  1599. }
  1600. if (5 == j) {
  1601. red = 0xff - iterationColor;
  1602. green = 0xff - iterationColor;
  1603. blue = 0xff;
  1604. }
  1605. else if (4 == j) {
  1606. red = 0xff - iterationColor;
  1607. green = 0xff;
  1608. blue = 0xff - iterationColor;
  1609. }
  1610. else if (3 == j) {
  1611. red = 0xff - iterationColor;
  1612. green = 0xff;
  1613. blue = 0xff;
  1614. }
  1615. else if (2 == j) {
  1616. red = 0xff;
  1617. green = 0xff - iterationColor;
  1618. blue = 0xff;
  1619. }
  1620. else if (1 == j){
  1621. red = 0xff;
  1622. green = 0xff;
  1623. blue = 0xff - iterationColor;
  1624. }
  1625. j++;
  1626. color = red + (green * 0x100) + (blue * 0x10000) + 0xff000000;
  1627. int itemNo = list_label_colors_.count();
  1628. QPixmap iconPix = QPixmap(20, 20);
  1629. iconPix.fill(color);
  1630. QIcon icon(iconPix);
  1631. list_label_->item(itemNo)->setIcon(icon);
  1632. list_label_colors_.append(color);
  1633. }
  1634. }
  1635. void
  1636. ImageLabeler::setBBoxFromData(
  1637. QString *aBBoxData,
  1638. int *ID
  1639. )
  1640. {
  1641. BoundingBox *bbox = new BoundingBox;
  1642. *bbox = BBoxFromData(aBBoxData);
  1643. if (!bbox->rect.isValid() || !ID) {
  1644. return;
  1645. /* NOTREACHED */
  1646. }
  1647. bbox->label_ID_ = *ID;
  1648. list_bounding_box_.append(bbox);
  1649. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  1650. }
  1651. BoundingBox
  1652. ImageLabeler::BBoxFromData(
  1653. QString *aBBoxData
  1654. )
  1655. {
  1656. BoundingBox bbox;
  1657. QString buffer;
  1658. bbox.rect.setRect(-1, -1, -1, -1);
  1659. int startPos = 0;
  1660. bool ok = 1;
  1661. for (int i = 0; i < aBBoxData->size(); i++) {
  1662. if (';' != aBBoxData->at(i))
  1663. continue;
  1664. buffer = aBBoxData->mid(startPos, i - startPos);
  1665. int bboxData = buffer.toInt(&ok, 10);
  1666. if (!ok) {
  1667. qDebug() <<
  1668. "while getting objects: "
  1669. "poly format is corrupted";
  1670. break;
  1671. }
  1672. if (-1 == bbox.rect.x()) {
  1673. bbox.rect.setX(bboxData);
  1674. bbox.rect.setWidth(-1);
  1675. }
  1676. else if (-1 == bbox.rect.y()) {
  1677. bbox.rect.setY(bboxData);
  1678. bbox.rect.setHeight(-1);
  1679. }
  1680. else if (-1 == bbox.rect.width()) {
  1681. bbox.rect.setWidth(bboxData);
  1682. }
  1683. else if (-1 == bbox.rect.height()) {
  1684. bbox.rect.setHeight(bboxData);
  1685. }
  1686. startPos = i + 1;
  1687. }
  1688. if (!bbox.rect.isValid()) {
  1689. qDebug() <<
  1690. "BBoxFromData: "
  1691. "bbox format is corrupted";
  1692. bbox.rect.setRect(-1, -1, -1, -1);
  1693. }
  1694. else if (!ok) {
  1695. bbox.rect.setRect(-1, -1, -1, -1);
  1696. }
  1697. return bbox;
  1698. }
  1699. BoundingBox
  1700. ImageLabeler::BBoxFromString(
  1701. QString *aString,
  1702. int *oldID
  1703. )
  1704. {
  1705. BoundingBox bbox;
  1706. bbox.label_ID_ = -1;
  1707. *oldID = -1;
  1708. if (!aString) {
  1709. return bbox;
  1710. /* NOTREACHED */
  1711. }
  1712. if (-1 == aString->indexOf("BBox")) {
  1713. return bbox;
  1714. /* NOTREACHED */
  1715. }
  1716. /* getting bbox id in the list(it cannot be changed) */
  1717. bool ok = 0;
  1718. int bboxID = getNumFromString(aString, "BBox #", ";", &ok);
  1719. if (!ok || bboxID <= -1) {
  1720. qDebug() <<
  1721. "BBoxFromString: poly ID is corrupted";
  1722. return bbox;
  1723. /* NOTREACHED */
  1724. }
  1725. /* getting new label id */
  1726. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  1727. if (!ok || labelID <= -1) {
  1728. showWarning(
  1729. tr("new LabelID is wrong, area can not be changed")
  1730. );
  1731. return bbox;
  1732. /* NOTREACHED */
  1733. }
  1734. /* getting new points */
  1735. int pointsPos = aString->indexOf("points:") + 7;
  1736. int pointsLen = aString->size() - pointsPos;
  1737. if (pointsLen <= 0) {
  1738. showWarning(
  1739. tr("new points data is wrong, area can not be changed")
  1740. );
  1741. return bbox;
  1742. /* NOTREACHED */
  1743. }
  1744. QString pointsData = aString->mid(pointsPos, pointsLen);
  1745. bbox = BBoxFromData(&pointsData);
  1746. bbox.label_ID_ = labelID;
  1747. *oldID = bboxID;
  1748. return bbox;
  1749. }
  1750. void
  1751. ImageLabeler::setPolyFromData(
  1752. QString *aPolyData,
  1753. int *ID
  1754. )
  1755. {
  1756. Polygon *poly = new Polygon;
  1757. *poly = polyFromData(aPolyData);
  1758. if (poly->poly.isEmpty() || !ID) {
  1759. return;
  1760. /* NOTREACHED */
  1761. }
  1762. poly->label_ID_ = *ID;
  1763. list_polygon_.append(poly);
  1764. addPolyArea(list_polygon_.count() - 1, *poly);
  1765. }
  1766. Polygon
  1767. ImageLabeler::polyFromData(
  1768. QString *aPolyData
  1769. )
  1770. {
  1771. Polygon poly;
  1772. poly.label_ID_ = -1;
  1773. QPoint point;
  1774. QString buffer;
  1775. int startPos = 0;
  1776. bool ok = 1;
  1777. bool evenFlag = 0;
  1778. for (int i = 0; i < aPolyData->size(); i++) {
  1779. if (';' != aPolyData->at(i))
  1780. continue;
  1781. buffer = aPolyData->mid(startPos, i - startPos);
  1782. int polyCoor = buffer.toInt(&ok, 10);
  1783. if (!ok) {
  1784. qDebug() <<
  1785. "while getting objects: "
  1786. "poly format is corrupted";
  1787. break;
  1788. }
  1789. if (!evenFlag) {
  1790. point.setX(polyCoor);
  1791. evenFlag = 1;
  1792. }
  1793. else {
  1794. point.setY(polyCoor);
  1795. poly.poly.append(point);
  1796. evenFlag = 0;
  1797. }
  1798. startPos = i + 1;
  1799. }
  1800. if (evenFlag) {
  1801. qDebug() <<
  1802. "polyFromData: "
  1803. "poly format is corrupted";
  1804. poly.poly.clear();
  1805. }
  1806. else if (!ok) {
  1807. poly.poly.clear();
  1808. }
  1809. return poly;
  1810. }
  1811. Polygon
  1812. ImageLabeler::polyFromString(
  1813. QString *aString,
  1814. int *oldID
  1815. )
  1816. {
  1817. Polygon poly;
  1818. poly.label_ID_ = -1;
  1819. *oldID = -1;
  1820. if (!aString) {
  1821. return poly;
  1822. /* NOTREACHED */
  1823. }
  1824. if (-1 == aString->indexOf("Poly")) {
  1825. return poly;
  1826. /* NOTREACHED */
  1827. }
  1828. /* getting poly id in the list(it cannot be changed) */
  1829. bool ok = 0;
  1830. int polyID = getNumFromString(aString, "Poly #", ";", &ok);
  1831. if (!ok || polyID <= -1) {
  1832. qDebug() <<
  1833. "polyFromString: poly ID is corrupted";
  1834. return poly;
  1835. /* NOTREACHED */
  1836. }
  1837. /* getting new label id */
  1838. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  1839. if (!ok || labelID <= -1) {
  1840. showWarning(
  1841. tr("new LabelID is wrong, area can not be changed")
  1842. );
  1843. return poly;
  1844. /* NOTREACHED */
  1845. }
  1846. /* getting new points */
  1847. int pointsPos = aString->indexOf("points:") + 7;
  1848. int pointsLen = aString->size() - pointsPos;
  1849. if (pointsLen <= 0) {
  1850. showWarning(
  1851. tr("new points data is wrong, area can not be changed")
  1852. );
  1853. return poly;
  1854. /* NOTREACHED */
  1855. }
  1856. QString pointsData = aString->mid(pointsPos, pointsLen);
  1857. poly = polyFromData(&pointsData);
  1858. poly.label_ID_ = labelID;
  1859. *oldID = polyID;
  1860. return poly;
  1861. }
  1862. void
  1863. ImageLabeler::showWarning(
  1864. const QString text
  1865. )
  1866. {
  1867. if (text.isEmpty()) {
  1868. return;
  1869. /* NOTREACHED */
  1870. }
  1871. QMessageBox msgBox;
  1872. msgBox.setText(text);
  1873. msgBox.setIcon(QMessageBox::Warning);
  1874. msgBox.exec();
  1875. }
  1876. bool
  1877. ImageLabeler::askForUnsavedData()
  1878. {
  1879. if (!list_bounding_box_.isEmpty() ||
  1880. !list_polygon_.isEmpty())
  1881. {
  1882. QMessageBox msgBox;
  1883. msgBox.setText(tr("There is some unsaved data"));
  1884. msgBox.setInformativeText(tr("Do you want to save your progress?"));
  1885. msgBox.setStandardButtons(
  1886. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  1887. msgBox.setDefaultButton(QMessageBox::Save);
  1888. msgBox.setIcon(QMessageBox::Question);
  1889. int ret = msgBox.exec();
  1890. if (QMessageBox::Save == ret)
  1891. saveAllInfo();
  1892. else if (QMessageBox::Cancel == ret)
  1893. return true;
  1894. }
  1895. return false;
  1896. }
  1897. void
  1898. ImageLabeler::setBoundingBoxTool(bool aButtonPressed)
  1899. {
  1900. if (aButtonPressed)
  1901. image_holder_->setTool(ImageHolder::BoundingBoxTool);
  1902. else {
  1903. image_holder_->setTool(ImageHolder::NoTool);
  1904. image_holder_->clearLast();
  1905. }
  1906. }
  1907. void
  1908. ImageLabeler::setPolygonTool(bool aButtonPressed)
  1909. {
  1910. if (aButtonPressed) {
  1911. image_holder_->setTool(ImageHolder::PolygonTool);
  1912. image_holder_->setFocus();
  1913. }
  1914. else {
  1915. image_holder_->setTool(ImageHolder::NoTool);
  1916. image_holder_->clearLast();
  1917. }
  1918. }
  1919. void
  1920. ImageLabeler::onSelectionStarted()
  1921. {
  1922. button_confirm_selection_->setEnabled(true);
  1923. }
  1924. void
  1925. ImageLabeler::onImageScaled()
  1926. {
  1927. }
  1928. void
  1929. ImageLabeler::confirmSelection()
  1930. {
  1931. if (!list_label_->count()) {
  1932. showWarning(tr("You haven't added any label"));
  1933. return;
  1934. /* NOTREACHED */
  1935. }
  1936. image_holder_->confirmSelection();
  1937. if (label_ID_ < 0)
  1938. label_ID_ = 0;
  1939. ImageHolder::Tool tool = image_holder_->tool();
  1940. switch (tool) {
  1941. case ImageHolder::BoundingBoxTool:
  1942. list_bounding_box_.last()->label_ID_ = label_ID_;
  1943. addBBoxArea(
  1944. list_bounding_box_.count() - 1,
  1945. *(list_bounding_box_.last())
  1946. );
  1947. break;
  1948. case ImageHolder::PolygonTool:
  1949. list_polygon_.last()->label_ID_ = label_ID_;
  1950. addPolyArea(
  1951. list_polygon_.count() - 1,
  1952. *(list_polygon_.last())
  1953. );
  1954. break;
  1955. default:
  1956. break;
  1957. }
  1958. button_confirm_selection_->setEnabled(false);
  1959. }
  1960. void
  1961. ImageLabeler::clearAll()
  1962. {
  1963. clearLabelList();
  1964. list_areas_->clear();
  1965. list_bounding_box_.clear();
  1966. list_polygon_.clear();
  1967. list_images_->clear();
  1968. main_label_ = -1;
  1969. image_holder_->clearAll();
  1970. segmented_image_.clear();
  1971. action_view_normal_->setEnabled(false);
  1972. action_view_segmented_->setEnabled(false);
  1973. }
  1974. void
  1975. ImageLabeler::clearAllTool()
  1976. {
  1977. list_areas_->clear();
  1978. list_bounding_box_.clear();
  1979. list_polygon_.clear();
  1980. main_label_ = -1;
  1981. image_holder_->clearAll();
  1982. }
  1983. void ImageLabeler::clearLabelList()
  1984. {
  1985. list_label_->clear();
  1986. addLabel(0, false, "BACKGROUND");
  1987. list_label_->item(0)->setFlags(
  1988. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  1989. Qt::ItemIsEnabled
  1990. );
  1991. }
  1992. void
  1993. ImageLabeler::enableTools()
  1994. {
  1995. action_save_labels_->setEnabled(true);
  1996. action_save_segmented_->setEnabled(true);
  1997. action_save_legend_->setEnabled(true);
  1998. action_bound_box_tool_->setEnabled(true);
  1999. action_polygon_tool_->setEnabled(true);
  2000. action_add_description_->setEnabled(true);
  2001. action_undo_->setEnabled(true);
  2002. action_redo_->setEnabled(true);
  2003. button_bound_box_tool_->setEnabled(true);
  2004. button_polygon_tool_->setEnabled(true);
  2005. button_add_label_->setEnabled(true);
  2006. button_remove_label_->setEnabled(true);
  2007. button_prev_image_->setEnabled(true);
  2008. button_next_image_->setEnabled(true);
  2009. button_clear_selection_tool_->setEnabled(true);
  2010. button_generate_colors_->setEnabled(true);
  2011. button_delete_all_labels_->setEnabled(true);
  2012. }
  2013. void
  2014. ImageLabeler::disableTools()
  2015. {
  2016. action_save_labels_->setEnabled(false);
  2017. action_save_segmented_->setEnabled(false);
  2018. action_save_legend_->setEnabled(false);
  2019. action_bound_box_tool_->setEnabled(false);
  2020. action_polygon_tool_->setEnabled(false);
  2021. action_add_description_->setEnabled(false);
  2022. action_undo_->setEnabled(false);
  2023. action_redo_->setEnabled(false);
  2024. button_bound_box_tool_->setEnabled(false);
  2025. button_polygon_tool_->setEnabled(false);
  2026. button_add_label_->setEnabled(false);
  2027. button_remove_label_->setEnabled(false);
  2028. button_prev_image_->setEnabled(false);
  2029. button_next_image_->setEnabled(false);
  2030. button_clear_selection_tool_->setEnabled(false);
  2031. button_generate_colors_->setEnabled(false);
  2032. button_delete_all_labels_->setEnabled(false);
  2033. }
  2034. void
  2035. ImageLabeler::areaListPopupMenu(const QPoint &aPos)
  2036. {
  2037. QPoint globalPos = list_areas_->mapToGlobal(aPos);
  2038. QModelIndex index = list_areas_->indexAt(aPos);
  2039. if (-1 == index.row()) {
  2040. return;
  2041. /* NOTREACHED */
  2042. }
  2043. list_areas_->item(index.row())->setSelected(true);
  2044. popup_area_list_->exec(globalPos);
  2045. }
  2046. void
  2047. ImageLabeler::labelListPopupMenu(const QPoint &aPos)
  2048. {
  2049. QPoint globalPos = list_label_->mapToGlobal(aPos);
  2050. QModelIndex index = list_label_->indexAt(aPos);
  2051. if (-1 == index.row() || !index.row()) {
  2052. return;
  2053. /* NOTREACHED */
  2054. }
  2055. list_label_->item(index.row())->setSelected(true);
  2056. popup_label_list_->exec(globalPos);
  2057. }
  2058. void
  2059. ImageLabeler::setDescription(QString aDescription)
  2060. {
  2061. image_description_ = aDescription;
  2062. setWindowTitle(image_description_);
  2063. }
  2064. void
  2065. ImageLabeler::setPureData()
  2066. {
  2067. /* initializing array */
  2068. if (pure_data_) {
  2069. delete[] *pure_data_;
  2070. delete pure_data_;
  2071. }
  2072. QSize imageSize = image_->size();
  2073. pure_data_ = new int *[imageSize.height()];
  2074. if (!pure_data_) {
  2075. return;
  2076. /* NOTREACHED */
  2077. }
  2078. for (int i = 0; i < imageSize.height(); i++) {
  2079. pure_data_[i] = new int[imageSize.width()];
  2080. if (!pure_data_[i]) {
  2081. return;
  2082. /* NOTREACHED */
  2083. }
  2084. }
  2085. int bboxCnt = list_bounding_box_.count();
  2086. int polyCnt = list_polygon_.count();
  2087. for (int i = 0; i < imageSize.height(); i++)
  2088. for (int j = 0; j < imageSize.width(); j++) {
  2089. pure_data_[i][j] = 0;
  2090. /* bboxes first */
  2091. for (int cnt = 0; cnt < bboxCnt; cnt++) {
  2092. BoundingBox *bbox = list_bounding_box_.at(cnt);
  2093. if (bbox->rect.contains(j, i)) {
  2094. pure_data_[i][j] = bbox->label_ID_;
  2095. }
  2096. }
  2097. /* polys next */
  2098. for (int cnt = 0; cnt < polyCnt; cnt++) {
  2099. Polygon *poly = list_polygon_.at(cnt);
  2100. if (poly->poly.containsPoint(QPoint(j, i), Qt::OddEvenFill)) {
  2101. pure_data_[i][j] = poly->label_ID_;
  2102. }
  2103. }
  2104. }
  2105. }
  2106. void
  2107. ImageLabeler::setLabelColor()
  2108. {
  2109. if (list_label_colors_.count() < list_label_->count()) {
  2110. generateColors();
  2111. }
  2112. QListWidgetItem *current = list_label_->currentItem();
  2113. int labelID = list_label_->row(current);
  2114. QColor defaultColor;
  2115. defaultColor.setRgb(list_label_colors_.at(labelID));
  2116. QColor newColor = QColorDialog::getColor(
  2117. defaultColor,
  2118. list_label_
  2119. );
  2120. if (!newColor.isValid()) {
  2121. return;
  2122. /* NOTREACHED */
  2123. }
  2124. list_label_colors_.takeAt(labelID);
  2125. list_label_colors_.insert(labelID, newColor.rgb());
  2126. QPixmap iconPix = QPixmap(20, 20);
  2127. iconPix.fill(newColor);
  2128. QIcon icon(iconPix);
  2129. current->setIcon(icon);
  2130. }
  2131. void
  2132. ImageLabeler::setLabelColor(int anID, QColor aColor)
  2133. {
  2134. if (anID < 0) {
  2135. return;
  2136. /* NOTREACHED */
  2137. }
  2138. if (list_label_colors_.count() < list_label_->count()) {
  2139. generateColors();
  2140. }
  2141. QListWidgetItem *item = list_label_->item(anID);
  2142. list_label_colors_.takeAt(anID);
  2143. list_label_colors_.insert(anID, aColor.rgb());
  2144. QPixmap iconPix = QPixmap(20, 20);
  2145. iconPix.fill(aColor);
  2146. QIcon icon(iconPix);
  2147. item->setIcon(icon);
  2148. }
  2149. void
  2150. ImageLabeler::onOptionsSet()
  2151. {
  2152. auto_color_generation_ = options_form_.autoColorGeneration();
  2153. }
  2154. void
  2155. ImageLabeler::viewNormal()
  2156. {
  2157. if ((*current_image_).isEmpty()) {
  2158. return;
  2159. /* NOTREACHED */
  2160. }
  2161. image_->load(*current_image_);
  2162. image_holder_->setPixmap(*image_);
  2163. action_view_segmented_->setEnabled(true);
  2164. action_view_normal_->setEnabled(false);
  2165. }
  2166. void
  2167. ImageLabeler::viewSegmented()
  2168. {
  2169. if (segmented_image_.isEmpty()) {
  2170. return;
  2171. /* NOTREACHED */
  2172. }
  2173. image_->load(segmented_image_);
  2174. image_holder_->setPixmap(*image_);
  2175. action_view_segmented_->setEnabled(false);
  2176. action_view_normal_->setEnabled(true);
  2177. }
  2178. void
  2179. ImageLabeler::interruptSearch()
  2180. {
  2181. interrupt_search_ = 1;
  2182. }
  2183. void
  2184. ImageLabeler::resizeEvent (QResizeEvent *anEvent)
  2185. {
  2186. QWidget::resizeEvent(anEvent);
  2187. }
  2188. void
  2189. ImageLabeler::mousePressEvent(QMouseEvent *anEvent)
  2190. {
  2191. QWidget::mousePressEvent(anEvent);
  2192. }
  2193. void
  2194. ImageLabeler::keyPressEvent(QKeyEvent *anEvent)
  2195. {
  2196. if (Qt::Key_Left == anEvent->key() &&
  2197. Qt::NoModifier == anEvent->modifiers()) {
  2198. prevImage();
  2199. }
  2200. if (Qt::Key_Right == anEvent->key() &&
  2201. Qt::NoModifier == anEvent->modifiers()) {
  2202. nextImage();
  2203. }
  2204. if ((Qt::Key_Enter == anEvent->key() ||
  2205. Qt::Key_Return == anEvent->key()) &&
  2206. // Qt::NoModifier == anEvent->modifiers() &&
  2207. ImageHolder::NewSelection == image_holder_->state()) {
  2208. confirmSelection();
  2209. }
  2210. if ((Qt::Key_Enter == anEvent->key() ||
  2211. Qt::Key_Return == anEvent->key()) &&
  2212. // Qt::NoModifier == anEvent->modifiers() &&
  2213. -1 != image_holder_->focusedSelection()) {
  2214. image_holder_->clearFocusOnArea();
  2215. }
  2216. if (Qt::Key_Escape == anEvent->key()) {
  2217. image_holder_->clearLast();
  2218. image_holder_->clearFocusOnArea();
  2219. }
  2220. if (Qt::Key_Z == anEvent->key() &&
  2221. Qt::ControlModifier == anEvent->modifiers()) {
  2222. image_holder_->undo();
  2223. }
  2224. if (Qt::Key_Y == anEvent->key() &&
  2225. Qt::ControlModifier == anEvent->modifiers()) {
  2226. image_holder_->redo();
  2227. }
  2228. QWidget::keyPressEvent(anEvent);
  2229. }
  2230. void
  2231. ImageLabeler::wheelEvent(QWheelEvent *anEvent)
  2232. {
  2233. /* zoomin */
  2234. if (0 < anEvent->delta()) {
  2235. image_holder_->scaleImage(ZoomIn, 1.1);
  2236. }
  2237. /* zoomout */
  2238. else if (anEvent->delta() < 0) {
  2239. image_holder_->scaleImage(ZoomOut, 1.1);
  2240. }
  2241. }
  2242. /*
  2243. *
  2244. */