ImageLabeler.cpp 59 KB

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