ImageLabeler.cpp 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542
  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 <QCheckBox>
  19. #include <QScrollArea>
  20. #include <QPushButton>
  21. #include <QButtonGroup>
  22. #include <QListWidget>
  23. #include <QListWidgetItem>
  24. #include <QDesktopWidget>
  25. #include <QFileDialog>
  26. #include <QColorDialog>
  27. #include <QDir>
  28. #include <QMessageBox>
  29. #include <QListIterator>
  30. #include <QDomDocument>
  31. #include <QFile>
  32. #include <QKeyEvent>
  33. #include <QSettings>
  34. #include <QDebug>
  35. ImageLabeler::ImageLabeler(QWidget *aParent, QString aSettingsPath) :
  36. QMainWindow(aParent)
  37. {
  38. /*
  39. * Variables
  40. */
  41. list_images_ = new QList< Image >;
  42. main_label_ = -1;
  43. pure_data_ = 0;
  44. //label_ID_ = -1;
  45. /* options */
  46. auto_color_generation_ = 0;
  47. /* flags */
  48. interrupt_search_ = 0;
  49. unsaved_data_ = 0;
  50. /*
  51. * menu bar part begins
  52. */
  53. menu_bar_ = new QMenuBar(this);
  54. setMenuBar(menu_bar_);
  55. menu_file_ = new QMenu(menu_bar_);
  56. menu_file_->setTitle(tr("&File"));
  57. menu_pascal_ = new QMenu(menu_bar_);
  58. menu_pascal_->setTitle(tr("&Pascal"));
  59. menu_view_ = new QMenu(menu_bar_);
  60. menu_view_->setTitle(tr("&View"));
  61. menu_edit_ = new QMenu(menu_bar_);
  62. menu_edit_->setTitle(tr("&Edit"));
  63. menu_help_ = new QMenu(menu_bar_);
  64. menu_help_->setTitle(tr("&Help"));
  65. /* menu file */
  66. action_open_image_ = new QAction(this);
  67. action_open_image_->setText(tr("&Load image"));
  68. action_open_images_ = new QAction(this);
  69. action_open_images_->setText(tr("&Load images(recursively)"));
  70. action_open_labeled_image_ = new QAction(this);
  71. action_open_labeled_image_->setText(tr("&Load labeled image"));
  72. action_load_legend_ = new QAction(this);
  73. action_load_legend_->setText(tr("&Load legend"));
  74. action_save_labels_ = new QAction(this);
  75. action_save_labels_->setText(tr("&Save all info"));
  76. action_save_labels_->setEnabled(false);
  77. action_save_segmented_ = new QAction(this);
  78. action_save_segmented_->setText(tr("Save segmented &picture"));
  79. action_save_segmented_->setEnabled(false);
  80. action_save_legend_ = new QAction(this);
  81. action_save_legend_->setText(tr("Save &legend"));
  82. action_save_legend_->setEnabled(false);
  83. action_quit_ = new QAction(this);
  84. action_quit_->setText(tr("&Quit"));
  85. /* menu pascal */
  86. action_load_pascal_file_ = new QAction(this);
  87. action_load_pascal_file_->setText(tr("&Load pascal file"));
  88. action_load_pascal_poly_ = new QAction(this);
  89. action_load_pascal_poly_->setText(tr("&Load poly info"));
  90. /* menu view */
  91. action_view_normal_ = new QAction(this);
  92. action_view_normal_->setText(tr("&Normal"));
  93. action_view_normal_->setEnabled(false);
  94. action_view_segmented_ = new QAction(this);
  95. action_view_segmented_->setText(tr("&Segmented"));
  96. action_view_segmented_->setEnabled(false);
  97. /* menu edit */
  98. action_undo_ = new QAction(this);
  99. action_undo_->setText(tr("&Undo"));
  100. action_undo_->setEnabled(false);
  101. action_redo_ = new QAction(this);
  102. action_redo_->setText(tr("&Redo"));
  103. action_redo_->setEnabled(false);
  104. action_bound_box_tool_ = new QAction(this);
  105. action_bound_box_tool_->setText(tr("Bounding box tool"));
  106. action_bound_box_tool_->setEnabled(false);
  107. action_polygon_tool_ = new QAction(this);
  108. action_polygon_tool_->setText(tr("&Polygon tool"));
  109. action_polygon_tool_->setEnabled(false);
  110. action_tagging_tool_ = new QAction(this);
  111. action_tagging_tool_->setText(tr("&Tagging tool"));
  112. action_tagging_tool_->setEnabled(false);
  113. action_add_description_ = new QAction(this);
  114. action_add_description_->setText(tr("&Add image description"));
  115. action_add_description_->setEnabled(false);
  116. action_options_ = new QAction(this);
  117. action_options_->setText(tr("&Options"));
  118. /* menu help */
  119. action_help_content_ = new QAction(this);
  120. action_help_content_->setText(tr("&Help content"));
  121. action_help_content_->setEnabled(false);
  122. action_about_ = new QAction(this);
  123. action_about_->setText(tr("&About"));
  124. action_about_->setEnabled(false);
  125. /* ------------------ */
  126. menu_file_->addAction(action_open_image_);
  127. menu_file_->addAction(action_open_images_);
  128. menu_file_->addAction(action_open_labeled_image_);
  129. menu_file_->addAction(action_load_legend_);
  130. menu_file_->addAction(menu_pascal_->menuAction());
  131. menu_file_->addSeparator();
  132. menu_file_->addAction(action_save_segmented_);
  133. menu_file_->addAction(action_save_legend_);
  134. menu_file_->addAction(action_save_labels_);
  135. menu_file_->addSeparator();
  136. menu_file_->addAction(action_quit_);
  137. menu_pascal_->addAction(action_load_pascal_file_);
  138. menu_pascal_->addAction(action_load_pascal_poly_);
  139. menu_view_->addAction(action_view_normal_);
  140. menu_view_->addAction(action_view_segmented_);
  141. menu_edit_->addAction(action_undo_);
  142. menu_edit_->addAction(action_redo_);
  143. menu_edit_->addSeparator();
  144. menu_edit_->addAction(action_bound_box_tool_);
  145. menu_edit_->addAction(action_polygon_tool_);
  146. menu_edit_->addAction(action_tagging_tool_);
  147. menu_edit_->addSeparator();
  148. menu_edit_->addAction(action_add_description_);
  149. menu_edit_->addAction(action_options_);
  150. menu_help_->addAction(action_help_content_);
  151. menu_help_->addAction(action_about_);
  152. menu_bar_->addAction(menu_file_->menuAction());
  153. menu_bar_->addAction(menu_view_->menuAction());
  154. menu_bar_->addAction(menu_edit_->menuAction());
  155. menu_bar_->addAction(menu_help_->menuAction());
  156. /*
  157. * popup menu part begins
  158. */
  159. popup_area_list_ = new QMenu;
  160. action_delete_area_ = new QAction(this);
  161. action_delete_area_->setText(tr("&Delete area"));
  162. action_edit_area_ = new QAction(this);
  163. action_edit_area_->setText(tr("&Change area"));
  164. popup_area_list_->addAction(action_delete_area_);
  165. popup_area_list_->addAction(action_edit_area_);
  166. popup_label_list_ = new QMenu;
  167. action_set_color_ = new QAction(this);
  168. action_set_color_->setText(tr("Set &color"));
  169. action_toggle_priority_ = new QAction(this);
  170. action_toggle_priority_->setText(tr("Toggle &priority"));
  171. action_delete_label_ = new QAction(this);
  172. action_delete_label_->setText(tr("&Delete"));
  173. popup_label_list_->addAction(action_set_color_);
  174. popup_label_list_->addAction(action_toggle_priority_);
  175. popup_label_list_->addAction(action_delete_label_);
  176. popup_images_list_ = new QMenu;
  177. action_remove_image_ = new QAction(this);
  178. action_remove_image_->setText(tr("&remove image"));
  179. popup_images_list_->addAction(action_remove_image_);
  180. /*
  181. * widgets part begins
  182. */
  183. central_widget_ = new QWidget(this);
  184. setCentralWidget(central_widget_);
  185. frame_toolbox_ = new QFrame(central_widget_);
  186. frame_toolbox_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  187. frame_toolbox_->setLineWidth(0);
  188. frame_toolbox_->setMidLineWidth(0);
  189. frame_center_ = new QFrame(central_widget_);
  190. //frame_image_->setFrameStyle(QFrame::Box | QFrame::Plain);
  191. //frame_image_->setLineWidth(1);
  192. frame_image_ = new QScrollArea(frame_center_);
  193. //frame_image_->setStyleSheet("QWidget {background: #888888;}");
  194. frame_image_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  195. frame_image_->setLineWidth(0);
  196. frame_image_->setMidLineWidth(0);
  197. frame_image_->setWidgetResizable(false);
  198. frame_image_->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
  199. frame_image_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  200. frame_labelbox_ = new QFrame(central_widget_);
  201. frame_labelbox_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  202. frame_labelbox_->setLineWidth(0);
  203. frame_labelbox_->setMidLineWidth(0);
  204. image_ = new QPixmap(500, 500);
  205. image_->fill(QColor(Qt::white));
  206. //image_holder_ = new ImageHolder(frame_image_);
  207. image_holder_ = new ImageHolder;//(frame_image_);
  208. image_holder_->setPixmap(*image_);
  209. //image_holder_->setStyleSheet("QLabel {background: #ffffff;}");
  210. image_holder_->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
  211. image_holder_->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
  212. image_holder_->setScaledContents(true);
  213. image_holder_->setFrameStyle(QFrame::Box | QFrame::Plain);
  214. image_holder_->setLineWidth(0);
  215. frame_image_->setWidget(image_holder_);
  216. list_label_ = new QListWidget(central_widget_);
  217. list_label_->setContextMenuPolicy(Qt::CustomContextMenu);
  218. list_areas_ = new QListWidget(central_widget_);
  219. list_areas_->setContextMenuPolicy(Qt::CustomContextMenu);
  220. list_images_widget_ = new QListWidget(central_widget_);
  221. list_images_widget_->setContextMenuPolicy(Qt::CustomContextMenu);
  222. // addLabel(0, false, "BACKGROUND");
  223. // list_label_->item(0)->setFlags(Qt::ItemIsSelectable);
  224. //QListWidgetItem *background = list_label_->item(0);
  225. //qDebug() << background;
  226. //background->setFlags(Qt::ItemIsSelectable);
  227. label_toolbox_ = new QLabel(tr("Tool box"), frame_toolbox_);
  228. label_list_areas_ = new QLabel(tr("Selected areas:"), central_widget_);
  229. label_list_images_ = new QLabel(tr("Loaded images:"), central_widget_);
  230. /* buttons */
  231. button_bound_box_tool_ = new QPushButton(frame_toolbox_);
  232. button_bound_box_tool_->setText(tr("bbox"));
  233. button_bound_box_tool_->setEnabled(false);
  234. button_bound_box_tool_->setCheckable(true);
  235. button_polygon_tool_ = new QPushButton(frame_toolbox_);
  236. button_polygon_tool_->setText(tr("poly tool"));
  237. button_polygon_tool_->setEnabled(false);
  238. button_polygon_tool_->setCheckable(true);
  239. button_tagging_tool_ = new QPushButton(frame_toolbox_);
  240. button_tagging_tool_->setText(tr("tagging"));
  241. button_tagging_tool_->setEnabled(false);
  242. button_clear_selection_tool_ = new QPushButton(frame_toolbox_);
  243. button_clear_selection_tool_->setText(tr("clear selection"));
  244. button_clear_selection_tool_->setEnabled(false);
  245. button_generate_colors_ = new QPushButton(frame_toolbox_);
  246. button_generate_colors_->setText(tr("generate label colors"));
  247. button_generate_colors_->setEnabled(false);
  248. button_delete_all_labels_ = new QPushButton(frame_toolbox_);
  249. button_delete_all_labels_->setText(tr("delete all labels"));
  250. button_delete_all_labels_->setEnabled(false);
  251. group_tools_ = new QButtonGroup;
  252. group_tools_->addButton(button_bound_box_tool_);
  253. group_tools_->addButton(button_polygon_tool_);
  254. button_confirm_selection_ = new QPushButton(central_widget_);
  255. button_confirm_selection_->setText(tr("Confirm selection"));
  256. button_confirm_selection_->setEnabled(false);
  257. button_add_label_ = new QPushButton(frame_labelbox_);
  258. button_add_label_->setText(tr("Add label"));
  259. button_add_label_->setEnabled(false);
  260. button_remove_label_ = new QPushButton(frame_labelbox_);
  261. button_remove_label_->setText(tr("Remove label"));
  262. button_remove_label_->setEnabled(false);
  263. button_prev_image_ = new QPushButton(central_widget_);
  264. button_prev_image_->setText("←");
  265. button_next_image_ = new QPushButton(central_widget_);
  266. button_next_image_->setText("→");
  267. /*
  268. * layouts part begins
  269. */
  270. layout_main_ = new QHBoxLayout(central_widget_);
  271. layout_left_ = new QVBoxLayout();
  272. layout_toolbox_ = new QVBoxLayout();
  273. layout_center_ = new QVBoxLayout();
  274. layout_frame_image_ = new QVBoxLayout();
  275. layout_image_widget_ = new QGridLayout();
  276. layout_center_buttons_ = new QHBoxLayout();
  277. layout_right_ = new QVBoxLayout();
  278. layout_labelbox_ = new QVBoxLayout();
  279. layout_labelbox_buttons_ = new QHBoxLayout();
  280. layout_main_->addLayout(layout_left_);
  281. layout_main_->addLayout(layout_center_);
  282. layout_main_->addLayout(layout_right_);
  283. /* making the center part stretchable */
  284. layout_main_->setStretch(1, 1);
  285. /* left part */
  286. layout_left_->addWidget(frame_toolbox_);
  287. frame_toolbox_->setLayout(layout_toolbox_);
  288. layout_toolbox_->addWidget(label_toolbox_);
  289. layout_toolbox_->addWidget(button_bound_box_tool_);
  290. layout_toolbox_->addWidget(button_polygon_tool_);
  291. layout_toolbox_->addSpacing(10);
  292. layout_toolbox_->addWidget(button_tagging_tool_);
  293. layout_toolbox_->addSpacing(10);
  294. layout_toolbox_->addWidget(button_clear_selection_tool_);
  295. layout_toolbox_->addWidget(button_delete_all_labels_);
  296. layout_toolbox_->addSpacing(10);
  297. layout_toolbox_->addWidget(button_generate_colors_);
  298. /* stretch is for making toolbox as small as it can be */
  299. layout_toolbox_->addSpacing(10);
  300. layout_left_->addWidget(label_list_images_);
  301. layout_left_->addWidget(list_images_widget_);
  302. list_images_widget_->setFixedWidth(200);
  303. layout_left_->addStretch(1);
  304. layout_left_->addWidget(button_confirm_selection_);
  305. /* central part */
  306. layout_center_->addWidget(frame_center_);
  307. frame_center_->setLayout(layout_frame_image_);
  308. //layout_frame_image_->addWidget(image_holder_);
  309. layout_frame_image_->setContentsMargins(0, 0, 0, 0);
  310. layout_frame_image_->addWidget(frame_image_);
  311. //frame_image_->setLayout(layout_image_widget_);
  312. // layout_image_widget_->setRowStretch(0, 1);
  313. // layout_image_widget_->setColumnStretch(0, 1);
  314. // layout_image_widget_->addWidget(image_holder_, 1, 1);
  315. // layout_image_widget_->setRowStretch(2, 1);
  316. // layout_image_widget_->setColumnStretch(2, 1);
  317. //layout_frame_image_->addStretch(1);
  318. layout_frame_image_->addLayout(layout_center_buttons_);
  319. layout_center_buttons_->addWidget(button_prev_image_);
  320. layout_center_buttons_->addWidget(button_next_image_);
  321. /* right part */
  322. layout_right_->addWidget(frame_labelbox_);
  323. frame_labelbox_->setFixedWidth(200);
  324. frame_labelbox_->setLayout(layout_labelbox_);
  325. layout_labelbox_->addLayout(layout_labelbox_buttons_);
  326. layout_labelbox_buttons_->addWidget(button_add_label_);
  327. layout_labelbox_buttons_->addWidget(button_remove_label_);
  328. layout_labelbox_->addWidget(list_label_);
  329. layout_labelbox_->addWidget(label_list_areas_);
  330. layout_labelbox_->addWidget(list_areas_);
  331. connect(
  332. action_quit_,
  333. SIGNAL(triggered()),
  334. this,
  335. SLOT(close())
  336. );
  337. connect(
  338. action_open_images_,
  339. SIGNAL(triggered()),
  340. this,
  341. SLOT(loadImages())
  342. );
  343. connect(
  344. action_open_image_,
  345. SIGNAL(triggered()),
  346. this,
  347. SLOT(loadImage())
  348. );
  349. connect(
  350. action_open_labeled_image_,
  351. SIGNAL(triggered()),
  352. this,
  353. SLOT(loadInfo())
  354. );
  355. connect(
  356. action_load_legend_,
  357. SIGNAL(triggered()),
  358. this,
  359. SLOT(loadLegendFromFile())
  360. );
  361. connect(
  362. action_load_pascal_file_,
  363. SIGNAL(triggered()),
  364. this,
  365. SLOT(loadPascalFile())
  366. );
  367. connect(
  368. action_load_pascal_poly_,
  369. SIGNAL(triggered()),
  370. this,
  371. SLOT(loadPascalPolys())
  372. );
  373. connect(
  374. action_save_legend_,
  375. SIGNAL(triggered()),
  376. this,
  377. SLOT(saveLegend())
  378. );
  379. connect(
  380. action_save_segmented_,
  381. SIGNAL(triggered()),
  382. this,
  383. SLOT(saveSegmentedPicture())
  384. );
  385. connect(
  386. action_save_labels_,
  387. SIGNAL(triggered()),
  388. this,
  389. SLOT(saveAllInfo())
  390. );
  391. connect(
  392. action_view_normal_,
  393. SIGNAL(triggered()),
  394. this,
  395. SLOT(viewNormal())
  396. );
  397. connect(
  398. action_view_segmented_,
  399. SIGNAL(triggered()),
  400. this,
  401. SLOT(viewSegmented())
  402. );
  403. connect(
  404. action_undo_,
  405. SIGNAL(triggered()),
  406. image_holder_,
  407. SLOT(undo())
  408. );
  409. connect(
  410. action_redo_,
  411. SIGNAL(triggered()),
  412. image_holder_,
  413. SLOT(redo())
  414. );
  415. connect(
  416. action_add_description_,
  417. SIGNAL(triggered()),
  418. &line_edit_form_,
  419. SLOT(setDescription())
  420. );
  421. connect(
  422. action_options_,
  423. SIGNAL(triggered()),
  424. &options_form_,
  425. SLOT(showOptions())
  426. );
  427. connect(
  428. button_add_label_,
  429. SIGNAL(clicked()),
  430. this,
  431. SLOT(addLabel())
  432. );
  433. connect(
  434. button_remove_label_,
  435. SIGNAL(clicked()),
  436. this, SLOT(removeLabel())
  437. );
  438. connect(
  439. button_next_image_,
  440. SIGNAL(clicked()),
  441. this,
  442. SLOT(nextImage())
  443. );
  444. connect(
  445. button_prev_image_,
  446. SIGNAL(clicked()),
  447. this,
  448. SLOT(prevImage())
  449. );
  450. connect(
  451. button_bound_box_tool_,
  452. SIGNAL(toggled(bool)),
  453. this,
  454. SLOT(setBoundingBoxTool(bool))
  455. );
  456. connect(
  457. button_polygon_tool_,
  458. SIGNAL(toggled(bool)),
  459. this,
  460. SLOT(setPolygonTool(bool))
  461. );
  462. connect(
  463. button_tagging_tool_,
  464. SIGNAL(clicked()),
  465. &line_edit_form_,
  466. SLOT(setTags())
  467. );
  468. connect(
  469. button_clear_selection_tool_,
  470. SIGNAL(clicked()),
  471. this,
  472. SLOT(clearAllTool())
  473. );
  474. connect(
  475. button_generate_colors_,
  476. SIGNAL(clicked()),
  477. this,
  478. SLOT(generateColors())
  479. );
  480. connect(
  481. button_delete_all_labels_,
  482. SIGNAL(clicked()),
  483. this,
  484. SLOT(clearLabelList())
  485. );
  486. connect(
  487. button_confirm_selection_,
  488. SIGNAL(clicked()),
  489. this,
  490. SLOT(confirmSelection())
  491. );
  492. connect(
  493. list_label_,
  494. SIGNAL(itemChanged(QListWidgetItem *)),
  495. this,
  496. SLOT(editLabel(QListWidgetItem *))
  497. );
  498. connect(
  499. list_label_,
  500. SIGNAL(itemClicked(QListWidgetItem *)),
  501. this,
  502. SLOT(setLabelID(QListWidgetItem *))
  503. );
  504. connect(
  505. list_areas_,
  506. SIGNAL(itemDoubleClicked(QListWidgetItem *)),
  507. image_holder_,
  508. SLOT(focusOnArea(QListWidgetItem *))
  509. );
  510. connect(
  511. list_areas_,
  512. SIGNAL(customContextMenuRequested(const QPoint &)),
  513. this,
  514. SLOT(areaListPopupMenu(const QPoint &))
  515. );
  516. connect(
  517. list_areas_,
  518. SIGNAL(itemChanged(QListWidgetItem *)),
  519. this,
  520. SLOT(onAreaItemChange(QListWidgetItem *))
  521. );
  522. connect(
  523. list_label_,
  524. SIGNAL(customContextMenuRequested(const QPoint &)),
  525. this,
  526. SLOT(labelListPopupMenu(const QPoint &))
  527. );
  528. connect(
  529. list_images_widget_,
  530. SIGNAL(itemDoubleClicked(QListWidgetItem *)),
  531. this,
  532. SLOT(selectImage(QListWidgetItem *))
  533. );
  534. connect(
  535. list_images_widget_,
  536. SIGNAL(customContextMenuRequested(const QPoint &)),
  537. this,
  538. SLOT(imageListPopupMenu(const QPoint &))
  539. );
  540. connect(
  541. action_delete_area_,
  542. SIGNAL(triggered()),
  543. this,
  544. SLOT(deleteArea())
  545. );
  546. connect(
  547. action_edit_area_,
  548. SIGNAL(triggered()),
  549. this,
  550. SLOT(editArea())
  551. );
  552. connect(
  553. action_set_color_,
  554. SIGNAL(triggered()),
  555. this,
  556. SLOT(setLabelColor())
  557. );
  558. connect(
  559. action_toggle_priority_,
  560. SIGNAL(triggered()),
  561. this,
  562. SLOT(toggleLabelPriority())
  563. );
  564. connect(
  565. action_delete_label_,
  566. SIGNAL(triggered()),
  567. this,
  568. SLOT(removeLabel())
  569. );
  570. connect(
  571. action_remove_image_,
  572. SIGNAL(triggered()),
  573. this,
  574. SLOT(removeImage())
  575. );
  576. connect(
  577. image_holder_,
  578. SIGNAL(selectionStarted()),
  579. this,
  580. SLOT(onSelectionStarted())
  581. );
  582. connect(
  583. &line_edit_form_,
  584. SIGNAL(dataSet(QString)),
  585. this,
  586. SLOT(setDataFromForm(QString))
  587. );
  588. connect(
  589. image_holder_,
  590. SIGNAL(imageScaled()),
  591. this,
  592. SLOT(onImageScaled())
  593. );
  594. connect(
  595. image_holder_,
  596. SIGNAL(areaEdited()),
  597. this,
  598. SLOT(onAreaEdit())
  599. );
  600. connect(
  601. &options_form_,
  602. SIGNAL(optionsSet()),
  603. this,
  604. SLOT(onOptionsSet())
  605. );
  606. QString settingsPath = aSettingsPath;
  607. if (settingsPath.isEmpty())
  608. settingsPath = QString("ImageLabeler.ini");
  609. settings_ = new QSettings(settingsPath, QSettings::IniFormat, this);
  610. readSettings(settings_);
  611. image_holder_->setBoundingBoxList(&list_bounding_box_);
  612. image_holder_->setPolygonList(&list_polygon_);
  613. image_holder_->setLabelColorList(&list_label_colors_);
  614. image_holder_->setMainLabelNum(&main_label_);
  615. image_holder_->setImage(image_);
  616. options_form_.setPASCALpath(&PASCALpath_);
  617. }
  618. ImageLabeler::~ImageLabeler()
  619. {
  620. delete action_quit_;
  621. delete action_open_labeled_image_;
  622. delete action_open_image_;
  623. delete action_open_images_;
  624. delete action_load_legend_;
  625. delete action_load_pascal_file_;
  626. delete action_load_pascal_poly_;
  627. delete action_save_legend_;
  628. delete action_save_segmented_;
  629. delete action_save_labels_;
  630. delete action_view_normal_;
  631. delete action_view_segmented_;
  632. delete action_undo_;
  633. delete action_redo_;
  634. delete action_bound_box_tool_;
  635. delete action_polygon_tool_;
  636. delete action_tagging_tool_;
  637. delete action_add_description_;
  638. delete action_options_;
  639. delete action_about_;
  640. delete action_help_content_;
  641. delete menu_pascal_;
  642. delete menu_file_;
  643. delete menu_view_;
  644. delete menu_edit_;
  645. delete menu_help_;
  646. delete menu_bar_;
  647. delete action_delete_area_;
  648. delete action_edit_area_;
  649. delete popup_area_list_;
  650. delete action_toggle_priority_;
  651. delete action_set_color_;
  652. delete action_delete_label_;
  653. delete popup_label_list_;
  654. delete action_remove_image_;
  655. delete popup_images_list_;
  656. delete image_;
  657. delete image_holder_;
  658. delete button_add_label_;
  659. delete button_remove_label_;
  660. delete button_bound_box_tool_;
  661. delete button_polygon_tool_;
  662. delete button_tagging_tool_;
  663. delete button_clear_selection_tool_;
  664. delete button_generate_colors_;
  665. delete button_delete_all_labels_;
  666. delete button_next_image_;
  667. delete button_prev_image_;
  668. delete button_confirm_selection_;
  669. delete label_list_areas_;
  670. delete label_toolbox_;
  671. delete label_list_images_;
  672. delete list_areas_;
  673. delete list_label_;
  674. delete list_images_widget_;
  675. delete layout_toolbox_;
  676. delete layout_right_;
  677. delete layout_center_buttons_;
  678. delete layout_frame_image_;
  679. delete layout_center_;
  680. delete layout_labelbox_buttons_;
  681. delete layout_labelbox_;
  682. delete layout_left_;
  683. delete layout_main_;
  684. delete frame_labelbox_;
  685. delete frame_toolbox_;
  686. delete frame_image_;
  687. delete frame_center_;
  688. delete central_widget_;
  689. //delete current_image_;
  690. if (pure_data_) {
  691. delete[] *pure_data_;
  692. delete pure_data_;
  693. }
  694. delete list_images_;
  695. delete settings_;
  696. }
  697. bool
  698. ImageLabeler::readSettings(QSettings *aSettings)
  699. {
  700. aSettings->beginGroup("/global");
  701. auto_color_generation_ =
  702. aSettings->value("/auto_label_color_generation", 0).toBool();
  703. options_form_.setAutoColorGeneration(auto_color_generation_);
  704. PASCALpath_ = aSettings->value("/PASCAL_root_path", "").toString();
  705. aSettings->endGroup();
  706. qDebug() << PASCALpath_;
  707. return true;
  708. }
  709. bool
  710. ImageLabeler::writeSettings(QSettings *aSettings)
  711. {
  712. aSettings->beginGroup("/global");
  713. aSettings->setValue("/auto_label_color_generation", auto_color_generation_);
  714. aSettings->setValue("/PASCAL_root_path", PASCALpath_);
  715. aSettings->endGroup();
  716. return true;
  717. }
  718. void
  719. ImageLabeler::writeSettings()
  720. {
  721. writeSettings(settings_);
  722. }
  723. void
  724. ImageLabeler::addImage(Image *anImage)
  725. {
  726. QListWidgetItem *newItem = new QListWidgetItem;
  727. QString itemText = QString("%1: %2").
  728. arg(list_images_widget_->count()).
  729. arg(getFilenameFromPath(&(anImage->image_)));
  730. if (anImage->labeled_)
  731. itemText.append(" #labeled");
  732. if (anImage->pas_)
  733. itemText.append(" #pas");
  734. newItem->setText(itemText);
  735. list_images_widget_->addItem(newItem);
  736. list_images_->append(*anImage);
  737. }
  738. void
  739. ImageLabeler::addLabel()
  740. {
  741. QListWidgetItem *newItem = new QListWidgetItem;
  742. QString label;
  743. int itemNum = list_label_->count();
  744. label.append(QString("%1: ").arg(itemNum));
  745. if (itemNum) {
  746. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  747. label.append("New label");
  748. }
  749. else {
  750. newItem->setFlags(
  751. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  752. Qt::ItemIsEnabled
  753. );
  754. label.append("BACKGROUND");
  755. }
  756. newItem->setText(label);
  757. QPixmap iconPix = QPixmap(20, 20);
  758. QColor color;
  759. if (0 != itemNum)
  760. color = Qt::white;
  761. else
  762. color = Qt::black;
  763. iconPix.fill(color);
  764. QIcon icon(iconPix);
  765. list_label_colors_.append(color.rgb());
  766. newItem->setIcon(icon);
  767. list_label_->addItem(newItem);
  768. list_label_->setItemSelected(newItem, true);
  769. label_ID_ = list_label_->count() - 1;
  770. unsaved_data_ = 1;
  771. }
  772. void
  773. ImageLabeler::addLabel(
  774. int aLabelID,
  775. bool isMain,
  776. QString aLabel
  777. )
  778. {
  779. QListWidgetItem *newItem = new QListWidgetItem;
  780. if (0 == aLabelID) {
  781. aLabel = QString("BACKGROUND");
  782. newItem->setFlags(
  783. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  784. Qt::ItemIsEnabled
  785. );
  786. }
  787. QString label;
  788. label.append(QString("%1: %2").
  789. arg(aLabelID).
  790. arg(aLabel)
  791. );
  792. /* TODO: check if there is another main label and make it common */
  793. if (isMain) {
  794. main_label_ = aLabelID;
  795. }
  796. QPixmap iconPix = QPixmap(20, 20);
  797. QColor color;
  798. if (0 != aLabelID)
  799. color = Qt::white;
  800. else
  801. color = Qt::black;
  802. iconPix.fill(color);
  803. QIcon icon(iconPix);
  804. list_label_colors_.append(color.rgb());
  805. newItem->setIcon(icon);
  806. newItem->setText(label);
  807. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  808. list_label_->addItem(newItem);
  809. list_label_->setItemSelected(newItem, true);
  810. unsaved_data_ = 1;
  811. }
  812. void
  813. ImageLabeler::editLabel(QListWidgetItem *anItem)
  814. {
  815. QString label = anItem->text();
  816. int itemRow = list_label_->row(anItem);
  817. QString prefix = QString("%1: ").arg(itemRow);
  818. if (-1 != label.indexOf(prefix)) {
  819. return;
  820. /* NOTREACHED */
  821. }
  822. label.prepend(QString("%1: ").arg(itemRow));
  823. if (main_label_ == itemRow) {
  824. label.append(" #main");
  825. }
  826. list_label_->blockSignals(true);
  827. anItem->setText(label);
  828. list_label_->blockSignals(false);
  829. unsaved_data_ = 1;
  830. }
  831. void
  832. ImageLabeler::removeLabel()
  833. {
  834. if (0 == list_label_->count()) {
  835. return;
  836. /* NOTREACHED */
  837. }
  838. //QListWidgetItem *current = list_label_->currentItem();
  839. /* we need to keep BACKGROUND category */
  840. if (label_ID_ < 1) {
  841. return;
  842. /* NOTREACHED */
  843. }
  844. if (list_label_->count() <= label_ID_ ||
  845. list_label_colors_.count() <= label_ID_) {
  846. return;
  847. /* NOTREACHED */
  848. }
  849. list_label_->takeItem(label_ID_);
  850. list_label_colors_.takeAt(label_ID_);
  851. unsaved_data_ = 1;
  852. }
  853. void
  854. ImageLabeler::setLabelID(
  855. QListWidgetItem *anItem
  856. )
  857. {
  858. Q_UNUSED(anItem)
  859. if (!list_label_->count()) {
  860. return;
  861. /* NOTREACHED */
  862. }
  863. label_ID_ = list_label_->row(anItem);
  864. }
  865. void
  866. ImageLabeler::addBBoxArea(
  867. int anID,
  868. BoundingBox aBBox,
  869. int itemID
  870. )
  871. {
  872. QListWidgetItem *newItem = new QListWidgetItem;
  873. if (-1 == itemID)
  874. itemID = list_areas_->count();
  875. QString label;
  876. label.append(QString("%1: ").arg(itemID));
  877. label.append(QString("BBox #%1; ").arg(anID));
  878. label.append(QString("LabelID: %1; ").arg(aBBox.label_ID_));
  879. label.append(
  880. QString("data:%1;%2;%3;%4; ").
  881. arg(aBBox.rect.topLeft().x()).
  882. arg(aBBox.rect.topLeft().y()).
  883. arg(aBBox.rect.width()).
  884. arg(aBBox.rect.height())
  885. );
  886. newItem->setText(label);
  887. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  888. list_areas_->insertItem(itemID, newItem);
  889. list_areas_->setItemSelected(newItem, true);
  890. }
  891. void
  892. ImageLabeler::addPolyArea(
  893. int aPolyID,
  894. Polygon aPoly,
  895. int itemID
  896. )
  897. {
  898. QListWidgetItem *newItem = new QListWidgetItem;
  899. if (-1 == itemID)
  900. itemID = list_areas_->count();
  901. QString label;
  902. label.append(QString("%1: ").arg(itemID));
  903. label.append(QString("Poly #%1; ").arg(aPolyID));
  904. label.append(QString("LabelID: %1; ").arg(aPoly.label_ID_));
  905. label.append("points:");
  906. for (int i = 0; i < aPoly.poly.count(); i++) {
  907. label.append(
  908. QString("%1;%2;").
  909. arg(aPoly.poly.point(i).x()).
  910. arg(aPoly.poly.point(i).y())
  911. );
  912. }
  913. newItem->setText(label);
  914. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  915. list_areas_->insertItem(itemID, newItem);
  916. list_areas_->setItemSelected(newItem, true);
  917. }
  918. void
  919. ImageLabeler::editArea()
  920. {
  921. bool oldState = list_areas_->blockSignals(true);
  922. if (!list_areas_->currentItem() || !list_areas_->count()) {
  923. return;
  924. /* NOTREACHED */
  925. }
  926. QListWidgetItem *current = list_areas_->currentItem();
  927. old_area_string_ = current->text();
  928. current->setFlags(current->flags() | Qt::ItemIsEditable);
  929. list_areas_->editItem(current);
  930. list_areas_->blockSignals(oldState);
  931. }
  932. void
  933. ImageLabeler::onAreaItemChange(QListWidgetItem *anItem)
  934. {
  935. list_areas_->blockSignals(true);
  936. QString areaString = anItem->text();
  937. int oldID = -1;
  938. if (-1 != areaString.indexOf("Poly")) {
  939. Polygon *poly = new Polygon;
  940. *poly = polyFromString(&areaString, &oldID);
  941. if (-1 < poly->label_ID_ && !poly->poly.isEmpty() &&
  942. -1 < oldID) {
  943. list_polygon_.takeAt(oldID);
  944. list_polygon_.insert(oldID, poly);
  945. }
  946. else
  947. anItem->setText(old_area_string_);
  948. }
  949. else if (-1 != areaString.indexOf("BBox")) {
  950. BoundingBox *bbox = new BoundingBox;
  951. *bbox = BBoxFromString(&areaString, &oldID);
  952. if (-1 < bbox->label_ID_ && -1 < oldID) {
  953. list_bounding_box_.takeAt(oldID);
  954. list_bounding_box_.insert(oldID, bbox);
  955. }
  956. else
  957. anItem->setText(old_area_string_);
  958. }
  959. else {
  960. showWarning(tr("record format is corrupted, try again"));
  961. anItem->setText(old_area_string_);
  962. }
  963. anItem->setFlags(anItem->flags() ^ Qt::ItemIsEditable);
  964. list_areas_->blockSignals(false);
  965. image_holder_->update();
  966. unsaved_data_ = 1;
  967. }
  968. void
  969. ImageLabeler::onAreaEdit()
  970. {
  971. if (!list_areas_->count() ||
  972. -1 == image_holder_->focusedSelection()) {
  973. //showWarning(tr("You haven't added any label"));
  974. return;
  975. /* NOTREACHED */
  976. }
  977. int figureID = image_holder_->focusedSelection();
  978. Figure figure = image_holder_->focusedSelectionType();
  979. for (int i = 0; i < list_areas_->count(); i++) {
  980. QListWidgetItem *item = list_areas_->item(i);
  981. QString text = item->text();
  982. if (RectFigure == figure && -1 != text.indexOf("BBox")) {
  983. bool ok = 0;
  984. int num = getNumFromString(&text, "BBox #", ";", &ok);
  985. if (ok && num == figureID) {
  986. list_areas_->takeItem(i);
  987. addBBoxArea(num, *list_bounding_box_.at(num), i);
  988. }
  989. }
  990. else if (PolyFigure == figure && -1 != text.indexOf("Poly")) {
  991. bool ok = 0;
  992. int num = getNumFromString(&text, "Poly #", ";", &ok);
  993. if (ok && num == figureID) {
  994. list_areas_->takeItem(i);
  995. addPolyArea(num, *list_polygon_.at(num), i);
  996. }
  997. }
  998. }
  999. unsaved_data_ = 1;
  1000. }
  1001. void
  1002. ImageLabeler::deleteArea()
  1003. {
  1004. if (!list_areas_->currentItem() || !list_areas_->count()) {
  1005. return;
  1006. /* NOTREACHED */
  1007. }
  1008. QString text = list_areas_->currentItem()->text();
  1009. bool ok = 0;
  1010. //int labelID = getNumFromString(&text, "LabelID:", ";", &ok);
  1011. // if (!ok) {
  1012. // return;
  1013. // /* NOTREACHED */
  1014. // }
  1015. /*
  1016. * 0 - bbox
  1017. * 1 - poly
  1018. */
  1019. QString shape;
  1020. if (-1 != text.indexOf("BBox"))
  1021. shape = QString("BBox");
  1022. else if (-1 != text.indexOf("Poly"))
  1023. shape = QString("Poly");
  1024. else {
  1025. return;
  1026. /* NOTREACHED */
  1027. }
  1028. int areaNum = getNumFromString(&text, "#", ";", &ok);
  1029. if (!ok) {
  1030. return;
  1031. /* NOTREACHED */
  1032. }
  1033. int currentItemRow = list_areas_->row(list_areas_->currentItem());
  1034. /* changing all shapes(depends on current) which are next in the list */
  1035. for (int i = list_areas_->count() - 1; i > currentItemRow; i--) {
  1036. QListWidgetItem item = *(list_areas_->item(i));
  1037. QString newText = item.text();
  1038. if (-1 == newText.indexOf(shape))
  1039. continue;
  1040. int num = getNumFromString(&newText, "#", ";", &ok);
  1041. num--;
  1042. QString numString = QString("%1").arg(num);
  1043. int numPos = newText.indexOf("#") + 1;
  1044. newText.replace(numPos, numString.size(), numString);
  1045. list_areas_->takeItem(i);
  1046. list_areas_->insertItem(i, newText);
  1047. }
  1048. list_areas_->takeItem(currentItemRow);
  1049. if (shape == "BBox")
  1050. list_bounding_box_.removeAt(areaNum);
  1051. else
  1052. list_polygon_.removeAt(areaNum);
  1053. image_holder_->update();
  1054. unsaved_data_ = 1;
  1055. }
  1056. void
  1057. ImageLabeler::toggleLabelPriority()
  1058. {
  1059. if (!list_label_->count()) {
  1060. return;
  1061. /* NOTREACHED */
  1062. }
  1063. QListWidgetItem *item = list_label_->currentItem();
  1064. /* because we need to keep BACKGROUND category */
  1065. if (0 == list_label_->row(item)) {
  1066. return;
  1067. /* NOTREACHED */
  1068. }
  1069. int itemRow = list_label_->row(item);
  1070. QString text = item->text();
  1071. /* cleaning previous " #main" mark */
  1072. if (-1 != main_label_) {
  1073. QListWidgetItem *lastMain = list_label_->item(main_label_);
  1074. QString lastMainText = lastMain->text();
  1075. int mainPos = lastMainText.indexOf(" #main");
  1076. lastMainText = lastMainText.mid(0, mainPos);
  1077. list_label_->blockSignals(true);
  1078. lastMain->setText(lastMainText);
  1079. list_label_->blockSignals(false);
  1080. }
  1081. if (main_label_ == itemRow) {
  1082. int mainPos = text.indexOf(" #main");
  1083. text = text.mid(0, mainPos);
  1084. main_label_ = -1;
  1085. }
  1086. else {
  1087. text.append(" #main");
  1088. main_label_ = list_label_->row(item);
  1089. }
  1090. list_label_->blockSignals(true);
  1091. item->setText(text);
  1092. list_label_->blockSignals(false);
  1093. image_holder_->update();
  1094. unsaved_data_ = 1;
  1095. }
  1096. void
  1097. ImageLabeler::getImagesFromDir(const QDir &dir)
  1098. {
  1099. /* avoiding freezing during recursive search for files */
  1100. QApplication::processEvents();
  1101. if (interrupt_search_) {
  1102. clearAll();
  1103. return;
  1104. }
  1105. QStringList filenameFilter;
  1106. filenameFilter <<
  1107. "*.jpeg" <<
  1108. "*.jpg" <<
  1109. "*.gif" <<
  1110. "*.png" <<
  1111. "*.bmp" <<
  1112. "*.tiff" <<
  1113. "*.dat"
  1114. ;
  1115. QStringList listImages =
  1116. dir.entryList(filenameFilter, QDir::Files);
  1117. QStringList ignoredFiles;
  1118. foreach (QString file, listImages) {
  1119. if (ignoredFiles.contains(file) ||
  1120. file.contains("_segmented", Qt::CaseInsensitive) ||
  1121. file.contains(".dat", Qt::CaseInsensitive)) {
  1122. continue;
  1123. }
  1124. Image newImage;
  1125. newImage.image_ = dir.absoluteFilePath(file);
  1126. /* TODO: think about loading pascal files */
  1127. newImage.pas_ = 0;
  1128. QString labeled = alterFileName(file, "_labeled");
  1129. labeled = removePath(labeled);
  1130. labeled.append(".dat");
  1131. if (listImages.contains(labeled, Qt::CaseInsensitive))
  1132. newImage.labeled_ = 1;
  1133. else
  1134. {
  1135. newImage.labeled_ = 0;
  1136. }
  1137. addImage(&newImage);
  1138. }
  1139. QStringList listDir = dir.entryList(QDir::Dirs);
  1140. foreach (QString subdir, listDir) {
  1141. if ("." == subdir || ".." == subdir)
  1142. continue;
  1143. getImagesFromDir(QDir(dir.absoluteFilePath(subdir)));
  1144. }
  1145. }
  1146. void
  1147. ImageLabeler::nextImage()
  1148. {
  1149. if (list_images_->isEmpty()) {
  1150. return;
  1151. /* NOTREACHED */
  1152. }
  1153. if (askForUnsavedData()) {
  1154. return;
  1155. /* NOTREACHED */
  1156. }
  1157. if (list_images_widget_->count() - 1 == image_ID_) {
  1158. //current_image_ = list_images_->at(0).image_;
  1159. image_ID_ = 0;
  1160. }
  1161. else {
  1162. image_ID_ ++;
  1163. //current_image_ = list_images_->at(image_ID_).image_;
  1164. }
  1165. if (!selectImage(image_ID_)) {
  1166. showWarning(tr("Next image is not available"));
  1167. return;
  1168. /* NOTREACHED */
  1169. }
  1170. list_images_widget_->setCurrentRow(image_ID_);
  1171. if (current_image_.isEmpty()) {
  1172. return;
  1173. /* NOTREACHED */
  1174. }
  1175. QString winTitle;
  1176. winTitle.append("ImageLabeler - ");
  1177. winTitle.append(current_image_);
  1178. setWindowTitle(winTitle);
  1179. image_->load(current_image_);
  1180. image_holder_->resize(image_->size());
  1181. image_holder_->setPixmap(*image_);
  1182. list_bounding_box_.clear();
  1183. list_polygon_.clear();
  1184. list_areas_->clear();
  1185. image_holder_->clearAll();
  1186. segmented_image_.clear();
  1187. clearLabelColorList();
  1188. //setWindowTitle(tr("ImageLabeler"));
  1189. }
  1190. void
  1191. ImageLabeler::prevImage()
  1192. {
  1193. if (!list_images_widget_->count()) {
  1194. return;
  1195. /* NOTREACHED */
  1196. }
  1197. if (askForUnsavedData()) {
  1198. return;
  1199. /* NOTREACHED */
  1200. }
  1201. if (!image_ID_) {
  1202. image_ID_ = list_images_widget_->count() - 1;
  1203. //current_image_ = list_images_->at(image_ID_).image_;
  1204. }
  1205. else {
  1206. image_ID_--;
  1207. //current_image_ = list_images_->at(image_ID_).image_;
  1208. }
  1209. list_images_widget_->setCurrentRow(image_ID_);
  1210. if (!selectImage(image_ID_)) {
  1211. showWarning(tr("Next image is not available"));
  1212. return;
  1213. /* NOTREACHED */
  1214. }
  1215. QString winTitle;
  1216. winTitle.append("ImageLabeler - ");
  1217. winTitle.append(current_image_);
  1218. setWindowTitle(winTitle);
  1219. image_->load(current_image_);
  1220. image_holder_->resize(image_->size());
  1221. image_holder_->setPixmap(*image_);
  1222. list_bounding_box_.clear();
  1223. list_polygon_.clear();
  1224. list_areas_->clear();
  1225. image_holder_->clearAll();
  1226. segmented_image_.clear();
  1227. clearLabelColorList();
  1228. //setWindowTitle(tr("ImageLabeler"));
  1229. }
  1230. void
  1231. ImageLabeler::saveAllInfo()
  1232. {
  1233. if (!list_images_widget_->count()) {
  1234. showWarning("You have not opened any image yet");
  1235. return;
  1236. /* NOTREACHED */
  1237. }
  1238. /* ------------------------------------------------------------------------
  1239. * XML part
  1240. */
  1241. QDomDocument doc(tr("ImageLabeler"));
  1242. QDomElement root = doc.createElement(tr("pixelwise_labeling"));
  1243. doc.appendChild(root);
  1244. QDomElement image = doc.createElement(tr("image"));
  1245. root.appendChild(image);
  1246. QDomText pathToImage = doc.createTextNode(current_image_);
  1247. image.appendChild(pathToImage);
  1248. if (!segmented_image_.isEmpty()) {
  1249. QDomElement segmentedImage = doc.createElement(tr("segmented"));
  1250. root.appendChild(segmentedImage);
  1251. QDomText pathToSegmented = doc.createTextNode(segmented_image_);
  1252. segmentedImage.appendChild(pathToSegmented);
  1253. }
  1254. QDomElement description = doc.createElement(tr("description"));
  1255. root.appendChild(description);
  1256. QDomText descriptionText = doc.createTextNode(image_description_);
  1257. description.appendChild(descriptionText);
  1258. QDomElement tags = doc.createElement(tr("tags"));
  1259. root.appendChild(tags);
  1260. QDomText tagsText = doc.createTextNode(tags_);
  1261. tags.appendChild(tagsText);
  1262. legendToXml(&doc, &root);
  1263. objectsToXml(&doc, &root);
  1264. setPureData();
  1265. QDomElement pureData = doc.createElement(tr("pure_data"));
  1266. QSize imageSize = image_->size();
  1267. QString pixelValues;
  1268. for (int i = 0; i < imageSize.height(); i++) {
  1269. for (int j = 0; j < imageSize.width(); j++) {
  1270. pixelValues.append(QString("%1").arg(pure_data_[i][j]));
  1271. //pixelValues.append(QString(""));
  1272. }
  1273. pixelValues.append("\n");
  1274. }
  1275. QDomText pureDataText = doc.createTextNode(pixelValues);
  1276. pureData.appendChild(pureDataText);
  1277. root.appendChild(pureData);
  1278. QString xml = doc.toString();
  1279. /* ------------------------------------------------------------------------
  1280. * XML part ends
  1281. */
  1282. QFileDialog fileDialog(0, tr("Save all info"));
  1283. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1284. fileDialog.setDefaultSuffix("dat");
  1285. fileDialog.setFileMode(QFileDialog::AnyFile);
  1286. QString dir = getDirFromPath(&current_image_);
  1287. /* altering the name of a new file */
  1288. QString newFileName = alterFileName(current_image_, "_labeled");
  1289. newFileName = removePath(newFileName);
  1290. fileDialog.selectFile(newFileName);
  1291. fileDialog.setDirectory(dir);
  1292. QString filename;
  1293. if (fileDialog.exec()) {
  1294. filename = fileDialog.selectedFiles().last();
  1295. }
  1296. else {
  1297. //showWarning(tr("Can not open file dialog"));
  1298. return;
  1299. /* NOTREACHED */
  1300. }
  1301. if (filename.isEmpty()) {
  1302. return;
  1303. /* NOTREACHED */
  1304. }
  1305. QFile file(filename);
  1306. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1307. showWarning(tr("Can not open file for writing"));
  1308. return;
  1309. /* NOTREACHED */
  1310. }
  1311. file.write(xml.toLocal8Bit());
  1312. file.close();
  1313. unsaved_data_ = 0;
  1314. }
  1315. void
  1316. ImageLabeler::saveSegmentedPicture()
  1317. {
  1318. if (list_bounding_box_.isEmpty() && list_polygon_.isEmpty()) {
  1319. return;
  1320. /* NOTREACHED */
  1321. }
  1322. setPureData();
  1323. // QString suffix = *current_image_;
  1324. // int dotPos = suffix.lastIndexOf(".");
  1325. // suffix.mid(dotPos + 1, suffix.size() - dotPos - 1);
  1326. QFileDialog fileDialog(0, tr("Save segmented picture"));
  1327. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1328. fileDialog.setDefaultSuffix("png");
  1329. fileDialog.setFileMode(QFileDialog::AnyFile);
  1330. QString dir = getDirFromPath(&current_image_);
  1331. /* altering the name of a new file */
  1332. QString newFileName = alterFileName(current_image_, "_segmented");
  1333. newFileName = removePath(newFileName);
  1334. fileDialog.selectFile(newFileName);
  1335. fileDialog.setDirectory(dir);
  1336. QString filename;
  1337. if (fileDialog.exec()) {
  1338. filename = fileDialog.selectedFiles().last();
  1339. }
  1340. else {
  1341. return;
  1342. /* NOTREACHED */
  1343. }
  1344. QSize imageSize = image_holder_->pixmap()->size();
  1345. QImage newImage(imageSize, QImage::Format_RGB32);
  1346. bool generateColorsFlag = auto_color_generation_;
  1347. bool flag = 0;
  1348. /* checking if all the colors are are different from white(default) */
  1349. if (!generateColorsFlag) {
  1350. for (int i = 1; i < list_label_->count(); i++) {
  1351. if (list_label_colors_.at(i) == 0xffffffff && !flag)
  1352. flag = 1;
  1353. else if (list_label_colors_.at(i) == 0xffffffff && flag) {
  1354. QMessageBox msgBox;
  1355. msgBox.setText(tr("There are few labels with default white color."));
  1356. msgBox.setInformativeText(tr("Do you want to generate all colors automatically? Otherwise you'll have to do it manually."));
  1357. msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  1358. msgBox.setDefaultButton(QMessageBox::Yes);
  1359. msgBox.setIcon(QMessageBox::Question);
  1360. int ret = msgBox.exec();
  1361. if (QMessageBox::Yes == ret) {
  1362. generateColorsFlag = 1;
  1363. break;
  1364. }
  1365. else {
  1366. return;
  1367. /* NOTREACHED */
  1368. }
  1369. }
  1370. }
  1371. }
  1372. /* generating colors for labels */
  1373. if (list_label_colors_.count() < list_label_->count() ||
  1374. generateColorsFlag) {
  1375. generateColors();
  1376. }
  1377. for (int i = 0; i < imageSize.height(); i++)
  1378. for (int j = 0; j < imageSize.width(); j++) {
  1379. newImage.setPixel(j, i, list_label_colors_.at(pure_data_[i][j]));
  1380. }
  1381. if (!newImage.save(filename, "png", 100)) {
  1382. showWarning(tr("An error occurred while saving the segmented image"));
  1383. return;
  1384. /* NOTREACHED */
  1385. }
  1386. segmented_image_ = filename;
  1387. action_view_segmented_->setEnabled(true);
  1388. }
  1389. void
  1390. ImageLabeler::saveLegend()
  1391. {
  1392. if (!list_label_->count()) {
  1393. showWarning("You have not added any label yet");
  1394. return;
  1395. /* NOTREACHED */
  1396. }
  1397. /* ------------------------------------------------------------------------
  1398. * XML part
  1399. */
  1400. QDomDocument doc(tr("ImageLabeler"));
  1401. QDomElement root = doc.createElement(tr("root"));
  1402. doc.appendChild(root);
  1403. legendToXml(&doc, &root);
  1404. QString xml = doc.toString();
  1405. /* ------------------------------------------------------------------------
  1406. * XML part ends
  1407. */
  1408. QFileDialog fileDialog(0, tr("Save legend"));
  1409. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1410. fileDialog.setDefaultSuffix("dat");
  1411. fileDialog.setFileMode(QFileDialog::AnyFile);
  1412. QString dir = getDirFromPath(&current_image_);
  1413. /* altering the name of a new file */
  1414. QString newFileName = alterFileName(current_image_, "_legend");
  1415. newFileName = removePath(newFileName);
  1416. fileDialog.selectFile(newFileName);
  1417. fileDialog.setDirectory(dir);
  1418. QString filename;
  1419. if (fileDialog.exec()) {
  1420. filename = fileDialog.selectedFiles().last();
  1421. }
  1422. else {
  1423. //showWarning(tr("Can not open file dialog"));
  1424. return;
  1425. /* NOTREACHED */
  1426. }
  1427. if (filename.isEmpty()) {
  1428. return;
  1429. /* NOTREACHED */
  1430. }
  1431. QFile file(filename);
  1432. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1433. showWarning(tr("Can not open file for writing"));
  1434. return;
  1435. /* NOTREACHED */
  1436. }
  1437. file.write(xml.toLocal8Bit());
  1438. file.close();
  1439. }
  1440. void
  1441. ImageLabeler::loadInfo()
  1442. {
  1443. if (askForUnsavedData()) {
  1444. return;
  1445. /* NOTREACHED */
  1446. }
  1447. QFileDialog fileDialog(0, tr("Load file with info"));
  1448. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1449. fileDialog.setDefaultSuffix("dat");
  1450. fileDialog.setFileMode(QFileDialog::AnyFile);
  1451. QString filename;
  1452. if (fileDialog.exec()) {
  1453. filename = fileDialog.selectedFiles().last();
  1454. }
  1455. else {
  1456. //showWarning(tr("Can not open file dialog"));
  1457. return;
  1458. /* NOTREACHED */
  1459. }
  1460. clearAllTool();
  1461. clearLabelList();
  1462. if (loadInfo(filename)) {
  1463. enableTools();
  1464. Image newImage;
  1465. newImage.image_ = current_image_;
  1466. newImage.labeled_ = 1;
  1467. newImage.pas_ = 0;
  1468. addImage(&newImage);
  1469. image_ID_ = list_images_widget_->count() - 1;
  1470. list_images_widget_->setCurrentRow(image_ID_);
  1471. }
  1472. unsaved_data_ = 0;
  1473. }
  1474. bool
  1475. ImageLabeler::loadInfo(QString filename)
  1476. {
  1477. QDomDocument doc("Image Labeler");
  1478. QFile file(filename);
  1479. if (!file.open(QIODevice::ReadOnly)) {
  1480. showWarning(tr("Can not open such file"));
  1481. return false;
  1482. /* NOTREACHED */
  1483. }
  1484. QString errMsg;
  1485. if (!doc.setContent(&file, &errMsg)) {
  1486. showWarning(errMsg);
  1487. file.close();
  1488. return false;
  1489. /* NOTREACHED */
  1490. }
  1491. file.close();
  1492. //clearAll();
  1493. //enableTools();
  1494. /* getting all info */
  1495. QDomElement elements = doc.documentElement();
  1496. QDomNode rootNode = elements.firstChild();
  1497. QString string;
  1498. while(!rootNode.isNull()) {
  1499. QDomElement element = rootNode.toElement();
  1500. if(!element.isNull()) {
  1501. /* path to the image */
  1502. if (element.tagName() == "image") {
  1503. string = element.text();
  1504. if (string.isEmpty()) {
  1505. showWarning(
  1506. tr(
  1507. "The file with data doesn't contain path to the image"
  1508. )
  1509. );
  1510. return false;
  1511. /* NOTREACHED */
  1512. }
  1513. if (!image_->load(string)) {
  1514. return false;
  1515. /* NOTREACHED */
  1516. }
  1517. // Image newImage;
  1518. // newImage.image_ = string;
  1519. // newImage.labeled_ = 1;
  1520. // addImage(&newImage);
  1521. current_image_ = string;
  1522. // image_ID_ = list_images_widget_->count() - 1;
  1523. // list_images_widget_->setCurrentRow(image_ID_);
  1524. QString winTitle;
  1525. winTitle.append("ImageLabeler - ");
  1526. winTitle.append(current_image_);
  1527. setWindowTitle(winTitle);
  1528. image_holder_->resize(image_->size());
  1529. image_holder_->setPixmap(*image_);
  1530. }
  1531. /* path to the segmented image */
  1532. if (element.tagName() == "segmented") {
  1533. string = element.text();
  1534. if (string.isEmpty()) {
  1535. continue;
  1536. }
  1537. segmented_image_ = string;
  1538. action_view_segmented_->setEnabled(true);
  1539. }
  1540. /* image description */
  1541. else if (element.tagName() == "description" &&
  1542. !element.text().isEmpty()) {
  1543. image_description_ = element.text();
  1544. }
  1545. /* legend */
  1546. else if (element.tagName() == "legend") {
  1547. loadLegendFromNode(&element);
  1548. }
  1549. /* objects */
  1550. else if (element.tagName() == "objects") {
  1551. QDomNode subNode = element.firstChild();
  1552. QDomElement subElement;
  1553. while(!subNode.isNull()) {
  1554. subElement = subNode.toElement();
  1555. if (subElement.isNull() || subElement.text().isEmpty()) {
  1556. subNode = subNode.nextSibling();
  1557. continue;
  1558. }
  1559. string = subElement.attribute("id");
  1560. bool ok = 1;
  1561. int id = string.toInt(&ok, 10);
  1562. if (!ok) {
  1563. qDebug() <<
  1564. "loadInfo: "
  1565. "poly id format is corrupted";
  1566. subNode = subNode.nextSibling();
  1567. continue;
  1568. }
  1569. string = subElement.text();
  1570. if (subElement.tagName() == "bbox") {
  1571. addBBoxFromData(&string, &id);
  1572. }
  1573. if (subElement.tagName() == "poly") {
  1574. addPolyFromData(&string, &id);
  1575. }
  1576. subNode = subNode.nextSibling();
  1577. }
  1578. }
  1579. }
  1580. rootNode = rootNode.nextSibling();
  1581. }
  1582. unsaved_data_ = 0;
  1583. return true;
  1584. }
  1585. void
  1586. ImageLabeler::loadPascalFile()
  1587. {
  1588. if (askForUnsavedData()) {
  1589. return;
  1590. /* NOTREACHED */
  1591. }
  1592. if (PASCALpath_.isEmpty()) {
  1593. showWarning(tr("before opening first PASCAL file please choose \"root\" directory"
  1594. " where a folder with segmentations,"
  1595. " a folder with polygons,"
  1596. " a folder with image descriptions and"
  1597. " a folder with the pure images are."));
  1598. QFileDialog fileDialog(0, tr("root directory for the PASCAL files"));
  1599. fileDialog.setFileMode(QFileDialog::Directory);
  1600. if (fileDialog.exec())
  1601. PASCALpath_ = fileDialog.selectedFiles().last();
  1602. else {
  1603. return;
  1604. /* NOTREACHED */
  1605. }
  1606. }
  1607. QFileDialog fileDialog(0, tr("Load pascal file"));
  1608. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1609. fileDialog.setDefaultSuffix("xml");
  1610. fileDialog.setFileMode(QFileDialog::AnyFile);
  1611. fileDialog.setDirectory(PASCALpath_);
  1612. QStringList filters;
  1613. filters << "PASCAL xml data (*.xml)"
  1614. << "Any files (*)";
  1615. fileDialog.setNameFilters(filters);
  1616. QString filename;
  1617. if (fileDialog.exec()) {
  1618. filename = fileDialog.selectedFiles().last();
  1619. }
  1620. else {
  1621. //showWarning(tr("Can not open file dialog"));
  1622. return;
  1623. /* NOTREACHED */
  1624. }
  1625. clearAllTool();
  1626. clearLabelList();
  1627. if (loadPascalFile(filename, PASCALpath_)) {
  1628. enableTools();
  1629. Image newImage;
  1630. newImage.image_ = current_image_;
  1631. newImage.labeled_ = 1;
  1632. newImage.pas_ = 1;
  1633. addImage(&newImage);
  1634. image_ID_ = list_images_widget_->count() - 1;
  1635. list_images_widget_->setCurrentRow(image_ID_);
  1636. }
  1637. unsaved_data_ = 0;
  1638. }
  1639. bool
  1640. ImageLabeler::loadPascalFile(QString aFilename, QString aPath)
  1641. {
  1642. QDomDocument doc;
  1643. QFile file(aFilename);
  1644. if (!file.open(QIODevice::ReadOnly)) {
  1645. showWarning(tr("Can not open such file"));
  1646. return false;
  1647. /* NOTREACHED */
  1648. }
  1649. QString errMsg;
  1650. if (!doc.setContent(&file, &errMsg)) {
  1651. showWarning(errMsg);
  1652. file.close();
  1653. return false;
  1654. /* NOTREACHED */
  1655. }
  1656. file.close();
  1657. //clearAll();
  1658. //enableTools();
  1659. /* getting all info */
  1660. QDomElement elements = doc.documentElement();
  1661. QDomNode rootNode = elements.firstChild();
  1662. QString string;
  1663. QString path;
  1664. if (aPath.isEmpty())
  1665. path = getPathFromFilename(aFilename);
  1666. else
  1667. path = aPath + "/";
  1668. QString filename;
  1669. QStringList labels;
  1670. labels << "BACKGROUND";
  1671. int labelID;
  1672. while(!rootNode.isNull()) {
  1673. QDomElement element = rootNode.toElement();
  1674. if(!element.isNull()) {
  1675. /* folder */
  1676. if (element.tagName() == "folder") {
  1677. string = element.text();
  1678. if (!string.isEmpty()) {
  1679. path.append(string);
  1680. }
  1681. }
  1682. /* filename */
  1683. else if (element.tagName() == "filename") {
  1684. string = element.text();
  1685. if (!string.isEmpty()) {
  1686. filename = string;
  1687. }
  1688. }
  1689. /* object */
  1690. else if (element.tagName() == "object") {
  1691. QDomNode subNode = element.firstChild();
  1692. QDomElement subElement;
  1693. while(!subNode.isNull()) {
  1694. subElement = subNode.toElement();
  1695. if (subElement.isNull() || subElement.text().isEmpty()) {
  1696. subNode = subNode.nextSibling();
  1697. continue;
  1698. }
  1699. /* label */
  1700. if (subElement.tagName() == "name") {
  1701. string = subElement.text();
  1702. if (!string.isEmpty() &&
  1703. !labels.contains(string, Qt::CaseInsensitive))
  1704. {
  1705. addLabel(list_label_->count(), 0, string);
  1706. labelID = labels.count();
  1707. labels << string;
  1708. }
  1709. else if (labels.contains(string, Qt::CaseInsensitive)) {
  1710. for (int i = 0; i < labels.count(); i++) {
  1711. if (labels.at(i) == string)
  1712. labelID = i;
  1713. }
  1714. }
  1715. }
  1716. if (subElement.tagName() == "bndbox") {
  1717. /* 2 points */
  1718. QPoint topLeft;
  1719. QPoint bottomRight;
  1720. QDomNode bboxNode = subElement.firstChild();
  1721. QDomElement bboxElement;
  1722. while(!bboxNode.isNull()) {
  1723. bboxElement = bboxNode.toElement();
  1724. string.clear();
  1725. bool ok = 1;
  1726. if (bboxElement.tagName() == "xmin") {
  1727. string = bboxElement.text();
  1728. if (string.isEmpty())
  1729. ok = 0;
  1730. double xmin = string.toDouble(&ok);
  1731. if (ok)
  1732. topLeft.setX(qRound(xmin));
  1733. }
  1734. else if (bboxElement.tagName() == "ymin") {
  1735. string = bboxElement.text();
  1736. if (string.isEmpty())
  1737. ok = 0;
  1738. double ymin = string.toDouble(&ok);
  1739. if (ok)
  1740. topLeft.setY(qRound(ymin));
  1741. }
  1742. else if (bboxElement.tagName() == "xmax") {
  1743. string = bboxElement.text();
  1744. if (string.isEmpty())
  1745. ok = 0;
  1746. double xmax = string.toDouble(&ok);
  1747. if (ok)
  1748. bottomRight.setX(qRound(xmax));
  1749. }
  1750. else if (bboxElement.tagName() == "ymax") {
  1751. string = bboxElement.text();
  1752. if (string.isEmpty())
  1753. ok = 0;
  1754. double ymax = string.toDouble(&ok);
  1755. if (ok)
  1756. bottomRight.setY(qRound(ymax));
  1757. }
  1758. if (string.isEmpty() || !ok)
  1759. break;
  1760. bboxNode = bboxNode.nextSibling();
  1761. if (bboxNode.isNull()) {
  1762. BoundingBox *bbox = new BoundingBox;
  1763. bbox->rect.setTopLeft(topLeft);
  1764. bbox->rect.setBottomRight(bottomRight);
  1765. bbox->label_ID_ = labelID;
  1766. addBBox(bbox);
  1767. }
  1768. }
  1769. }
  1770. subNode = subNode.nextSibling();
  1771. }
  1772. }
  1773. }
  1774. rootNode = rootNode.nextSibling();
  1775. }
  1776. if (!image_->load(path + "/JPEGImages/" + filename)) {
  1777. return false;
  1778. /* NOTREACHED */
  1779. }
  1780. current_image_ = path + "/JPEGImages/" + filename;
  1781. QString winTitle;
  1782. winTitle.append("ImageLabeler - ");
  1783. winTitle.append(current_image_);
  1784. setWindowTitle(winTitle);
  1785. image_holder_->resize(image_->size());
  1786. image_holder_->setPixmap(*image_);
  1787. unsaved_data_ = 0;
  1788. return true;
  1789. }
  1790. void
  1791. ImageLabeler::loadPascalPolys()
  1792. {
  1793. if (current_image_.isEmpty()) {
  1794. return;
  1795. /* NOTREACHED */
  1796. }
  1797. QFileDialog fileDialog(0, tr("Load pascal polygons"));
  1798. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1799. fileDialog.setDefaultSuffix("polygon");
  1800. fileDialog.setFileMode(QFileDialog::AnyFile);
  1801. QStringList filters;
  1802. filters << "PASCAL polygons (*.polygon)"
  1803. << "Any files (*)";
  1804. fileDialog.setNameFilters(filters);
  1805. QString filename;
  1806. if (fileDialog.exec()) {
  1807. filename = fileDialog.selectedFiles().last();
  1808. }
  1809. else {
  1810. //showWarning(tr("Can not open file dialog"));
  1811. return;
  1812. /* NOTREACHED */
  1813. }
  1814. //clearAllTool();
  1815. //clearLabelList();
  1816. if (!loadPascalPolys(filename)) {
  1817. showWarning(tr("File format is corrupted."));
  1818. }
  1819. unsaved_data_ = 0;
  1820. image_holder_->update();
  1821. }
  1822. bool
  1823. ImageLabeler::loadPascalPolys(QString aFilename)
  1824. {
  1825. QFile file(aFilename);
  1826. if (!file.open(QIODevice::ReadOnly)) {
  1827. showWarning(tr("Can not open such file"));
  1828. return false;
  1829. /* NOTREACHED */
  1830. }
  1831. QByteArray data = file.readAll();
  1832. file.close();
  1833. QString label;
  1834. int lastSpace = 0;
  1835. int pointCount = 0;
  1836. Polygon *poly = 0;
  1837. QPoint point;
  1838. bool evenFlag = 0;
  1839. //for (int i = 0; i < data.length(); i++) {
  1840. int i = 0;
  1841. while (i < data.length()) {
  1842. if (data.at(i) == ' ' && label.isEmpty() && !pointCount) {
  1843. label = QString(data.mid(lastSpace, i - lastSpace));
  1844. lastSpace = i + 1;
  1845. }
  1846. else if (data.at(i) == ' ' && !label.isEmpty() && !pointCount) {
  1847. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1848. bool ok = 1;
  1849. pointCount = num.toInt(&ok, 10);
  1850. if (!ok) {
  1851. return false;
  1852. /* NOTREACHED */
  1853. }
  1854. poly = new Polygon;
  1855. poly->label_ID_ = -1;
  1856. lastSpace = i + 1;
  1857. }
  1858. else if (data.at(i) == ' ' &&
  1859. !label.isEmpty() &&
  1860. pointCount &&
  1861. !evenFlag)
  1862. {
  1863. evenFlag = 1;
  1864. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1865. bool ok = 1;
  1866. int coor = qRound(num.toDouble(&ok));
  1867. if (!ok) {
  1868. return false;
  1869. /* NOTREACHED */
  1870. }
  1871. point.setX(coor);
  1872. lastSpace = i + 1;
  1873. }
  1874. else if (data.at(i) == ' ' &&
  1875. !label.isEmpty() &&
  1876. pointCount &&
  1877. evenFlag)
  1878. {
  1879. evenFlag = 0;
  1880. pointCount--;
  1881. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1882. bool ok = 1;
  1883. int coor = qRound(num.toDouble(&ok));
  1884. if (!ok) {
  1885. return false;
  1886. /* NOTREACHED */
  1887. }
  1888. point.setY(coor);
  1889. lastSpace = i + 1;
  1890. poly->poly << point;
  1891. }
  1892. else if (data.at(i) == '\n') {
  1893. if (pointCount || !poly) {
  1894. return false;
  1895. /* NOTREACHED */
  1896. }
  1897. lastSpace = i + 1;
  1898. for (int j = 0; j < list_label_->count(); j++) {
  1899. if (list_label_->item(j)->text().contains(label, Qt::CaseInsensitive)) {
  1900. poly->label_ID_ = j;
  1901. }
  1902. }
  1903. if (-1 == poly->label_ID_) {
  1904. int labelID = list_label_->count();
  1905. addLabel(labelID, 0, label);
  1906. poly->label_ID_ = labelID;
  1907. }
  1908. addPoly(poly);
  1909. label.clear();
  1910. }
  1911. i++;
  1912. }
  1913. //clearAll();
  1914. //enableTools();
  1915. return true;
  1916. }
  1917. void
  1918. ImageLabeler::loadImage()
  1919. {
  1920. if (askForUnsavedData()) {
  1921. return;
  1922. /* NOTREACHED */
  1923. }
  1924. QFileDialog fileDialog(0, tr("Load image"));
  1925. fileDialog.setFileMode(QFileDialog::ExistingFile);
  1926. QStringList filters;
  1927. filters << "Image files (*.jpeg *.jpg *.gif *.png *.bmp *.tiff)"
  1928. << "Any files (*)";
  1929. fileDialog.setNameFilters(filters);
  1930. QString filename;
  1931. if (fileDialog.exec()) {
  1932. filename = fileDialog.selectedFiles().last();
  1933. }
  1934. else {
  1935. //showWarning(tr("Could not open file dialog"));
  1936. return;
  1937. /* NOTREACHED */
  1938. }
  1939. if (filename.isEmpty()) {
  1940. return;
  1941. /* NOTREACHED */
  1942. }
  1943. QString dirPath = getPathFromFilename(filename);
  1944. QDir dir(dirPath);
  1945. QStringList filter;
  1946. filter << "*.dat";
  1947. QStringList fileList = dir.entryList(filter, QDir::Files);
  1948. QString labeled = alterFileName(filename, "_labeled");
  1949. labeled = removePath(labeled);
  1950. labeled.append(".dat");
  1951. if (fileList.contains(labeled, Qt::CaseInsensitive)) {
  1952. labeled = dir.absoluteFilePath(labeled);
  1953. loadInfo(labeled);
  1954. Image *newImage = new Image;
  1955. newImage->image_ = filename;
  1956. newImage->labeled_ = 1;
  1957. newImage->pas_ = 0;
  1958. addImage(newImage);
  1959. enableTools();
  1960. return;
  1961. /* NOTREACHED */
  1962. }
  1963. clearAllTool();
  1964. QString winTitle;
  1965. winTitle.append("ImageLabeler - ");
  1966. winTitle.append(current_image_);
  1967. setWindowTitle(winTitle);
  1968. if (!image_->load(filename)) {
  1969. return;
  1970. /* NOTREACHED */
  1971. }
  1972. image_holder_->resize(image_->size());
  1973. image_holder_->setPixmap(*image_);
  1974. current_image_ = filename;
  1975. image_ID_ = list_images_widget_->count() - 1;
  1976. list_images_widget_->setCurrentRow(image_ID_);
  1977. Image *newImage = new Image;
  1978. newImage->image_ = filename;
  1979. newImage->labeled_ = 0;
  1980. newImage->pas_ = 0;
  1981. addImage(newImage);
  1982. if (!list_images_widget_->count()) {
  1983. return;
  1984. /* NOTREACHED */
  1985. }
  1986. enableTools();
  1987. }
  1988. void
  1989. ImageLabeler::loadImages()
  1990. {
  1991. if (askForUnsavedData()) {
  1992. return;
  1993. /* NOTREACHED */
  1994. }
  1995. QFileDialog fileDialog(0, tr("Load images"));
  1996. fileDialog.setFileMode(QFileDialog::Directory);
  1997. QString dirName("");
  1998. if (fileDialog.exec()) {
  1999. /* TODO: make it possible to select multiple folders */
  2000. dirName = fileDialog.selectedFiles().last();
  2001. }
  2002. else {
  2003. //showWarning(tr("Could not open file dialog"));
  2004. return;
  2005. /* NOTREACHED */
  2006. }
  2007. clearAllTool();
  2008. QWidget *searchInProgress = new QWidget(0);
  2009. searchInProgress->setWindowTitle(tr("Loading images"));
  2010. QLabel *info = new QLabel(searchInProgress);
  2011. info->setText(
  2012. tr("Program is looking for all image files in your directory recursively."));
  2013. QPushButton *cancel = new QPushButton(tr("Cancel"), searchInProgress);
  2014. connect(cancel, SIGNAL(clicked()), this, SLOT(interruptSearch()));
  2015. QVBoxLayout *layout = new QVBoxLayout(searchInProgress);
  2016. searchInProgress->setLayout(layout);
  2017. layout->addWidget(info);
  2018. layout->addWidget(cancel);
  2019. searchInProgress->adjustSize();
  2020. searchInProgress->move(QApplication::desktop()->screen()->rect().center() - rect().center());
  2021. searchInProgress->show();
  2022. getImagesFromDir(QDir(dirName));
  2023. cancel->disconnect();
  2024. searchInProgress->hide();
  2025. delete info;
  2026. delete cancel;
  2027. delete layout;
  2028. delete searchInProgress;
  2029. if (interrupt_search_) {
  2030. interrupt_search_ = 0;
  2031. return;
  2032. /* NOTREACHED */
  2033. }
  2034. if (!list_images_widget_->count()) {
  2035. showWarning(tr("The folder you selected contains no images"));
  2036. return;
  2037. /* NOTREACHED */
  2038. }
  2039. bool ret = 0;
  2040. if (list_images_->at(0).labeled_) {
  2041. QString labeled =
  2042. alterFileName(list_images_->at(0).image_, "_labeled");
  2043. labeled.append(".dat");
  2044. ret = loadInfo(labeled);
  2045. }
  2046. else
  2047. ret = image_->load(list_images_->at(0).image_);
  2048. if (!ret) {
  2049. return;
  2050. /* NOTREACHED */
  2051. }
  2052. current_image_ = list_images_->at(0).image_;
  2053. image_ID_ = 0;
  2054. list_images_widget_->setCurrentRow(image_ID_);
  2055. QString winTitle;
  2056. winTitle.append("ImageLabeler - ");
  2057. winTitle.append(current_image_);
  2058. setWindowTitle(winTitle);
  2059. image_holder_->resize(image_->size());
  2060. image_holder_->setPixmap(*image_);
  2061. enableTools();
  2062. }
  2063. void
  2064. ImageLabeler::loadLegendFromFile()
  2065. {
  2066. QFileDialog fileDialog(0, tr("Load legend"));
  2067. fileDialog.setFileMode(QFileDialog::AnyFile);
  2068. QString filename;
  2069. if (fileDialog.exec()) {
  2070. filename = fileDialog.selectedFiles().last();
  2071. }
  2072. else {
  2073. //showWarning(tr("Could not open file dialog"));
  2074. return;
  2075. /* NOTREACHED */
  2076. }
  2077. QDomDocument doc("Image Labeler");
  2078. QFile file(filename);
  2079. if (!file.open(QIODevice::ReadOnly)) {
  2080. showWarning(tr("Can not open such file"));
  2081. return;
  2082. /* NOTREACHED */
  2083. }
  2084. QString errMsg;
  2085. if (!doc.setContent(&file, &errMsg)) {
  2086. showWarning(errMsg);
  2087. file.close();
  2088. return;
  2089. /* NOTREACHED */
  2090. }
  2091. file.close();
  2092. list_label_->clear();
  2093. /* getting legend */
  2094. QDomElement elements = doc.documentElement();
  2095. QDomNode rootNode = elements.firstChild();
  2096. QString string;
  2097. while(!rootNode.isNull()) {
  2098. QDomElement element = rootNode.toElement();
  2099. if(!element.isNull()) {
  2100. if (element.tagName() == "legend") {
  2101. loadLegendFromNode(&element);
  2102. }
  2103. }
  2104. rootNode = rootNode.nextSibling();
  2105. }
  2106. }
  2107. void
  2108. ImageLabeler::loadLegendFromNode(QDomElement *anElement)
  2109. {
  2110. if (!anElement) {
  2111. return;
  2112. /* NOTREACHED */
  2113. }
  2114. QDomNode subNode = anElement->firstChild();
  2115. QDomElement subElement;
  2116. QString string;
  2117. int id = -1;
  2118. bool isMain;
  2119. uint color = 0xff000000;
  2120. while(!subNode.isNull()) {
  2121. subElement = subNode.toElement();
  2122. if (!subElement.isNull() && !subElement.text().isEmpty()) {
  2123. string = subElement.attribute("id");
  2124. bool ok = 0;
  2125. id = string.toInt(&ok, 10);
  2126. if (!ok) {
  2127. qDebug() <<
  2128. "loadLegendFromNode: "
  2129. "label id format is corrupted";
  2130. subNode = subNode.nextSibling();
  2131. continue;
  2132. }
  2133. string = subElement.attribute("isMain");
  2134. isMain = string.toInt(&ok, 2);
  2135. if (!ok) {
  2136. qDebug() <<
  2137. "loadLegendFromNode: "
  2138. "label isMain flag format is corrupted";
  2139. subNode = subNode.nextSibling();
  2140. continue;
  2141. }
  2142. string = subElement.attribute("color");
  2143. color = string.toUInt(&ok, 16);
  2144. if (!ok) {
  2145. qDebug() <<
  2146. "loadLegendFromNode: "
  2147. "label color format is corrupted";
  2148. subNode = subNode.nextSibling();
  2149. continue;
  2150. }
  2151. string = subElement.text();
  2152. addLabel(id, isMain, string);
  2153. setLabelColor(id, color);
  2154. }
  2155. subNode = subNode.nextSibling();
  2156. }
  2157. }
  2158. void
  2159. ImageLabeler::legendToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2160. {
  2161. QDomElement legend = aDoc->createElement(tr("legend"));
  2162. aRoot->appendChild(legend);
  2163. /* storing all labels made by user */
  2164. int labelCount = list_label_->count();
  2165. for (int i = 0; i < labelCount; i++) {
  2166. QDomElement label = aDoc->createElement(tr("label"));
  2167. label.setAttribute("color", QString("%1").arg(list_label_colors_.at(i), 0, 16));
  2168. label.setAttribute("id", i);
  2169. QString priority;
  2170. if (main_label_ == i)
  2171. priority.append("1");
  2172. else
  2173. priority.append("0");
  2174. label.setAttribute("isMain", priority);
  2175. QString labelText = list_label_->item(i)->text();
  2176. /* removing the number prefix of label */
  2177. if (-1 != labelText.indexOf(QString("%1: ").arg(i))) {
  2178. labelText = labelText.mid(3, labelText.size() - 3);
  2179. }
  2180. QDomText labelName = aDoc->createTextNode(labelText);
  2181. label.appendChild(labelName);
  2182. legend.appendChild(label);
  2183. }
  2184. /* in case we have no labels */
  2185. if (0 == labelCount) {
  2186. QDomElement label = aDoc->createElement(tr("label"));
  2187. label.setAttribute(tr("id"), -1);
  2188. legend.appendChild(label);
  2189. }
  2190. }
  2191. void
  2192. ImageLabeler::objectsToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2193. {
  2194. QDomElement objects = aDoc->createElement(tr("objects"));
  2195. aRoot->appendChild(objects);
  2196. /* rects first */
  2197. for (int i = 0; i < list_bounding_box_.size(); i++) {
  2198. QDomElement rectData = aDoc->createElement(tr("bbox"));
  2199. rectData.setAttribute("id", list_bounding_box_.at(i)->label_ID_);
  2200. QRect rect = list_bounding_box_.at(i)->rect.normalized();
  2201. QString rectDataString =
  2202. QString("%1;%2;%3;%4;").
  2203. arg(rect.x()).
  2204. arg(rect.y()).
  2205. arg(rect.width()).
  2206. arg(rect.height());
  2207. QDomText rectDataText = aDoc->createTextNode(rectDataString);
  2208. rectData.appendChild(rectDataText);
  2209. objects.appendChild(rectData);
  2210. }
  2211. /* polys next */
  2212. for (int i = 0; i < list_polygon_.size(); i++) {
  2213. QDomElement polyData = aDoc->createElement(tr("poly"));
  2214. polyData.setAttribute("id", list_polygon_.at(i)->label_ID_);
  2215. QPolygon poly = list_polygon_.at(i)->poly;
  2216. QString polyDataString;
  2217. for (int j = 0; j < poly.count(); j++)
  2218. polyDataString.append(
  2219. QString("%1;%2;").
  2220. arg(poly.point(j).x()).
  2221. arg(poly.point(j).y())
  2222. );
  2223. QDomText polyDataText = aDoc->createTextNode(polyDataString);
  2224. polyData.appendChild(polyDataText);
  2225. objects.appendChild(polyData);
  2226. }
  2227. }
  2228. void
  2229. ImageLabeler::generateColors()
  2230. {
  2231. int labelCount = list_label_->count();
  2232. if (!labelCount) {
  2233. showWarning(tr("you have not added any labels yet"));
  2234. return;
  2235. /* NOTREACHED */
  2236. }
  2237. int coeff = (0xff / labelCount) * 3;
  2238. //QList< uint > colors;
  2239. list_label_colors_.clear();
  2240. list_label_colors_.append(0);
  2241. uchar red = 0xff;
  2242. uchar green = 0xff;
  2243. uchar blue = 0xff;
  2244. uchar iterationColor = coeff;
  2245. uint color = 0xffffffff;
  2246. int j = 1;
  2247. for (int i = 1; i < labelCount; i++) {
  2248. if (6 == j) {
  2249. iterationColor += coeff;
  2250. j = 1;
  2251. }
  2252. if (5 == j) {
  2253. red = 0xff - iterationColor;
  2254. green = 0xff - iterationColor;
  2255. blue = 0xff;
  2256. }
  2257. else if (4 == j) {
  2258. red = 0xff - iterationColor;
  2259. green = 0xff;
  2260. blue = 0xff - iterationColor;
  2261. }
  2262. else if (3 == j) {
  2263. red = 0xff - iterationColor;
  2264. green = 0xff;
  2265. blue = 0xff;
  2266. }
  2267. else if (2 == j) {
  2268. red = 0xff;
  2269. green = 0xff - iterationColor;
  2270. blue = 0xff;
  2271. }
  2272. else if (1 == j){
  2273. red = 0xff;
  2274. green = 0xff;
  2275. blue = 0xff - iterationColor;
  2276. }
  2277. j++;
  2278. color = red + (green * 0x100) + (blue * 0x10000) + 0xff000000;
  2279. int itemNo = list_label_colors_.count();
  2280. QPixmap iconPix = QPixmap(20, 20);
  2281. iconPix.fill(color);
  2282. QIcon icon(iconPix);
  2283. list_label_->item(itemNo)->setIcon(icon);
  2284. list_label_colors_.append(color);
  2285. }
  2286. image_holder_->update();
  2287. }
  2288. void
  2289. ImageLabeler::addBBoxFromData(
  2290. QString *aBBoxData,
  2291. int *ID
  2292. )
  2293. {
  2294. BoundingBox *bbox = new BoundingBox;
  2295. *bbox = BBoxFromData(aBBoxData);
  2296. if (!bbox->rect.isValid() || !ID) {
  2297. return;
  2298. /* NOTREACHED */
  2299. }
  2300. bbox->label_ID_ = *ID;
  2301. list_bounding_box_.append(bbox);
  2302. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  2303. }
  2304. BoundingBox
  2305. ImageLabeler::BBoxFromData(
  2306. QString *aBBoxData
  2307. )
  2308. {
  2309. BoundingBox bbox;
  2310. QString buffer;
  2311. bbox.rect.setRect(-1, -1, -1, -1);
  2312. int startPos = 0;
  2313. bool ok = 1;
  2314. for (int i = 0; i < aBBoxData->size(); i++) {
  2315. if (';' != aBBoxData->at(i))
  2316. continue;
  2317. buffer = aBBoxData->mid(startPos, i - startPos);
  2318. int bboxData = buffer.toInt(&ok, 10);
  2319. if (!ok) {
  2320. qDebug() <<
  2321. "BBoxFromData: "
  2322. "bbox format is corrupted";
  2323. break;
  2324. }
  2325. if (-1 == bbox.rect.x()) {
  2326. bbox.rect.setX(bboxData);
  2327. bbox.rect.setWidth(-1);
  2328. }
  2329. else if (-1 == bbox.rect.y()) {
  2330. bbox.rect.setY(bboxData);
  2331. bbox.rect.setHeight(-1);
  2332. }
  2333. else if (-1 == bbox.rect.width()) {
  2334. bbox.rect.setWidth(bboxData);
  2335. }
  2336. else if (-1 == bbox.rect.height()) {
  2337. bbox.rect.setHeight(bboxData);
  2338. }
  2339. startPos = i + 1;
  2340. }
  2341. if (!bbox.rect.isValid()) {
  2342. qDebug() <<
  2343. "BBoxFromData: "
  2344. "bbox format is corrupted";
  2345. bbox.rect.setRect(-1, -1, -1, -1);
  2346. }
  2347. else if (!ok) {
  2348. bbox.rect.setRect(-1, -1, -1, -1);
  2349. }
  2350. return bbox;
  2351. }
  2352. BoundingBox
  2353. ImageLabeler::BBoxFromString(
  2354. QString *aString,
  2355. int *oldID
  2356. )
  2357. {
  2358. BoundingBox bbox;
  2359. bbox.label_ID_ = -1;
  2360. *oldID = -1;
  2361. if (!aString) {
  2362. return bbox;
  2363. /* NOTREACHED */
  2364. }
  2365. if (-1 == aString->indexOf("BBox")) {
  2366. return bbox;
  2367. /* NOTREACHED */
  2368. }
  2369. /* getting bbox id in the list(it cannot be changed) */
  2370. bool ok = 0;
  2371. int bboxID = getNumFromString(aString, "BBox #", ";", &ok);
  2372. if (!ok || bboxID <= -1) {
  2373. qDebug() <<
  2374. "BBoxFromString: bboxID is corrupted";
  2375. return bbox;
  2376. /* NOTREACHED */
  2377. }
  2378. /* getting new label id */
  2379. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  2380. if (!ok || labelID <= -1) {
  2381. showWarning(
  2382. tr("new LabelID is wrong, area can not be changed")
  2383. );
  2384. return bbox;
  2385. /* NOTREACHED */
  2386. }
  2387. /* getting new points */
  2388. int pointsPos = aString->indexOf("data:") + 5;
  2389. int pointsLen = aString->size() - pointsPos;
  2390. if (pointsLen <= 0) {
  2391. showWarning(
  2392. tr("new data is wrong, area can not be changed")
  2393. );
  2394. return bbox;
  2395. /* NOTREACHED */
  2396. }
  2397. QString pointsData = aString->mid(pointsPos, pointsLen);
  2398. bbox = BBoxFromData(&pointsData);
  2399. bbox.label_ID_ = labelID;
  2400. *oldID = bboxID;
  2401. return bbox;
  2402. }
  2403. void
  2404. ImageLabeler::addPoly(Polygon *poly)
  2405. {
  2406. if (poly->poly.isEmpty() || poly->label_ID_ < 0) {
  2407. return;
  2408. /* NOTREACHED */
  2409. }
  2410. list_polygon_.append(poly);
  2411. addPolyArea(list_polygon_.count() - 1, *poly);
  2412. }
  2413. void
  2414. ImageLabeler::addBBox(BoundingBox *bbox)
  2415. {
  2416. if (bbox->rect.isEmpty() || bbox->label_ID_ < 0) {
  2417. return;
  2418. /* NOTREACHED */
  2419. }
  2420. list_bounding_box_.append(bbox);
  2421. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  2422. }
  2423. void
  2424. ImageLabeler::addPolyFromData(
  2425. QString *aPolyData,
  2426. int *labelID
  2427. )
  2428. {
  2429. Polygon *poly = new Polygon;
  2430. *poly = polyFromData(aPolyData);
  2431. if (poly->poly.isEmpty() || !labelID) {
  2432. return;
  2433. /* NOTREACHED */
  2434. }
  2435. poly->label_ID_ = *labelID;
  2436. list_polygon_.append(poly);
  2437. addPolyArea(list_polygon_.count() - 1, *poly);
  2438. }
  2439. Polygon
  2440. ImageLabeler::polyFromData(
  2441. QString *aPolyData
  2442. )
  2443. {
  2444. Polygon poly;
  2445. poly.label_ID_ = -1;
  2446. QPoint point;
  2447. QString buffer;
  2448. int startPos = 0;
  2449. bool ok = 1;
  2450. bool evenFlag = 0;
  2451. for (int i = 0; i < aPolyData->size(); i++) {
  2452. if (';' != aPolyData->at(i))
  2453. continue;
  2454. buffer = aPolyData->mid(startPos, i - startPos);
  2455. int polyCoor = buffer.toInt(&ok, 10);
  2456. if (!ok) {
  2457. qDebug() <<
  2458. "polyFromData: "
  2459. "poly format is corrupted";
  2460. break;
  2461. }
  2462. if (!evenFlag) {
  2463. point.setX(polyCoor);
  2464. evenFlag = 1;
  2465. }
  2466. else {
  2467. point.setY(polyCoor);
  2468. poly.poly.append(point);
  2469. evenFlag = 0;
  2470. }
  2471. startPos = i + 1;
  2472. }
  2473. if (evenFlag) {
  2474. qDebug() <<
  2475. "polyFromData: "
  2476. "poly format is corrupted";
  2477. poly.poly.clear();
  2478. }
  2479. else if (!ok) {
  2480. poly.poly.clear();
  2481. }
  2482. return poly;
  2483. }
  2484. Polygon
  2485. ImageLabeler::polyFromString(
  2486. QString *aString,
  2487. int *oldID
  2488. )
  2489. {
  2490. Polygon poly;
  2491. poly.label_ID_ = -1;
  2492. *oldID = -1;
  2493. if (!aString) {
  2494. return poly;
  2495. /* NOTREACHED */
  2496. }
  2497. if (-1 == aString->indexOf("Poly")) {
  2498. return poly;
  2499. /* NOTREACHED */
  2500. }
  2501. /* getting poly id in the list(it cannot be changed) */
  2502. bool ok = 0;
  2503. int polyID = getNumFromString(aString, "Poly #", ";", &ok);
  2504. if (!ok || polyID <= -1) {
  2505. qDebug() <<
  2506. "polyFromString: poly ID is corrupted";
  2507. return poly;
  2508. /* NOTREACHED */
  2509. }
  2510. /* getting new label id */
  2511. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  2512. if (!ok || labelID <= -1) {
  2513. showWarning(
  2514. tr("new LabelID is wrong, area can not be changed")
  2515. );
  2516. return poly;
  2517. /* NOTREACHED */
  2518. }
  2519. /* getting new points */
  2520. int pointsPos = aString->indexOf("points:") + 7;
  2521. int pointsLen = aString->size() - pointsPos;
  2522. if (pointsLen <= 0) {
  2523. showWarning(
  2524. tr("new points data is wrong, area can not be changed")
  2525. );
  2526. return poly;
  2527. /* NOTREACHED */
  2528. }
  2529. QString pointsData = aString->mid(pointsPos, pointsLen);
  2530. poly = polyFromData(&pointsData);
  2531. poly.label_ID_ = labelID;
  2532. *oldID = polyID;
  2533. return poly;
  2534. }
  2535. void
  2536. ImageLabeler::showWarning(
  2537. const QString text
  2538. )
  2539. {
  2540. if (text.isEmpty()) {
  2541. return;
  2542. /* NOTREACHED */
  2543. }
  2544. QMessageBox msgBox;
  2545. msgBox.setText(text);
  2546. msgBox.setIcon(QMessageBox::Warning);
  2547. msgBox.exec();
  2548. }
  2549. bool
  2550. ImageLabeler::askForUnsavedData()
  2551. {
  2552. if ((!list_bounding_box_.isEmpty() ||
  2553. !list_polygon_.isEmpty()) &&
  2554. unsaved_data_)
  2555. {
  2556. QMessageBox msgBox;
  2557. msgBox.setText(tr("There is some unsaved data"));
  2558. msgBox.setInformativeText(tr("Do you want to save your progress?"));
  2559. msgBox.setStandardButtons(
  2560. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  2561. msgBox.setDefaultButton(QMessageBox::Save);
  2562. msgBox.setIcon(QMessageBox::Question);
  2563. int ret = msgBox.exec();
  2564. if (QMessageBox::Save == ret)
  2565. saveAllInfo();
  2566. else if (QMessageBox::Cancel == ret)
  2567. return true;
  2568. }
  2569. return false;
  2570. }
  2571. void
  2572. ImageLabeler::setBoundingBoxTool(bool aButtonPressed)
  2573. {
  2574. if (aButtonPressed)
  2575. image_holder_->setTool(ImageHolder::BoundingBoxTool);
  2576. else {
  2577. image_holder_->setTool(ImageHolder::NoTool);
  2578. image_holder_->clearLast();
  2579. }
  2580. }
  2581. void
  2582. ImageLabeler::setPolygonTool(bool aButtonPressed)
  2583. {
  2584. if (aButtonPressed) {
  2585. image_holder_->setTool(ImageHolder::PolygonTool);
  2586. image_holder_->setFocus();
  2587. }
  2588. else {
  2589. image_holder_->setTool(ImageHolder::NoTool);
  2590. image_holder_->clearLast();
  2591. }
  2592. }
  2593. void
  2594. ImageLabeler::onSelectionStarted()
  2595. {
  2596. button_confirm_selection_->setEnabled(true);
  2597. }
  2598. void
  2599. ImageLabeler::onImageScaled()
  2600. {
  2601. }
  2602. void
  2603. ImageLabeler::confirmSelection()
  2604. {
  2605. if (!list_label_->count()) {
  2606. showWarning(tr("You haven't added any label"));
  2607. return;
  2608. /* NOTREACHED */
  2609. }
  2610. image_holder_->confirmSelection();
  2611. if (label_ID_ < 0)
  2612. label_ID_ = 0;
  2613. ImageHolder::Tool tool = image_holder_->tool();
  2614. switch (tool) {
  2615. case ImageHolder::BoundingBoxTool:
  2616. list_bounding_box_.last()->label_ID_ = label_ID_;
  2617. addBBoxArea(
  2618. list_bounding_box_.count() - 1,
  2619. *(list_bounding_box_.last())
  2620. );
  2621. break;
  2622. case ImageHolder::PolygonTool:
  2623. list_polygon_.last()->label_ID_ = label_ID_;
  2624. addPolyArea(
  2625. list_polygon_.count() - 1,
  2626. *(list_polygon_.last())
  2627. );
  2628. break;
  2629. default:
  2630. break;
  2631. }
  2632. button_confirm_selection_->setEnabled(false);
  2633. unsaved_data_ = 1;
  2634. }
  2635. void
  2636. ImageLabeler::clearAll()
  2637. {
  2638. clearLabelList();
  2639. list_areas_->clear();
  2640. list_bounding_box_.clear();
  2641. list_polygon_.clear();
  2642. list_images_->clear();
  2643. list_images_widget_->clear();
  2644. main_label_ = -1;
  2645. image_holder_->clearAll();
  2646. segmented_image_.clear();
  2647. action_view_normal_->setEnabled(false);
  2648. action_view_segmented_->setEnabled(false);
  2649. }
  2650. void
  2651. ImageLabeler::clearAllTool()
  2652. {
  2653. list_areas_->clear();
  2654. list_bounding_box_.clear();
  2655. list_polygon_.clear();
  2656. main_label_ = -1;
  2657. image_holder_->clearAll();
  2658. }
  2659. void ImageLabeler::clearLabelList()
  2660. {
  2661. list_label_->clear();
  2662. addLabel(0, false, "BACKGROUND");
  2663. list_label_->item(0)->setFlags(
  2664. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  2665. Qt::ItemIsEnabled
  2666. );
  2667. }
  2668. void ImageLabeler::clearLabelColorList()
  2669. {
  2670. list_label_colors_.clear();
  2671. list_label_colors_.append(0x0);
  2672. }
  2673. void
  2674. ImageLabeler::enableTools()
  2675. {
  2676. action_save_labels_->setEnabled(true);
  2677. action_save_segmented_->setEnabled(true);
  2678. action_save_legend_->setEnabled(true);
  2679. action_bound_box_tool_->setEnabled(true);
  2680. action_polygon_tool_->setEnabled(true);
  2681. action_add_description_->setEnabled(true);
  2682. action_undo_->setEnabled(true);
  2683. action_redo_->setEnabled(true);
  2684. button_bound_box_tool_->setEnabled(true);
  2685. button_polygon_tool_->setEnabled(true);
  2686. button_tagging_tool_->setEnabled(true);
  2687. button_add_label_->setEnabled(true);
  2688. button_remove_label_->setEnabled(true);
  2689. button_prev_image_->setEnabled(true);
  2690. button_next_image_->setEnabled(true);
  2691. button_clear_selection_tool_->setEnabled(true);
  2692. button_generate_colors_->setEnabled(true);
  2693. button_delete_all_labels_->setEnabled(true);
  2694. }
  2695. void
  2696. ImageLabeler::disableTools()
  2697. {
  2698. action_save_labels_->setEnabled(false);
  2699. action_save_segmented_->setEnabled(false);
  2700. action_save_legend_->setEnabled(false);
  2701. action_bound_box_tool_->setEnabled(false);
  2702. action_polygon_tool_->setEnabled(false);
  2703. button_tagging_tool_->setEnabled(false);
  2704. action_add_description_->setEnabled(false);
  2705. action_undo_->setEnabled(false);
  2706. action_redo_->setEnabled(false);
  2707. button_bound_box_tool_->setEnabled(false);
  2708. button_polygon_tool_->setEnabled(false);
  2709. button_add_label_->setEnabled(false);
  2710. button_remove_label_->setEnabled(false);
  2711. button_prev_image_->setEnabled(false);
  2712. button_next_image_->setEnabled(false);
  2713. button_clear_selection_tool_->setEnabled(false);
  2714. button_generate_colors_->setEnabled(false);
  2715. button_delete_all_labels_->setEnabled(false);
  2716. }
  2717. void
  2718. ImageLabeler::areaListPopupMenu(const QPoint &aPos)
  2719. {
  2720. QPoint globalPos = list_areas_->mapToGlobal(aPos);
  2721. QModelIndex index = list_areas_->indexAt(aPos);
  2722. if (-1 == index.row()) {
  2723. return;
  2724. /* NOTREACHED */
  2725. }
  2726. list_areas_->item(index.row())->setSelected(true);
  2727. popup_area_list_->exec(globalPos);
  2728. }
  2729. void
  2730. ImageLabeler::labelListPopupMenu(const QPoint &aPos)
  2731. {
  2732. QPoint globalPos = list_label_->mapToGlobal(aPos);
  2733. QModelIndex index = list_label_->indexAt(aPos);
  2734. if (-1 == index.row() || !index.row()) {
  2735. return;
  2736. /* NOTREACHED */
  2737. }
  2738. list_label_->item(index.row())->setSelected(true);
  2739. popup_label_list_->exec(globalPos);
  2740. }
  2741. void
  2742. ImageLabeler::setDataFromForm(QString aData)
  2743. {
  2744. if (ImageDescriptionPurpose == line_edit_form_.purpose()) {
  2745. image_description_ = aData;
  2746. setWindowTitle(image_description_);
  2747. }
  2748. else if (TaggingPurpose == line_edit_form_.purpose()) {
  2749. tags_ = aData;
  2750. }
  2751. }
  2752. void
  2753. ImageLabeler::setPureData()
  2754. {
  2755. /* initializing array */
  2756. if (pure_data_) {
  2757. delete[] *pure_data_;
  2758. delete pure_data_;
  2759. }
  2760. QSize imageSize = image_->size();
  2761. pure_data_ = new int *[imageSize.height()];
  2762. if (!pure_data_) {
  2763. return;
  2764. /* NOTREACHED */
  2765. }
  2766. for (int i = 0; i < imageSize.height(); i++) {
  2767. pure_data_[i] = new int[imageSize.width()];
  2768. if (!pure_data_[i]) {
  2769. return;
  2770. /* NOTREACHED */
  2771. }
  2772. }
  2773. int bboxCnt = list_bounding_box_.count();
  2774. int polyCnt = list_polygon_.count();
  2775. for (int i = 0; i < imageSize.height(); i++)
  2776. for (int j = 0; j < imageSize.width(); j++) {
  2777. pure_data_[i][j] = 0;
  2778. /* bboxes first */
  2779. for (int cnt = 0; cnt < bboxCnt; cnt++) {
  2780. BoundingBox *bbox = list_bounding_box_.at(cnt);
  2781. if (bbox->rect.contains(j, i)) {
  2782. pure_data_[i][j] = bbox->label_ID_;
  2783. }
  2784. }
  2785. /* polys next */
  2786. for (int cnt = 0; cnt < polyCnt; cnt++) {
  2787. Polygon *poly = list_polygon_.at(cnt);
  2788. if (poly->poly.containsPoint(QPoint(j, i), Qt::OddEvenFill)) {
  2789. pure_data_[i][j] = poly->label_ID_;
  2790. }
  2791. }
  2792. }
  2793. }
  2794. void
  2795. ImageLabeler::setLabelColor()
  2796. {
  2797. if (list_label_colors_.count() < list_label_->count()) {
  2798. generateColors();
  2799. }
  2800. QListWidgetItem *current = list_label_->currentItem();
  2801. int labelID = list_label_->row(current);
  2802. QColor defaultColor;
  2803. defaultColor.setRgb(list_label_colors_.at(labelID));
  2804. QColor newColor = QColorDialog::getColor(
  2805. defaultColor,
  2806. list_label_
  2807. );
  2808. if (!newColor.isValid()) {
  2809. return;
  2810. /* NOTREACHED */
  2811. }
  2812. list_label_colors_.takeAt(labelID);
  2813. list_label_colors_.insert(labelID, newColor.rgb());
  2814. QPixmap iconPix = QPixmap(20, 20);
  2815. iconPix.fill(newColor);
  2816. QIcon icon(iconPix);
  2817. current->setIcon(icon);
  2818. image_holder_->update();
  2819. }
  2820. void
  2821. ImageLabeler::setLabelColor(int anID, QColor aColor)
  2822. {
  2823. if (anID < 0) {
  2824. return;
  2825. /* NOTREACHED */
  2826. }
  2827. if (list_label_colors_.count() < list_label_->count()) {
  2828. generateColors();
  2829. }
  2830. QListWidgetItem *item = list_label_->item(anID);
  2831. list_label_colors_.takeAt(anID);
  2832. list_label_colors_.insert(anID, aColor.rgb());
  2833. QPixmap iconPix = QPixmap(20, 20);
  2834. iconPix.fill(aColor);
  2835. QIcon icon(iconPix);
  2836. item->setIcon(icon);
  2837. image_holder_->update();
  2838. }
  2839. void
  2840. ImageLabeler::onOptionsSet()
  2841. {
  2842. auto_color_generation_ = options_form_.autoColorGeneration();
  2843. }
  2844. void
  2845. ImageLabeler::viewNormal()
  2846. {
  2847. if (current_image_.isEmpty()) {
  2848. return;
  2849. /* NOTREACHED */
  2850. }
  2851. image_->load(current_image_);
  2852. image_holder_->setPixmap(*image_);
  2853. action_view_segmented_->setEnabled(true);
  2854. action_view_normal_->setEnabled(false);
  2855. }
  2856. void
  2857. ImageLabeler::viewSegmented()
  2858. {
  2859. if (segmented_image_.isEmpty()) {
  2860. return;
  2861. /* NOTREACHED */
  2862. }
  2863. image_->load(segmented_image_);
  2864. image_holder_->setPixmap(*image_);
  2865. action_view_segmented_->setEnabled(false);
  2866. action_view_normal_->setEnabled(true);
  2867. }
  2868. void
  2869. ImageLabeler::interruptSearch()
  2870. {
  2871. interrupt_search_ = 1;
  2872. }
  2873. void
  2874. ImageLabeler::selectImage(QListWidgetItem *anItem)
  2875. {
  2876. if (!anItem || list_images_widget_->row(anItem) < 0 ||
  2877. list_images_->isEmpty())
  2878. {
  2879. return;
  2880. /* NOTREACHED */
  2881. }
  2882. clearAllTool();
  2883. clearLabelList();
  2884. clearLabelColorList();
  2885. image_ID_ = list_images_widget_->row(anItem);
  2886. selectImage(image_ID_);
  2887. // if (list_images_->at(image_ID_).labeled_ &&
  2888. // !list_images_->at(image_ID_).pas_)
  2889. // {
  2890. // list_label_->clear();
  2891. // QString labeled =
  2892. // alterFileName(list_images_->at(image_ID_).image_, "_labeled");
  2893. // labeled.append(".dat");
  2894. // loadInfo(labeled);
  2895. // }
  2896. // else if (list_images_->at(image_ID_).labeled_ &&
  2897. // list_images_->at(image_ID_).pas_)
  2898. // {
  2899. // /* TODO: do the pascal file selecting */
  2900. // showWarning("this function doesn't work at the moment, sorry.");
  2901. // }
  2902. // else {
  2903. // current_image_ = list_images_->at(image_ID_).image_;
  2904. // image_->load(current_image_);
  2905. // image_holder_->setPixmap(*image_);
  2906. // image_holder_->resize(image_->size());
  2907. // }
  2908. }
  2909. bool
  2910. ImageLabeler::selectImage(int anImageID)
  2911. {
  2912. if (anImageID < 0 || list_images_->isEmpty() ||
  2913. list_images_->count() <= anImageID)
  2914. {
  2915. return false;
  2916. /* NOTREACHED */
  2917. }
  2918. if (list_images_->at(anImageID).labeled_ &&
  2919. !list_images_->at(anImageID).pas_)
  2920. {
  2921. list_label_->clear();
  2922. QString labeled =
  2923. alterFileName(list_images_->at(anImageID).image_, "_labeled");
  2924. labeled.append(".dat");
  2925. loadInfo(labeled);
  2926. }
  2927. else if (list_images_->at(anImageID).labeled_ &&
  2928. list_images_->at(anImageID).pas_)
  2929. {
  2930. /* TODO: do the pascal file selecting */
  2931. showWarning("this function doesn't work at the moment, sorry.");
  2932. }
  2933. else {
  2934. current_image_ = list_images_->at(anImageID).image_;
  2935. image_->load(current_image_);
  2936. image_holder_->setPixmap(*image_);
  2937. image_holder_->resize(image_->size());
  2938. }
  2939. return true;
  2940. }
  2941. void
  2942. ImageLabeler::imageListPopupMenu(const QPoint &aPos)
  2943. {
  2944. QPoint globalPos = list_images_widget_->mapToGlobal(aPos);
  2945. QModelIndex index = list_images_widget_->indexAt(aPos);
  2946. if (-1 == index.row()) {
  2947. return;
  2948. /* NOTREACHED */
  2949. }
  2950. list_images_widget_->item(index.row())->setSelected(true);
  2951. popup_images_list_->exec(globalPos);
  2952. }
  2953. void
  2954. ImageLabeler::removeImage()
  2955. {
  2956. int num = list_images_widget_->currentRow();
  2957. list_images_widget_->takeItem(num);
  2958. list_images_->takeAt(num);
  2959. for (int i = num ; i < list_images_widget_->count(); i++) {
  2960. QString newStr = getFilenameFromPath(&(list_images_->at(i).image_));
  2961. newStr.prepend(QString("%1: ").arg(i));
  2962. list_images_widget_->item(i)->setText(newStr);
  2963. }
  2964. }
  2965. void
  2966. ImageLabeler::resizeEvent (QResizeEvent *anEvent)
  2967. {
  2968. QWidget::resizeEvent(anEvent);
  2969. }
  2970. void
  2971. ImageLabeler::mousePressEvent(QMouseEvent *anEvent)
  2972. {
  2973. QWidget::mousePressEvent(anEvent);
  2974. }
  2975. void
  2976. ImageLabeler::keyPressEvent(QKeyEvent *anEvent)
  2977. {
  2978. keyboard_modifier_ = anEvent->modifiers();
  2979. if (Qt::Key_Left == anEvent->key() &&
  2980. Qt::ControlModifier == keyboard_modifier_) {
  2981. prevImage();
  2982. }
  2983. if (Qt::Key_Right == anEvent->key() &&
  2984. Qt::ControlModifier == keyboard_modifier_) {
  2985. nextImage();
  2986. }
  2987. if ((Qt::Key_Enter == anEvent->key() ||
  2988. Qt::Key_Return == anEvent->key()) &&
  2989. // Qt::NoModifier == anEvent->modifiers() &&
  2990. ImageHolder::NewSelection == image_holder_->state()) {
  2991. confirmSelection();
  2992. }
  2993. if ((Qt::Key_Enter == anEvent->key() ||
  2994. Qt::Key_Return == anEvent->key()) &&
  2995. // Qt::NoModifier == anEvent->modifiers() &&
  2996. -1 != image_holder_->focusedSelection()) {
  2997. image_holder_->clearFocusOnArea();
  2998. image_holder_->clearEdition();
  2999. image_holder_->update();
  3000. }
  3001. if (Qt::Key_Escape == anEvent->key()) {
  3002. image_holder_->clearLast();
  3003. image_holder_->clearFocusOnArea();
  3004. image_holder_->clearEdition();
  3005. image_holder_->update();
  3006. }
  3007. if (Qt::Key_Z == anEvent->key() &&
  3008. Qt::ControlModifier == anEvent->modifiers()) {
  3009. image_holder_->undo();
  3010. }
  3011. if (Qt::Key_Y == anEvent->key() &&
  3012. Qt::ControlModifier == anEvent->modifiers()) {
  3013. image_holder_->redo();
  3014. }
  3015. QWidget::keyPressEvent(anEvent);
  3016. }
  3017. void
  3018. ImageLabeler::keyReleaseEvent(QKeyEvent *anEvent)
  3019. {
  3020. keyboard_modifier_ = anEvent->modifiers();
  3021. }
  3022. void
  3023. ImageLabeler::wheelEvent(QWheelEvent *anEvent)
  3024. {
  3025. /* zoomin */
  3026. if (0 < anEvent->delta() &&
  3027. Qt::ControlModifier == keyboard_modifier_) {
  3028. image_holder_->scaleImage(ZoomIn, 1.1);
  3029. }
  3030. /* zoomout */
  3031. else if (anEvent->delta() < 0 &&
  3032. Qt::ControlModifier == keyboard_modifier_) {
  3033. image_holder_->scaleImage(ZoomOut, 1.1);
  3034. }
  3035. }
  3036. void
  3037. ImageLabeler::closeEvent(QCloseEvent *anEvent)
  3038. {
  3039. Q_UNUSED(anEvent)
  3040. writeSettings();
  3041. }
  3042. /*
  3043. *
  3044. */