ImageLabeler.cpp 90 KB

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