ImageLabeler.cpp 101 KB

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