ImageLabeler.cpp 101 KB

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