ImageLabeler.cpp 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263
  1. /*!
  2. * \file ImageLabeler.cpp
  3. * \brief implementation of the ImageLabeler class
  4. *
  5. * Main widget which contains all GUI elements
  6. * and connects them with each other.
  7. *
  8. * Created on: Oct 4, 2011
  9. * Author: Gapchich Vladislav
  10. */
  11. #include "ImageLabeler.h"
  12. #include "functions.h"
  13. #include <QApplication>
  14. #include <QFrame>
  15. #include <QMenuBar>
  16. #include <QMenu>
  17. #include <QAction>
  18. #include <QBoxLayout>
  19. #include <QGridLayout>
  20. #include <QPixmap>
  21. #include <QLabel>
  22. #include <QCheckBox>
  23. #include <QScrollArea>
  24. #include <QPushButton>
  25. #include <QButtonGroup>
  26. #include <QListWidget>
  27. #include <QListWidgetItem>
  28. #include <QDesktopWidget>
  29. #include <QFileDialog>
  30. #include <QColorDialog>
  31. #include <QDir>
  32. #include <QMessageBox>
  33. #include <QListIterator>
  34. #include <QDomDocument>
  35. #include <QFile>
  36. #include <QKeyEvent>
  37. #include <QSettings>
  38. #include <QDebug>
  39. //! A constructor of the main class
  40. /*!
  41. * \param[in,out] aParent a pointer to the parent widget.
  42. * If aParent is 0, then ImageLabeler becomes a window, otherwise
  43. * it becomes a child of some other widget
  44. * \param[in] aSettingsPath alters the path to the file with settings
  45. * (default path is /path/to/binary/ImageLabeler.ini)
  46. *
  47. * A constructor is responsible for allocating memory for all the
  48. * GUI objects, arranging and connecting them in the right order.
  49. */
  50. ImageLabeler::ImageLabeler(QWidget *aParent, QString aSettingsPath) :
  51. QMainWindow(aParent)
  52. {
  53. setFocusPolicy(Qt::StrongFocus);
  54. /*
  55. * Variables
  56. */
  57. list_images_ = new QList< Image >;
  58. main_label_ = -1;
  59. pure_data_ = 0;
  60. //label_ID_ = -1;
  61. /* options */
  62. auto_color_generation_ = 0;
  63. /* flags */
  64. interrupt_search_ = 0;
  65. unsaved_data_ = 0;
  66. /*
  67. * menu bar part begins
  68. */
  69. menu_bar_ = new QMenuBar(this);
  70. setMenuBar(menu_bar_);
  71. menu_file_ = new QMenu(menu_bar_);
  72. menu_file_->setTitle(tr("&File"));
  73. menu_save_ = new QMenu(menu_bar_);
  74. menu_save_->setTitle(tr("&Save"));
  75. menu_load_ = new QMenu(menu_bar_);
  76. menu_load_->setTitle(tr("&Load"));
  77. menu_pascal_ = new QMenu(menu_bar_);
  78. menu_pascal_->setTitle(tr("&Pascal"));
  79. menu_view_ = new QMenu(menu_bar_);
  80. menu_view_->setTitle(tr("&View"));
  81. menu_edit_ = new QMenu(menu_bar_);
  82. menu_edit_->setTitle(tr("&Edit"));
  83. menu_help_ = new QMenu(menu_bar_);
  84. menu_help_->setTitle(tr("&Help"));
  85. /* menu file */
  86. action_open_image_ = new QAction(this);
  87. action_open_image_->setText(tr("&Load image"));
  88. action_open_images_ = new QAction(this);
  89. action_open_images_->setText(tr("&Load images (recursively)"));
  90. action_open_labeled_image_ = new QAction(this);
  91. action_open_labeled_image_->setText(tr("&Load labeled image"));
  92. action_load_legend_ = new QAction(this);
  93. action_load_legend_->setText(tr("&Load legend"));
  94. action_save_all_ = new QAction(this);
  95. action_save_all_->setText(tr("&Save all info"));
  96. action_save_all_->setEnabled(false);
  97. action_save_segmented_ = new QAction(this);
  98. action_save_segmented_->setText(tr("Save segmented &picture"));
  99. action_save_segmented_->setEnabled(false);
  100. action_save_legend_ = new QAction(this);
  101. action_save_legend_->setText(tr("Save &legend"));
  102. action_save_legend_->setEnabled(false);
  103. action_quit_ = new QAction(this);
  104. action_quit_->setText(tr("&Quit"));
  105. /* menu pascal */
  106. action_load_pascal_file_ = new QAction(this);
  107. action_load_pascal_file_->setText(tr("&Load pascal file"));
  108. action_load_pascal_poly_ = new QAction(this);
  109. action_load_pascal_poly_->setText(tr("&Load poly info"));
  110. /* menu view */
  111. action_view_normal_ = new QAction(this);
  112. action_view_normal_->setText(tr("&Normal"));
  113. action_view_normal_->setEnabled(false);
  114. action_view_segmented_ = new QAction(this);
  115. action_view_segmented_->setText(tr("&Segmented"));
  116. action_view_segmented_->setEnabled(false);
  117. /* menu edit */
  118. action_undo_ = new QAction(this);
  119. action_undo_->setText(tr("&Undo"));
  120. action_undo_->setEnabled(false);
  121. action_redo_ = new QAction(this);
  122. action_redo_->setText(tr("&Redo"));
  123. action_redo_->setEnabled(false);
  124. action_bound_box_tool_ = new QAction(this);
  125. action_bound_box_tool_->setText(tr("Bounding box tool"));
  126. action_bound_box_tool_->setEnabled(false);
  127. action_polygon_tool_ = new QAction(this);
  128. action_polygon_tool_->setText(tr("&Polygon tool"));
  129. action_polygon_tool_->setEnabled(false);
  130. action_tagging_tool_ = new QAction(this);
  131. action_tagging_tool_->setText(tr("&Tagging tool"));
  132. action_tagging_tool_->setEnabled(false);
  133. action_add_description_ = new QAction(this);
  134. action_add_description_->setText(tr("&Add image description"));
  135. action_add_description_->setEnabled(false);
  136. action_options_ = new QAction(this);
  137. action_options_->setText(tr("&Options"));
  138. /* menu help */
  139. action_help_content_ = new QAction(this);
  140. action_help_content_->setText(tr("&Help content"));
  141. action_help_content_->setEnabled(false);
  142. action_about_ = new QAction(this);
  143. action_about_->setText(tr("&About"));
  144. action_about_->setEnabled(false);
  145. /* ------------------ */
  146. menu_load_->addAction(action_open_image_);
  147. menu_load_->addAction(action_open_images_);
  148. menu_load_->addAction(action_open_labeled_image_);
  149. menu_load_->addAction(action_load_legend_);
  150. menu_save_->addAction(action_save_segmented_);
  151. menu_save_->addAction(action_save_legend_);
  152. menu_save_->addAction(action_save_all_);
  153. menu_file_->addAction(menu_load_->menuAction());
  154. menu_file_->addAction(menu_save_->menuAction());
  155. menu_file_->addAction(menu_pascal_->menuAction());
  156. //menu_file_->addSeparator();
  157. menu_file_->addSeparator();
  158. menu_file_->addAction(action_quit_);
  159. menu_pascal_->addAction(action_load_pascal_file_);
  160. menu_pascal_->addAction(action_load_pascal_poly_);
  161. menu_view_->addAction(action_view_normal_);
  162. menu_view_->addAction(action_view_segmented_);
  163. menu_edit_->addAction(action_undo_);
  164. menu_edit_->addAction(action_redo_);
  165. menu_edit_->addSeparator();
  166. menu_edit_->addAction(action_bound_box_tool_);
  167. menu_edit_->addAction(action_polygon_tool_);
  168. menu_edit_->addAction(action_tagging_tool_);
  169. menu_edit_->addSeparator();
  170. menu_edit_->addAction(action_add_description_);
  171. menu_edit_->addAction(action_options_);
  172. menu_help_->addAction(action_help_content_);
  173. menu_help_->addAction(action_about_);
  174. menu_bar_->addAction(menu_file_->menuAction());
  175. menu_bar_->addAction(menu_view_->menuAction());
  176. menu_bar_->addAction(menu_edit_->menuAction());
  177. menu_bar_->addAction(menu_help_->menuAction());
  178. /*
  179. * popup menu part begins
  180. */
  181. popup_area_list_ = new QMenu;
  182. action_delete_area_ = new QAction(this);
  183. action_delete_area_->setText(tr("&Delete area"));
  184. action_edit_area_ = new QAction(this);
  185. action_edit_area_->setText(tr("&Change area"));
  186. popup_area_list_->addAction(action_delete_area_);
  187. popup_area_list_->addAction(action_edit_area_);
  188. popup_label_list_ = new QMenu;
  189. action_set_color_ = new QAction(this);
  190. action_set_color_->setText(tr("Set &color"));
  191. action_toggle_priority_ = new QAction(this);
  192. action_toggle_priority_->setText(tr("Toggle &priority"));
  193. action_delete_label_ = new QAction(this);
  194. action_delete_label_->setText(tr("&Delete"));
  195. popup_label_list_->addAction(action_set_color_);
  196. popup_label_list_->addAction(action_toggle_priority_);
  197. popup_label_list_->addAction(action_delete_label_);
  198. popup_images_list_ = new QMenu;
  199. action_remove_image_ = new QAction(this);
  200. action_remove_image_->setText(tr("&remove image"));
  201. popup_images_list_->addAction(action_remove_image_);
  202. /*
  203. * widgets part begins
  204. */
  205. central_widget_ = new QWidget(this);
  206. setCentralWidget(central_widget_);
  207. frame_toolbox_ = new QFrame(central_widget_);
  208. frame_toolbox_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  209. frame_toolbox_->setLineWidth(0);
  210. frame_toolbox_->setMidLineWidth(0);
  211. frame_center_ = new QFrame(central_widget_);
  212. frame_image_ = new QScrollArea(frame_center_);
  213. frame_image_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  214. frame_image_->setLineWidth(0);
  215. frame_image_->setMidLineWidth(0);
  216. frame_image_->setWidgetResizable(false);
  217. frame_image_->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
  218. frame_image_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  219. frame_labelbox_ = new QFrame(central_widget_);
  220. frame_labelbox_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  221. frame_labelbox_->setLineWidth(0);
  222. frame_labelbox_->setMidLineWidth(0);
  223. /* just dummy */
  224. image_ = new QPixmap(500, 500);
  225. image_->fill(QColor(Qt::white));
  226. image_holder_ = new ImageHolder;
  227. image_holder_->setPixmap(*image_);
  228. image_holder_->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
  229. image_holder_->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
  230. image_holder_->setScaledContents(true);
  231. image_holder_->setFrameStyle(QFrame::Box | QFrame::Plain);
  232. image_holder_->setLineWidth(0);
  233. image_holder_->setFocusPolicy(Qt::StrongFocus);
  234. frame_image_->setWidget(image_holder_);
  235. list_label_ = new QListWidget(central_widget_);
  236. list_label_->setContextMenuPolicy(Qt::CustomContextMenu);
  237. list_areas_ = new QListWidget(central_widget_);
  238. list_areas_->setContextMenuPolicy(Qt::CustomContextMenu);
  239. list_images_widget_ = new QListWidget(central_widget_);
  240. list_images_widget_->setContextMenuPolicy(Qt::CustomContextMenu);
  241. label_toolbox_ = new QLabel(tr("Tool box"), frame_toolbox_);
  242. label_list_label_ = new QLabel(tr("Object labels:"), central_widget_);
  243. label_list_areas_ = new QLabel(tr("Selected areas:"), central_widget_);
  244. label_list_images_ = new QLabel(tr("Loaded images:"), central_widget_);
  245. /* buttons */
  246. button_bound_box_tool_ = new QPushButton(frame_toolbox_);
  247. button_bound_box_tool_->setText(tr("bbox"));
  248. button_bound_box_tool_->setEnabled(false);
  249. button_bound_box_tool_->setCheckable(true);
  250. button_polygon_tool_ = new QPushButton(frame_toolbox_);
  251. button_polygon_tool_->setText(tr("poly tool"));
  252. button_polygon_tool_->setEnabled(false);
  253. button_polygon_tool_->setCheckable(true);
  254. button_tagging_tool_ = new QPushButton(frame_toolbox_);
  255. button_tagging_tool_->setText(tr("tagging"));
  256. button_tagging_tool_->setEnabled(false);
  257. button_clear_selection_tool_ = new QPushButton(frame_toolbox_);
  258. button_clear_selection_tool_->setText(tr("clear selection"));
  259. button_clear_selection_tool_->setEnabled(false);
  260. button_generate_colors_ = new QPushButton(frame_toolbox_);
  261. button_generate_colors_->setText(tr("generate label colors"));
  262. button_generate_colors_->setEnabled(false);
  263. button_delete_all_labels_ = new QPushButton(frame_toolbox_);
  264. button_delete_all_labels_->setText(tr("delete all labels"));
  265. button_delete_all_labels_->setEnabled(false);
  266. group_tools_ = new QButtonGroup;
  267. group_tools_->addButton(button_bound_box_tool_);
  268. group_tools_->addButton(button_polygon_tool_);
  269. button_confirm_selection_ = new QPushButton(central_widget_);
  270. button_confirm_selection_->setText(tr("Confirm selection"));
  271. button_confirm_selection_->setEnabled(false);
  272. button_add_label_ = new QPushButton(frame_labelbox_);
  273. button_add_label_->setText(tr("Add label"));
  274. button_add_label_->setEnabled(false);
  275. button_remove_label_ = new QPushButton(frame_labelbox_);
  276. button_remove_label_->setText(tr("Remove label"));
  277. button_remove_label_->setEnabled(false);
  278. button_delete_area_ = new QPushButton(frame_labelbox_);
  279. button_delete_area_->setText(tr("Delete area"));
  280. button_delete_area_->setEnabled(false);
  281. button_change_area_ = new QPushButton(frame_labelbox_);
  282. button_change_area_->setText(tr("Change area"));
  283. button_change_area_->setEnabled(false);
  284. button_change_area_text_ = new QPushButton(frame_labelbox_);
  285. button_change_area_text_->setText(tr("Change area text"));
  286. button_change_area_text_->setEnabled(false);
  287. button_add_image_ = new QPushButton(frame_toolbox_);
  288. button_add_image_->setText(tr("Add"));
  289. button_remove_image_ = new QPushButton(frame_toolbox_);
  290. button_remove_image_->setText(tr("Remove"));
  291. button_remove_image_->setEnabled(false);
  292. button_prev_image_ = new QPushButton(central_widget_);
  293. button_prev_image_->setText("←");
  294. button_prev_image_->setAutoRepeat(true);
  295. button_next_image_ = new QPushButton(central_widget_);
  296. button_next_image_->setText("→");
  297. button_next_image_->setAutoRepeat(true);
  298. /*
  299. * layouts part begins
  300. */
  301. layout_main_ = new QHBoxLayout(central_widget_);
  302. layout_left_ = new QVBoxLayout();
  303. layout_toolbox_ = new QVBoxLayout();
  304. layout_imagelist_buttons_ = new QHBoxLayout();
  305. layout_center_ = new QVBoxLayout();
  306. layout_frame_image_ = new QVBoxLayout();
  307. layout_image_widget_ = new QGridLayout();
  308. layout_center_buttons_ = new QHBoxLayout();
  309. layout_right_ = new QVBoxLayout();
  310. layout_labelbox_ = new QVBoxLayout();
  311. layout_labelbox_buttons_ = new QHBoxLayout();
  312. layout_areabox_buttons_ = new QVBoxLayout();
  313. layout_main_->addLayout(layout_left_);
  314. layout_main_->addLayout(layout_center_);
  315. layout_main_->addLayout(layout_right_);
  316. /* making the center part stretchable */
  317. layout_main_->setStretch(1, 1);
  318. /* left part */
  319. layout_left_->addWidget(frame_toolbox_);
  320. frame_toolbox_->setLayout(layout_toolbox_);
  321. layout_toolbox_->addWidget(label_toolbox_);
  322. layout_toolbox_->addWidget(button_bound_box_tool_);
  323. layout_toolbox_->addWidget(button_polygon_tool_);
  324. layout_toolbox_->addSpacing(10);
  325. layout_toolbox_->addWidget(button_tagging_tool_);
  326. layout_toolbox_->addSpacing(10);
  327. layout_toolbox_->addWidget(button_clear_selection_tool_);
  328. layout_toolbox_->addWidget(button_delete_all_labels_);
  329. layout_toolbox_->addSpacing(10);
  330. layout_toolbox_->addWidget(button_generate_colors_);
  331. /* stretch is for making toolbox as small as it can be */
  332. layout_toolbox_->addSpacing(10);
  333. layout_left_->addWidget(label_list_images_);
  334. layout_left_->addLayout(layout_imagelist_buttons_);
  335. layout_imagelist_buttons_->addWidget(button_add_image_);
  336. layout_imagelist_buttons_->addWidget(button_remove_image_);
  337. layout_left_->addWidget(list_images_widget_);
  338. list_images_widget_->setFixedWidth(200);
  339. layout_left_->addStretch(1);
  340. layout_left_->addWidget(button_confirm_selection_);
  341. /* central part */
  342. layout_center_->addWidget(frame_center_);
  343. frame_center_->setLayout(layout_frame_image_);
  344. layout_frame_image_->setContentsMargins(0, 0, 0, 0);
  345. layout_frame_image_->addWidget(frame_image_);
  346. layout_frame_image_->addLayout(layout_center_buttons_);
  347. layout_center_buttons_->addWidget(button_prev_image_);
  348. layout_center_buttons_->addWidget(button_next_image_);
  349. /* right part */
  350. layout_right_->addWidget(frame_labelbox_);
  351. frame_labelbox_->setFixedWidth(200);
  352. frame_labelbox_->setLayout(layout_labelbox_);
  353. layout_labelbox_->addWidget(label_list_label_);
  354. layout_labelbox_->addLayout(layout_labelbox_buttons_);
  355. layout_labelbox_buttons_->addWidget(button_add_label_);
  356. layout_labelbox_buttons_->addWidget(button_remove_label_);
  357. layout_labelbox_->addWidget(list_label_);
  358. layout_labelbox_->addWidget(label_list_areas_);
  359. layout_labelbox_->addLayout(layout_areabox_buttons_);
  360. layout_areabox_buttons_->addWidget(button_delete_area_);
  361. layout_areabox_buttons_->addWidget(button_change_area_);
  362. layout_areabox_buttons_->addWidget(button_change_area_text_);
  363. layout_areabox_buttons_->setSpacing(0);
  364. layout_labelbox_->addWidget(list_areas_);
  365. connect(
  366. action_quit_,
  367. SIGNAL(triggered()),
  368. this,
  369. SLOT(close())
  370. );
  371. connect(
  372. action_open_images_,
  373. SIGNAL(triggered()),
  374. this,
  375. SLOT(loadImages())
  376. );
  377. connect(
  378. action_open_image_,
  379. SIGNAL(triggered()),
  380. this,
  381. SLOT(loadImage())
  382. );
  383. connect(
  384. action_open_labeled_image_,
  385. SIGNAL(triggered()),
  386. this,
  387. SLOT(loadInfo())
  388. );
  389. connect(
  390. action_load_legend_,
  391. SIGNAL(triggered()),
  392. this,
  393. SLOT(loadLegendFromFile())
  394. );
  395. connect(
  396. action_load_pascal_file_,
  397. SIGNAL(triggered()),
  398. this,
  399. SLOT(loadPascalFile())
  400. );
  401. connect(
  402. action_load_pascal_poly_,
  403. SIGNAL(triggered()),
  404. this,
  405. SLOT(loadPascalPolys())
  406. );
  407. connect(
  408. action_save_legend_,
  409. SIGNAL(triggered()),
  410. this,
  411. SLOT(saveLegend())
  412. );
  413. connect(
  414. action_save_segmented_,
  415. SIGNAL(triggered()),
  416. this,
  417. SLOT(saveSegmentedPicture())
  418. );
  419. connect(
  420. action_save_all_,
  421. SIGNAL(triggered()),
  422. this,
  423. SLOT(saveAllInfo())
  424. );
  425. connect(
  426. action_view_normal_,
  427. SIGNAL(triggered()),
  428. this,
  429. SLOT(viewNormal())
  430. );
  431. connect(
  432. action_view_segmented_,
  433. SIGNAL(triggered()),
  434. this,
  435. SLOT(viewSegmented())
  436. );
  437. connect(
  438. action_undo_,
  439. SIGNAL(triggered()),
  440. image_holder_,
  441. SLOT(undo())
  442. );
  443. connect(
  444. action_redo_,
  445. SIGNAL(triggered()),
  446. image_holder_,
  447. SLOT(redo())
  448. );
  449. connect(
  450. action_tagging_tool_,
  451. SIGNAL(triggered()),
  452. &line_edit_form_,
  453. SLOT(setTags())
  454. );
  455. connect(
  456. action_add_description_,
  457. SIGNAL(triggered()),
  458. &line_edit_form_,
  459. SLOT(setDescription())
  460. );
  461. connect(
  462. action_options_,
  463. SIGNAL(triggered()),
  464. &options_form_,
  465. SLOT(showOptions())
  466. );
  467. connect(
  468. button_add_label_,
  469. SIGNAL(clicked()),
  470. this,
  471. SLOT(addLabel())
  472. );
  473. connect(
  474. button_remove_label_,
  475. SIGNAL(clicked()),
  476. this,
  477. SLOT(removeLabel())
  478. );
  479. connect(
  480. button_delete_area_,
  481. SIGNAL(clicked()),
  482. this,
  483. SLOT(deleteArea())
  484. );
  485. connect(
  486. button_change_area_,
  487. SIGNAL(clicked()),
  488. this,
  489. SLOT(focusOnArea())
  490. );
  491. connect(
  492. button_change_area_text_,
  493. SIGNAL(clicked()),
  494. this,
  495. SLOT(editArea())
  496. );
  497. connect(
  498. button_next_image_,
  499. SIGNAL(clicked()),
  500. this,
  501. SLOT(nextImage())
  502. );
  503. connect(
  504. button_prev_image_,
  505. SIGNAL(clicked()),
  506. this,
  507. SLOT(prevImage())
  508. );
  509. connect(
  510. button_bound_box_tool_,
  511. SIGNAL(toggled(bool)),
  512. this,
  513. SLOT(setBoundingBoxTool(bool))
  514. );
  515. connect(
  516. button_polygon_tool_,
  517. SIGNAL(toggled(bool)),
  518. this,
  519. SLOT(setPolygonTool(bool))
  520. );
  521. connect(
  522. button_tagging_tool_,
  523. SIGNAL(clicked()),
  524. &line_edit_form_,
  525. SLOT(setTags())
  526. );
  527. connect(
  528. button_clear_selection_tool_,
  529. SIGNAL(clicked()),
  530. this,
  531. SLOT(clearAllTool())
  532. );
  533. connect(
  534. button_generate_colors_,
  535. SIGNAL(clicked()),
  536. this,
  537. SLOT(generateColors())
  538. );
  539. connect(
  540. button_delete_all_labels_,
  541. SIGNAL(clicked()),
  542. this,
  543. SLOT(clearLabelList())
  544. );
  545. connect(
  546. button_add_image_,
  547. SIGNAL(clicked()),
  548. this,
  549. SLOT(loadImage())
  550. );
  551. connect(
  552. button_remove_image_,
  553. SIGNAL(clicked()),
  554. this,
  555. SLOT(removeImage())
  556. );
  557. connect(
  558. button_confirm_selection_,
  559. SIGNAL(clicked()),
  560. this,
  561. SLOT(confirmSelection())
  562. );
  563. connect(
  564. list_label_,
  565. SIGNAL(itemChanged(QListWidgetItem *)),
  566. this,
  567. SLOT(editLabel(QListWidgetItem *))
  568. );
  569. connect(
  570. list_label_,
  571. SIGNAL(itemClicked(QListWidgetItem *)),
  572. this,
  573. SLOT(setLabelID(QListWidgetItem *))
  574. );
  575. connect(
  576. list_areas_,
  577. SIGNAL(itemDoubleClicked(QListWidgetItem *)),
  578. image_holder_,
  579. SLOT(focusOnArea(QListWidgetItem *))
  580. );
  581. connect(
  582. list_areas_,
  583. SIGNAL(customContextMenuRequested(const QPoint &)),
  584. this,
  585. SLOT(areaListPopupMenu(const QPoint &))
  586. );
  587. connect(
  588. list_areas_,
  589. SIGNAL(itemChanged(QListWidgetItem *)),
  590. this,
  591. SLOT(onAreaItemChange(QListWidgetItem *))
  592. );
  593. connect(
  594. list_label_,
  595. SIGNAL(customContextMenuRequested(const QPoint &)),
  596. this,
  597. SLOT(labelListPopupMenu(const QPoint &))
  598. );
  599. connect(
  600. list_images_widget_,
  601. SIGNAL(itemDoubleClicked(QListWidgetItem *)),
  602. this,
  603. SLOT(selectImage(QListWidgetItem *))
  604. );
  605. connect(
  606. list_images_widget_,
  607. SIGNAL(customContextMenuRequested(const QPoint &)),
  608. this,
  609. SLOT(imageListPopupMenu(const QPoint &))
  610. );
  611. connect(
  612. action_delete_area_,
  613. SIGNAL(triggered()),
  614. this,
  615. SLOT(deleteArea())
  616. );
  617. connect(
  618. action_edit_area_,
  619. SIGNAL(triggered()),
  620. this,
  621. SLOT(editArea())
  622. );
  623. connect(
  624. action_set_color_,
  625. SIGNAL(triggered()),
  626. this,
  627. SLOT(setLabelColor())
  628. );
  629. connect(
  630. action_toggle_priority_,
  631. SIGNAL(triggered()),
  632. this,
  633. SLOT(toggleLabelPriority())
  634. );
  635. connect(
  636. action_delete_label_,
  637. SIGNAL(triggered()),
  638. this,
  639. SLOT(removeLabel())
  640. );
  641. connect(
  642. action_remove_image_,
  643. SIGNAL(triggered()),
  644. this,
  645. SLOT(removeImage())
  646. );
  647. connect(
  648. image_holder_,
  649. SIGNAL(selectionStarted()),
  650. this,
  651. SLOT(onSelectionStarted())
  652. );
  653. connect(
  654. &line_edit_form_,
  655. SIGNAL(dataSet(QString)),
  656. this,
  657. SLOT(setDataFromForm(QString))
  658. );
  659. connect(
  660. image_holder_,
  661. SIGNAL(areaEdited()),
  662. this,
  663. SLOT(onAreaEdit())
  664. );
  665. QString settingsPath = aSettingsPath;
  666. if (settingsPath.isEmpty())
  667. settingsPath = QString("ImageLabeler.ini");
  668. settings_ = new QSettings(settingsPath, QSettings::IniFormat, this);
  669. readSettings(settings_);
  670. /* giving the pointers to some properties for image_holder_ */
  671. image_holder_->setBoundingBoxList(&list_bounding_box_);
  672. image_holder_->setPolygonList(&list_polygon_);
  673. image_holder_->setLabelColorList(&list_label_colors_);
  674. image_holder_->setMainLabelNum(&main_label_);
  675. image_holder_->setImage(image_);
  676. /* the same for options_form_ */
  677. options_form_.setPASCALpath(&PASCALpath_);
  678. options_form_.setAutoColorGeneration(&auto_color_generation_);
  679. }
  680. //! A destructor of the ImageLabeler class
  681. /*!
  682. * Simply frees all the allocated memory
  683. * in the right order
  684. */
  685. ImageLabeler::~ImageLabeler()
  686. {
  687. delete action_quit_;
  688. delete action_open_labeled_image_;
  689. delete action_open_image_;
  690. delete action_open_images_;
  691. delete action_load_legend_;
  692. delete action_load_pascal_file_;
  693. delete action_load_pascal_poly_;
  694. delete action_save_legend_;
  695. delete action_save_segmented_;
  696. delete action_save_all_;
  697. delete action_view_normal_;
  698. delete action_view_segmented_;
  699. delete action_undo_;
  700. delete action_redo_;
  701. delete action_bound_box_tool_;
  702. delete action_polygon_tool_;
  703. delete action_tagging_tool_;
  704. delete action_add_description_;
  705. delete action_options_;
  706. delete action_about_;
  707. delete action_help_content_;
  708. delete menu_load_;
  709. delete menu_save_;
  710. delete menu_pascal_;
  711. delete menu_file_;
  712. delete menu_view_;
  713. delete menu_edit_;
  714. delete menu_help_;
  715. delete menu_bar_;
  716. delete action_delete_area_;
  717. delete action_edit_area_;
  718. delete popup_area_list_;
  719. delete action_toggle_priority_;
  720. delete action_set_color_;
  721. delete action_delete_label_;
  722. delete popup_label_list_;
  723. delete action_remove_image_;
  724. delete popup_images_list_;
  725. delete image_;
  726. delete image_holder_;
  727. delete button_add_label_;
  728. delete button_remove_label_;
  729. delete button_bound_box_tool_;
  730. delete button_delete_area_;
  731. delete button_change_area_;
  732. delete button_change_area_text_;
  733. delete button_polygon_tool_;
  734. delete button_tagging_tool_;
  735. delete button_clear_selection_tool_;
  736. delete button_generate_colors_;
  737. delete button_delete_all_labels_;
  738. delete button_next_image_;
  739. delete button_prev_image_;
  740. delete button_confirm_selection_;
  741. delete label_list_label_;
  742. delete label_list_areas_;
  743. delete label_toolbox_;
  744. delete label_list_images_;
  745. delete list_areas_;
  746. delete list_label_;
  747. delete list_images_widget_;
  748. delete layout_imagelist_buttons_;
  749. delete layout_toolbox_;
  750. delete layout_right_;
  751. delete layout_center_buttons_;
  752. delete layout_frame_image_;
  753. delete layout_center_;
  754. delete layout_labelbox_buttons_;
  755. delete layout_areabox_buttons_;
  756. delete layout_labelbox_;
  757. delete layout_left_;
  758. delete layout_main_;
  759. delete frame_labelbox_;
  760. delete frame_toolbox_;
  761. delete frame_image_;
  762. delete frame_center_;
  763. delete central_widget_;
  764. if (pure_data_) {
  765. delete[] *pure_data_;
  766. delete pure_data_;
  767. }
  768. delete list_images_;
  769. delete settings_;
  770. }
  771. //! A member which reads the settings from file to RAM
  772. /*!
  773. * \see writeSettings(QSettings *aSettings)
  774. * \param[in] aSettings a pointer to the QSettings object
  775. * which should be previously created and set to the right
  776. * file with settings.
  777. */
  778. bool
  779. ImageLabeler::readSettings(QSettings *aSettings)
  780. {
  781. aSettings->beginGroup("/global");
  782. auto_color_generation_ =
  783. aSettings->value("/auto_label_color_generation", 0).toBool();
  784. options_form_.setAutoColorGeneration(&auto_color_generation_);
  785. PASCALpath_ = aSettings->value("/PASCAL_root_path", "").toString();
  786. aSettings->endGroup();
  787. return true;
  788. }
  789. //! A member which reads the settings from file to RAM
  790. /*!
  791. * \see readSettings(QSettings *aSettings)
  792. */
  793. void
  794. ImageLabeler::readSettings()
  795. {
  796. readSettings(settings_);
  797. }
  798. //! A member which writes settings from RAM to file
  799. /*!
  800. * \see readSettings(QSettings *aSettings)
  801. * \param[in] aSettings a pointer to the QSettings object
  802. * which should be previously created and set to the right
  803. * file with settings
  804. */
  805. bool
  806. ImageLabeler::writeSettings(QSettings *aSettings)
  807. {
  808. aSettings->beginGroup("/global");
  809. aSettings->setValue("/auto_label_color_generation", auto_color_generation_);
  810. aSettings->setValue("/PASCAL_root_path", PASCALpath_);
  811. aSettings->endGroup();
  812. return true;
  813. }
  814. //! A member which writes settings from RAM to file
  815. /*!
  816. * \see writeSettings(QSettings *aSettings)
  817. */
  818. void
  819. ImageLabeler::writeSettings()
  820. {
  821. writeSettings(settings_);
  822. }
  823. //! \brief A member taking a pointer to the Image struct and adding it to all necessary
  824. //! containers and widgets
  825. /*!
  826. * \param[in] anImage a pointer to the Image struct which should contain path to
  827. * the image, and two boolean flags:
  828. * labeled_ indicates whether it is labeled or not
  829. * pas_ indicates whether it was read from the PASCAL file or not
  830. */
  831. void
  832. ImageLabeler::addImage(Image *anImage)
  833. {
  834. QListWidgetItem *newItem = new QListWidgetItem;
  835. QString itemText = QString("%1: %2").
  836. arg(list_images_widget_->count()).
  837. arg(removePath(anImage->image_));
  838. if (anImage->labeled_)
  839. itemText.append(" #labeled");
  840. if (anImage->pas_)
  841. itemText.append(" #pas");
  842. newItem->setText(itemText);
  843. list_images_widget_->addItem(newItem);
  844. list_images_->append(*anImage);
  845. button_remove_image_->setEnabled(true);
  846. }
  847. //! A slot member creating new label with default parameters
  848. /*!
  849. * New label will be added to the list_label_ widget and
  850. * it's color will be added to the list_label_colors_ container.
  851. * By default the color of new label is white and it's name is
  852. * "New Label".
  853. * If there was no labels before and the new one get number 0 then
  854. * it becomes a background label width black color and name "BACKGROUND"
  855. */
  856. void
  857. ImageLabeler::addLabel()
  858. {
  859. QListWidgetItem *newItem = new QListWidgetItem;
  860. QString label;
  861. int itemNum = list_label_->count();
  862. label.append(QString("%1: ").arg(itemNum));
  863. if (itemNum) {
  864. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  865. label.append("New label");
  866. }
  867. /* label #0 is always BACKGROUND */
  868. else {
  869. newItem->setFlags(
  870. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  871. Qt::ItemIsEnabled
  872. );
  873. label.append("BACKGROUND");
  874. }
  875. newItem->setText(label);
  876. QPixmap iconPix = QPixmap(20, 20);
  877. QColor color;
  878. /* white by default */
  879. if (0 != itemNum)
  880. color = Qt::white;
  881. /* label #0 is always black */
  882. else
  883. color = Qt::black;
  884. iconPix.fill(color);
  885. QIcon icon(iconPix);
  886. list_label_colors_.append(color.rgb());
  887. newItem->setIcon(icon);
  888. list_label_->addItem(newItem);
  889. list_label_->setItemSelected(newItem, true);
  890. label_ID_ = list_label_->count() - 1;
  891. /* adding new label means changing file data */
  892. unsaved_data_ = 1;
  893. }
  894. /* TODO: label color could be specified also */
  895. //! A member creating new specified label
  896. /*!
  897. * \see addLabel()
  898. * \param[in] aLabelID an integer specifying label position(ID also) in the list
  899. * \param[in] isMain a bool flag indicates whether this label is main or not
  900. * \param[in] aLabel a QString object containing new label's name
  901. */
  902. void
  903. ImageLabeler::addLabel(
  904. int aLabelID,
  905. bool isMain,
  906. QString aLabel
  907. )
  908. {
  909. QListWidgetItem *newItem = new QListWidgetItem;
  910. /* label #0 is always BACKGROUND */
  911. if (0 == aLabelID) {
  912. aLabel = QString("BACKGROUND");
  913. newItem->setFlags(
  914. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  915. Qt::ItemIsEnabled
  916. );
  917. }
  918. QString label;
  919. label.append(QString("%1: %2").
  920. arg(aLabelID).
  921. arg(aLabel)
  922. );
  923. /* TODO: check if there is another main label and make it common */
  924. if (isMain) {
  925. main_label_ = aLabelID;
  926. }
  927. QPixmap iconPix = QPixmap(20, 20);
  928. QColor color;
  929. if (0 != aLabelID)
  930. color = Qt::white;
  931. else
  932. color = Qt::black;
  933. iconPix.fill(color);
  934. QIcon icon(iconPix);
  935. list_label_colors_.append(color.rgb());
  936. newItem->setIcon(icon);
  937. newItem->setText(label);
  938. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  939. list_label_->addItem(newItem);
  940. list_label_->setItemSelected(newItem, true);
  941. unsaved_data_ = 1;
  942. }
  943. //! A slot member being called after a label gets changed
  944. /*!
  945. * \param[in] anItem a pointer to the QListWidgetItem object
  946. * which contains a text of just edited label
  947. *
  948. * Checking label if it has a label ID and puting a mark #main
  949. * if it is so
  950. */
  951. void
  952. ImageLabeler::editLabel(QListWidgetItem *anItem)
  953. {
  954. QString label = anItem->text();
  955. int itemRow = list_label_->row(anItem);
  956. QString prefix = QString("%1: ").arg(itemRow);
  957. if (-1 == label.indexOf(prefix))
  958. label.prepend(QString("%1: ").arg(itemRow));
  959. if (main_label_ == itemRow) {
  960. label.append(" #main");
  961. }
  962. list_label_->blockSignals(true);
  963. anItem->setText(label);
  964. list_label_->blockSignals(false);
  965. unsaved_data_ = 1;
  966. }
  967. //! A slot member removing selected label
  968. /*!
  969. * \see removeLabel(int aLabelID)
  970. *
  971. * Simply removing selected label. Label no 0 can not be removed
  972. */
  973. void
  974. ImageLabeler::removeLabel()
  975. {
  976. removeLabel(label_ID_);
  977. }
  978. //! A slot member removing selected label
  979. /*!
  980. * \see removeLabel()
  981. * \param[in] aLabelID a integer specifying a label to remove
  982. *
  983. * Removing label number aLabelID. Label no 0 can not be removed
  984. */
  985. void
  986. ImageLabeler::removeLabel(int aLabelID)
  987. {
  988. /* we need to keep BACKGROUND category */
  989. if (0 == list_label_->count() || aLabelID < 1) {
  990. return;
  991. /* NOTREACHED */
  992. }
  993. if (list_label_->count() <= aLabelID ||
  994. list_label_colors_.count() <= aLabelID) {
  995. return;
  996. /* NOTREACHED */
  997. }
  998. list_label_->takeItem(aLabelID);
  999. list_label_colors_.takeAt(aLabelID);
  1000. unsaved_data_ = 1;
  1001. }
  1002. //! \brief a slot Member setting label_ID_ variable according to the selected row
  1003. //! in the list_label_ widget
  1004. /*!
  1005. * \param[in] anItem a pointer to the QListWidgetItem object which can be
  1006. * used to get current selected label in the list
  1007. *
  1008. * This slot just duplicates QListWidget::row() functionality to get
  1009. * previously selected label even if it is not focused.
  1010. */
  1011. void
  1012. ImageLabeler::setLabelID(
  1013. QListWidgetItem *anItem
  1014. )
  1015. {
  1016. if (!list_label_->count()) {
  1017. return;
  1018. /* NOTREACHED */
  1019. }
  1020. label_ID_ = list_label_->row(anItem);
  1021. }
  1022. //! A protected member adding a bbox area to the QListWidget list_areas_
  1023. /*!
  1024. * \param[in] anID a integer which indicates bbox id in the
  1025. * list_bounding_boxes_ container
  1026. * \param[in] aBBox a BoundingBox struct containing
  1027. * a rectangle data and it's label ID
  1028. * \param[in] itemID an integer indicates a position
  1029. * of a new item in the list_areas_
  1030. *
  1031. * itemID is -1 by default so the new area item will be placed in the
  1032. * end of the list
  1033. */
  1034. void
  1035. ImageLabeler::addBBoxArea(
  1036. int anID,
  1037. BoundingBox aBBox,
  1038. int itemID
  1039. )
  1040. {
  1041. QListWidgetItem *newItem = new QListWidgetItem;
  1042. if (itemID < 0)
  1043. itemID = list_areas_->count();
  1044. QString label;
  1045. label.append(QString("%1: ").arg(itemID));
  1046. label.append(QString("BBox #%1; ").arg(anID));
  1047. label.append(QString("LabelID: %1; ").arg(aBBox.label_ID_));
  1048. label.append(
  1049. QString("data:%1;%2;%3;%4; ").
  1050. arg(aBBox.rect.topLeft().x()).
  1051. arg(aBBox.rect.topLeft().y()).
  1052. arg(aBBox.rect.width()).
  1053. arg(aBBox.rect.height())
  1054. );
  1055. newItem->setText(label);
  1056. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  1057. list_areas_->insertItem(itemID, newItem);
  1058. list_areas_->setItemSelected(newItem, true);
  1059. }
  1060. //! A protected member adding a poly area to the QListWidget list_areas_
  1061. /*!
  1062. * \param[in] aPolyID a integer which indicates poly id in the
  1063. * list_polygon_ container
  1064. * \param[in] aPoly a Polygon struct containing
  1065. * a polygon data(QPolygon) and it's label ID
  1066. * \param[in] itemID an integer indicates a position
  1067. * of a new item in the list_areas_
  1068. *
  1069. * itemID is -1 by default so the new area item will be placed in the
  1070. * end of the list
  1071. */
  1072. void
  1073. ImageLabeler::addPolyArea(
  1074. int aPolyID,
  1075. Polygon aPoly,
  1076. int itemID
  1077. )
  1078. {
  1079. QListWidgetItem *newItem = new QListWidgetItem;
  1080. if (-1 == itemID)
  1081. itemID = list_areas_->count();
  1082. QString label;
  1083. label.append(QString("%1: ").arg(itemID));
  1084. label.append(QString("Poly #%1; ").arg(aPolyID));
  1085. label.append(QString("LabelID: %1; ").arg(aPoly.label_ID_));
  1086. label.append("points:");
  1087. for (int i = 0; i < aPoly.poly.count(); i++) {
  1088. label.append(
  1089. QString("%1;%2;").
  1090. arg(aPoly.poly.point(i).x()).
  1091. arg(aPoly.poly.point(i).y())
  1092. );
  1093. }
  1094. newItem->setText(label);
  1095. list_areas_->insertItem(itemID, newItem);
  1096. list_areas_->setItemSelected(newItem, true);
  1097. }
  1098. //! A slot member allowing manual editing the selected item in the list_areas_
  1099. /*!
  1100. * Slot being called after "change area" item in the popup menu of the list_areas_
  1101. * widget was clicked
  1102. */
  1103. void
  1104. ImageLabeler::editArea()
  1105. {
  1106. /* signals are blocked to avoid recursive call of the same slot */
  1107. bool oldState = list_areas_->blockSignals(true);
  1108. if (!list_areas_->currentItem() || !list_areas_->count()) {
  1109. return;
  1110. /* NOTREACHED */
  1111. }
  1112. QListWidgetItem *current = list_areas_->currentItem();
  1113. old_area_string_ = current->text();
  1114. current->setFlags(current->flags() | Qt::ItemIsEditable);
  1115. list_areas_->editItem(current);
  1116. list_areas_->blockSignals(oldState);
  1117. }
  1118. void
  1119. ImageLabeler::focusOnArea()
  1120. {
  1121. QListWidgetItem *current = list_areas_->currentItem();
  1122. if (!current || !list_areas_->count()) {
  1123. return;
  1124. /* NOTREACHED */
  1125. }
  1126. image_holder_->focusOnArea(current);
  1127. }
  1128. //! \brief A slot member parsing a text of the changed item
  1129. //! and tries to edit the corresponding area
  1130. /*!
  1131. * \see polyFromListItemText(QString *aString,int *oldID)
  1132. * \see BBoxFromListItemText(QString *aString,int *oldID)
  1133. * \param[in] anItem a pointer to the QListWidgetItem object which contains
  1134. * area's changed data
  1135. *
  1136. * Slot usually being called after item manual changing is done
  1137. */
  1138. void
  1139. ImageLabeler::onAreaItemChange(QListWidgetItem *anItem)
  1140. {
  1141. /* signals are blocked to avoid recursive call of the same slot */
  1142. list_areas_->blockSignals(true);
  1143. QString areaString = anItem->text();
  1144. int oldID = -1;
  1145. if (-1 != areaString.indexOf("Poly")) {
  1146. Polygon *poly = new Polygon;
  1147. *poly = polyFromListItemText(&areaString, &oldID);
  1148. if (-1 < poly->label_ID_ && !poly->poly.isEmpty() &&
  1149. -1 < oldID) {
  1150. list_polygon_.takeAt(oldID);
  1151. list_polygon_.insert(oldID, poly);
  1152. }
  1153. else
  1154. anItem->setText(old_area_string_);
  1155. }
  1156. else if (-1 != areaString.indexOf("BBox")) {
  1157. BoundingBox *bbox = new BoundingBox;
  1158. *bbox = BBoxFromListItemText(&areaString, &oldID);
  1159. if (-1 < bbox->label_ID_ && -1 < oldID) {
  1160. list_bounding_box_.takeAt(oldID);
  1161. list_bounding_box_.insert(oldID, bbox);
  1162. }
  1163. else
  1164. anItem->setText(old_area_string_);
  1165. }
  1166. else {
  1167. showWarning(tr("record format is corrupted, try again"));
  1168. anItem->setText(old_area_string_);
  1169. }
  1170. anItem->setFlags(anItem->flags() ^ Qt::ItemIsEditable);
  1171. list_areas_->blockSignals(false);
  1172. image_holder_->update();
  1173. unsaved_data_ = 1;
  1174. }
  1175. //! \brief A slot member changing the item in the list_areas_ to meet the changes
  1176. //! being made during area editing(where editing means moving the points of
  1177. //! an area)
  1178. /*!
  1179. * Slot usually being called on every change which was done with any area.
  1180. */
  1181. void
  1182. ImageLabeler::onAreaEdit()
  1183. {
  1184. if (!list_areas_->count() ||
  1185. -1 == image_holder_->focusedSelection()) {
  1186. //showWarning(tr("You haven't added any label"));
  1187. return;
  1188. /* NOTREACHED */
  1189. }
  1190. /* a figure was focused by double click on the list_areas_ widget
  1191. * see ImageHolder::focusOnArea(QListWidgetItem *) */
  1192. int figureID = image_holder_->focusedSelection();
  1193. Figure figure = image_holder_->focusedSelectionType();
  1194. /* looking for the corresponding item in the list_areas_ */
  1195. for (int i = 0; i < list_areas_->count(); i++) {
  1196. QListWidgetItem *item = list_areas_->item(i);
  1197. QString text = item->text();
  1198. if (RectFigure == figure && -1 != text.indexOf("BBox")) {
  1199. bool ok = 0;
  1200. int num = getNumFromString(&text, "BBox #", ";", &ok);
  1201. if (ok && num == figureID) {
  1202. list_areas_->takeItem(i);
  1203. addBBoxArea(num, *list_bounding_box_.at(num), i);
  1204. }
  1205. }
  1206. else if (PolyFigure == figure && -1 != text.indexOf("Poly")) {
  1207. bool ok = 0;
  1208. int num = getNumFromString(&text, "Poly #", ";", &ok);
  1209. if (ok && num == figureID) {
  1210. list_areas_->takeItem(i);
  1211. addPolyArea(num, *list_polygon_.at(num), i);
  1212. }
  1213. }
  1214. }
  1215. unsaved_data_ = 1;
  1216. }
  1217. //! \brief A slot member deleting selected area in the list_areas_ widget
  1218. //! and all other corresponding containers
  1219. /*!
  1220. * \see deleteArea(QListWidgetItem *anItem)
  1221. */
  1222. void
  1223. ImageLabeler::deleteArea()
  1224. {
  1225. deleteArea(list_areas_->currentItem());
  1226. /*if (!deleteArea(list_areas_->currentItem()))
  1227. showWarning(tr("An error occurred while deleting the area"));*/
  1228. }
  1229. //! A slot member deleting certain area in the list_areas_ widget
  1230. //! and all other corresponding containers
  1231. /*!
  1232. * \param[in] anItem a pointer to the QListWidgetItem which corresponds to
  1233. * some certain area(bbox or poly)
  1234. *
  1235. * Slot parses the text from anItem and deletes area from list_areas_ and
  1236. * from list_bounding_box_ or list_polygon depends on which type of area it
  1237. * was
  1238. *
  1239. * returns true on success
  1240. */
  1241. bool
  1242. ImageLabeler::deleteArea(QListWidgetItem *anItem)
  1243. {
  1244. if (!anItem || !list_areas_->count()) {
  1245. return false;
  1246. /* NOTREACHED */
  1247. }
  1248. QString text = anItem->text();
  1249. bool ok = 0;
  1250. /*
  1251. * 0 - bbox
  1252. * 1 - poly
  1253. */
  1254. QString shape;
  1255. if (-1 != text.indexOf("BBox"))
  1256. shape = QString("BBox");
  1257. else if (-1 != text.indexOf("Poly"))
  1258. shape = QString("Poly");
  1259. else {
  1260. return false;
  1261. /* NOTREACHED */
  1262. }
  1263. int areaNum = getNumFromString(&text, "#", ";", &ok);
  1264. if (!ok) {
  1265. return false;
  1266. /* NOTREACHED */
  1267. }
  1268. int currentItemRow = list_areas_->row(anItem);
  1269. /* changing all shapes(depends on current) which are next in the list */
  1270. for (int i = list_areas_->count() - 1; i > currentItemRow; i--) {
  1271. QListWidgetItem item = *(list_areas_->item(i));
  1272. QString newText = item.text();
  1273. if (-1 == newText.indexOf(shape))
  1274. continue;
  1275. int num = getNumFromString(&newText, "#", ";", &ok);
  1276. num--;
  1277. QString numString = QString("%1").arg(num);
  1278. int numPos = newText.indexOf("#") + 1;
  1279. newText.replace(numPos, numString.size(), numString);
  1280. list_areas_->takeItem(i);
  1281. list_areas_->insertItem(i, newText);
  1282. }
  1283. list_areas_->takeItem(currentItemRow);
  1284. if (shape == "BBox")
  1285. list_bounding_box_.removeAt(areaNum);
  1286. else
  1287. list_polygon_.removeAt(areaNum);
  1288. image_holder_->update();
  1289. unsaved_data_ = 1;
  1290. if (!list_areas_->count()) {
  1291. button_delete_area_->setEnabled(false);
  1292. button_change_area_->setEnabled(false);
  1293. button_change_area_text_->setEnabled(false);
  1294. }
  1295. return true;
  1296. }
  1297. //! A slot member toggling the priority of current item(main/not main)
  1298. /*!
  1299. * \see toggleLabelPriority(QListWidgetItem *anItem)
  1300. * Slot not just only toggles current label priority but also checks other labels
  1301. * if any of them are also main. If so it just makes them common(not main)
  1302. */
  1303. void
  1304. ImageLabeler::toggleLabelPriority()
  1305. {
  1306. if (!toggleLabelPriority(list_label_->currentItem()))
  1307. showWarning(tr("An error occurred while toggling a label priority"));
  1308. }
  1309. //! A slot member toggling the priority of current item(main/not main)
  1310. /*!
  1311. * \param[in] anItem a pointer to the QListWidgetItem object which corresponds to
  1312. * some certain label
  1313. *
  1314. * Slot not just only toggles current label priority but also checks other labels
  1315. * if any of the are also main. If so it just makes them common(not main)
  1316. *
  1317. * returns true on success
  1318. */
  1319. bool
  1320. ImageLabeler::toggleLabelPriority(QListWidgetItem *anItem)
  1321. {
  1322. if (!list_label_->count()) {
  1323. return false;
  1324. /* NOTREACHED */
  1325. }
  1326. /* because we need to keep BACKGROUND category */
  1327. if (0 == list_label_->row(anItem)) {
  1328. return false;
  1329. /* NOTREACHED */
  1330. }
  1331. int itemRow = list_label_->row(anItem);
  1332. QString text = anItem->text();
  1333. /* cleaning previous " #main" mark */
  1334. if (-1 != main_label_) {
  1335. QListWidgetItem *lastMain = list_label_->item(main_label_);
  1336. QString lastMainText = lastMain->text();
  1337. int mainPos = lastMainText.indexOf(" #main");
  1338. lastMainText = lastMainText.mid(0, mainPos);
  1339. list_label_->blockSignals(true);
  1340. lastMain->setText(lastMainText);
  1341. list_label_->blockSignals(false);
  1342. }
  1343. if (main_label_ == itemRow) {
  1344. int mainPos = text.indexOf(" #main");
  1345. text = text.mid(0, mainPos);
  1346. main_label_ = -1;
  1347. }
  1348. else {
  1349. text.append(" #main");
  1350. main_label_ = list_label_->row(anItem);
  1351. }
  1352. list_label_->blockSignals(true);
  1353. anItem->setText(text);
  1354. list_label_->blockSignals(false);
  1355. image_holder_->update();
  1356. unsaved_data_ = 1;
  1357. return true;
  1358. }
  1359. //! A protected member recursively getting all images from directory dir
  1360. /*!
  1361. * \param[in] dir a const QDir object indicating current directory
  1362. *
  1363. * Looking for images in the directory and adding them to the list at once
  1364. *
  1365. * Recursive search can be interrupted by interrupt_search flag being true;
  1366. *
  1367. * If there are any labeled images in the folder they will be loaded with all
  1368. * corresponding data.
  1369. */
  1370. void
  1371. ImageLabeler::getImagesFromDir(const QDir &dir)
  1372. {
  1373. /* avoiding freezing during recursive search for files */
  1374. QApplication::processEvents();
  1375. /* interrupting */
  1376. if (interrupt_search_) {
  1377. clearAll();
  1378. return;
  1379. }
  1380. QStringList filenameFilter;
  1381. /* *.dat are included also but only to indicate labeled images */
  1382. filenameFilter <<
  1383. "*.jpeg" <<
  1384. "*.jpg" <<
  1385. "*.gif" <<
  1386. "*.png" <<
  1387. "*.bmp" <<
  1388. "*.tiff" <<
  1389. "*.dat" <<
  1390. "*.pgm"
  1391. ;
  1392. QStringList listImages =
  1393. dir.entryList(filenameFilter, QDir::Files);
  1394. foreach (QString file, listImages) {
  1395. /* ignoring segmented images and .dat files */
  1396. if (file.contains("_segmented", Qt::CaseInsensitive) ||
  1397. file.contains(".dat", Qt::CaseInsensitive)) {
  1398. continue;
  1399. }
  1400. Image newImage;
  1401. newImage.image_ = dir.absoluteFilePath(file);
  1402. /* TODO: think about loading pascal files */
  1403. newImage.pas_ = 0;
  1404. /* checking if there is a data for current image */
  1405. QString labeled = alterFileName(file, "_labeled");
  1406. labeled = removePath(labeled);
  1407. labeled.append(".dat");
  1408. if (listImages.contains(labeled, Qt::CaseInsensitive))
  1409. newImage.labeled_ = 1;
  1410. else
  1411. newImage.labeled_ = 0;
  1412. addImage(&newImage);
  1413. }
  1414. /* recursively going into subdirectory */
  1415. QStringList listDir = dir.entryList(QDir::Dirs);
  1416. foreach (QString subdir, listDir) {
  1417. if ("." == subdir || ".." == subdir)
  1418. continue;
  1419. getImagesFromDir(QDir(dir.absoluteFilePath(subdir)));
  1420. }
  1421. }
  1422. //! \brief A slot member changing current image to the next one
  1423. //! and clearing all the data(except legend)
  1424. /*!
  1425. * Asks about unsaved data if there is any. If current picture was the
  1426. * last in the image list then it goes to the first one.
  1427. * Adds to filename of the current image to the window title
  1428. */
  1429. void
  1430. ImageLabeler::nextImage(int iImageStep)
  1431. {
  1432. if (list_images_->isEmpty()) {
  1433. return;
  1434. /* NOTREACHED */
  1435. }
  1436. if (!askForUnsavedData()) {
  1437. return;
  1438. /* NOTREACHED */
  1439. }
  1440. list_bounding_box_.clear();
  1441. list_polygon_.clear();
  1442. list_areas_->clear();
  1443. image_holder_->clearAll();
  1444. segmented_image_.clear();
  1445. //clearLabelColorList();
  1446. if (list_images_widget_->count() - iImageStep == image_ID_) {
  1447. image_ID_ = 0;
  1448. }
  1449. else {
  1450. image_ID_ += iImageStep;
  1451. }
  1452. if (!selectImage(image_ID_)) {
  1453. showWarning(tr("Next image is not available"));
  1454. return;
  1455. /* NOTREACHED */
  1456. }
  1457. list_images_widget_->setCurrentRow(image_ID_);
  1458. if (current_image_.isEmpty()) {
  1459. return;
  1460. /* NOTREACHED */
  1461. }
  1462. QString winTitle;
  1463. winTitle.append("ImageLabeler - ");
  1464. winTitle.append(current_image_);
  1465. setWindowTitle(winTitle);
  1466. image_->load(current_image_);
  1467. image_holder_->resize(image_->size());
  1468. image_holder_->setPixmap(*image_);
  1469. }
  1470. //! \brief A slot member changing current image to the previous one
  1471. //! and clearing all the data(except legend)
  1472. /*!
  1473. * Asks about unsaved data if there is any. If current picture was the
  1474. * first in the image list then it goes to the last one.
  1475. * Adds to filename of the current image to the window title
  1476. */
  1477. void
  1478. ImageLabeler::prevImage(int iImageStep)
  1479. {
  1480. if (!list_images_widget_->count()) {
  1481. return;
  1482. /* NOTREACHED */
  1483. }
  1484. if (!askForUnsavedData()) {
  1485. return;
  1486. /* NOTREACHED */
  1487. }
  1488. list_bounding_box_.clear();
  1489. list_polygon_.clear();
  1490. list_areas_->clear();
  1491. image_holder_->clearAll();
  1492. segmented_image_.clear();
  1493. //clearLabelColorList();
  1494. if (image_ID_ < iImageStep) {
  1495. image_ID_ = list_images_widget_->count() - 1;
  1496. }
  1497. else {
  1498. image_ID_ -= iImageStep;
  1499. }
  1500. list_images_widget_->setCurrentRow(image_ID_);
  1501. if (!selectImage(image_ID_)) {
  1502. showWarning(tr("Next image is not available"));
  1503. return;
  1504. /* NOTREACHED */
  1505. }
  1506. QString winTitle;
  1507. winTitle.append("ImageLabeler - ");
  1508. winTitle.append(current_image_);
  1509. setWindowTitle(winTitle);
  1510. image_->load(current_image_);
  1511. image_holder_->resize(image_->size());
  1512. image_holder_->setPixmap(*image_);
  1513. }
  1514. //! A Slot member saving all info about labeled image
  1515. /*!
  1516. * It saves a file in xml format which contains:
  1517. * - path to the original image
  1518. * - path to the segmented image(if any)
  1519. * - image description
  1520. * - tags
  1521. * - labels
  1522. * - objects data
  1523. * - segmented representation of the image as 2-dimensional array of label ids
  1524. */
  1525. void
  1526. ImageLabeler::saveAllInfo()
  1527. {
  1528. if (!list_images_widget_->count()) {
  1529. showWarning("You have not opened any image yet");
  1530. return;
  1531. /* NOTREACHED */
  1532. }
  1533. /* ------------------------------------------------------------------------
  1534. * XML part
  1535. */
  1536. QDomDocument doc(tr("ImageLabeler"));
  1537. QDomElement root = doc.createElement(tr("pixelwise_labeling"));
  1538. doc.appendChild(root);
  1539. QDomElement image = doc.createElement(tr("image"));
  1540. root.appendChild(image);
  1541. QDomText pathToImage = doc.createTextNode(current_image_);
  1542. image.appendChild(pathToImage);
  1543. if (!segmented_image_.isEmpty()) {
  1544. QDomElement segmentedImage = doc.createElement(tr("segmented"));
  1545. root.appendChild(segmentedImage);
  1546. QDomText pathToSegmented = doc.createTextNode(segmented_image_);
  1547. segmentedImage.appendChild(pathToSegmented);
  1548. }
  1549. QDomElement description = doc.createElement(tr("description"));
  1550. root.appendChild(description);
  1551. QDomText descriptionText = doc.createTextNode(image_description_);
  1552. description.appendChild(descriptionText);
  1553. QDomElement tags = doc.createElement(tr("tags"));
  1554. root.appendChild(tags);
  1555. QDomText tagsText = doc.createTextNode(tags_);
  1556. tags.appendChild(tagsText);
  1557. legendToXml(&doc, &root);
  1558. objectsToXml(&doc, &root);
  1559. /* image size */
  1560. QSize imageSize = image_->size();
  1561. QString imageSizeString;
  1562. imageSizeString.append(QString("%1;%2").
  1563. arg(imageSize.width()).
  1564. arg(imageSize.height()));
  1565. QDomElement imageSizeElement = doc.createElement(tr("image_size"));
  1566. QDomText imageSizeText = doc.createTextNode(imageSizeString);
  1567. imageSizeElement.appendChild(imageSizeText);
  1568. root.appendChild(imageSizeElement);
  1569. /* pure data */
  1570. setPureData();
  1571. QString pixelValues;
  1572. for (int i = 0; i < imageSize.height(); i++) {
  1573. for (int j = 0; j < imageSize.width(); j++) {
  1574. pixelValues.append(QString("%1;").arg(pure_data_[i][j]));
  1575. }
  1576. pixelValues.append("\n");
  1577. }
  1578. QDomElement pureData = doc.createElement(tr("pure_data"));
  1579. QDomText pureDataText = doc.createTextNode(pixelValues);
  1580. pureData.appendChild(pureDataText);
  1581. root.appendChild(pureData);
  1582. QString xml = doc.toString();
  1583. /* ------------------------------------------------------------------------
  1584. * XML part ends
  1585. */
  1586. QFileDialog fileDialog(0, tr("Save all info"));
  1587. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1588. fileDialog.setDefaultSuffix("dat");
  1589. fileDialog.setFileMode(QFileDialog::AnyFile);
  1590. QString dir = getPathFromFilename(current_image_);
  1591. /* altering the name of a new file */
  1592. QString newFileName = alterFileName(current_image_, "_labeled");
  1593. newFileName = removePath(newFileName);
  1594. fileDialog.selectFile(newFileName);
  1595. fileDialog.setDirectory(dir);
  1596. QString filename;
  1597. if (fileDialog.exec()) {
  1598. filename = fileDialog.selectedFiles().last();
  1599. }
  1600. else {
  1601. //showWarning(tr("Can not open file dialog"));
  1602. return;
  1603. /* NOTREACHED */
  1604. }
  1605. if (filename.isEmpty()) {
  1606. return;
  1607. /* NOTREACHED */
  1608. }
  1609. QFile file(filename);
  1610. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1611. showWarning(tr("Can not open file for writing"));
  1612. return;
  1613. /* NOTREACHED */
  1614. }
  1615. file.write(xml.toLocal8Bit());
  1616. file.close();
  1617. unsaved_data_ = 0;
  1618. }
  1619. //! A slot member saving a segmented image from pure_data_ array
  1620. /*!
  1621. * \see setPureData()
  1622. *
  1623. * If label colors are not set it asks user about automatic color generation.
  1624. * New image format is .png
  1625. */
  1626. void
  1627. ImageLabeler::saveSegmentedPicture()
  1628. {
  1629. if (list_bounding_box_.isEmpty() && list_polygon_.isEmpty()) {
  1630. return;
  1631. /* NOTREACHED */
  1632. }
  1633. setPureData();
  1634. QFileDialog fileDialog(0, tr("Save segmented picture"));
  1635. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1636. fileDialog.setDefaultSuffix("png");
  1637. fileDialog.setFileMode(QFileDialog::AnyFile);
  1638. QString dir = getPathFromFilename(current_image_);
  1639. /* altering the name of a new file */
  1640. QString newFileName = alterFileName(current_image_, "_segmented");
  1641. newFileName = removePath(newFileName);
  1642. fileDialog.selectFile(newFileName);
  1643. fileDialog.setDirectory(dir);
  1644. QString filename;
  1645. if (fileDialog.exec()) {
  1646. filename = fileDialog.selectedFiles().last();
  1647. }
  1648. else {
  1649. return;
  1650. /* NOTREACHED */
  1651. }
  1652. QSize imageSize = image_holder_->pixmap()->size();
  1653. QImage newImage(imageSize, QImage::Format_RGB32);
  1654. bool generateColorsFlag = auto_color_generation_;
  1655. bool flag = 0;
  1656. /* checking if all the colors are are different from white(default) */
  1657. if (!generateColorsFlag) {
  1658. for (int i = 1; i < list_label_->count(); i++) {
  1659. if (list_label_colors_.at(i) == 0xffffffff && !flag)
  1660. flag = 1;
  1661. else if (list_label_colors_.at(i) == 0xffffffff && flag) {
  1662. QMessageBox msgBox;
  1663. msgBox.setText(tr("There are few labels with default white color."));
  1664. msgBox.setInformativeText(tr("Do you want to generate all colors automatically? Otherwise you'll have to do it manually."));
  1665. msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  1666. msgBox.setDefaultButton(QMessageBox::Yes);
  1667. msgBox.setIcon(QMessageBox::Question);
  1668. int ret = msgBox.exec();
  1669. if (QMessageBox::Yes == ret) {
  1670. generateColorsFlag = 1;
  1671. break;
  1672. }
  1673. else {
  1674. return;
  1675. /* NOTREACHED */
  1676. }
  1677. }
  1678. }
  1679. }
  1680. /* generating colors for labels */
  1681. if (list_label_colors_.count() < list_label_->count() ||
  1682. generateColorsFlag) {
  1683. generateColors();
  1684. }
  1685. for (int i = 0; i < imageSize.height(); i++)
  1686. for (int j = 0; j < imageSize.width(); j++) {
  1687. newImage.setPixel(j, i, list_label_colors_.at(pure_data_[i][j]));
  1688. }
  1689. if (!newImage.save(filename, "png", 100)) {
  1690. showWarning(tr("An error occurred while saving the segmented image"));
  1691. return;
  1692. /* NOTREACHED */
  1693. }
  1694. segmented_image_ = filename;
  1695. action_view_segmented_->setEnabled(true);
  1696. }
  1697. //! A slot member saving only labels(legend) to the separate xml file
  1698. /*!
  1699. * \see legendToXml(QDomDocument *aDoc, QDomElement *aRoot)
  1700. *
  1701. * Slot asks user where to save a file with the legend and saves it.
  1702. */
  1703. void
  1704. ImageLabeler::saveLegend()
  1705. {
  1706. if (!list_label_->count()) {
  1707. showWarning("You have not added any label yet");
  1708. return;
  1709. /* NOTREACHED */
  1710. }
  1711. /* ------------------------------------------------------------------------
  1712. * XML part
  1713. */
  1714. QDomDocument doc(tr("ImageLabeler"));
  1715. QDomElement root = doc.createElement(tr("root"));
  1716. doc.appendChild(root);
  1717. legendToXml(&doc, &root);
  1718. QString xml = doc.toString();
  1719. /* ------------------------------------------------------------------------
  1720. * XML part ends
  1721. */
  1722. QFileDialog fileDialog(0, tr("Save legend"));
  1723. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1724. fileDialog.setDefaultSuffix("dat");
  1725. fileDialog.setFileMode(QFileDialog::AnyFile);
  1726. QString dir = getPathFromFilename(current_image_);
  1727. /* altering the name of a new file */
  1728. QString newFileName = alterFileName(current_image_, "_legend");
  1729. newFileName = removePath(newFileName);
  1730. fileDialog.selectFile(newFileName);
  1731. fileDialog.setDirectory(dir);
  1732. QString filename;
  1733. if (fileDialog.exec()) {
  1734. filename = fileDialog.selectedFiles().last();
  1735. }
  1736. else {
  1737. //showWarning(tr("Can not open file dialog"));
  1738. return;
  1739. /* NOTREACHED */
  1740. }
  1741. if (filename.isEmpty()) {
  1742. return;
  1743. /* NOTREACHED */
  1744. }
  1745. QFile file(filename);
  1746. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1747. showWarning(tr("Can not open file for writing"));
  1748. return;
  1749. /* NOTREACHED */
  1750. }
  1751. file.write(xml.toLocal8Bit());
  1752. file.close();
  1753. }
  1754. //! A slot member loading labeled image from formatted xml file.
  1755. /*!
  1756. * \see loadInfo(QString filename)
  1757. */
  1758. void
  1759. ImageLabeler::loadInfo()
  1760. {
  1761. if (!askForUnsavedData()) {
  1762. return;
  1763. /* NOTREACHED */
  1764. }
  1765. QFileDialog fileDialog(0, tr("Load file with info"));
  1766. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1767. fileDialog.setDefaultSuffix("dat");
  1768. fileDialog.setFileMode(QFileDialog::AnyFile);
  1769. QString filename;
  1770. if (fileDialog.exec()) {
  1771. filename = fileDialog.selectedFiles().last();
  1772. }
  1773. else {
  1774. //showWarning(tr("Can not open file dialog"));
  1775. return;
  1776. /* NOTREACHED */
  1777. }
  1778. clearAllTool();
  1779. clearLabelList();
  1780. if (loadInfo(filename)) {
  1781. enableTools();
  1782. Image newImage;
  1783. newImage.image_ = current_image_;
  1784. newImage.labeled_ = 1;
  1785. newImage.pas_ = 0;
  1786. addImage(&newImage);
  1787. image_ID_ = list_images_widget_->count() - 1;
  1788. list_images_widget_->setCurrentRow(image_ID_);
  1789. }
  1790. unsaved_data_ = 0;
  1791. }
  1792. //! A slot member loading labeled image from formatted xml file.
  1793. /*!
  1794. * \param[in] filename a QString object containing a path to the file
  1795. * we need to load data from
  1796. *
  1797. * \see loadLegendFromNode(QDomElement *)
  1798. * \see addBBoxFromData(QString *aBBoxData, int *ID)
  1799. * \see addPolyFromData(QString *aPolyData, int *labelID)
  1800. */
  1801. bool
  1802. ImageLabeler::loadInfo(QString filename)
  1803. {
  1804. QDomDocument doc("Image Labeler");
  1805. QFile file(filename);
  1806. if (!file.open(QIODevice::ReadOnly)) {
  1807. showWarning(tr("Can not open such file"));
  1808. return false;
  1809. /* NOTREACHED */
  1810. }
  1811. QString errMsg;
  1812. if (!doc.setContent(&file, &errMsg)) {
  1813. showWarning(errMsg);
  1814. file.close();
  1815. return false;
  1816. /* NOTREACHED */
  1817. }
  1818. file.close();
  1819. /* getting all info */
  1820. QDomElement elements = doc.documentElement();
  1821. QDomNode rootNode = elements.firstChild();
  1822. QString string;
  1823. while(!rootNode.isNull()) {
  1824. QDomElement element = rootNode.toElement();
  1825. if(!element.isNull()) {
  1826. /* path to the image */
  1827. if (element.tagName() == "image") {
  1828. //qDebug() << "image";
  1829. string = element.text();
  1830. if (string.isEmpty()) {
  1831. showWarning(
  1832. tr(
  1833. "The file with data doesn't contain path to the image"
  1834. )
  1835. );
  1836. return false;
  1837. /* NOTREACHED */
  1838. }
  1839. QString sImageStr = string;
  1840. if( QFileInfo(string).isRelative() )
  1841. {
  1842. sImageStr = QFileInfo(filename).path() + "/" + string;
  1843. }
  1844. if (!image_->load( sImageStr )) {
  1845. return false;
  1846. /* NOTREACHED */
  1847. }
  1848. current_image_ = string;
  1849. QString winTitle;
  1850. winTitle.append("ImageLabeler - ");
  1851. winTitle.append(current_image_);
  1852. setWindowTitle(winTitle);
  1853. image_holder_->resize(image_->size());
  1854. image_holder_->setPixmap(*image_);
  1855. }
  1856. /* path to the segmented image */
  1857. if (element.tagName() == "segmented") {
  1858. //qDebug() << "segmented";
  1859. string = element.text();
  1860. if ( !string.isEmpty()) {
  1861. segmented_image_ = string;
  1862. action_view_segmented_->setEnabled(true);
  1863. }
  1864. }
  1865. /* image description */
  1866. else if (element.tagName() == "description" &&
  1867. !element.text().isEmpty()) {
  1868. //qDebug() << "description";
  1869. image_description_ = element.text();
  1870. }
  1871. /* tags */
  1872. else if (element.tagName() == "tags" &&
  1873. !element.text().isEmpty()) {
  1874. //qDebug() << "tags";
  1875. tags_ = element.text();
  1876. }
  1877. /* legend */
  1878. else if (element.tagName() == "legend") {
  1879. //qDebug() << "legend";
  1880. list_label_->clear();
  1881. loadLegendFromNode(&element);
  1882. }
  1883. /* objects */
  1884. else if (element.tagName() == "objects") {
  1885. //qDebug() << "objects";
  1886. QDomNode subNode = element.firstChild();
  1887. QDomElement subElement;
  1888. button_delete_area_->setEnabled(false);
  1889. button_change_area_->setEnabled(false);
  1890. button_change_area_text_->setEnabled(false);
  1891. while(!subNode.isNull()) {
  1892. subElement = subNode.toElement();
  1893. if (subElement.isNull() || subElement.text().isEmpty()) {
  1894. subNode = subNode.nextSibling();
  1895. continue;
  1896. }
  1897. string = subElement.attribute("id");
  1898. bool ok = 1;
  1899. int id = string.toInt(&ok, 10);
  1900. if (!ok) {
  1901. qDebug() <<
  1902. "loadInfo: "
  1903. "poly id format is corrupted";
  1904. subNode = subNode.nextSibling();
  1905. continue;
  1906. }
  1907. string = subElement.text();
  1908. if (subElement.tagName() == "bbox")
  1909. addBBoxFromData(&string, &id);
  1910. else if (subElement.tagName() == "poly")
  1911. addPolyFromData(&string, &id);
  1912. else {
  1913. qDebug() <<
  1914. "loadInfo: "
  1915. "poly id format is corrupted";
  1916. subNode = subNode.nextSibling();
  1917. continue;
  1918. }
  1919. button_delete_area_->setEnabled(true);
  1920. button_change_area_->setEnabled(true);
  1921. button_change_area_text_->setEnabled(true);
  1922. subNode = subNode.nextSibling();
  1923. }
  1924. }
  1925. }
  1926. rootNode = rootNode.nextSibling();
  1927. }
  1928. qDebug() << "image info loaded for "<<filename;
  1929. unsaved_data_ = 0;
  1930. return true;
  1931. }
  1932. //! A slot member loading info about labeled image from PASCAL file(xml)
  1933. /*!
  1934. * \see loadPascalFile(QString aFilename, QString aPath)
  1935. *
  1936. * Slot asks user to set default "root" directory for the PASCAL files if
  1937. * it wan't set before
  1938. */
  1939. void
  1940. ImageLabeler::loadPascalFile()
  1941. {
  1942. if (!askForUnsavedData()) {
  1943. return;
  1944. /* NOTREACHED */
  1945. }
  1946. if (PASCALpath_.isEmpty()) {
  1947. showWarning(tr("before opening first PASCAL file please choose \"root\" directory"
  1948. " where a folder with segmentations,"
  1949. " a folder with polygons,"
  1950. " a folder with image descriptions and"
  1951. " a folder with the pure images are."));
  1952. QFileDialog fileDialog(0, tr("root directory for the PASCAL files"));
  1953. fileDialog.setFileMode(QFileDialog::Directory);
  1954. if (fileDialog.exec())
  1955. PASCALpath_ = fileDialog.selectedFiles().last();
  1956. else {
  1957. return;
  1958. /* NOTREACHED */
  1959. }
  1960. }
  1961. QFileDialog fileDialog(0, tr("Load pascal file"));
  1962. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1963. fileDialog.setDefaultSuffix("xml");
  1964. fileDialog.setFileMode(QFileDialog::AnyFile);
  1965. fileDialog.setDirectory(PASCALpath_);
  1966. QStringList filters;
  1967. filters << "PASCAL xml data (*.xml)"
  1968. << "Any files (*)";
  1969. fileDialog.setNameFilters(filters);
  1970. QString filename;
  1971. if (fileDialog.exec()) {
  1972. filename = fileDialog.selectedFiles().last();
  1973. }
  1974. else {
  1975. //showWarning(tr("Can not open file dialog"));
  1976. return;
  1977. /* NOTREACHED */
  1978. }
  1979. clearAllTool();
  1980. clearLabelList();
  1981. if (loadPascalFile(filename, PASCALpath_)) {
  1982. enableTools();
  1983. Image newImage;
  1984. newImage.image_ = current_image_;
  1985. newImage.labeled_ = 1;
  1986. newImage.pas_ = 1;
  1987. addImage(&newImage);
  1988. image_ID_ = list_images_widget_->count() - 1;
  1989. list_images_widget_->setCurrentRow(image_ID_);
  1990. }
  1991. unsaved_data_ = 0;
  1992. }
  1993. //! A slot member loading info about labeled image from PASCAL file(xml)
  1994. /*!
  1995. * Slot parses xml file and gets all needed info out of it
  1996. */
  1997. bool
  1998. ImageLabeler::loadPascalFile(QString aFilename, QString aPath)
  1999. {
  2000. QDomDocument doc;
  2001. QFile file(aFilename);
  2002. if (!file.open(QIODevice::ReadOnly)) {
  2003. showWarning(tr("Can not open such file"));
  2004. return false;
  2005. /* NOTREACHED */
  2006. }
  2007. QString errMsg;
  2008. if (!doc.setContent(&file, &errMsg)) {
  2009. showWarning(errMsg);
  2010. file.close();
  2011. return false;
  2012. /* NOTREACHED */
  2013. }
  2014. file.close();
  2015. //clearAll();
  2016. //enableTools();
  2017. /* getting all info */
  2018. QDomElement elements = doc.documentElement();
  2019. QDomNode rootNode = elements.firstChild();
  2020. QString string;
  2021. QString path;
  2022. if (aPath.isEmpty())
  2023. path = getPathFromFilename(aFilename);
  2024. else
  2025. path = aPath + "/";
  2026. QString filename;
  2027. QStringList labels;
  2028. labels << "BACKGROUND";
  2029. int labelID;
  2030. while(!rootNode.isNull()) {
  2031. QDomElement element = rootNode.toElement();
  2032. if(!element.isNull()) {
  2033. /* folder */
  2034. if (element.tagName() == "folder") {
  2035. string = element.text();
  2036. if (!string.isEmpty()) {
  2037. path.append(string);
  2038. }
  2039. }
  2040. /* filename */
  2041. else if (element.tagName() == "filename") {
  2042. string = element.text();
  2043. if (!string.isEmpty()) {
  2044. filename = string;
  2045. }
  2046. }
  2047. /* object */
  2048. else if (element.tagName() == "object") {
  2049. QDomNode subNode = element.firstChild();
  2050. QDomElement subElement;
  2051. while(!subNode.isNull()) {
  2052. subElement = subNode.toElement();
  2053. if (subElement.isNull() || subElement.text().isEmpty()) {
  2054. subNode = subNode.nextSibling();
  2055. continue;
  2056. }
  2057. /* label */
  2058. if (subElement.tagName() == "name") {
  2059. string = subElement.text();
  2060. if (!string.isEmpty() &&
  2061. !labels.contains(string, Qt::CaseInsensitive))
  2062. {
  2063. addLabel(list_label_->count(), 0, string);
  2064. labelID = labels.count();
  2065. labels << string;
  2066. }
  2067. else if (labels.contains(string, Qt::CaseInsensitive)) {
  2068. for (int i = 0; i < labels.count(); i++) {
  2069. if (labels.at(i) == string)
  2070. labelID = i;
  2071. }
  2072. }
  2073. }
  2074. if (subElement.tagName() == "bndbox") {
  2075. /* 2 points */
  2076. QPoint topLeft;
  2077. QPoint bottomRight;
  2078. QDomNode bboxNode = subElement.firstChild();
  2079. QDomElement bboxElement;
  2080. while(!bboxNode.isNull()) {
  2081. bboxElement = bboxNode.toElement();
  2082. string.clear();
  2083. bool ok = 1;
  2084. if (bboxElement.tagName() == "xmin") {
  2085. string = bboxElement.text();
  2086. if (string.isEmpty())
  2087. ok = 0;
  2088. double xmin = string.toDouble(&ok);
  2089. if (ok)
  2090. topLeft.setX(qRound(xmin));
  2091. }
  2092. else if (bboxElement.tagName() == "ymin") {
  2093. string = bboxElement.text();
  2094. if (string.isEmpty())
  2095. ok = 0;
  2096. double ymin = string.toDouble(&ok);
  2097. if (ok)
  2098. topLeft.setY(qRound(ymin));
  2099. }
  2100. else if (bboxElement.tagName() == "xmax") {
  2101. string = bboxElement.text();
  2102. if (string.isEmpty())
  2103. ok = 0;
  2104. double xmax = string.toDouble(&ok);
  2105. if (ok)
  2106. bottomRight.setX(qRound(xmax));
  2107. }
  2108. else if (bboxElement.tagName() == "ymax") {
  2109. string = bboxElement.text();
  2110. if (string.isEmpty())
  2111. ok = 0;
  2112. double ymax = string.toDouble(&ok);
  2113. if (ok)
  2114. bottomRight.setY(qRound(ymax));
  2115. }
  2116. if (string.isEmpty() || !ok)
  2117. break;
  2118. bboxNode = bboxNode.nextSibling();
  2119. if (bboxNode.isNull()) {
  2120. BoundingBox *bbox = new BoundingBox;
  2121. bbox->rect.setTopLeft(topLeft);
  2122. bbox->rect.setBottomRight(bottomRight);
  2123. bbox->label_ID_ = labelID;
  2124. addBBox(bbox);
  2125. }
  2126. }
  2127. }
  2128. subNode = subNode.nextSibling();
  2129. }
  2130. }
  2131. }
  2132. rootNode = rootNode.nextSibling();
  2133. }
  2134. if (!image_->load(path + "/JPEGImages/" + filename)) {
  2135. return false;
  2136. /* NOTREACHED */
  2137. }
  2138. current_image_ = path + "/JPEGImages/" + filename;
  2139. QString winTitle;
  2140. winTitle.append("ImageLabeler - ");
  2141. winTitle.append(current_image_);
  2142. setWindowTitle(winTitle);
  2143. image_holder_->resize(image_->size());
  2144. image_holder_->setPixmap(*image_);
  2145. unsaved_data_ = 0;
  2146. return true;
  2147. }
  2148. //! A slot member loading only information about polygons on the image
  2149. /*!
  2150. * \see loadPascalPolys(QString aFilename)
  2151. * Slot parses text file with .polygon suffix
  2152. */
  2153. void
  2154. ImageLabeler::loadPascalPolys()
  2155. {
  2156. if (current_image_.isEmpty()) {
  2157. return;
  2158. /* NOTREACHED */
  2159. }
  2160. QFileDialog fileDialog(0, tr("Load pascal polygons"));
  2161. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  2162. fileDialog.setDefaultSuffix("polygon");
  2163. fileDialog.setFileMode(QFileDialog::AnyFile);
  2164. QStringList filters;
  2165. filters << "PASCAL polygons (*.polygon)"
  2166. << "Any files (*)";
  2167. fileDialog.setNameFilters(filters);
  2168. QString filename;
  2169. if (fileDialog.exec()) {
  2170. filename = fileDialog.selectedFiles().last();
  2171. }
  2172. else {
  2173. //showWarning(tr("Can not open file dialog"));
  2174. return;
  2175. /* NOTREACHED */
  2176. }
  2177. //clearAllTool();
  2178. //clearLabelList();
  2179. if (!loadPascalPolys(filename)) {
  2180. showWarning(tr("File format is corrupted."));
  2181. }
  2182. unsaved_data_ = 0;
  2183. image_holder_->update();
  2184. }
  2185. //! A slot member loading only information about polygons on the image
  2186. /*!
  2187. * Slot parses text file with .polygon suffix
  2188. *
  2189. * returns true on success
  2190. */
  2191. bool
  2192. ImageLabeler::loadPascalPolys(QString aFilename)
  2193. {
  2194. QFile file(aFilename);
  2195. if (!file.open(QIODevice::ReadOnly)) {
  2196. showWarning(tr("Can not open such file"));
  2197. return false;
  2198. /* NOTREACHED */
  2199. }
  2200. QByteArray data = file.readAll();
  2201. file.close();
  2202. QString label;
  2203. int lastSpace = 0;
  2204. int pointCount = 0;
  2205. Polygon *poly = 0;
  2206. QPoint point;
  2207. bool evenFlag = 0;
  2208. //for (int i = 0; i < data.length(); i++) {
  2209. int i = 0;
  2210. while (i < data.length()) {
  2211. if (data.at(i) == ' ' && label.isEmpty() && !pointCount) {
  2212. label = QString(data.mid(lastSpace, i - lastSpace));
  2213. lastSpace = i + 1;
  2214. }
  2215. else if (data.at(i) == ' ' && !label.isEmpty() && !pointCount) {
  2216. QString num = QString(data.mid(lastSpace, i - lastSpace));
  2217. bool ok = 1;
  2218. pointCount = num.toInt(&ok, 10);
  2219. if (!ok) {
  2220. return false;
  2221. /* NOTREACHED */
  2222. }
  2223. poly = new Polygon;
  2224. poly->label_ID_ = -1;
  2225. lastSpace = i + 1;
  2226. }
  2227. else if (data.at(i) == ' ' &&
  2228. !label.isEmpty() &&
  2229. pointCount &&
  2230. !evenFlag)
  2231. {
  2232. evenFlag = 1;
  2233. QString num = QString(data.mid(lastSpace, i - lastSpace));
  2234. bool ok = 1;
  2235. int coor = qRound(num.toDouble(&ok));
  2236. if (!ok) {
  2237. return false;
  2238. /* NOTREACHED */
  2239. }
  2240. point.setX(coor);
  2241. lastSpace = i + 1;
  2242. }
  2243. else if (data.at(i) == ' ' &&
  2244. !label.isEmpty() &&
  2245. pointCount &&
  2246. evenFlag)
  2247. {
  2248. evenFlag = 0;
  2249. pointCount--;
  2250. QString num = QString(data.mid(lastSpace, i - lastSpace));
  2251. bool ok = 1;
  2252. int coor = qRound(num.toDouble(&ok));
  2253. if (!ok) {
  2254. return false;
  2255. /* NOTREACHED */
  2256. }
  2257. point.setY(coor);
  2258. lastSpace = i + 1;
  2259. poly->poly << point;
  2260. }
  2261. else if (data.at(i) == '\n') {
  2262. if (pointCount || !poly) {
  2263. return false;
  2264. /* NOTREACHED */
  2265. }
  2266. lastSpace = i + 1;
  2267. for (int j = 0; j < list_label_->count(); j++) {
  2268. if (list_label_->item(j)->text().contains(label, Qt::CaseInsensitive)) {
  2269. poly->label_ID_ = j;
  2270. }
  2271. }
  2272. if (-1 == poly->label_ID_) {
  2273. int labelID = list_label_->count();
  2274. addLabel(labelID, 0, label);
  2275. poly->label_ID_ = labelID;
  2276. }
  2277. addPoly(poly);
  2278. label.clear();
  2279. }
  2280. i++;
  2281. }
  2282. return true;
  2283. }
  2284. //! A Slot member loading single image.
  2285. /*!
  2286. * \see loadImages()
  2287. *
  2288. * Slot asks for unsaved data and loads an image.
  2289. * If selected image was previously labeled and the file with data
  2290. * was named like imagename_labeled.dat then it would be loaded
  2291. * with all data
  2292. */
  2293. void
  2294. ImageLabeler::loadImage()
  2295. {
  2296. if (!askForUnsavedData()) {
  2297. return;
  2298. /* NOTREACHED */
  2299. }
  2300. QFileDialog fileDialog(0, tr("Load image"));
  2301. fileDialog.setFileMode(QFileDialog::ExistingFile);
  2302. QStringList filters;
  2303. filters << "Image files (*.jpeg *.jpg *.gif *.png *.bmp *.tiff *.pgm)"
  2304. << "Any files (*)";
  2305. fileDialog.setNameFilters(filters);
  2306. QString filename;
  2307. if (fileDialog.exec()) {
  2308. filename = fileDialog.selectedFiles().last();
  2309. }
  2310. else {
  2311. //showWarning(tr("Could not open file dialog"));
  2312. return;
  2313. /* NOTREACHED */
  2314. }
  2315. if (filename.isEmpty()) {
  2316. return;
  2317. /* NOTREACHED */
  2318. }
  2319. /* checking if it was previously labeled */
  2320. QString dirPath = getPathFromFilename(filename);
  2321. QDir dir(dirPath);
  2322. QStringList filter;
  2323. filter << "*.dat";
  2324. QStringList fileList = dir.entryList(filter, QDir::Files);
  2325. QString labeled = alterFileName(filename, "_labeled");
  2326. labeled = removePath(labeled);
  2327. labeled.append(".dat");
  2328. if (fileList.contains(labeled, Qt::CaseInsensitive)) {
  2329. labeled = dir.absoluteFilePath(labeled);
  2330. loadInfo(labeled);
  2331. Image *newImage = new Image;
  2332. newImage->image_ = filename;
  2333. newImage->labeled_ = 1;
  2334. newImage->pas_ = 0;
  2335. addImage(newImage);
  2336. enableTools();
  2337. return;
  2338. /* NOTREACHED */
  2339. }
  2340. clearAllTool();
  2341. QString winTitle;
  2342. winTitle.append("ImageLabeler - ");
  2343. winTitle.append(current_image_);
  2344. setWindowTitle(winTitle);
  2345. if (!image_->load(filename)) {
  2346. return;
  2347. /* NOTREACHED */
  2348. }
  2349. image_holder_->resize(image_->size());
  2350. image_holder_->setPixmap(*image_);
  2351. current_image_ = filename;
  2352. image_ID_ = list_images_widget_->count() - 1;
  2353. list_images_widget_->setCurrentRow(image_ID_);
  2354. Image *newImage = new Image;
  2355. newImage->image_ = filename;
  2356. newImage->labeled_ = 0;
  2357. newImage->pas_ = 0;
  2358. addImage(newImage);
  2359. if (!list_images_widget_->count()) {
  2360. return;
  2361. /* NOTREACHED */
  2362. }
  2363. enableTools();
  2364. }
  2365. //! A slot member loading images recursively
  2366. /*!
  2367. * \see getImagesFromDir(const QDir &dir)
  2368. *
  2369. * Slot asks for unsaved data.
  2370. * It gives user a possibility to break a recursive search(with the widget
  2371. * and a button "cancel" on it.)
  2372. *
  2373. */
  2374. void
  2375. ImageLabeler::loadImages()
  2376. {
  2377. if (!askForUnsavedData()) {
  2378. return;
  2379. /* NOTREACHED */
  2380. }
  2381. QFileDialog fileDialog(0, tr("Load images"));
  2382. fileDialog.setFileMode(QFileDialog::Directory);
  2383. QString dirName("");
  2384. if (fileDialog.exec()) {
  2385. /* TODO: make it possible to select multiple folders */
  2386. dirName = fileDialog.selectedFiles().last();
  2387. }
  2388. else {
  2389. //showWarning(tr("Could not open file dialog"));
  2390. return;
  2391. /* NOTREACHED */
  2392. }
  2393. clearAllTool();
  2394. /* creating a widget with a "cancel" button to
  2395. * give user an opportunity to stop the recursive search */
  2396. QWidget *searchInProgress = new QWidget(0);
  2397. searchInProgress->setWindowTitle(tr("Loading images"));
  2398. QLabel *info = new QLabel(searchInProgress);
  2399. info->setText(
  2400. tr("Program is looking for all image files in your directory recursively."));
  2401. QPushButton *cancel = new QPushButton(tr("Cancel"), searchInProgress);
  2402. connect(cancel, SIGNAL(clicked()), this, SLOT(interruptSearch()));
  2403. QVBoxLayout *layout = new QVBoxLayout(searchInProgress);
  2404. searchInProgress->setLayout(layout);
  2405. layout->addWidget(info);
  2406. layout->addWidget(cancel);
  2407. searchInProgress->adjustSize();
  2408. searchInProgress->move(QApplication::desktop()->screen()->rect().center() - rect().center());
  2409. searchInProgress->show();
  2410. getImagesFromDir(QDir(dirName));
  2411. /* deleting everything related to searchInProgress widget */
  2412. cancel->disconnect();
  2413. searchInProgress->hide();
  2414. delete info;
  2415. delete cancel;
  2416. delete layout;
  2417. delete searchInProgress;
  2418. if (interrupt_search_) {
  2419. interrupt_search_ = 0;
  2420. return;
  2421. /* NOTREACHED */
  2422. }
  2423. if (!list_images_widget_->count()) {
  2424. showWarning(tr("The folder you selected contains no images"));
  2425. return;
  2426. /* NOTREACHED */
  2427. }
  2428. bool ret = 0;
  2429. if (list_images_->at(0).labeled_) {
  2430. QString labeled =
  2431. alterFileName(list_images_->at(0).image_, "_labeled");
  2432. labeled.append(".dat");
  2433. ret = loadInfo(labeled);
  2434. }
  2435. else
  2436. ret = image_->load(list_images_->at(0).image_);
  2437. if (!ret) {
  2438. return;
  2439. /* NOTREACHED */
  2440. }
  2441. current_image_ = list_images_->at(0).image_;
  2442. image_ID_ = 0;
  2443. list_images_widget_->setCurrentRow(image_ID_);
  2444. QString winTitle;
  2445. winTitle.append("ImageLabeler - ");
  2446. winTitle.append(current_image_);
  2447. setWindowTitle(winTitle);
  2448. image_holder_->resize(image_->size());
  2449. image_holder_->setPixmap(*image_);
  2450. enableTools();
  2451. }
  2452. //! A slot member loading legend(labels) from xml file
  2453. /*!
  2454. * \see loadLegendFromNode(QDomElement *anElement)
  2455. */
  2456. void
  2457. ImageLabeler::loadLegendFromFile()
  2458. {
  2459. QFileDialog fileDialog(0, tr("Load legend"));
  2460. fileDialog.setFileMode(QFileDialog::AnyFile);
  2461. QString filename;
  2462. if (fileDialog.exec()) {
  2463. filename = fileDialog.selectedFiles().last();
  2464. }
  2465. else {
  2466. //showWarning(tr("Could not open file dialog"));
  2467. return;
  2468. /* NOTREACHED */
  2469. }
  2470. QDomDocument doc("Image Labeler");
  2471. QFile file(filename);
  2472. if (!file.open(QIODevice::ReadOnly)) {
  2473. showWarning(tr("Can not open such file"));
  2474. return;
  2475. /* NOTREACHED */
  2476. }
  2477. QString errMsg;
  2478. if (!doc.setContent(&file, &errMsg)) {
  2479. showWarning(errMsg);
  2480. file.close();
  2481. return;
  2482. /* NOTREACHED */
  2483. }
  2484. file.close();
  2485. list_label_->clear();
  2486. /* getting legend */
  2487. QDomElement elements = doc.documentElement();
  2488. QDomNode rootNode = elements.firstChild();
  2489. QString string;
  2490. while(!rootNode.isNull()) {
  2491. QDomElement element = rootNode.toElement();
  2492. if(!element.isNull()) {
  2493. if (element.tagName() == "legend") {
  2494. loadLegendFromNode(&element);
  2495. }
  2496. }
  2497. rootNode = rootNode.nextSibling();
  2498. }
  2499. }
  2500. //! A protected member loading legend from xml node
  2501. /*!
  2502. * \see addLabel(int aLabelID, bool isMain, QString aLabel)
  2503. * \see setLabelColor(int anID, QColor aColor)
  2504. *
  2505. * Slot gets all label information from xml node.
  2506. */
  2507. void
  2508. ImageLabeler::loadLegendFromNode(QDomElement *anElement)
  2509. {
  2510. if (!anElement) {
  2511. return;
  2512. /* NOTREACHED */
  2513. }
  2514. QDomNode subNode = anElement->firstChild();
  2515. QDomElement subElement;
  2516. QString string;
  2517. int id = -1;
  2518. bool isMain;
  2519. uint color = 0xff000000;
  2520. while(!subNode.isNull()) {
  2521. subElement = subNode.toElement();
  2522. if (!subElement.isNull() && !subElement.text().isEmpty()) {
  2523. /* id attribute */
  2524. string = subElement.attribute("id");
  2525. bool ok = 0;
  2526. id = string.toInt(&ok, 10);
  2527. if (!ok) {
  2528. qDebug() <<
  2529. "loadLegendFromNode: "
  2530. "label id format is corrupted";
  2531. subNode = subNode.nextSibling();
  2532. continue;
  2533. }
  2534. /* isMain attribute */
  2535. string = subElement.attribute("isMain");
  2536. isMain = string.toInt(&ok, 2);
  2537. if (!ok) {
  2538. qDebug() <<
  2539. "loadLegendFromNode: "
  2540. "label isMain flag format is corrupted";
  2541. subNode = subNode.nextSibling();
  2542. continue;
  2543. }
  2544. /* color attribute */
  2545. string = subElement.attribute("color");
  2546. color = string.toUInt(&ok, 16);
  2547. if (!ok) {
  2548. qDebug() <<
  2549. "loadLegendFromNode: "
  2550. "label color format is corrupted";
  2551. subNode = subNode.nextSibling();
  2552. continue;
  2553. }
  2554. /* label name */
  2555. string = subElement.text();
  2556. addLabel(id, isMain, string);
  2557. setLabelColor(id, color);
  2558. }
  2559. subNode = subNode.nextSibling();
  2560. }
  2561. }
  2562. //! A protected member converting all the label information into xml format
  2563. /*!
  2564. * \see objectsToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2565. * \param[in] aDoc a pointer to QDomDocument object - represents an xml document
  2566. * \param[out] aRoot a pointer to QDomElement object - contains all the results from
  2567. * the converting
  2568. */
  2569. void
  2570. ImageLabeler::legendToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2571. {
  2572. QDomElement legend = aDoc->createElement(tr("legend"));
  2573. aRoot->appendChild(legend);
  2574. /* storing all labels made by user */
  2575. int labelCount = list_label_->count();
  2576. for (int i = 0; i < labelCount; i++) {
  2577. QDomElement label = aDoc->createElement(tr("label"));
  2578. label.setAttribute("color", QString("%1").arg(list_label_colors_.at(i), 0, 16));
  2579. label.setAttribute("id", i);
  2580. QString priority;
  2581. if (main_label_ == i)
  2582. priority.append("1");
  2583. else
  2584. priority.append("0");
  2585. label.setAttribute("isMain", priority);
  2586. QString labelText = list_label_->item(i)->text();
  2587. /* removing the number prefix of label */
  2588. if (-1 != labelText.indexOf(QString("%1: ").arg(i))) {
  2589. labelText = labelText.mid(3, labelText.size() - 3);
  2590. }
  2591. QDomText labelName = aDoc->createTextNode(labelText);
  2592. label.appendChild(labelName);
  2593. legend.appendChild(label);
  2594. }
  2595. /* in case we have no labels */
  2596. if (0 == labelCount) {
  2597. QDomElement label = aDoc->createElement(tr("label"));
  2598. label.setAttribute(tr("id"), -1);
  2599. legend.appendChild(label);
  2600. }
  2601. }
  2602. //! A protected member converting all the objects information into xml format
  2603. /*!
  2604. * \see legendToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2605. * \param[in] aDoc a pointer to the QDomDocument object - represents an xml document
  2606. * \param[out] aRoot a pointer to the QDomElement object - contains all the results from
  2607. * the converting
  2608. */
  2609. void
  2610. ImageLabeler::objectsToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2611. {
  2612. QDomElement objects = aDoc->createElement(tr("objects"));
  2613. aRoot->appendChild(objects);
  2614. /* rects first */
  2615. for (int i = 0; i < list_bounding_box_.size(); i++) {
  2616. QDomElement rectData = aDoc->createElement(tr("bbox"));
  2617. rectData.setAttribute("id", list_bounding_box_.at(i)->label_ID_);
  2618. QRect rect = list_bounding_box_.at(i)->rect.normalized();
  2619. QString rectDataString =
  2620. QString("%1;%2;%3;%4;").
  2621. arg(rect.x()).
  2622. arg(rect.y()).
  2623. arg(rect.width()).
  2624. arg(rect.height());
  2625. QDomText rectDataText = aDoc->createTextNode(rectDataString);
  2626. rectData.appendChild(rectDataText);
  2627. objects.appendChild(rectData);
  2628. }
  2629. /* polys next */
  2630. for (int i = 0; i < list_polygon_.size(); i++) {
  2631. QDomElement polyData = aDoc->createElement(tr("poly"));
  2632. polyData.setAttribute("id", list_polygon_.at(i)->label_ID_);
  2633. QPolygon poly = list_polygon_.at(i)->poly;
  2634. QString polyDataString;
  2635. for (int j = 0; j < poly.count(); j++)
  2636. polyDataString.append(
  2637. QString("%1;%2;").
  2638. arg(poly.point(j).x()).
  2639. arg(poly.point(j).y())
  2640. );
  2641. QDomText polyDataText = aDoc->createTextNode(polyDataString);
  2642. polyData.appendChild(polyDataText);
  2643. objects.appendChild(polyData);
  2644. }
  2645. }
  2646. //! A slot member generating color for all labels
  2647. /*!
  2648. * A very primitive temporary solution for color generation
  2649. */
  2650. void
  2651. ImageLabeler::generateColors()
  2652. {
  2653. int labelCount = list_label_->count();
  2654. if (!labelCount) {
  2655. showWarning(tr("you have not added any labels yet"));
  2656. return;
  2657. /* NOTREACHED */
  2658. }
  2659. int coeff = (0xff / labelCount) * 3;
  2660. list_label_colors_.clear();
  2661. list_label_colors_.append(0);
  2662. uchar red = 0xff;
  2663. uchar green = 0xff;
  2664. uchar blue = 0xff;
  2665. uchar iterationColor = coeff;
  2666. uint color = 0xffffffff;
  2667. int j = 1;
  2668. for (int i = 1; i < labelCount; i++) {
  2669. if (6 == j) {
  2670. iterationColor += coeff;
  2671. j = 1;
  2672. }
  2673. if (5 == j) {
  2674. red = 0xff - iterationColor;
  2675. green = 0xff - iterationColor;
  2676. blue = 0xff;
  2677. }
  2678. else if (4 == j) {
  2679. red = 0xff - iterationColor;
  2680. green = 0xff;
  2681. blue = 0xff - iterationColor;
  2682. }
  2683. else if (3 == j) {
  2684. red = 0xff - iterationColor;
  2685. green = 0xff;
  2686. blue = 0xff;
  2687. }
  2688. else if (2 == j) {
  2689. red = 0xff;
  2690. green = 0xff - iterationColor;
  2691. blue = 0xff;
  2692. }
  2693. else if (1 == j){
  2694. red = 0xff;
  2695. green = 0xff;
  2696. blue = 0xff - iterationColor;
  2697. }
  2698. j++;
  2699. color = red + (green * 0x100) + (blue * 0x10000) + 0xff000000;
  2700. int itemNo = list_label_colors_.count();
  2701. QPixmap iconPix = QPixmap(20, 20);
  2702. iconPix.fill(color);
  2703. QIcon icon(iconPix);
  2704. list_label_->item(itemNo)->setIcon(icon);
  2705. list_label_colors_.append(color);
  2706. }
  2707. image_holder_->update();
  2708. }
  2709. //! A protected member creating new boundary box from text data
  2710. /*!
  2711. * \see addBBoxArea(int anID, BoundingBox aBBox, int itemID)
  2712. * \see BBoxFromData(QString *aBBoxData)
  2713. *
  2714. * It creates bbox both in the container and in the list widget
  2715. */
  2716. void
  2717. ImageLabeler::addBBoxFromData(
  2718. QString *aBBoxData,
  2719. int *ID
  2720. )
  2721. {
  2722. BoundingBox *bbox = new BoundingBox;
  2723. *bbox = BBoxFromData(aBBoxData);
  2724. if (!bbox->rect.isValid() || !ID) {
  2725. return;
  2726. /* NOTREACHED */
  2727. }
  2728. bbox->label_ID_ = *ID;
  2729. list_bounding_box_.append(bbox);
  2730. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  2731. }
  2732. //! A protected member parsing string data and returning a BoundingBox from it
  2733. /*!
  2734. * \see addBBoxArea(int anID, BoundingBox aBBox, int itemID)
  2735. *
  2736. * format is x;y;w;h where w - width and h - height
  2737. */
  2738. BoundingBox
  2739. ImageLabeler::BBoxFromData(
  2740. QString *aBBoxData
  2741. )
  2742. {
  2743. BoundingBox bbox;
  2744. QString buffer;
  2745. bbox.rect.setRect(-1, -1, -1, -1);
  2746. int startPos = 0;
  2747. bool ok = 1;
  2748. for (int i = 0; i < aBBoxData->size(); i++) {
  2749. if (';' != aBBoxData->at(i))
  2750. continue;
  2751. buffer = aBBoxData->mid(startPos, i - startPos);
  2752. int bboxData = buffer.toInt(&ok, 10);
  2753. if (!ok) {
  2754. qDebug() <<
  2755. "BBoxFromData: "
  2756. "bbox format is corrupted";
  2757. break;
  2758. }
  2759. if (-1 == bbox.rect.x()) {
  2760. bbox.rect.setX(bboxData);
  2761. bbox.rect.setWidth(-1);
  2762. }
  2763. else if (-1 == bbox.rect.y()) {
  2764. bbox.rect.setY(bboxData);
  2765. bbox.rect.setHeight(-1);
  2766. }
  2767. else if (-1 == bbox.rect.width()) {
  2768. bbox.rect.setWidth(bboxData);
  2769. }
  2770. else if (-1 == bbox.rect.height()) {
  2771. bbox.rect.setHeight(bboxData);
  2772. }
  2773. startPos = i + 1;
  2774. }
  2775. if (!bbox.rect.isValid()) {
  2776. qDebug() <<
  2777. "BBoxFromData: "
  2778. "bbox format is corrupted";
  2779. bbox.rect.setRect(-1, -1, -1, -1);
  2780. }
  2781. else if (!ok) {
  2782. bbox.rect.setRect(-1, -1, -1, -1);
  2783. }
  2784. return bbox;
  2785. }
  2786. //! A protected member getting a BoundaryBox from text of the list_areas_ item
  2787. /*!
  2788. * \see BBoxFromData(QString *aBBoxData)
  2789. *
  2790. * Slot gets label ID an points from the string data. BBox ID can not be changed.
  2791. */
  2792. BoundingBox
  2793. ImageLabeler::BBoxFromListItemText(
  2794. QString *aString,
  2795. int *oldID
  2796. )
  2797. {
  2798. BoundingBox bbox;
  2799. bbox.label_ID_ = -1;
  2800. *oldID = -1;
  2801. if (!aString) {
  2802. return bbox;
  2803. /* NOTREACHED */
  2804. }
  2805. if (-1 == aString->indexOf("BBox")) {
  2806. return bbox;
  2807. /* NOTREACHED */
  2808. }
  2809. /* getting bbox id in the list(it cannot be changed) */
  2810. bool ok = 0;
  2811. int bboxID = getNumFromString(aString, "BBox #", ";", &ok);
  2812. if (!ok || bboxID <= -1) {
  2813. qDebug() <<
  2814. "BBoxFromListItemText: bboxID is corrupted";
  2815. return bbox;
  2816. /* NOTREACHED */
  2817. }
  2818. /* getting new label id */
  2819. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  2820. if (!ok || labelID <= -1) {
  2821. showWarning(
  2822. tr("new LabelID is wrong, area can not be changed")
  2823. );
  2824. return bbox;
  2825. /* NOTREACHED */
  2826. }
  2827. /* getting new points */
  2828. int pointsPos = aString->indexOf("data:") + 5;
  2829. int pointsLen = aString->size() - pointsPos;
  2830. if (pointsLen <= 0) {
  2831. showWarning(
  2832. tr("new data is wrong, area can not be changed")
  2833. );
  2834. return bbox;
  2835. /* NOTREACHED */
  2836. }
  2837. QString pointsData = aString->mid(pointsPos, pointsLen);
  2838. bbox = BBoxFromData(&pointsData);
  2839. bbox.label_ID_ = labelID;
  2840. *oldID = bboxID;
  2841. return bbox;
  2842. }
  2843. //! A protected member adding poly to the list widget and to the list container
  2844. /*!
  2845. * \see addBBox(BoundingBox *bbox)
  2846. * \param[in] poly a pointer to the Polygon structure
  2847. * containing new poly information
  2848. *
  2849. */
  2850. void
  2851. ImageLabeler::addPoly(Polygon *poly)
  2852. {
  2853. if (poly->poly.isEmpty() || poly->label_ID_ < 0) {
  2854. return;
  2855. /* NOTREACHED */
  2856. }
  2857. list_polygon_.append(poly);
  2858. addPolyArea(list_polygon_.count() - 1, *poly);
  2859. }
  2860. //! A protected member adding bounding box to the list widget
  2861. //! and to the list container
  2862. /*!
  2863. * \see addPoly(Polygon *poly)
  2864. * \param[in] bbox a pointer to the BoundingBox structure
  2865. * containing new bounding box information
  2866. *
  2867. */
  2868. void
  2869. ImageLabeler::addBBox(BoundingBox *bbox)
  2870. {
  2871. if (bbox->rect.isEmpty() || bbox->label_ID_ < 0) {
  2872. return;
  2873. /* NOTREACHED */
  2874. }
  2875. list_bounding_box_.append(bbox);
  2876. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  2877. }
  2878. //! A protected member creating new polygon from text data
  2879. /*!
  2880. * \see addPolyArea(int anID, Polygon aPoly, int itemID)
  2881. * \see polyFromData(QString *aPolyData)
  2882. *
  2883. * It creates polygon both in the container and in the list widget
  2884. */
  2885. void
  2886. ImageLabeler::addPolyFromData(
  2887. QString *aPolyData,
  2888. int *labelID
  2889. )
  2890. {
  2891. Polygon *poly = new Polygon;
  2892. *poly = polyFromData(aPolyData);
  2893. if (poly->poly.isEmpty() || !labelID) {
  2894. return;
  2895. /* NOTREACHED */
  2896. }
  2897. poly->label_ID_ = *labelID;
  2898. list_polygon_.append(poly);
  2899. addPolyArea(list_polygon_.count() - 1, *poly);
  2900. }
  2901. //! A protected member parsing string data and returning a Polygon from it
  2902. /*!
  2903. * \see addPolyArea(int anID, Polygon aPoly, int itemID)
  2904. *
  2905. * format is x0;y0;x1;y1;...
  2906. */
  2907. Polygon
  2908. ImageLabeler::polyFromData(
  2909. QString *aPolyData
  2910. )
  2911. {
  2912. Polygon poly;
  2913. poly.label_ID_ = -1;
  2914. QPoint point;
  2915. QString buffer;
  2916. int startPos = 0;
  2917. bool ok = 1;
  2918. /* indicates whether coordinate x or y */
  2919. bool evenFlag = 0;
  2920. ///jojo
  2921. QStringList coordsList = aPolyData->split(";", QString::SkipEmptyParts);
  2922. Polygon newPoly;
  2923. try
  2924. {
  2925. if( coordsList.size() % 2 == 0)
  2926. {
  2927. for( int i = 0; i < coordsList.size(); i += 2)
  2928. {
  2929. newPoly.poly.append( QPoint( QVariant(coordsList[i]).toInt(),
  2930. QVariant(coordsList[i+1]).toInt() ) );
  2931. }
  2932. }
  2933. }
  2934. catch(std::exception &e)
  2935. {
  2936. newPoly.poly.clear();
  2937. }
  2938. return newPoly;
  2939. ///#jojo
  2940. /*
  2941. for (int i = 0; i < aPolyData->size(); i++) {
  2942. // ";" is a separator
  2943. if (';' != aPolyData->at(i))
  2944. continue;
  2945. buffer = aPolyData->mid(startPos, i - startPos);
  2946. int polyCoor = buffer.toInt(&ok, 10);
  2947. if (!ok) {
  2948. qDebug() <<
  2949. "polyFromData: "
  2950. "poly format is corrupted";
  2951. break;
  2952. }
  2953. if (!evenFlag) {
  2954. point.setX(polyCoor);
  2955. evenFlag = 1;
  2956. }
  2957. else {
  2958. point.setY(polyCoor);
  2959. poly.poly.append(point);
  2960. evenFlag = 0;
  2961. }
  2962. startPos = i + 1;
  2963. }
  2964. // last coordinate was Xi what means an error
  2965. if (evenFlag) {
  2966. qDebug() <<
  2967. "polyFromData: "
  2968. "poly format is corrupted";
  2969. poly.poly.clear();
  2970. }
  2971. // last converting from string was not successful
  2972. else if (!ok) {
  2973. poly.poly.clear();
  2974. }
  2975. return poly;
  2976. */
  2977. }
  2978. //! A protected member getting a Polygon from text of the list_areas_ item
  2979. /*!
  2980. * \see polyFromData(QString *aPolyData)
  2981. *
  2982. * Slot gets label ID an points from the string data. Poly ID can not be changed.
  2983. */
  2984. Polygon
  2985. ImageLabeler::polyFromListItemText(
  2986. QString *aString,
  2987. int *oldID
  2988. )
  2989. {
  2990. Polygon poly;
  2991. poly.label_ID_ = -1;
  2992. *oldID = -1;
  2993. if (!aString) {
  2994. return poly;
  2995. /* NOTREACHED */
  2996. }
  2997. if (-1 == aString->indexOf("Poly")) {
  2998. return poly;
  2999. /* NOTREACHED */
  3000. }
  3001. /* getting poly id in the list(it cannot be changed) */
  3002. bool ok = 0;
  3003. int polyID = getNumFromString(aString, "Poly #", ";", &ok);
  3004. if (!ok || polyID <= -1) {
  3005. qDebug() <<
  3006. "polyFromListItemText: poly ID is corrupted";
  3007. return poly;
  3008. /* NOTREACHED */
  3009. }
  3010. /* getting new label id */
  3011. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  3012. if (!ok || labelID <= -1) {
  3013. showWarning(
  3014. tr("new LabelID is wrong, area can not be changed")
  3015. );
  3016. return poly;
  3017. /* NOTREACHED */
  3018. }
  3019. /* getting new points */
  3020. int pointsPos = aString->indexOf("points:") + 7;
  3021. int pointsLen = aString->size() - pointsPos;
  3022. if (pointsLen <= 0) {
  3023. showWarning(
  3024. tr("new points data is wrong, area can not be changed")
  3025. );
  3026. return poly;
  3027. /* NOTREACHED */
  3028. }
  3029. QString pointsData = aString->mid(pointsPos, pointsLen);
  3030. poly = polyFromData(&pointsData);
  3031. poly.label_ID_ = labelID;
  3032. *oldID = polyID;
  3033. return poly;
  3034. }
  3035. //! A protected member only for internal use. Showing a message box with warning
  3036. /*!
  3037. * \param[in] text a QString object containing message you want
  3038. * to deliver to the user
  3039. */
  3040. void
  3041. ImageLabeler::showWarning(
  3042. const QString &text
  3043. )
  3044. {
  3045. if (text.isEmpty()) {
  3046. return;
  3047. /* NOTREACHED */
  3048. }
  3049. QMessageBox msgBox;
  3050. msgBox.setText(text);
  3051. msgBox.setIcon(QMessageBox::Warning);
  3052. msgBox.exec();
  3053. }
  3054. //! A protected member only for internal use.
  3055. //! Shows a message box asking about unsaved progress
  3056. /*!
  3057. * It depends on object containers(list_bounding_box_ and list_polygon_)
  3058. * and on unsaved_data_ flag which must be true to make this dialog appear
  3059. */
  3060. bool
  3061. ImageLabeler::askForUnsavedData()
  3062. {
  3063. if ((!list_bounding_box_.isEmpty() ||
  3064. !list_polygon_.isEmpty()) &&
  3065. unsaved_data_)
  3066. {
  3067. QMessageBox msgBox;
  3068. msgBox.setText(tr("There is some unsaved data"));
  3069. msgBox.setInformativeText(tr("Do you want to save your progress?"));
  3070. msgBox.setStandardButtons(
  3071. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  3072. msgBox.setDefaultButton(QMessageBox::Save);
  3073. msgBox.setIcon(QMessageBox::Question);
  3074. int ret = msgBox.exec();
  3075. if (QMessageBox::Save == ret)
  3076. saveAllInfo();
  3077. else if (QMessageBox::Cancel == ret)
  3078. return true;
  3079. }
  3080. return true;
  3081. }
  3082. //! A slot member setting bbox tool active
  3083. /*!
  3084. * \param[in] aButtonPressed is a bool flag which is needed for toggled buttons
  3085. */
  3086. void
  3087. ImageLabeler::setBoundingBoxTool(bool aButtonPressed)
  3088. {
  3089. if (aButtonPressed)
  3090. image_holder_->setTool(ImageHolder::BoundingBoxTool);
  3091. else {
  3092. image_holder_->setTool(ImageHolder::NoTool);
  3093. image_holder_->clearLast();
  3094. }
  3095. }
  3096. //! A slot member setting polygon tool active
  3097. /*!
  3098. * \param[in] aButtonPressed is a bool flag which is needed for toggled buttons
  3099. */
  3100. void
  3101. ImageLabeler::setPolygonTool(bool aButtonPressed)
  3102. {
  3103. if (aButtonPressed) {
  3104. image_holder_->setTool(ImageHolder::PolygonTool);
  3105. image_holder_->setFocus();
  3106. }
  3107. else {
  3108. image_holder_->setTool(ImageHolder::NoTool);
  3109. image_holder_->clearLast();
  3110. }
  3111. }
  3112. //! A slot member enables button_confirm_selection_ button
  3113. void
  3114. ImageLabeler::onSelectionStarted()
  3115. {
  3116. button_confirm_selection_->setEnabled(true);
  3117. }
  3118. //! A slot member adding information about new selected area
  3119. /*!
  3120. * \see addBBoxArea(int anID, BoundingBox aBBox, int itemID)
  3121. * \see addPolyArea(int aPolyID, Polygon aPoly, int itemID)
  3122. *
  3123. * After user has created a new selection area it should be confirmed or else it
  3124. * wouldn't be added to the item list and containers. This slot takes all data from
  3125. * unconfirmed selection and places it into list widget and corresponding container
  3126. * (list_bounding_box_ or list_polygon_)
  3127. */
  3128. void
  3129. ImageLabeler::confirmSelection()
  3130. {
  3131. if (!list_label_->count()) {
  3132. showWarning(tr("You haven't added any label"));
  3133. return;
  3134. /* NOTREACHED */
  3135. }
  3136. image_holder_->confirmSelection();
  3137. if (label_ID_ < 0)
  3138. label_ID_ = 0;
  3139. ImageHolder::Tool tool = image_holder_->tool();
  3140. switch (tool) {
  3141. case ImageHolder::BoundingBoxTool:
  3142. list_bounding_box_.last()->label_ID_ = label_ID_;
  3143. addBBoxArea(
  3144. list_bounding_box_.count() - 1,
  3145. *(list_bounding_box_.last())
  3146. );
  3147. break;
  3148. case ImageHolder::PolygonTool:
  3149. list_polygon_.last()->label_ID_ = label_ID_;
  3150. addPolyArea(
  3151. list_polygon_.count() - 1,
  3152. *(list_polygon_.last())
  3153. );
  3154. break;
  3155. default:
  3156. break;
  3157. }
  3158. button_confirm_selection_->setEnabled(false);
  3159. button_delete_area_->setEnabled(true);
  3160. button_change_area_->setEnabled(true);
  3161. button_change_area_text_->setEnabled(true);
  3162. unsaved_data_ = 1;
  3163. }
  3164. //! \brief A slot member clears all widgets and containers
  3165. //! (not recommended to use without necessity)
  3166. /*!
  3167. * \see clearAllTool()
  3168. * \see clearLabelList()
  3169. * \see clearLabelColorList()
  3170. */
  3171. void
  3172. ImageLabeler::clearAll()
  3173. {
  3174. clearLabelList();
  3175. list_areas_->clear();
  3176. list_bounding_box_.clear();
  3177. list_polygon_.clear();
  3178. list_images_->clear();
  3179. list_images_widget_->clear();
  3180. main_label_ = -1;
  3181. image_holder_->clearAll();
  3182. segmented_image_.clear();
  3183. action_view_normal_->setEnabled(false);
  3184. action_view_segmented_->setEnabled(false);
  3185. }
  3186. //! A slot member clears only selections made by user
  3187. /*!
  3188. * \see clearAll()
  3189. * \see clearLabelList()
  3190. * \see clearLabelColorList()
  3191. */
  3192. void
  3193. ImageLabeler::clearAllTool()
  3194. {
  3195. list_areas_->clear();
  3196. list_bounding_box_.clear();
  3197. list_polygon_.clear();
  3198. main_label_ = -1;
  3199. image_holder_->clearAll();
  3200. }
  3201. //! A slot member clears label list
  3202. /*!
  3203. * \see clearAll()
  3204. * \see clearAllTool()
  3205. * \see clearLabelColorList()
  3206. *
  3207. * As far as BACKGROUND label should always be present slot adds
  3208. * it after clearing all the label list
  3209. */
  3210. void ImageLabeler::clearLabelList()
  3211. {
  3212. list_label_->clear();
  3213. addLabel(0, false, "BACKGROUND");
  3214. list_label_->item(0)->setFlags(
  3215. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  3216. Qt::ItemIsEnabled
  3217. );
  3218. }
  3219. //! A slot member clears the list of label colors
  3220. /*!
  3221. * \see clearAll()
  3222. * \see clearAllTool()
  3223. * \see clearLabelList()
  3224. *
  3225. * As far as BACKGROUND label should always be present slot adds
  3226. * it's color(black by default) after clearing all the label color list
  3227. */
  3228. void ImageLabeler::clearLabelColorList()
  3229. {
  3230. list_label_colors_.clear();
  3231. list_label_colors_.append(0x0);
  3232. }
  3233. //! A slot member enabling all the tools
  3234. /*!
  3235. * \see disableTools()
  3236. *
  3237. * By default the initial state of all tools is disabled
  3238. * so this slot is recommended to use when at least one image
  3239. * has been loaded
  3240. */
  3241. void
  3242. ImageLabeler::enableTools()
  3243. {
  3244. action_save_all_->setEnabled(true);
  3245. action_save_segmented_->setEnabled(true);
  3246. action_save_legend_->setEnabled(true);
  3247. action_bound_box_tool_->setEnabled(true);
  3248. action_polygon_tool_->setEnabled(true);
  3249. action_tagging_tool_->setEnabled(true);
  3250. action_add_description_->setEnabled(true);
  3251. action_undo_->setEnabled(true);
  3252. action_redo_->setEnabled(true);
  3253. button_bound_box_tool_->setEnabled(true);
  3254. button_polygon_tool_->setEnabled(true);
  3255. button_tagging_tool_->setEnabled(true);
  3256. button_add_label_->setEnabled(true);
  3257. button_remove_label_->setEnabled(true);
  3258. button_prev_image_->setEnabled(true);
  3259. button_next_image_->setEnabled(true);
  3260. button_clear_selection_tool_->setEnabled(true);
  3261. button_generate_colors_->setEnabled(true);
  3262. button_delete_all_labels_->setEnabled(true);
  3263. }
  3264. //! A slot member disabling all the tools
  3265. /*!
  3266. * \see enableTools()
  3267. */
  3268. void
  3269. ImageLabeler::disableTools()
  3270. {
  3271. action_save_all_->setEnabled(false);
  3272. action_save_segmented_->setEnabled(false);
  3273. action_save_legend_->setEnabled(false);
  3274. action_bound_box_tool_->setEnabled(false);
  3275. action_polygon_tool_->setEnabled(false);
  3276. action_tagging_tool_->setEnabled(false);
  3277. button_tagging_tool_->setEnabled(false);
  3278. action_add_description_->setEnabled(false);
  3279. action_undo_->setEnabled(false);
  3280. action_redo_->setEnabled(false);
  3281. button_bound_box_tool_->setEnabled(false);
  3282. button_polygon_tool_->setEnabled(false);
  3283. button_add_label_->setEnabled(false);
  3284. button_remove_label_->setEnabled(false);
  3285. button_prev_image_->setEnabled(false);
  3286. button_next_image_->setEnabled(false);
  3287. button_clear_selection_tool_->setEnabled(false);
  3288. button_generate_colors_->setEnabled(false);
  3289. button_delete_all_labels_->setEnabled(false);
  3290. }
  3291. //! \brief A slot member triggering popup_area_list_ menu with the coordinates
  3292. //! of the mouse pointer
  3293. /*!
  3294. * \see deleteArea()
  3295. * \see editArea()
  3296. */
  3297. void
  3298. ImageLabeler::areaListPopupMenu(const QPoint &aPos)
  3299. {
  3300. QPoint globalPos = list_areas_->mapToGlobal(aPos);
  3301. QModelIndex index = list_areas_->indexAt(aPos);
  3302. if (-1 == index.row()) {
  3303. return;
  3304. /* NOTREACHED */
  3305. }
  3306. list_areas_->item(index.row())->setSelected(true);
  3307. popup_area_list_->exec(globalPos);
  3308. }
  3309. //! \brief A slot member triggering popup_label_list_ menu width the coordinates
  3310. //! of the mouse pointer
  3311. /*!
  3312. * \see setLabelColor()
  3313. * \see toggleLabelPriority()
  3314. * \see removeLabel()
  3315. */
  3316. void
  3317. ImageLabeler::labelListPopupMenu(const QPoint &aPos)
  3318. {
  3319. QPoint globalPos = list_label_->mapToGlobal(aPos);
  3320. QModelIndex index = list_label_->indexAt(aPos);
  3321. if (-1 == index.row() || !index.row()) {
  3322. return;
  3323. /* NOTREACHED */
  3324. }
  3325. list_label_->item(index.row())->setSelected(true);
  3326. popup_label_list_->exec(globalPos);
  3327. }
  3328. //! \brief A slot member triggering popup_images_list_ menu width the coordinates
  3329. //! of the mouse pointer
  3330. /*!
  3331. * \see removeImage()
  3332. */
  3333. void
  3334. ImageLabeler::imageListPopupMenu(const QPoint &aPos)
  3335. {
  3336. QPoint globalPos = list_images_widget_->mapToGlobal(aPos);
  3337. QModelIndex index = list_images_widget_->indexAt(aPos);
  3338. if (-1 == index.row()) {
  3339. return;
  3340. /* NOTREACHED */
  3341. }
  3342. list_images_widget_->item(index.row())->setSelected(true);
  3343. popup_images_list_->exec(globalPos);
  3344. }
  3345. //! \brief A slot member gets image description or tags from line_edit_form_
  3346. //! depends on line_edit_form_.purpose()
  3347. /*!
  3348. * \see LineEditForm
  3349. */
  3350. void
  3351. ImageLabeler::setDataFromForm(QString aData)
  3352. {
  3353. if (ImageDescriptionPurpose == line_edit_form_.purpose()) {
  3354. image_description_ = aData;
  3355. setWindowTitle(image_description_);
  3356. }
  3357. else if (TaggingPurpose == line_edit_form_.purpose()) {
  3358. tags_ = aData;
  3359. }
  3360. }
  3361. //! A slot member creating 2-dimensional array for the segmented image
  3362. /*!
  3363. * Array size is equal to a size of the current image loaded
  3364. * and each pixel has the label id which corresponds to the objects in
  3365. * list_bounding_box_ and list_polygon_.
  3366. */
  3367. void
  3368. ImageLabeler::setPureData()
  3369. {
  3370. /* initializing array */
  3371. if (pure_data_) {
  3372. delete[] *pure_data_;
  3373. delete pure_data_;
  3374. }
  3375. /* getting image size */
  3376. QSize imageSize = image_->size();
  3377. pure_data_ = new int *[imageSize.height()];
  3378. if (!pure_data_) {
  3379. return;
  3380. /* NOTREACHED */
  3381. }
  3382. /* allocating memory */
  3383. for (int i = 0; i < imageSize.height(); i++) {
  3384. pure_data_[i] = new int[imageSize.width()];
  3385. if (!pure_data_[i]) {
  3386. return;
  3387. /* NOTREACHED */
  3388. }
  3389. }
  3390. /* getting the number of all selections */
  3391. int bboxCnt = list_bounding_box_.count();
  3392. int polyCnt = list_polygon_.count();
  3393. /* checking if each coordinate(pixel) belongs to any object */
  3394. for (int i = 0; i < imageSize.height(); i++)
  3395. for (int j = 0; j < imageSize.width(); j++) {
  3396. pure_data_[i][j] = 0;
  3397. /* bboxes first */
  3398. for (int cnt = 0; cnt < bboxCnt; cnt++) {
  3399. BoundingBox *bbox = list_bounding_box_.at(cnt);
  3400. if (bbox->rect.contains(j, i)) {
  3401. pure_data_[i][j] = bbox->label_ID_;
  3402. }
  3403. }
  3404. /* polys next */
  3405. for (int cnt = 0; cnt < polyCnt; cnt++) {
  3406. Polygon *poly = list_polygon_.at(cnt);
  3407. if (poly->poly.containsPoint(QPoint(j, i), Qt::OddEvenFill)) {
  3408. pure_data_[i][j] = poly->label_ID_;
  3409. }
  3410. }
  3411. }
  3412. }
  3413. //! \brief A slot member setting new color for
  3414. //! the current label from QColorDialog
  3415. /*!
  3416. * \see setLabelColor(int anID, QColor aColor)
  3417. */
  3418. void
  3419. ImageLabeler::setLabelColor()
  3420. {
  3421. if (list_label_colors_.count() < list_label_->count()) {
  3422. generateColors();
  3423. }
  3424. /* getting current item */
  3425. QListWidgetItem *current = list_label_->currentItem();
  3426. int labelID = list_label_->row(current);
  3427. QColor defaultColor;
  3428. defaultColor.setRgb(list_label_colors_.at(labelID));
  3429. QColor newColor = QColorDialog::getColor(
  3430. defaultColor,
  3431. list_label_
  3432. );
  3433. if (!newColor.isValid()) {
  3434. return;
  3435. /* NOTREACHED */
  3436. }
  3437. list_label_colors_.takeAt(labelID);
  3438. list_label_colors_.insert(labelID, newColor.rgb());
  3439. QPixmap iconPix = QPixmap(20, 20);
  3440. iconPix.fill(newColor);
  3441. QIcon icon(iconPix);
  3442. current->setIcon(icon);
  3443. image_holder_->update();
  3444. }
  3445. //! A protected member setting color aColor for the label number anID
  3446. /*!
  3447. * \param[in] anID an integer indicating label number in the list_label_ widget
  3448. * \param[in] aColor a QColor object contains color you want to set for the label
  3449. */
  3450. void
  3451. ImageLabeler::setLabelColor(int anID, QColor aColor)
  3452. {
  3453. if (anID < 0 || list_label_->count() < anID) {
  3454. return;
  3455. /* NOTREACHED */
  3456. }
  3457. if (list_label_colors_.count() < list_label_->count()) {
  3458. generateColors();
  3459. }
  3460. QListWidgetItem *item = list_label_->item(anID);
  3461. list_label_colors_.takeAt(anID);
  3462. list_label_colors_.insert(anID, aColor.rgb());
  3463. QPixmap iconPix = QPixmap(20, 20);
  3464. iconPix.fill(aColor);
  3465. QIcon icon(iconPix);
  3466. item->setIcon(icon);
  3467. image_holder_->update();
  3468. }
  3469. //! A slot member loading normal image which path is in the current_image_
  3470. /*!
  3471. * \see viewSegmented()
  3472. */
  3473. void
  3474. ImageLabeler::viewNormal()
  3475. {
  3476. if (current_image_.isEmpty()) {
  3477. return;
  3478. /* NOTREACHED */
  3479. }
  3480. image_->load(current_image_);
  3481. image_holder_->setPixmap(*image_);
  3482. action_view_segmented_->setEnabled(true);
  3483. action_view_normal_->setEnabled(false);
  3484. }
  3485. //! A slot member loading segmented image which path is in the segmented_image_
  3486. /*!
  3487. * \see viewNormal()
  3488. */
  3489. void
  3490. ImageLabeler::viewSegmented()
  3491. {
  3492. if (segmented_image_.isEmpty()) {
  3493. return;
  3494. /* NOTREACHED */
  3495. }
  3496. image_->load(segmented_image_);
  3497. image_holder_->setPixmap(*image_);
  3498. action_view_segmented_->setEnabled(false);
  3499. action_view_normal_->setEnabled(true);
  3500. }
  3501. //! \brief A slot member setting interrupt_search_ to true for interrupting
  3502. //! the recursive search
  3503. /*!
  3504. * \see loadImages()
  3505. * \see getImagesFromDir(const QDir &dir)
  3506. */
  3507. void
  3508. ImageLabeler::interruptSearch()
  3509. {
  3510. interrupt_search_ = 1;
  3511. }
  3512. //! \brief A slot member selecting image corresponding to the item
  3513. //! in the list_images_widget_
  3514. /*!
  3515. * \see selectImage(int anImageID)
  3516. * \param[in] anItem a pointer to the QListWidgetItem object which indicates
  3517. * certain image in the image_list_
  3518. */
  3519. void
  3520. ImageLabeler::selectImage(QListWidgetItem *anItem)
  3521. {
  3522. if (!anItem || list_images_widget_->row(anItem) < 0 ||
  3523. list_images_->isEmpty())
  3524. {
  3525. return;
  3526. /* NOTREACHED */
  3527. }
  3528. clearAllTool();
  3529. //clearLabelList();
  3530. //clearLabelColorList();
  3531. image_ID_ = list_images_widget_->row(anItem);
  3532. selectImage(image_ID_);
  3533. }
  3534. //! A protected member loading image from list_images_
  3535. /*!
  3536. * \see loadInfo(QString filename)
  3537. * \param[in] anImageID an integer indicates certain
  3538. * image in the image_list_
  3539. *
  3540. * if that image was labeled before, selectImage(int anImageID)
  3541. * will try to load all info
  3542. */
  3543. bool
  3544. ImageLabeler::selectImage(int anImageID)
  3545. {
  3546. if (anImageID < 0 || list_images_->isEmpty() ||
  3547. list_images_->count() <= anImageID)
  3548. {
  3549. return false;
  3550. /* NOTREACHED */
  3551. }
  3552. action_view_segmented_->setEnabled(false);
  3553. /* checking if it was labeled before */
  3554. if (list_images_->at(anImageID).labeled_ &&
  3555. !list_images_->at(anImageID).pas_)
  3556. {
  3557. list_label_colors_.clear();
  3558. list_label_->clear();
  3559. QString labeled =
  3560. alterFileName(list_images_->at(anImageID).image_, "_labeled");
  3561. labeled.append(".dat");
  3562. loadInfo(labeled);
  3563. }
  3564. /* if it was loaded from PASCAL file then we're in trouble */
  3565. else if (list_images_->at(anImageID).labeled_ &&
  3566. list_images_->at(anImageID).pas_)
  3567. {
  3568. /* TODO: do the pascal file selecting */
  3569. showWarning("this function doesn't work at the moment, sorry.");
  3570. }
  3571. /* loading clean unlabeled image */
  3572. else {
  3573. current_image_ = list_images_->at(anImageID).image_;
  3574. image_->load(current_image_);
  3575. image_holder_->setPixmap(*image_);
  3576. image_holder_->resize(image_->size());
  3577. }
  3578. return true;
  3579. }
  3580. //! \brief A slot member removing selected image from list
  3581. //! (image will still be loaded in the image_holder_)
  3582. /*!
  3583. * Image will be removed from list so it will not be possible to load
  3584. * it again without using loadImage() or other slots for image loading.
  3585. * if you want to remove image from image_holder_ just call
  3586. * image_holder_->clear()
  3587. */
  3588. void
  3589. ImageLabeler::removeImage()
  3590. {
  3591. int imageCount = list_images_widget_->count();
  3592. int num = list_images_widget_->currentRow();
  3593. if (!imageCount || num < 0 || imageCount <= num ||
  3594. list_images_->count() != imageCount)
  3595. {
  3596. return;
  3597. /* NOTREACHED */
  3598. }
  3599. list_images_widget_->takeItem(num);
  3600. list_images_->takeAt(num);
  3601. for (int i = num ; i < list_images_widget_->count(); i++) {
  3602. QString newStr = removePath(list_images_->at(i).image_);
  3603. newStr.prepend(QString("%1: ").arg(i));
  3604. list_images_widget_->item(i)->setText(newStr);
  3605. }
  3606. if (1 == imageCount)
  3607. button_remove_image_->setEnabled(false);
  3608. }
  3609. //! A protected member which is being automatically called on every image resize
  3610. /*!
  3611. *
  3612. */
  3613. void
  3614. ImageLabeler::resizeEvent (QResizeEvent *anEvent)
  3615. {
  3616. QWidget::resizeEvent(anEvent);
  3617. }
  3618. //! A protected member which is being automatically called on every mouse press
  3619. /*!
  3620. *
  3621. */
  3622. void
  3623. ImageLabeler::mousePressEvent(QMouseEvent *anEvent)
  3624. {
  3625. QWidget::mousePressEvent(anEvent);
  3626. }
  3627. //! A protected member which is being automatically called on every key press
  3628. /*!
  3629. * current list of available commands:
  3630. * - key left/right - prevImage() / nextImage()
  3631. * - enter/return - confirmSelection()
  3632. * - esc - clear current selection
  3633. * - ctrl+z - image_holder_->undo()
  3634. * - ctrl+y - iamge_holder_->redo()
  3635. */
  3636. void
  3637. ImageLabeler::keyPressEvent(QKeyEvent *anEvent)
  3638. {
  3639. keyboard_modifier_ = anEvent->modifiers();
  3640. if (Qt::Key_Left == anEvent->key() &&
  3641. Qt::ControlModifier == keyboard_modifier_) {
  3642. prevImage();
  3643. }
  3644. if (Qt::Key_Down == anEvent->key() &&
  3645. Qt::ControlModifier == keyboard_modifier_) {
  3646. prevImage(5);
  3647. }
  3648. if (Qt::Key_Right == anEvent->key() &&
  3649. Qt::ControlModifier == keyboard_modifier_) {
  3650. nextImage();
  3651. }
  3652. if (Qt::Key_Up == anEvent->key() &&
  3653. Qt::ControlModifier == keyboard_modifier_) {
  3654. nextImage(5);
  3655. }
  3656. if ((Qt::Key_Enter == anEvent->key() ||
  3657. Qt::Key_Return == anEvent->key()) &&
  3658. // Qt::NoModifier == anEvent->modifiers() &&
  3659. ImageHolder::NewSelection == image_holder_->state()) {
  3660. confirmSelection();
  3661. }
  3662. if ((Qt::Key_Enter == anEvent->key() ||
  3663. Qt::Key_Return == anEvent->key()) &&
  3664. // Qt::NoModifier == anEvent->modifiers() &&
  3665. -1 != image_holder_->focusedSelection()) {
  3666. image_holder_->clearFocusOnArea();
  3667. image_holder_->clearHoveredPoint();
  3668. image_holder_->update();
  3669. }
  3670. if (Qt::Key_Escape == anEvent->key()) {
  3671. image_holder_->clearLast();
  3672. image_holder_->clearFocusOnArea();
  3673. image_holder_->clearHoveredPoint();
  3674. image_holder_->update();
  3675. }
  3676. if (Qt::Key_Z == anEvent->key() &&
  3677. Qt::ControlModifier == anEvent->modifiers()) {
  3678. image_holder_->undo();
  3679. }
  3680. if (Qt::Key_Y == anEvent->key() &&
  3681. Qt::ControlModifier == anEvent->modifiers()) {
  3682. image_holder_->redo();
  3683. }
  3684. if (Qt::Key_Delete == anEvent->key() &&
  3685. list_areas_->hasFocus()) {
  3686. deleteArea();
  3687. }
  3688. if (Qt::Key_Delete == anEvent->key() &&
  3689. list_label_->hasFocus()) {
  3690. removeLabel();
  3691. }
  3692. if (Qt::Key_Delete == anEvent->key() &&
  3693. list_images_widget_->hasFocus()) {
  3694. removeImage();
  3695. }
  3696. if (Qt::Key_Delete == anEvent->key() &&
  3697. image_holder_->hasFocus()) {
  3698. image_holder_->removeSelectedPoint();
  3699. }
  3700. QWidget::keyPressEvent(anEvent);
  3701. }
  3702. //!
  3703. /*!
  3704. *
  3705. */
  3706. void
  3707. ImageLabeler::keyReleaseEvent(QKeyEvent *anEvent)
  3708. {
  3709. keyboard_modifier_ = anEvent->modifiers();
  3710. }
  3711. //! A protected member which is being automatically called on every mouse wheel turn
  3712. /*!
  3713. * implements zooming in and out on ctrl+wheel
  3714. */
  3715. void
  3716. ImageLabeler::wheelEvent(QWheelEvent *anEvent)
  3717. {
  3718. /* zoomin */
  3719. if (0 < anEvent->delta() &&
  3720. Qt::ControlModifier == keyboard_modifier_) {
  3721. image_holder_->scaleImage(ZoomIn, 1.1);
  3722. }
  3723. /* zoomout */
  3724. else if (anEvent->delta() < 0 &&
  3725. Qt::ControlModifier == keyboard_modifier_) {
  3726. image_holder_->scaleImage(ZoomOut, 1.1);
  3727. }
  3728. }
  3729. //!
  3730. /*!
  3731. *
  3732. */
  3733. void
  3734. ImageLabeler::closeEvent(QCloseEvent *anEvent)
  3735. {
  3736. Q_UNUSED(anEvent)
  3737. writeSettings();
  3738. askForUnsavedData();
  3739. }
  3740. /*
  3741. *
  3742. */