ImageLabeler.cpp 101 KB

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