ImageLabeler.cpp 102 KB

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