ImageLabeler.cpp 96 KB

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