ImageLabeler.cpp 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880
  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. list_bounding_box_.clear();
  1111. list_polygon_.clear();
  1112. list_areas_->clear();
  1113. image_holder_->clearAll();
  1114. segmented_image_.clear();
  1115. //clearLabelColorList();
  1116. if (list_images_widget_->count() - iImageStep == image_ID_) {
  1117. image_ID_ = 0;
  1118. }
  1119. else {
  1120. image_ID_ += iImageStep;
  1121. }
  1122. if (!selectImage(image_ID_)) {
  1123. showWarning(tr("Next image is not available"));
  1124. return;
  1125. }
  1126. list_images_widget_->setCurrentRow(image_ID_);
  1127. if (current_image_.isEmpty()) {
  1128. return;
  1129. }
  1130. QString winTitle;
  1131. winTitle.append("ImageLabeler - ");
  1132. winTitle.append(current_image_);
  1133. setWindowTitle(winTitle);
  1134. image_->load(current_image_);
  1135. image_holder_->resize(image_->size());
  1136. image_holder_->setPixmap(*image_);
  1137. }
  1138. //! \brief A slot member changing current image to the previous one
  1139. //! and clearing all the data(except legend)
  1140. /*!
  1141. * Asks about unsaved data if there is any. If current picture was the
  1142. * first in the image list then it goes to the last one.
  1143. * Adds to filename of the current image to the window title
  1144. */
  1145. void
  1146. ImageLabeler::prevImage(int iImageStep)
  1147. {
  1148. if (!list_images_widget_->count()) {
  1149. return;
  1150. }
  1151. if (!askForUnsavedData()) {
  1152. return;
  1153. }
  1154. list_bounding_box_.clear();
  1155. list_polygon_.clear();
  1156. list_areas_->clear();
  1157. image_holder_->clearAll();
  1158. segmented_image_.clear();
  1159. //clearLabelColorList();
  1160. if (image_ID_ < iImageStep) {
  1161. image_ID_ = list_images_widget_->count() - 1;
  1162. }
  1163. else {
  1164. image_ID_ -= iImageStep;
  1165. }
  1166. list_images_widget_->setCurrentRow(image_ID_);
  1167. if (!selectImage(image_ID_)) {
  1168. showWarning(tr("Next image is not available"));
  1169. return;
  1170. /* NOTREACHED */
  1171. }
  1172. QString winTitle;
  1173. winTitle.append("ImageLabeler - ");
  1174. winTitle.append(current_image_);
  1175. setWindowTitle(winTitle);
  1176. image_->load(current_image_);
  1177. image_holder_->resize(image_->size());
  1178. image_holder_->setPixmap(*image_);
  1179. }
  1180. //! A Slot member saving all info about labeled image
  1181. /*!
  1182. * It saves a file in xml format which contains:
  1183. * - path to the original image
  1184. * - path to the segmented image(if any)
  1185. * - image description
  1186. * - tags
  1187. * - labels
  1188. * - objects data
  1189. * - segmented representation of the image as 2-dimensional array of label ids
  1190. */
  1191. void
  1192. ImageLabeler::saveAllInfo()
  1193. {
  1194. if (!list_images_widget_->count()) {
  1195. showWarning("You have not opened any image yet");
  1196. return;
  1197. /* NOTREACHED */
  1198. }
  1199. /* ------------------------------------------------------------------------
  1200. * XML part
  1201. */
  1202. QDomDocument doc(tr("ImageLabeler"));
  1203. QDomElement root = doc.createElement(tr("pixelwise_labeling"));
  1204. doc.appendChild(root);
  1205. QDomElement image = doc.createElement(tr("image"));
  1206. root.appendChild(image);
  1207. QDomText pathToImage = doc.createTextNode(current_image_);
  1208. image.appendChild(pathToImage);
  1209. if (!segmented_image_.isEmpty()) {
  1210. QDomElement segmentedImage = doc.createElement(tr("segmented"));
  1211. root.appendChild(segmentedImage);
  1212. QDomText pathToSegmented = doc.createTextNode(segmented_image_);
  1213. segmentedImage.appendChild(pathToSegmented);
  1214. }
  1215. QDomElement description = doc.createElement(tr("description"));
  1216. root.appendChild(description);
  1217. QDomText descriptionText = doc.createTextNode(image_description_);
  1218. description.appendChild(descriptionText);
  1219. QDomElement tags = doc.createElement(tr("tags"));
  1220. root.appendChild(tags);
  1221. QDomText tagsText = doc.createTextNode(tags_);
  1222. tags.appendChild(tagsText);
  1223. legendToXml(&doc, &root);
  1224. objectsToXml(&doc, &root);
  1225. /* image size */
  1226. QSize imageSize = image_->size();
  1227. QString imageSizeString;
  1228. imageSizeString.append(QString("%1;%2").
  1229. arg(imageSize.width()).
  1230. arg(imageSize.height()));
  1231. QDomElement imageSizeElement = doc.createElement(tr("image_size"));
  1232. QDomText imageSizeText = doc.createTextNode(imageSizeString);
  1233. imageSizeElement.appendChild(imageSizeText);
  1234. root.appendChild(imageSizeElement);
  1235. /* pure data */
  1236. setPureData();
  1237. QString pixelValues;
  1238. for (int i = 0; i < imageSize.height(); i++) {
  1239. for (int j = 0; j < imageSize.width(); j++) {
  1240. pixelValues.append(QString("%1;").arg(pure_data_[i][j]));
  1241. }
  1242. pixelValues.append("\n");
  1243. }
  1244. QDomElement pureData = doc.createElement(tr("pure_data"));
  1245. QDomText pureDataText = doc.createTextNode(pixelValues);
  1246. pureData.appendChild(pureDataText);
  1247. root.appendChild(pureData);
  1248. QString xml = doc.toString();
  1249. /* ------------------------------------------------------------------------
  1250. * XML part ends
  1251. */
  1252. QFileDialog fileDialog(0, tr("Save all info"));
  1253. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1254. fileDialog.setDefaultSuffix("dat");
  1255. fileDialog.setFileMode(QFileDialog::AnyFile);
  1256. QString dir = getPathFromFilename(current_image_);
  1257. /* altering the name of a new file */
  1258. QString newFileName = alterFileName(current_image_, "_labeled");
  1259. newFileName = removePath(newFileName);
  1260. fileDialog.selectFile(newFileName);
  1261. fileDialog.setDirectory(dir);
  1262. QString filename;
  1263. if (fileDialog.exec()) {
  1264. filename = fileDialog.selectedFiles().last();
  1265. }
  1266. else {
  1267. //showWarning(tr("Can not open file dialog"));
  1268. return;
  1269. /* NOTREACHED */
  1270. }
  1271. if (filename.isEmpty()) {
  1272. return;
  1273. /* NOTREACHED */
  1274. }
  1275. QFile file(filename);
  1276. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1277. showWarning(tr("Can not open file for writing"));
  1278. return;
  1279. /* NOTREACHED */
  1280. }
  1281. file.write(xml.toLocal8Bit());
  1282. file.close();
  1283. unsaved_data_ = 0;
  1284. }
  1285. //! A slot member saving a segmented image from pure_data_ array
  1286. /*!
  1287. * \see setPureData()
  1288. *
  1289. * If label colors are not set it asks user about automatic color generation.
  1290. * New image format is .png
  1291. */
  1292. void
  1293. ImageLabeler::saveSegmentedPicture()
  1294. {
  1295. if (list_bounding_box_.isEmpty() && list_polygon_.isEmpty()) {
  1296. return;
  1297. /* NOTREACHED */
  1298. }
  1299. setPureData();
  1300. QFileDialog fileDialog(0, tr("Save segmented picture"));
  1301. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1302. fileDialog.setDefaultSuffix("png");
  1303. fileDialog.setFileMode(QFileDialog::AnyFile);
  1304. QString dir = getPathFromFilename(current_image_);
  1305. /* altering the name of a new file */
  1306. QString newFileName = alterFileName(current_image_, "_segmented");
  1307. newFileName = removePath(newFileName);
  1308. fileDialog.selectFile(newFileName);
  1309. fileDialog.setDirectory(dir);
  1310. QString filename;
  1311. if (fileDialog.exec()) {
  1312. filename = fileDialog.selectedFiles().last();
  1313. }
  1314. else {
  1315. return;
  1316. /* NOTREACHED */
  1317. }
  1318. QSize imageSize = image_holder_->pixmap()->size();
  1319. QImage newImage(imageSize, QImage::Format_RGB32);
  1320. bool generateColorsFlag = auto_color_generation_;
  1321. bool flag = 0;
  1322. /* checking if all the colors are are different from white(default) */
  1323. if (!generateColorsFlag) {
  1324. for (int i = 1; i < list_label_->count(); i++) {
  1325. if (list_label_colors_.at(i) == 0xffffffff && !flag)
  1326. flag = 1;
  1327. else if (list_label_colors_.at(i) == 0xffffffff && flag) {
  1328. QMessageBox msgBox;
  1329. msgBox.setText(tr("There are few labels with default white color."));
  1330. msgBox.setInformativeText(tr("Do you want to generate all colors automatically? Otherwise you'll have to do it manually."));
  1331. msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  1332. msgBox.setDefaultButton(QMessageBox::Yes);
  1333. msgBox.setIcon(QMessageBox::Question);
  1334. int ret = msgBox.exec();
  1335. if (QMessageBox::Yes == ret) {
  1336. generateColorsFlag = 1;
  1337. break;
  1338. }
  1339. else {
  1340. return;
  1341. /* NOTREACHED */
  1342. }
  1343. }
  1344. }
  1345. }
  1346. /* generating colors for labels */
  1347. if (list_label_colors_.count() < list_label_->count() ||
  1348. generateColorsFlag) {
  1349. generateColors();
  1350. }
  1351. for (int i = 0; i < imageSize.height(); i++)
  1352. for (int j = 0; j < imageSize.width(); j++) {
  1353. newImage.setPixel(j, i, list_label_colors_.at(pure_data_[i][j]));
  1354. }
  1355. if (!newImage.save(filename, "png", 100)) {
  1356. showWarning(tr("An error occurred while saving the segmented image"));
  1357. return;
  1358. /* NOTREACHED */
  1359. }
  1360. segmented_image_ = filename;
  1361. action_view_segmented_->setEnabled(true);
  1362. }
  1363. //! A slot member saving only labels(legend) to the separate xml file
  1364. /*!
  1365. * \see legendToXml(QDomDocument *aDoc, QDomElement *aRoot)
  1366. *
  1367. * Slot asks user where to save a file with the legend and saves it.
  1368. */
  1369. void
  1370. ImageLabeler::saveLegend()
  1371. {
  1372. if (!list_label_->count()) {
  1373. showWarning("You have not added any label yet");
  1374. return;
  1375. /* NOTREACHED */
  1376. }
  1377. /* ------------------------------------------------------------------------
  1378. * XML part
  1379. */
  1380. QDomDocument doc(tr("ImageLabeler"));
  1381. QDomElement root = doc.createElement(tr("root"));
  1382. doc.appendChild(root);
  1383. legendToXml(&doc, &root);
  1384. QString xml = doc.toString();
  1385. /* ------------------------------------------------------------------------
  1386. * XML part ends
  1387. */
  1388. QFileDialog fileDialog(0, tr("Save legend"));
  1389. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1390. fileDialog.setDefaultSuffix("dat");
  1391. fileDialog.setFileMode(QFileDialog::AnyFile);
  1392. QString dir = getPathFromFilename(current_image_);
  1393. /* altering the name of a new file */
  1394. QString newFileName = alterFileName(current_image_, "_legend");
  1395. newFileName = removePath(newFileName);
  1396. fileDialog.selectFile(newFileName);
  1397. fileDialog.setDirectory(dir);
  1398. QString filename;
  1399. if (fileDialog.exec()) {
  1400. filename = fileDialog.selectedFiles().last();
  1401. }
  1402. else {
  1403. //showWarning(tr("Can not open file dialog"));
  1404. return;
  1405. /* NOTREACHED */
  1406. }
  1407. if (filename.isEmpty()) {
  1408. return;
  1409. /* NOTREACHED */
  1410. }
  1411. QFile file(filename);
  1412. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1413. showWarning(tr("Can not open file for writing"));
  1414. return;
  1415. /* NOTREACHED */
  1416. }
  1417. file.write(xml.toLocal8Bit());
  1418. file.close();
  1419. }
  1420. //! A slot member loading labeled image from formatted xml file.
  1421. /*!
  1422. * \see loadInfo(QString filename)
  1423. */
  1424. void
  1425. ImageLabeler::loadInfo()
  1426. {
  1427. if (!askForUnsavedData()) {
  1428. return;
  1429. /* NOTREACHED */
  1430. }
  1431. QFileDialog fileDialog(0, tr("Load file with info"));
  1432. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1433. fileDialog.setDefaultSuffix("dat");
  1434. fileDialog.setFileMode(QFileDialog::AnyFile);
  1435. QString filename;
  1436. if (fileDialog.exec()) {
  1437. filename = fileDialog.selectedFiles().last();
  1438. }
  1439. else {
  1440. //showWarning(tr("Can not open file dialog"));
  1441. return;
  1442. /* NOTREACHED */
  1443. }
  1444. clearAllTool();
  1445. clearLabelList();
  1446. if (loadInfo(filename)) {
  1447. enableTools();
  1448. Image newImage;
  1449. newImage.image_ = current_image_;
  1450. newImage.labeled_ = 1;
  1451. newImage.pas_ = 0;
  1452. addImage(&newImage);
  1453. image_ID_ = list_images_widget_->count() - 1;
  1454. list_images_widget_->setCurrentRow(image_ID_);
  1455. }
  1456. unsaved_data_ = 0;
  1457. }
  1458. //! A slot member loading labeled image from formatted xml file.
  1459. /*!
  1460. * \param[in] filename a QString object containing a path to the file
  1461. * we need to load data from
  1462. *
  1463. * \see loadLegendFromNode(QDomElement *)
  1464. * \see addBBoxFromData(QString *aBBoxData, int *ID)
  1465. * \see addPolyFromData(QString *aPolyData, int *labelID)
  1466. */
  1467. bool
  1468. ImageLabeler::loadInfo(QString filename)
  1469. {
  1470. QDomDocument doc("Image Labeler");
  1471. QFile file(filename);
  1472. if (!file.open(QIODevice::ReadOnly)) {
  1473. showWarning(tr("Can not open such file"));
  1474. return false;
  1475. /* NOTREACHED */
  1476. }
  1477. QString errMsg;
  1478. if (!doc.setContent(&file, &errMsg)) {
  1479. showWarning(errMsg);
  1480. file.close();
  1481. return false;
  1482. /* NOTREACHED */
  1483. }
  1484. file.close();
  1485. /* getting all info */
  1486. QDomElement elements = doc.documentElement();
  1487. QDomNode rootNode = elements.firstChild();
  1488. QString string;
  1489. while(!rootNode.isNull()) {
  1490. QDomElement element = rootNode.toElement();
  1491. if(!element.isNull()) {
  1492. /* path to the image */
  1493. if (element.tagName() == "image") {
  1494. //qDebug() << "image";
  1495. string = element.text();
  1496. if (string.isEmpty()) {
  1497. showWarning(
  1498. tr(
  1499. "The file with data doesn't contain path to the image"
  1500. )
  1501. );
  1502. return false;
  1503. /* NOTREACHED */
  1504. }
  1505. QString sImageStr = string;
  1506. if( QFileInfo(string).isRelative() )
  1507. {
  1508. sImageStr = QFileInfo(filename).path() + "/" + string;
  1509. }
  1510. if (!image_->load( sImageStr )) {
  1511. return false;
  1512. /* NOTREACHED */
  1513. }
  1514. current_image_ = string;
  1515. QString winTitle;
  1516. winTitle.append("ImageLabeler - ");
  1517. winTitle.append(current_image_);
  1518. setWindowTitle(winTitle);
  1519. image_holder_->resize(image_->size());
  1520. image_holder_->setPixmap(*image_);
  1521. }
  1522. /* path to the segmented image */
  1523. if (element.tagName() == "segmented") {
  1524. //qDebug() << "segmented";
  1525. string = element.text();
  1526. if ( !string.isEmpty()) {
  1527. segmented_image_ = string;
  1528. action_view_segmented_->setEnabled(true);
  1529. }
  1530. }
  1531. /* image description */
  1532. else if (element.tagName() == "description" &&
  1533. !element.text().isEmpty()) {
  1534. //qDebug() << "description";
  1535. image_description_ = element.text();
  1536. }
  1537. /* tags */
  1538. else if (element.tagName() == "tags" &&
  1539. !element.text().isEmpty()) {
  1540. //qDebug() << "tags";
  1541. tags_ = element.text();
  1542. }
  1543. /* legend */
  1544. else if (element.tagName() == "legend") {
  1545. //qDebug() << "legend";
  1546. list_label_->clear();
  1547. loadLegendFromNode(&element);
  1548. }
  1549. /* objects */
  1550. else if (element.tagName() == "objects") {
  1551. //qDebug() << "objects";
  1552. QDomNode subNode = element.firstChild();
  1553. QDomElement subElement;
  1554. button_delete_area_->setEnabled(false);
  1555. button_change_area_->setEnabled(false);
  1556. button_change_area_text_->setEnabled(false);
  1557. while(!subNode.isNull()) {
  1558. subElement = subNode.toElement();
  1559. if (subElement.isNull() || subElement.text().isEmpty()) {
  1560. subNode = subNode.nextSibling();
  1561. continue;
  1562. }
  1563. string = subElement.attribute("id");
  1564. bool ok = 1;
  1565. int id = string.toInt(&ok, 10);
  1566. if (!ok) {
  1567. qDebug() <<
  1568. "loadInfo: "
  1569. "poly id format is corrupted";
  1570. subNode = subNode.nextSibling();
  1571. continue;
  1572. }
  1573. string = subElement.text();
  1574. if (subElement.tagName() == "bbox")
  1575. addBBoxFromData(&string, &id);
  1576. else if (subElement.tagName() == "poly")
  1577. addPolyFromData(&string, &id);
  1578. else {
  1579. qDebug() <<
  1580. "loadInfo: "
  1581. "poly id format is corrupted";
  1582. subNode = subNode.nextSibling();
  1583. continue;
  1584. }
  1585. button_delete_area_->setEnabled(true);
  1586. button_change_area_->setEnabled(true);
  1587. button_change_area_text_->setEnabled(true);
  1588. subNode = subNode.nextSibling();
  1589. }
  1590. }
  1591. }
  1592. rootNode = rootNode.nextSibling();
  1593. }
  1594. qDebug() << "image info loaded for "<<filename;
  1595. unsaved_data_ = 0;
  1596. return true;
  1597. }
  1598. //! A slot member loading info about labeled image from PASCAL file(xml)
  1599. /*!
  1600. * \see loadPascalFile(QString aFilename, QString aPath)
  1601. *
  1602. * Slot asks user to set default "root" directory for the PASCAL files if
  1603. * it wan't set before
  1604. */
  1605. void
  1606. ImageLabeler::loadPascalFile()
  1607. {
  1608. if (!askForUnsavedData()) {
  1609. return;
  1610. /* NOTREACHED */
  1611. }
  1612. if (PASCALpath_.isEmpty()) {
  1613. showWarning(tr("before opening first PASCAL file please choose \"root\" directory"
  1614. " where a folder with segmentations,"
  1615. " a folder with polygons,"
  1616. " a folder with image descriptions and"
  1617. " a folder with the pure images are."));
  1618. QFileDialog fileDialog(0, tr("root directory for the PASCAL files"));
  1619. fileDialog.setFileMode(QFileDialog::Directory);
  1620. if (fileDialog.exec())
  1621. PASCALpath_ = fileDialog.selectedFiles().last();
  1622. else {
  1623. return;
  1624. /* NOTREACHED */
  1625. }
  1626. }
  1627. QFileDialog fileDialog(0, tr("Load pascal file"));
  1628. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1629. fileDialog.setDefaultSuffix("xml");
  1630. fileDialog.setFileMode(QFileDialog::AnyFile);
  1631. fileDialog.setDirectory(PASCALpath_);
  1632. QStringList filters;
  1633. filters << "PASCAL xml data (*.xml)"
  1634. << "Any files (*)";
  1635. fileDialog.setNameFilters(filters);
  1636. QString filename;
  1637. if (fileDialog.exec()) {
  1638. filename = fileDialog.selectedFiles().last();
  1639. }
  1640. else {
  1641. //showWarning(tr("Can not open file dialog"));
  1642. return;
  1643. /* NOTREACHED */
  1644. }
  1645. clearAllTool();
  1646. clearLabelList();
  1647. if (loadPascalFile(filename, PASCALpath_)) {
  1648. enableTools();
  1649. Image newImage;
  1650. newImage.image_ = current_image_;
  1651. newImage.labeled_ = 1;
  1652. newImage.pas_ = 1;
  1653. addImage(&newImage);
  1654. image_ID_ = list_images_widget_->count() - 1;
  1655. list_images_widget_->setCurrentRow(image_ID_);
  1656. }
  1657. unsaved_data_ = 0;
  1658. }
  1659. //! A slot member loading info about labeled image from PASCAL file(xml)
  1660. /*!
  1661. * Slot parses xml file and gets all needed info out of it
  1662. */
  1663. bool
  1664. ImageLabeler::loadPascalFile(QString aFilename, QString aPath)
  1665. {
  1666. QDomDocument doc;
  1667. QFile file(aFilename);
  1668. if (!file.open(QIODevice::ReadOnly)) {
  1669. showWarning(tr("Can not open such file"));
  1670. return false;
  1671. /* NOTREACHED */
  1672. }
  1673. QString errMsg;
  1674. if (!doc.setContent(&file, &errMsg)) {
  1675. showWarning(errMsg);
  1676. file.close();
  1677. return false;
  1678. /* NOTREACHED */
  1679. }
  1680. file.close();
  1681. //clearAll();
  1682. //enableTools();
  1683. /* getting all info */
  1684. QDomElement elements = doc.documentElement();
  1685. QDomNode rootNode = elements.firstChild();
  1686. QString string;
  1687. QString path;
  1688. if (aPath.isEmpty())
  1689. path = getPathFromFilename(aFilename);
  1690. else
  1691. path = aPath + "/";
  1692. QString filename;
  1693. QStringList labels;
  1694. labels << "BACKGROUND";
  1695. int labelID;
  1696. while(!rootNode.isNull()) {
  1697. QDomElement element = rootNode.toElement();
  1698. if(!element.isNull()) {
  1699. /* folder */
  1700. if (element.tagName() == "folder") {
  1701. string = element.text();
  1702. if (!string.isEmpty()) {
  1703. path.append(string);
  1704. }
  1705. }
  1706. /* filename */
  1707. else if (element.tagName() == "filename") {
  1708. string = element.text();
  1709. if (!string.isEmpty()) {
  1710. filename = string;
  1711. }
  1712. }
  1713. /* object */
  1714. else if (element.tagName() == "object") {
  1715. QDomNode subNode = element.firstChild();
  1716. QDomElement subElement;
  1717. while(!subNode.isNull()) {
  1718. subElement = subNode.toElement();
  1719. if (subElement.isNull() || subElement.text().isEmpty()) {
  1720. subNode = subNode.nextSibling();
  1721. continue;
  1722. }
  1723. /* label */
  1724. if (subElement.tagName() == "name") {
  1725. string = subElement.text();
  1726. if (!string.isEmpty() &&
  1727. !labels.contains(string, Qt::CaseInsensitive))
  1728. {
  1729. addLabel(list_label_->count(), 0, string);
  1730. labelID = labels.count();
  1731. labels << string;
  1732. }
  1733. else if (labels.contains(string, Qt::CaseInsensitive)) {
  1734. for (int i = 0; i < labels.count(); i++) {
  1735. if (labels.at(i) == string)
  1736. labelID = i;
  1737. }
  1738. }
  1739. }
  1740. if (subElement.tagName() == "bndbox") {
  1741. /* 2 points */
  1742. QPoint topLeft;
  1743. QPoint bottomRight;
  1744. QDomNode bboxNode = subElement.firstChild();
  1745. QDomElement bboxElement;
  1746. while(!bboxNode.isNull()) {
  1747. bboxElement = bboxNode.toElement();
  1748. string.clear();
  1749. bool ok = 1;
  1750. if (bboxElement.tagName() == "xmin") {
  1751. string = bboxElement.text();
  1752. if (string.isEmpty())
  1753. ok = 0;
  1754. double xmin = string.toDouble(&ok);
  1755. if (ok)
  1756. topLeft.setX(qRound(xmin));
  1757. }
  1758. else if (bboxElement.tagName() == "ymin") {
  1759. string = bboxElement.text();
  1760. if (string.isEmpty())
  1761. ok = 0;
  1762. double ymin = string.toDouble(&ok);
  1763. if (ok)
  1764. topLeft.setY(qRound(ymin));
  1765. }
  1766. else if (bboxElement.tagName() == "xmax") {
  1767. string = bboxElement.text();
  1768. if (string.isEmpty())
  1769. ok = 0;
  1770. double xmax = string.toDouble(&ok);
  1771. if (ok)
  1772. bottomRight.setX(qRound(xmax));
  1773. }
  1774. else if (bboxElement.tagName() == "ymax") {
  1775. string = bboxElement.text();
  1776. if (string.isEmpty())
  1777. ok = 0;
  1778. double ymax = string.toDouble(&ok);
  1779. if (ok)
  1780. bottomRight.setY(qRound(ymax));
  1781. }
  1782. if (string.isEmpty() || !ok)
  1783. break;
  1784. bboxNode = bboxNode.nextSibling();
  1785. if (bboxNode.isNull()) {
  1786. BoundingBox *bbox = new BoundingBox;
  1787. bbox->rect.setTopLeft(topLeft);
  1788. bbox->rect.setBottomRight(bottomRight);
  1789. bbox->label_ID_ = labelID;
  1790. addBBox(bbox);
  1791. }
  1792. }
  1793. }
  1794. subNode = subNode.nextSibling();
  1795. }
  1796. }
  1797. }
  1798. rootNode = rootNode.nextSibling();
  1799. }
  1800. if (!image_->load(path + "/JPEGImages/" + filename)) {
  1801. return false;
  1802. /* NOTREACHED */
  1803. }
  1804. current_image_ = path + "/JPEGImages/" + filename;
  1805. QString winTitle;
  1806. winTitle.append("ImageLabeler - ");
  1807. winTitle.append(current_image_);
  1808. setWindowTitle(winTitle);
  1809. image_holder_->resize(image_->size());
  1810. image_holder_->setPixmap(*image_);
  1811. unsaved_data_ = 0;
  1812. return true;
  1813. }
  1814. //! A slot member loading only information about polygons on the image
  1815. /*!
  1816. * \see loadPascalPolys(QString aFilename)
  1817. * Slot parses text file with .polygon suffix
  1818. */
  1819. void
  1820. ImageLabeler::loadPascalPolys()
  1821. {
  1822. if (current_image_.isEmpty()) {
  1823. return;
  1824. /* NOTREACHED */
  1825. }
  1826. QFileDialog fileDialog(0, tr("Load pascal polygons"));
  1827. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1828. fileDialog.setDefaultSuffix("polygon");
  1829. fileDialog.setFileMode(QFileDialog::AnyFile);
  1830. QStringList filters;
  1831. filters << "PASCAL polygons (*.polygon)"
  1832. << "Any files (*)";
  1833. fileDialog.setNameFilters(filters);
  1834. QString filename;
  1835. if (fileDialog.exec()) {
  1836. filename = fileDialog.selectedFiles().last();
  1837. }
  1838. else {
  1839. //showWarning(tr("Can not open file dialog"));
  1840. return;
  1841. /* NOTREACHED */
  1842. }
  1843. //clearAllTool();
  1844. //clearLabelList();
  1845. if (!loadPascalPolys(filename)) {
  1846. showWarning(tr("File format is corrupted."));
  1847. }
  1848. unsaved_data_ = 0;
  1849. image_holder_->update();
  1850. }
  1851. //! A slot member loading only information about polygons on the image
  1852. /*!
  1853. * Slot parses text file with .polygon suffix
  1854. *
  1855. * returns true on success
  1856. */
  1857. bool
  1858. ImageLabeler::loadPascalPolys(QString aFilename)
  1859. {
  1860. QFile file(aFilename);
  1861. if (!file.open(QIODevice::ReadOnly)) {
  1862. showWarning(tr("Can not open such file"));
  1863. return false;
  1864. /* NOTREACHED */
  1865. }
  1866. QByteArray data = file.readAll();
  1867. file.close();
  1868. QString label;
  1869. int lastSpace = 0;
  1870. int pointCount = 0;
  1871. Polygon *poly = 0;
  1872. QPoint point;
  1873. bool evenFlag = 0;
  1874. //for (int i = 0; i < data.length(); i++) {
  1875. int i = 0;
  1876. while (i < data.length()) {
  1877. if (data.at(i) == ' ' && label.isEmpty() && !pointCount) {
  1878. label = QString(data.mid(lastSpace, i - lastSpace));
  1879. lastSpace = i + 1;
  1880. }
  1881. else if (data.at(i) == ' ' && !label.isEmpty() && !pointCount) {
  1882. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1883. bool ok = 1;
  1884. pointCount = num.toInt(&ok, 10);
  1885. if (!ok) {
  1886. return false;
  1887. /* NOTREACHED */
  1888. }
  1889. poly = new Polygon;
  1890. poly->label_ID_ = -1;
  1891. lastSpace = i + 1;
  1892. }
  1893. else if (data.at(i) == ' ' &&
  1894. !label.isEmpty() &&
  1895. pointCount &&
  1896. !evenFlag)
  1897. {
  1898. evenFlag = 1;
  1899. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1900. bool ok = 1;
  1901. int coor = qRound(num.toDouble(&ok));
  1902. if (!ok) {
  1903. return false;
  1904. /* NOTREACHED */
  1905. }
  1906. point.setX(coor);
  1907. lastSpace = i + 1;
  1908. }
  1909. else if (data.at(i) == ' ' &&
  1910. !label.isEmpty() &&
  1911. pointCount &&
  1912. evenFlag)
  1913. {
  1914. evenFlag = 0;
  1915. pointCount--;
  1916. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1917. bool ok = 1;
  1918. int coor = qRound(num.toDouble(&ok));
  1919. if (!ok) {
  1920. return false;
  1921. /* NOTREACHED */
  1922. }
  1923. point.setY(coor);
  1924. lastSpace = i + 1;
  1925. poly->poly << point;
  1926. }
  1927. else if (data.at(i) == '\n') {
  1928. if (pointCount || !poly) {
  1929. return false;
  1930. /* NOTREACHED */
  1931. }
  1932. lastSpace = i + 1;
  1933. for (int j = 0; j < list_label_->count(); j++) {
  1934. if (list_label_->item(j)->text().contains(label, Qt::CaseInsensitive)) {
  1935. poly->label_ID_ = j;
  1936. }
  1937. }
  1938. if (-1 == poly->label_ID_) {
  1939. int labelID = list_label_->count();
  1940. addLabel(labelID, 0, label);
  1941. poly->label_ID_ = labelID;
  1942. }
  1943. addPoly(poly);
  1944. label.clear();
  1945. }
  1946. i++;
  1947. }
  1948. return true;
  1949. }
  1950. //! A Slot member loading single image.
  1951. /*!
  1952. * \see loadImages()
  1953. *
  1954. * Slot asks for unsaved data and loads an image.
  1955. * If selected image was previously labeled and the file with data
  1956. * was named like imagename_labeled.dat then it would be loaded
  1957. * with all data
  1958. */
  1959. void
  1960. ImageLabeler::loadImage()
  1961. {
  1962. if (!askForUnsavedData()) {
  1963. return;
  1964. /* NOTREACHED */
  1965. }
  1966. QFileDialog fileDialog(0, tr("Load image"));
  1967. fileDialog.setFileMode(QFileDialog::ExistingFile);
  1968. QStringList filters;
  1969. filters << "Image files (*.jpeg *.jpg *.gif *.png *.bmp *.tiff *.pgm)"
  1970. << "Any files (*)";
  1971. fileDialog.setNameFilters(filters);
  1972. QString filename;
  1973. if (fileDialog.exec()) {
  1974. filename = fileDialog.selectedFiles().last();
  1975. }
  1976. else {
  1977. //showWarning(tr("Could not open file dialog"));
  1978. return;
  1979. /* NOTREACHED */
  1980. }
  1981. if (filename.isEmpty()) {
  1982. return;
  1983. /* NOTREACHED */
  1984. }
  1985. /* checking if it was previously labeled */
  1986. QString dirPath = getPathFromFilename(filename);
  1987. QDir dir(dirPath);
  1988. QStringList filter;
  1989. filter << "*.dat";
  1990. QStringList fileList = dir.entryList(filter, QDir::Files);
  1991. QString labeled = alterFileName(filename, "_labeled");
  1992. labeled = removePath(labeled);
  1993. labeled.append(".dat");
  1994. if (fileList.contains(labeled, Qt::CaseInsensitive)) {
  1995. labeled = dir.absoluteFilePath(labeled);
  1996. loadInfo(labeled);
  1997. Image *newImage = new Image;
  1998. newImage->image_ = filename;
  1999. newImage->labeled_ = 1;
  2000. newImage->pas_ = 0;
  2001. addImage(newImage);
  2002. enableTools();
  2003. return;
  2004. /* NOTREACHED */
  2005. }
  2006. clearAllTool();
  2007. QString winTitle;
  2008. winTitle.append("ImageLabeler - ");
  2009. winTitle.append(current_image_);
  2010. setWindowTitle(winTitle);
  2011. if (!image_->load(filename)) {
  2012. return;
  2013. /* NOTREACHED */
  2014. }
  2015. image_holder_->resize(image_->size());
  2016. image_holder_->setPixmap(*image_);
  2017. current_image_ = filename;
  2018. image_ID_ = list_images_widget_->count() - 1;
  2019. list_images_widget_->setCurrentRow(image_ID_);
  2020. Image *newImage = new Image;
  2021. newImage->image_ = filename;
  2022. newImage->labeled_ = 0;
  2023. newImage->pas_ = 0;
  2024. addImage(newImage);
  2025. if (!list_images_widget_->count()) {
  2026. return;
  2027. /* NOTREACHED */
  2028. }
  2029. enableTools();
  2030. }
  2031. //! A slot member loading images recursively
  2032. /*!
  2033. * \see getImagesFromDir(const QDir &dir)
  2034. *
  2035. * Slot asks for unsaved data.
  2036. * It gives user a possibility to break a recursive search(with the widget
  2037. * and a button "cancel" on it.)
  2038. *
  2039. */
  2040. void
  2041. ImageLabeler::loadImages()
  2042. {
  2043. if (!askForUnsavedData()) {
  2044. return;
  2045. /* NOTREACHED */
  2046. }
  2047. QFileDialog fileDialog(0, tr("Load images"));
  2048. fileDialog.setFileMode(QFileDialog::Directory);
  2049. QString dirName("");
  2050. if (fileDialog.exec()) {
  2051. /* TODO: make it possible to select multiple folders */
  2052. dirName = fileDialog.selectedFiles().last();
  2053. }
  2054. else {
  2055. //showWarning(tr("Could not open file dialog"));
  2056. return;
  2057. /* NOTREACHED */
  2058. }
  2059. clearAllTool();
  2060. /* creating a widget with a "cancel" button to
  2061. * give user an opportunity to stop the recursive search */
  2062. QWidget *searchInProgress = new QWidget(0);
  2063. searchInProgress->setWindowTitle(tr("Loading images"));
  2064. QLabel *info = new QLabel(searchInProgress);
  2065. info->setText(
  2066. tr("Program is looking for all image files in your directory recursively."));
  2067. QPushButton *cancel = new QPushButton(tr("Cancel"), searchInProgress);
  2068. connect(cancel, SIGNAL(clicked()), this, SLOT(interruptSearch()));
  2069. QVBoxLayout *layout = new QVBoxLayout(searchInProgress);
  2070. searchInProgress->setLayout(layout);
  2071. layout->addWidget(info);
  2072. layout->addWidget(cancel);
  2073. searchInProgress->adjustSize();
  2074. searchInProgress->move(QApplication::desktop()->screen()->rect().center() - rect().center());
  2075. searchInProgress->show();
  2076. getImagesFromDir(QDir(dirName));
  2077. /* deleting everything related to searchInProgress widget */
  2078. cancel->disconnect();
  2079. searchInProgress->hide();
  2080. delete info;
  2081. delete cancel;
  2082. delete layout;
  2083. delete searchInProgress;
  2084. if (interrupt_search_) {
  2085. interrupt_search_ = 0;
  2086. return;
  2087. /* NOTREACHED */
  2088. }
  2089. if (!list_images_widget_->count()) {
  2090. showWarning(tr("The folder you selected contains no images"));
  2091. return;
  2092. /* NOTREACHED */
  2093. }
  2094. bool ret = 0;
  2095. if (list_images_->at(0).labeled_) {
  2096. QString labeled =
  2097. alterFileName(list_images_->at(0).image_, "_labeled");
  2098. labeled.append(".dat");
  2099. ret = loadInfo(labeled);
  2100. }
  2101. else
  2102. ret = image_->load(list_images_->at(0).image_);
  2103. if (!ret) {
  2104. return;
  2105. /* NOTREACHED */
  2106. }
  2107. current_image_ = list_images_->at(0).image_;
  2108. image_ID_ = 0;
  2109. list_images_widget_->setCurrentRow(image_ID_);
  2110. QString winTitle;
  2111. winTitle.append("ImageLabeler - ");
  2112. winTitle.append(current_image_);
  2113. setWindowTitle(winTitle);
  2114. image_holder_->resize(image_->size());
  2115. image_holder_->setPixmap(*image_);
  2116. enableTools();
  2117. }
  2118. //! A slot member loading legend(labels) from xml file
  2119. /*!
  2120. * \see loadLegendFromNode(QDomElement *anElement)
  2121. */
  2122. void
  2123. ImageLabeler::loadLegendFromFile()
  2124. {
  2125. QFileDialog fileDialog(0, tr("Load legend"));
  2126. fileDialog.setFileMode(QFileDialog::AnyFile);
  2127. QString filename;
  2128. if (fileDialog.exec()) {
  2129. filename = fileDialog.selectedFiles().last();
  2130. }
  2131. else {
  2132. //showWarning(tr("Could not open file dialog"));
  2133. return;
  2134. /* NOTREACHED */
  2135. }
  2136. QDomDocument doc("Image Labeler");
  2137. QFile file(filename);
  2138. if (!file.open(QIODevice::ReadOnly)) {
  2139. showWarning(tr("Can not open such file"));
  2140. return;
  2141. /* NOTREACHED */
  2142. }
  2143. QString errMsg;
  2144. if (!doc.setContent(&file, &errMsg)) {
  2145. showWarning(errMsg);
  2146. file.close();
  2147. return;
  2148. /* NOTREACHED */
  2149. }
  2150. file.close();
  2151. list_label_->clear();
  2152. /* getting legend */
  2153. QDomElement elements = doc.documentElement();
  2154. QDomNode rootNode = elements.firstChild();
  2155. QString string;
  2156. while(!rootNode.isNull()) {
  2157. QDomElement element = rootNode.toElement();
  2158. if(!element.isNull()) {
  2159. if (element.tagName() == "legend") {
  2160. loadLegendFromNode(&element);
  2161. }
  2162. }
  2163. rootNode = rootNode.nextSibling();
  2164. }
  2165. }
  2166. //! A protected member loading legend from xml node
  2167. /*!
  2168. * \see addLabel(int aLabelID, bool isMain, QString aLabel)
  2169. * \see setLabelColor(int anID, QColor aColor)
  2170. *
  2171. * Slot gets all label information from xml node.
  2172. */
  2173. void
  2174. ImageLabeler::loadLegendFromNode(QDomElement *anElement)
  2175. {
  2176. if (!anElement) {
  2177. return;
  2178. /* NOTREACHED */
  2179. }
  2180. QDomNode subNode = anElement->firstChild();
  2181. QDomElement subElement;
  2182. QString string;
  2183. int id = -1;
  2184. bool isMain;
  2185. uint color = 0xff000000;
  2186. while(!subNode.isNull()) {
  2187. subElement = subNode.toElement();
  2188. if (!subElement.isNull() && !subElement.text().isEmpty()) {
  2189. /* id attribute */
  2190. string = subElement.attribute("id");
  2191. bool ok = 0;
  2192. id = string.toInt(&ok, 10);
  2193. if (!ok) {
  2194. qDebug() <<
  2195. "loadLegendFromNode: "
  2196. "label id format is corrupted";
  2197. subNode = subNode.nextSibling();
  2198. continue;
  2199. }
  2200. /* isMain attribute */
  2201. string = subElement.attribute("isMain");
  2202. isMain = string.toInt(&ok, 2);
  2203. if (!ok) {
  2204. qDebug() <<
  2205. "loadLegendFromNode: "
  2206. "label isMain flag format is corrupted";
  2207. subNode = subNode.nextSibling();
  2208. continue;
  2209. }
  2210. /* color attribute */
  2211. string = subElement.attribute("color");
  2212. color = string.toUInt(&ok, 16);
  2213. if (!ok) {
  2214. qDebug() <<
  2215. "loadLegendFromNode: "
  2216. "label color format is corrupted";
  2217. subNode = subNode.nextSibling();
  2218. continue;
  2219. }
  2220. /* label name */
  2221. string = subElement.text();
  2222. addLabel(id, isMain, string);
  2223. setLabelColor(id, color);
  2224. }
  2225. subNode = subNode.nextSibling();
  2226. }
  2227. }
  2228. //! A protected member converting all the label information into xml format
  2229. /*!
  2230. * \see objectsToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2231. * \param[in] aDoc a pointer to QDomDocument object - represents an xml document
  2232. * \param[out] aRoot a pointer to QDomElement object - contains all the results from
  2233. * the converting
  2234. */
  2235. void
  2236. ImageLabeler::legendToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2237. {
  2238. QDomElement legend = aDoc->createElement(tr("legend"));
  2239. aRoot->appendChild(legend);
  2240. /* storing all labels made by user */
  2241. int labelCount = list_label_->count();
  2242. for (int i = 0; i < labelCount; i++) {
  2243. QDomElement label = aDoc->createElement(tr("label"));
  2244. label.setAttribute("color", QString("%1").arg(list_label_colors_.at(i), 0, 16));
  2245. label.setAttribute("id", i);
  2246. QString priority;
  2247. if (main_label_ == i)
  2248. priority.append("1");
  2249. else
  2250. priority.append("0");
  2251. label.setAttribute("isMain", priority);
  2252. QString labelText = list_label_->item(i)->text();
  2253. /* removing the number prefix of label */
  2254. if (-1 != labelText.indexOf(QString("%1: ").arg(i))) {
  2255. labelText = labelText.mid(3, labelText.size() - 3);
  2256. }
  2257. QDomText labelName = aDoc->createTextNode(labelText);
  2258. label.appendChild(labelName);
  2259. legend.appendChild(label);
  2260. }
  2261. /* in case we have no labels */
  2262. if (0 == labelCount) {
  2263. QDomElement label = aDoc->createElement(tr("label"));
  2264. label.setAttribute(tr("id"), -1);
  2265. legend.appendChild(label);
  2266. }
  2267. }
  2268. //! A protected member converting all the objects information into xml format
  2269. /*!
  2270. * \see legendToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2271. * \param[in] aDoc a pointer to the QDomDocument object - represents an xml document
  2272. * \param[out] aRoot a pointer to the QDomElement object - contains all the results from
  2273. * the converting
  2274. */
  2275. void
  2276. ImageLabeler::objectsToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2277. {
  2278. QDomElement objects = aDoc->createElement(tr("objects"));
  2279. aRoot->appendChild(objects);
  2280. /* rects first */
  2281. for (int i = 0; i < list_bounding_box_.size(); i++) {
  2282. QDomElement rectData = aDoc->createElement(tr("bbox"));
  2283. rectData.setAttribute("id", list_bounding_box_.at(i)->label_ID_);
  2284. QRect rect = list_bounding_box_.at(i)->rect.normalized();
  2285. QString rectDataString =
  2286. QString("%1;%2;%3;%4;").
  2287. arg(rect.x()).
  2288. arg(rect.y()).
  2289. arg(rect.width()).
  2290. arg(rect.height());
  2291. QDomText rectDataText = aDoc->createTextNode(rectDataString);
  2292. rectData.appendChild(rectDataText);
  2293. objects.appendChild(rectData);
  2294. }
  2295. /* polys next */
  2296. for (int i = 0; i < list_polygon_.size(); i++) {
  2297. QDomElement polyData = aDoc->createElement(tr("poly"));
  2298. polyData.setAttribute("id", list_polygon_.at(i)->label_ID_);
  2299. QPolygon poly = list_polygon_.at(i)->poly;
  2300. QString polyDataString;
  2301. for (int j = 0; j < poly.count(); j++)
  2302. polyDataString.append(
  2303. QString("%1;%2;").
  2304. arg(poly.point(j).x()).
  2305. arg(poly.point(j).y())
  2306. );
  2307. QDomText polyDataText = aDoc->createTextNode(polyDataString);
  2308. polyData.appendChild(polyDataText);
  2309. objects.appendChild(polyData);
  2310. }
  2311. }
  2312. //! A slot member generating color for all labels
  2313. /*!
  2314. * A very primitive temporary solution for color generation
  2315. */
  2316. void
  2317. ImageLabeler::generateColors()
  2318. {
  2319. int labelCount = list_label_->count();
  2320. if (!labelCount) {
  2321. showWarning(tr("you have not added any labels yet"));
  2322. return;
  2323. /* NOTREACHED */
  2324. }
  2325. int coeff = (0xff / labelCount) * 3;
  2326. list_label_colors_.clear();
  2327. list_label_colors_.append(0);
  2328. uchar red = 0xff;
  2329. uchar green = 0xff;
  2330. uchar blue = 0xff;
  2331. uchar iterationColor = coeff;
  2332. uint color = 0xffffffff;
  2333. int j = 1;
  2334. for (int i = 1; i < labelCount; i++) {
  2335. if (6 == j) {
  2336. iterationColor += coeff;
  2337. j = 1;
  2338. }
  2339. if (5 == j) {
  2340. red = 0xff - iterationColor;
  2341. green = 0xff - iterationColor;
  2342. blue = 0xff;
  2343. }
  2344. else if (4 == j) {
  2345. red = 0xff - iterationColor;
  2346. green = 0xff;
  2347. blue = 0xff - iterationColor;
  2348. }
  2349. else if (3 == j) {
  2350. red = 0xff - iterationColor;
  2351. green = 0xff;
  2352. blue = 0xff;
  2353. }
  2354. else if (2 == j) {
  2355. red = 0xff;
  2356. green = 0xff - iterationColor;
  2357. blue = 0xff;
  2358. }
  2359. else if (1 == j){
  2360. red = 0xff;
  2361. green = 0xff;
  2362. blue = 0xff - iterationColor;
  2363. }
  2364. j++;
  2365. color = red + (green * 0x100) + (blue * 0x10000) + 0xff000000;
  2366. int itemNo = list_label_colors_.count();
  2367. QPixmap iconPix = QPixmap(20, 20);
  2368. iconPix.fill(color);
  2369. QIcon icon(iconPix);
  2370. list_label_->item(itemNo)->setIcon(icon);
  2371. list_label_colors_.append(color);
  2372. }
  2373. image_holder_->update();
  2374. }
  2375. //! A protected member creating new boundary box from text data
  2376. /*!
  2377. * \see addBBoxArea(int anID, BoundingBox aBBox, int itemID)
  2378. * \see BBoxFromData(QString *aBBoxData)
  2379. *
  2380. * It creates bbox both in the container and in the list widget
  2381. */
  2382. void
  2383. ImageLabeler::addBBoxFromData(
  2384. QString *aBBoxData,
  2385. int *ID
  2386. )
  2387. {
  2388. BoundingBox *bbox = new BoundingBox;
  2389. if (!this->BBoxFromData(aBBoxData, *bbox) )
  2390. return;
  2391. if (!bbox->rect.isValid() || !ID)
  2392. {
  2393. return;
  2394. }
  2395. bbox->label_ID_ = *ID;
  2396. list_bounding_box_.append(bbox);
  2397. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  2398. }
  2399. //! A protected member parsing string data and returning a BoundingBox from it
  2400. /*!
  2401. * \see addBBoxArea(int anID, BoundingBox aBBox, int itemID)
  2402. *
  2403. * format is x;y;w;h where w - width and h - height
  2404. */
  2405. bool ImageLabeler::BBoxFromData(const QString *aBBoxData, BoundingBox &p_BBox)
  2406. {
  2407. QString buffer;
  2408. p_BBox.rect.setRect(-1, -1, -1, -1);
  2409. int startPos = 0;
  2410. bool ok = 1;
  2411. for (int i = 0; i < aBBoxData->size(); i++) {
  2412. if (';' != aBBoxData->at(i))
  2413. continue;
  2414. buffer = aBBoxData->mid(startPos, i - startPos);
  2415. int bboxData = buffer.toInt(&ok, 10);
  2416. if (!ok) {
  2417. qDebug() <<
  2418. "BBoxFromData: "
  2419. "bbox format is corrupted";
  2420. break;
  2421. }
  2422. if (-1 == p_BBox.rect.x()) {
  2423. p_BBox.rect.setX(bboxData);
  2424. p_BBox.rect.setWidth(-1);
  2425. }
  2426. else if (-1 == p_BBox.rect.y()) {
  2427. p_BBox.rect.setY(bboxData);
  2428. p_BBox.rect.setHeight(-1);
  2429. }
  2430. else if (-1 == p_BBox.rect.width()) {
  2431. p_BBox.rect.setWidth(bboxData);
  2432. }
  2433. else if (-1 == p_BBox.rect.height()) {
  2434. p_BBox.rect.setHeight(bboxData);
  2435. }
  2436. startPos = i + 1;
  2437. }
  2438. if (!p_BBox.rect.isValid()) {
  2439. qDebug() <<
  2440. "BBoxFromData: "
  2441. "bbox format is corrupted";
  2442. p_BBox.rect.setRect(-1, -1, -1, -1);
  2443. return false;
  2444. }
  2445. else if (!ok) {
  2446. p_BBox.rect.setRect(-1, -1, -1, -1);
  2447. return false;
  2448. }
  2449. return true;
  2450. }
  2451. //! A protected member getting a BoundaryBox from text of the list_areas_ item
  2452. /*!
  2453. * \see BBoxFromData(QString *aBBoxData)
  2454. *
  2455. * Slot gets label ID an points from the string data. BBox ID can not be changed.
  2456. */
  2457. BoundingBox
  2458. ImageLabeler::BBoxFromListItemText(
  2459. QString *aString,
  2460. int *oldID
  2461. )
  2462. {
  2463. BoundingBox bbox;
  2464. bbox.label_ID_ = -1;
  2465. *oldID = -1;
  2466. if (!aString) {
  2467. return bbox;
  2468. /* NOTREACHED */
  2469. }
  2470. if (-1 == aString->indexOf("BBox")) {
  2471. return bbox;
  2472. /* NOTREACHED */
  2473. }
  2474. /* getting bbox id in the list(it cannot be changed) */
  2475. bool ok = 0;
  2476. int bboxID = getNumFromString(aString, "BBox #", ";", &ok);
  2477. if (!ok || bboxID <= -1) {
  2478. qDebug() <<
  2479. "BBoxFromListItemText: bboxID is corrupted";
  2480. return bbox;
  2481. /* NOTREACHED */
  2482. }
  2483. /* getting new label id */
  2484. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  2485. if (!ok || labelID <= -1) {
  2486. showWarning(
  2487. tr("new LabelID is wrong, area can not be changed")
  2488. );
  2489. return bbox;
  2490. /* NOTREACHED */
  2491. }
  2492. /* getting new points */
  2493. int pointsPos = aString->indexOf("data:") + 5;
  2494. int pointsLen = aString->size() - pointsPos;
  2495. if (pointsLen <= 0) {
  2496. showWarning(
  2497. tr("new data is wrong, area can not be changed")
  2498. );
  2499. return bbox;
  2500. /* NOTREACHED */
  2501. }
  2502. QString pointsData = aString->mid(pointsPos, pointsLen);
  2503. BBoxFromData(&pointsData, bbox);
  2504. bbox.label_ID_ = labelID;
  2505. *oldID = bboxID;
  2506. return bbox;
  2507. }
  2508. //! A protected member adding poly to the list widget and to the list container
  2509. /*!
  2510. * \see addBBox(BoundingBox *bbox)
  2511. * \param[in] poly a pointer to the Polygon structure
  2512. * containing new poly information
  2513. *
  2514. */
  2515. void
  2516. ImageLabeler::addPoly(Polygon *poly)
  2517. {
  2518. if (poly->poly.isEmpty() || poly->label_ID_ < 0) {
  2519. return;
  2520. /* NOTREACHED */
  2521. }
  2522. list_polygon_.append(poly);
  2523. addPolyArea(list_polygon_.count() - 1, *poly);
  2524. }
  2525. //! A protected member adding bounding box to the list widget
  2526. //! and to the list container
  2527. /*!
  2528. * \see addPoly(Polygon *poly)
  2529. * \param[in] bbox a pointer to the BoundingBox structure
  2530. * containing new bounding box information
  2531. *
  2532. */
  2533. void
  2534. ImageLabeler::addBBox(BoundingBox *bbox)
  2535. {
  2536. if (bbox->rect.isEmpty() || bbox->label_ID_ < 0) {
  2537. return;
  2538. /* NOTREACHED */
  2539. }
  2540. list_bounding_box_.append(bbox);
  2541. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  2542. }
  2543. //! A protected member creating new polygon from text data
  2544. /*!
  2545. * \see addPolyArea(int anID, Polygon aPoly, int itemID)
  2546. * \see polyFromData(QString *aPolyData)
  2547. *
  2548. * It creates polygon both in the container and in the list widget
  2549. */
  2550. void
  2551. ImageLabeler::addPolyFromData(
  2552. QString *aPolyData,
  2553. int *labelID
  2554. )
  2555. {
  2556. Polygon *poly = new Polygon;
  2557. *poly = polyFromData(aPolyData);
  2558. if (poly->poly.isEmpty() || !labelID) {
  2559. return;
  2560. /* NOTREACHED */
  2561. }
  2562. poly->label_ID_ = *labelID;
  2563. list_polygon_.append(poly);
  2564. addPolyArea(list_polygon_.count() - 1, *poly);
  2565. }
  2566. //! A protected member parsing string data and returning a Polygon from it
  2567. /*!
  2568. * \see addPolyArea(int anID, Polygon aPoly, int itemID)
  2569. *
  2570. * format is x0;y0;x1;y1;...
  2571. */
  2572. Polygon
  2573. ImageLabeler::polyFromData(
  2574. QString *aPolyData
  2575. )
  2576. {
  2577. Polygon poly;
  2578. poly.label_ID_ = -1;
  2579. QPoint point;
  2580. QString buffer;
  2581. int startPos = 0;
  2582. bool ok = 1;
  2583. /* indicates whether coordinate x or y */
  2584. bool evenFlag = 0;
  2585. ///jojo
  2586. QStringList coordsList = aPolyData->split(";", QString::SkipEmptyParts);
  2587. Polygon newPoly;
  2588. try
  2589. {
  2590. if( coordsList.size() % 2 == 0)
  2591. {
  2592. for( int i = 0; i < coordsList.size(); i += 2)
  2593. {
  2594. newPoly.poly.append( QPoint( QVariant(coordsList[i]).toInt(),
  2595. QVariant(coordsList[i+1]).toInt() ) );
  2596. }
  2597. }
  2598. }
  2599. catch(std::exception &e)
  2600. {
  2601. newPoly.poly.clear();
  2602. }
  2603. return newPoly;
  2604. ///#jojo
  2605. /*
  2606. for (int i = 0; i < aPolyData->size(); i++) {
  2607. // ";" is a separator
  2608. if (';' != aPolyData->at(i))
  2609. continue;
  2610. buffer = aPolyData->mid(startPos, i - startPos);
  2611. int polyCoor = buffer.toInt(&ok, 10);
  2612. if (!ok) {
  2613. qDebug() <<
  2614. "polyFromData: "
  2615. "poly format is corrupted";
  2616. break;
  2617. }
  2618. if (!evenFlag) {
  2619. point.setX(polyCoor);
  2620. evenFlag = 1;
  2621. }
  2622. else {
  2623. point.setY(polyCoor);
  2624. poly.poly.append(point);
  2625. evenFlag = 0;
  2626. }
  2627. startPos = i + 1;
  2628. }
  2629. // last coordinate was Xi what means an error
  2630. if (evenFlag) {
  2631. qDebug() <<
  2632. "polyFromData: "
  2633. "poly format is corrupted";
  2634. poly.poly.clear();
  2635. }
  2636. // last converting from string was not successful
  2637. else if (!ok) {
  2638. poly.poly.clear();
  2639. }
  2640. return poly;
  2641. */
  2642. }
  2643. //! A protected member getting a Polygon from text of the list_areas_ item
  2644. /*!
  2645. * \see polyFromData(QString *aPolyData)
  2646. *
  2647. * Slot gets label ID an points from the string data. Poly ID can not be changed.
  2648. */
  2649. Polygon
  2650. ImageLabeler::polyFromListItemText(
  2651. QString *aString,
  2652. int *oldID
  2653. )
  2654. {
  2655. Polygon poly;
  2656. poly.label_ID_ = -1;
  2657. *oldID = -1;
  2658. if (!aString) {
  2659. return poly;
  2660. /* NOTREACHED */
  2661. }
  2662. if (-1 == aString->indexOf("Poly")) {
  2663. return poly;
  2664. /* NOTREACHED */
  2665. }
  2666. /* getting poly id in the list(it cannot be changed) */
  2667. bool ok = 0;
  2668. int polyID = getNumFromString(aString, "Poly #", ";", &ok);
  2669. if (!ok || polyID <= -1) {
  2670. qDebug() <<
  2671. "polyFromListItemText: poly ID is corrupted";
  2672. return poly;
  2673. /* NOTREACHED */
  2674. }
  2675. /* getting new label id */
  2676. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  2677. if (!ok || labelID <= -1) {
  2678. showWarning(
  2679. tr("new LabelID is wrong, area can not be changed")
  2680. );
  2681. return poly;
  2682. /* NOTREACHED */
  2683. }
  2684. /* getting new points */
  2685. int pointsPos = aString->indexOf("points:") + 7;
  2686. int pointsLen = aString->size() - pointsPos;
  2687. if (pointsLen <= 0) {
  2688. showWarning(
  2689. tr("new points data is wrong, area can not be changed")
  2690. );
  2691. return poly;
  2692. /* NOTREACHED */
  2693. }
  2694. QString pointsData = aString->mid(pointsPos, pointsLen);
  2695. poly = polyFromData(&pointsData);
  2696. poly.label_ID_ = labelID;
  2697. *oldID = polyID;
  2698. return poly;
  2699. }
  2700. //! A protected member only for internal use. Showing a message box with warning
  2701. /*!
  2702. * \param[in] text a QString object containing message you want
  2703. * to deliver to the user
  2704. */
  2705. void
  2706. ImageLabeler::showWarning(
  2707. const QString &text
  2708. )
  2709. {
  2710. if (text.isEmpty()) {
  2711. return;
  2712. /* NOTREACHED */
  2713. }
  2714. QMessageBox msgBox;
  2715. msgBox.setText(text);
  2716. msgBox.setIcon(QMessageBox::Warning);
  2717. msgBox.exec();
  2718. }
  2719. //! A protected member only for internal use.
  2720. //! Shows a message box asking about unsaved progress
  2721. /*!
  2722. * It depends on object containers(list_bounding_box_ and list_polygon_)
  2723. * and on unsaved_data_ flag which must be true to make this dialog appear
  2724. */
  2725. bool
  2726. ImageLabeler::askForUnsavedData()
  2727. {
  2728. if ((!list_bounding_box_.isEmpty() ||
  2729. !list_polygon_.isEmpty()) &&
  2730. unsaved_data_)
  2731. {
  2732. QMessageBox msgBox;
  2733. msgBox.setText(tr("There is some unsaved data"));
  2734. msgBox.setInformativeText(tr("Do you want to save your progress?"));
  2735. msgBox.setStandardButtons(
  2736. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  2737. msgBox.setDefaultButton(QMessageBox::Save);
  2738. msgBox.setIcon(QMessageBox::Question);
  2739. int ret = msgBox.exec();
  2740. if (QMessageBox::Save == ret)
  2741. saveAllInfo();
  2742. else if (QMessageBox::Cancel == ret)
  2743. return true;
  2744. }
  2745. return true;
  2746. }
  2747. //! A slot member setting bbox tool active
  2748. /*!
  2749. * \param[in] aButtonPressed is a bool flag which is needed for toggled buttons
  2750. */
  2751. void
  2752. ImageLabeler::setBoundingBoxTool(bool aButtonPressed)
  2753. {
  2754. if (aButtonPressed)
  2755. image_holder_->setTool(ImageHolder::BoundingBoxTool);
  2756. else {
  2757. image_holder_->setTool(ImageHolder::NoTool);
  2758. image_holder_->clearLast();
  2759. }
  2760. }
  2761. //! A slot member setting polygon tool active
  2762. /*!
  2763. * \param[in] aButtonPressed is a bool flag which is needed for toggled buttons
  2764. */
  2765. void
  2766. ImageLabeler::setPolygonTool(bool aButtonPressed)
  2767. {
  2768. if (aButtonPressed) {
  2769. image_holder_->setTool(ImageHolder::PolygonTool);
  2770. image_holder_->setFocus();
  2771. }
  2772. else {
  2773. image_holder_->setTool(ImageHolder::NoTool);
  2774. image_holder_->clearLast();
  2775. }
  2776. }
  2777. //! A slot member enables button_confirm_selection_ button
  2778. void
  2779. ImageLabeler::onSelectionStarted()
  2780. {
  2781. button_confirm_selection_->setEnabled(true);
  2782. }
  2783. //! A slot member adding information about new selected area
  2784. /*!
  2785. * \see addBBoxArea(int anID, BoundingBox aBBox, int itemID)
  2786. * \see addPolyArea(int aPolyID, Polygon aPoly, int itemID)
  2787. *
  2788. * After user has created a new selection area it should be confirmed or else it
  2789. * wouldn't be added to the item list and containers. This slot takes all data from
  2790. * unconfirmed selection and places it into list widget and corresponding container
  2791. * (list_bounding_box_ or list_polygon_)
  2792. */
  2793. void
  2794. ImageLabeler::confirmSelection()
  2795. {
  2796. if (!list_label_->count()) {
  2797. showWarning(tr("You haven't added any label"));
  2798. return;
  2799. /* NOTREACHED */
  2800. }
  2801. image_holder_->confirmSelection();
  2802. if (label_ID_ < 0)
  2803. label_ID_ = 0;
  2804. ImageHolder::Tool tool = image_holder_->tool();
  2805. switch (tool) {
  2806. case ImageHolder::BoundingBoxTool:
  2807. list_bounding_box_.last()->label_ID_ = label_ID_;
  2808. addBBoxArea(
  2809. list_bounding_box_.count() - 1,
  2810. *(list_bounding_box_.last())
  2811. );
  2812. break;
  2813. case ImageHolder::PolygonTool:
  2814. list_polygon_.last()->label_ID_ = label_ID_;
  2815. addPolyArea(
  2816. list_polygon_.count() - 1,
  2817. *(list_polygon_.last())
  2818. );
  2819. break;
  2820. default:
  2821. break;
  2822. }
  2823. button_confirm_selection_->setEnabled(false);
  2824. button_delete_area_->setEnabled(true);
  2825. button_change_area_->setEnabled(true);
  2826. button_change_area_text_->setEnabled(true);
  2827. unsaved_data_ = 1;
  2828. }
  2829. //! \brief A slot member clears all widgets and containers
  2830. //! (not recommended to use without necessity)
  2831. /*!
  2832. * \see clearAllTool()
  2833. * \see clearLabelList()
  2834. * \see clearLabelColorList()
  2835. */
  2836. void
  2837. ImageLabeler::clearAll()
  2838. {
  2839. clearLabelList();
  2840. list_areas_->clear();
  2841. list_bounding_box_.clear();
  2842. list_polygon_.clear();
  2843. list_images_->clear();
  2844. list_images_widget_->clear();
  2845. main_label_ = -1;
  2846. image_holder_->clearAll();
  2847. segmented_image_.clear();
  2848. action_view_normal_->setEnabled(false);
  2849. action_view_segmented_->setEnabled(false);
  2850. }
  2851. //! A slot member clears only selections made by user
  2852. /*!
  2853. * \see clearAll()
  2854. * \see clearLabelList()
  2855. * \see clearLabelColorList()
  2856. */
  2857. void
  2858. ImageLabeler::clearAllTool()
  2859. {
  2860. list_areas_->clear();
  2861. list_bounding_box_.clear();
  2862. list_polygon_.clear();
  2863. main_label_ = -1;
  2864. image_holder_->clearAll();
  2865. }
  2866. //! A slot member clears label list
  2867. /*!
  2868. * \see clearAll()
  2869. * \see clearAllTool()
  2870. * \see clearLabelColorList()
  2871. *
  2872. * As far as BACKGROUND label should always be present slot adds
  2873. * it after clearing all the label list
  2874. */
  2875. void ImageLabeler::clearLabelList()
  2876. {
  2877. list_label_->clear();
  2878. addLabel(0, false, "BACKGROUND");
  2879. list_label_->item(0)->setFlags(
  2880. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  2881. Qt::ItemIsEnabled
  2882. );
  2883. }
  2884. //! A slot member clears the list of label colors
  2885. /*!
  2886. * \see clearAll()
  2887. * \see clearAllTool()
  2888. * \see clearLabelList()
  2889. *
  2890. * As far as BACKGROUND label should always be present slot adds
  2891. * it's color(black by default) after clearing all the label color list
  2892. */
  2893. void ImageLabeler::clearLabelColorList()
  2894. {
  2895. list_label_colors_.clear();
  2896. list_label_colors_.append(0x0);
  2897. }
  2898. //! A slot member enabling all the tools
  2899. /*!
  2900. * \see disableTools()
  2901. *
  2902. * By default the initial state of all tools is disabled
  2903. * so this slot is recommended to use when at least one image
  2904. * has been loaded
  2905. */
  2906. void
  2907. ImageLabeler::enableTools()
  2908. {
  2909. action_save_all_->setEnabled(true);
  2910. action_save_segmented_->setEnabled(true);
  2911. action_save_legend_->setEnabled(true);
  2912. action_bound_box_tool_->setEnabled(true);
  2913. action_polygon_tool_->setEnabled(true);
  2914. action_tagging_tool_->setEnabled(true);
  2915. action_add_description_->setEnabled(true);
  2916. action_undo_->setEnabled(true);
  2917. action_redo_->setEnabled(true);
  2918. button_bound_box_tool_->setEnabled(true);
  2919. button_polygon_tool_->setEnabled(true);
  2920. button_tagging_tool_->setEnabled(true);
  2921. button_add_label_->setEnabled(true);
  2922. button_remove_label_->setEnabled(true);
  2923. button_prev_image_->setEnabled(true);
  2924. button_next_image_->setEnabled(true);
  2925. button_clear_selection_tool_->setEnabled(true);
  2926. button_generate_colors_->setEnabled(true);
  2927. button_delete_all_labels_->setEnabled(true);
  2928. }
  2929. //! A slot member disabling all the tools
  2930. /*!
  2931. * \see enableTools()
  2932. */
  2933. void
  2934. ImageLabeler::disableTools()
  2935. {
  2936. action_save_all_->setEnabled(false);
  2937. action_save_segmented_->setEnabled(false);
  2938. action_save_legend_->setEnabled(false);
  2939. action_bound_box_tool_->setEnabled(false);
  2940. action_polygon_tool_->setEnabled(false);
  2941. action_tagging_tool_->setEnabled(false);
  2942. button_tagging_tool_->setEnabled(false);
  2943. action_add_description_->setEnabled(false);
  2944. action_undo_->setEnabled(false);
  2945. action_redo_->setEnabled(false);
  2946. button_bound_box_tool_->setEnabled(false);
  2947. button_polygon_tool_->setEnabled(false);
  2948. button_add_label_->setEnabled(false);
  2949. button_remove_label_->setEnabled(false);
  2950. button_prev_image_->setEnabled(false);
  2951. button_next_image_->setEnabled(false);
  2952. button_clear_selection_tool_->setEnabled(false);
  2953. button_generate_colors_->setEnabled(false);
  2954. button_delete_all_labels_->setEnabled(false);
  2955. }
  2956. //! \brief A slot member triggering popup_area_list_ menu with the coordinates
  2957. //! of the mouse pointer
  2958. /*!
  2959. * \see deleteArea()
  2960. * \see editArea()
  2961. */
  2962. void
  2963. ImageLabeler::areaListPopupMenu(const QPoint &aPos)
  2964. {
  2965. QPoint globalPos = list_areas_->mapToGlobal(aPos);
  2966. QModelIndex index = list_areas_->indexAt(aPos);
  2967. if (-1 == index.row()) {
  2968. return;
  2969. /* NOTREACHED */
  2970. }
  2971. list_areas_->item(index.row())->setSelected(true);
  2972. popup_area_list_->exec(globalPos);
  2973. }
  2974. //! \brief A slot member triggering popup_label_list_ menu width the coordinates
  2975. //! of the mouse pointer
  2976. /*!
  2977. * \see setLabelColor()
  2978. * \see toggleLabelPriority()
  2979. * \see removeLabel()
  2980. */
  2981. void
  2982. ImageLabeler::labelListPopupMenu(const QPoint &aPos)
  2983. {
  2984. QPoint globalPos = list_label_->mapToGlobal(aPos);
  2985. QModelIndex index = list_label_->indexAt(aPos);
  2986. if (-1 == index.row() || !index.row()) {
  2987. return;
  2988. /* NOTREACHED */
  2989. }
  2990. list_label_->item(index.row())->setSelected(true);
  2991. popup_label_list_->exec(globalPos);
  2992. }
  2993. //! \brief A slot member triggering popup_images_list_ menu width the coordinates
  2994. //! of the mouse pointer
  2995. /*!
  2996. * \see removeImage()
  2997. */
  2998. void
  2999. ImageLabeler::imageListPopupMenu(const QPoint &aPos)
  3000. {
  3001. QPoint globalPos = list_images_widget_->mapToGlobal(aPos);
  3002. QModelIndex index = list_images_widget_->indexAt(aPos);
  3003. if (-1 == index.row()) {
  3004. return;
  3005. /* NOTREACHED */
  3006. }
  3007. list_images_widget_->item(index.row())->setSelected(true);
  3008. popup_images_list_->exec(globalPos);
  3009. }
  3010. //! \brief A slot member gets image description or tags from line_edit_form_
  3011. //! depends on line_edit_form_.purpose()
  3012. /*!
  3013. * \see LineEditForm
  3014. */
  3015. void
  3016. ImageLabeler::setDataFromForm(QString aData)
  3017. {
  3018. if (ImageDescriptionPurpose == line_edit_form_.purpose()) {
  3019. image_description_ = aData;
  3020. setWindowTitle(image_description_);
  3021. }
  3022. else if (TaggingPurpose == line_edit_form_.purpose()) {
  3023. tags_ = aData;
  3024. }
  3025. }
  3026. //! A slot member creating 2-dimensional array for the segmented image
  3027. /*!
  3028. * Array size is equal to a size of the current image loaded
  3029. * and each pixel has the label id which corresponds to the objects in
  3030. * list_bounding_box_ and list_polygon_.
  3031. */
  3032. void
  3033. ImageLabeler::setPureData()
  3034. {
  3035. /* initializing array */
  3036. if (pure_data_) {
  3037. delete[] *pure_data_;
  3038. delete pure_data_;
  3039. }
  3040. /* getting image size */
  3041. QSize imageSize = image_->size();
  3042. pure_data_ = new int *[imageSize.height()];
  3043. if (!pure_data_) {
  3044. return;
  3045. /* NOTREACHED */
  3046. }
  3047. /* allocating memory */
  3048. for (int i = 0; i < imageSize.height(); i++) {
  3049. pure_data_[i] = new int[imageSize.width()];
  3050. if (!pure_data_[i]) {
  3051. return;
  3052. /* NOTREACHED */
  3053. }
  3054. }
  3055. /* getting the number of all selections */
  3056. int bboxCnt = list_bounding_box_.count();
  3057. int polyCnt = list_polygon_.count();
  3058. /* checking if each coordinate(pixel) belongs to any object */
  3059. for (int i = 0; i < imageSize.height(); i++)
  3060. for (int j = 0; j < imageSize.width(); j++) {
  3061. pure_data_[i][j] = 0;
  3062. /* bboxes first */
  3063. for (int cnt = 0; cnt < bboxCnt; cnt++) {
  3064. BoundingBox *bbox = list_bounding_box_.at(cnt);
  3065. if (bbox->rect.contains(j, i)) {
  3066. pure_data_[i][j] = bbox->label_ID_;
  3067. }
  3068. }
  3069. /* polys next */
  3070. for (int cnt = 0; cnt < polyCnt; cnt++) {
  3071. Polygon *poly = list_polygon_.at(cnt);
  3072. if (poly->poly.containsPoint(QPoint(j, i), Qt::OddEvenFill)) {
  3073. pure_data_[i][j] = poly->label_ID_;
  3074. }
  3075. }
  3076. }
  3077. }
  3078. //! \brief A slot member setting new color for
  3079. //! the current label from QColorDialog
  3080. /*!
  3081. * \see setLabelColor(int anID, QColor aColor)
  3082. */
  3083. void
  3084. ImageLabeler::setLabelColor()
  3085. {
  3086. if (list_label_colors_.count() < list_label_->count()) {
  3087. generateColors();
  3088. }
  3089. /* getting current item */
  3090. QListWidgetItem *current = list_label_->currentItem();
  3091. int labelID = list_label_->row(current);
  3092. QColor defaultColor;
  3093. defaultColor.setRgb(list_label_colors_.at(labelID));
  3094. QColor newColor = QColorDialog::getColor(
  3095. defaultColor,
  3096. list_label_
  3097. );
  3098. if (!newColor.isValid()) {
  3099. return;
  3100. /* NOTREACHED */
  3101. }
  3102. list_label_colors_.takeAt(labelID);
  3103. list_label_colors_.insert(labelID, newColor.rgb());
  3104. QPixmap iconPix = QPixmap(20, 20);
  3105. iconPix.fill(newColor);
  3106. QIcon icon(iconPix);
  3107. current->setIcon(icon);
  3108. image_holder_->update();
  3109. }
  3110. //! A protected member setting color aColor for the label number anID
  3111. /*!
  3112. * \param[in] anID an integer indicating label number in the list_label_ widget
  3113. * \param[in] aColor a QColor object contains color you want to set for the label
  3114. */
  3115. void
  3116. ImageLabeler::setLabelColor(int anID, QColor aColor)
  3117. {
  3118. if (anID < 0 || list_label_->count() < anID) {
  3119. return;
  3120. /* NOTREACHED */
  3121. }
  3122. if (list_label_colors_.count() < list_label_->count()) {
  3123. generateColors();
  3124. }
  3125. QListWidgetItem *item = list_label_->item(anID);
  3126. list_label_colors_.takeAt(anID);
  3127. list_label_colors_.insert(anID, aColor.rgb());
  3128. QPixmap iconPix = QPixmap(20, 20);
  3129. iconPix.fill(aColor);
  3130. QIcon icon(iconPix);
  3131. item->setIcon(icon);
  3132. image_holder_->update();
  3133. }
  3134. //! A slot member loading normal image which path is in the current_image_
  3135. /*!
  3136. * \see viewSegmented()
  3137. */
  3138. void
  3139. ImageLabeler::viewNormal()
  3140. {
  3141. if (current_image_.isEmpty()) {
  3142. return;
  3143. /* NOTREACHED */
  3144. }
  3145. image_->load(current_image_);
  3146. image_holder_->setPixmap(*image_);
  3147. action_view_segmented_->setEnabled(true);
  3148. action_view_normal_->setEnabled(false);
  3149. }
  3150. //! A slot member loading segmented image which path is in the segmented_image_
  3151. /*!
  3152. * \see viewNormal()
  3153. */
  3154. void
  3155. ImageLabeler::viewSegmented()
  3156. {
  3157. if (segmented_image_.isEmpty()) {
  3158. return;
  3159. /* NOTREACHED */
  3160. }
  3161. image_->load(segmented_image_);
  3162. image_holder_->setPixmap(*image_);
  3163. action_view_segmented_->setEnabled(false);
  3164. action_view_normal_->setEnabled(true);
  3165. }
  3166. //! \brief A slot member setting interrupt_search_ to true for interrupting
  3167. //! the recursive search
  3168. /*!
  3169. * \see loadImages()
  3170. * \see getImagesFromDir(const QDir &dir)
  3171. */
  3172. void
  3173. ImageLabeler::interruptSearch()
  3174. {
  3175. interrupt_search_ = 1;
  3176. }
  3177. //! \brief A slot member selecting image corresponding to the item
  3178. //! in the list_images_widget_
  3179. /*!
  3180. * \see selectImage(int anImageID)
  3181. * \param[in] anItem a pointer to the QListWidgetItem object which indicates
  3182. * certain image in the image_list_
  3183. */
  3184. void
  3185. ImageLabeler::selectImage(QListWidgetItem *anItem)
  3186. {
  3187. if (!anItem || list_images_widget_->row(anItem) < 0 ||
  3188. list_images_->isEmpty())
  3189. {
  3190. return;
  3191. /* NOTREACHED */
  3192. }
  3193. clearAllTool();
  3194. //clearLabelList();
  3195. //clearLabelColorList();
  3196. image_ID_ = list_images_widget_->row(anItem);
  3197. selectImage(image_ID_);
  3198. }
  3199. //! A protected member loading image from list_images_
  3200. /*!
  3201. * \see loadInfo(QString filename)
  3202. * \param[in] anImageID an integer indicates certain
  3203. * image in the image_list_
  3204. *
  3205. * if that image was labeled before, selectImage(int anImageID)
  3206. * will try to load all info
  3207. */
  3208. bool
  3209. ImageLabeler::selectImage(int anImageID)
  3210. {
  3211. if (anImageID < 0 || list_images_->isEmpty() ||
  3212. list_images_->count() <= anImageID)
  3213. {
  3214. return false;
  3215. /* NOTREACHED */
  3216. }
  3217. action_view_segmented_->setEnabled(false);
  3218. /* checking if it was labeled before */
  3219. if (list_images_->at(anImageID).labeled_ &&
  3220. !list_images_->at(anImageID).pas_)
  3221. {
  3222. list_label_colors_.clear();
  3223. list_label_->clear();
  3224. QString labeled =
  3225. alterFileName(list_images_->at(anImageID).image_, "_labeled");
  3226. labeled.append(".dat");
  3227. loadInfo(labeled);
  3228. }
  3229. /* if it was loaded from PASCAL file then we're in trouble */
  3230. else if (list_images_->at(anImageID).labeled_ &&
  3231. list_images_->at(anImageID).pas_)
  3232. {
  3233. /* TODO: do the pascal file selecting */
  3234. showWarning("this function doesn't work at the moment, sorry.");
  3235. }
  3236. /* loading clean unlabeled image */
  3237. else {
  3238. current_image_ = list_images_->at(anImageID).image_;
  3239. image_->load(current_image_);
  3240. image_holder_->setPixmap(*image_);
  3241. image_holder_->resize(image_->size());
  3242. }
  3243. return true;
  3244. }
  3245. //! \brief A slot member removing selected image from list
  3246. //! (image will still be loaded in the image_holder_)
  3247. /*!
  3248. * Image will be removed from list so it will not be possible to load
  3249. * it again without using loadImage() or other slots for image loading.
  3250. * if you want to remove image from image_holder_ just call
  3251. * image_holder_->clear()
  3252. */
  3253. void
  3254. ImageLabeler::removeImage()
  3255. {
  3256. int imageCount = list_images_widget_->count();
  3257. int num = list_images_widget_->currentRow();
  3258. if (!imageCount || num < 0 || imageCount <= num ||
  3259. list_images_->count() != imageCount)
  3260. {
  3261. return;
  3262. /* NOTREACHED */
  3263. }
  3264. list_images_widget_->takeItem(num);
  3265. list_images_->takeAt(num);
  3266. for (int i = num ; i < list_images_widget_->count(); i++) {
  3267. QString newStr = removePath(list_images_->at(i).image_);
  3268. newStr.prepend(QString("%1: ").arg(i));
  3269. list_images_widget_->item(i)->setText(newStr);
  3270. }
  3271. if (1 == imageCount)
  3272. button_remove_image_->setEnabled(false);
  3273. }
  3274. //! A protected member which is being automatically called on every image resize
  3275. /*!
  3276. *
  3277. */
  3278. void
  3279. ImageLabeler::resizeEvent (QResizeEvent *anEvent)
  3280. {
  3281. QWidget::resizeEvent(anEvent);
  3282. }
  3283. //! A protected member which is being automatically called on every mouse press
  3284. /*!
  3285. *
  3286. */
  3287. void
  3288. ImageLabeler::mousePressEvent(QMouseEvent *anEvent)
  3289. {
  3290. QWidget::mousePressEvent(anEvent);
  3291. }
  3292. //! A protected member which is being automatically called on every key press
  3293. /*!
  3294. * current list of available commands:
  3295. * - key left/right - prevImage() / nextImage()
  3296. * - enter/return - confirmSelection()
  3297. * - esc - clear current selection
  3298. * - ctrl+z - image_holder_->undo()
  3299. * - ctrl+y - iamge_holder_->redo()
  3300. */
  3301. void
  3302. ImageLabeler::keyPressEvent(QKeyEvent *anEvent)
  3303. {
  3304. keyboard_modifier_ = anEvent->modifiers();
  3305. if (Qt::Key_Left == anEvent->key() &&
  3306. Qt::ControlModifier == keyboard_modifier_) {
  3307. prevImage();
  3308. }
  3309. if (Qt::Key_Down == anEvent->key() &&
  3310. Qt::ControlModifier == keyboard_modifier_) {
  3311. prevImage(5);
  3312. }
  3313. if (Qt::Key_Right == anEvent->key() &&
  3314. Qt::ControlModifier == keyboard_modifier_) {
  3315. nextImage();
  3316. }
  3317. if (Qt::Key_Up == anEvent->key() &&
  3318. Qt::ControlModifier == keyboard_modifier_) {
  3319. nextImage(5);
  3320. }
  3321. if ((Qt::Key_Enter == anEvent->key() ||
  3322. Qt::Key_Return == anEvent->key()) &&
  3323. // Qt::NoModifier == anEvent->modifiers() &&
  3324. ImageHolder::NewSelection == image_holder_->state()) {
  3325. confirmSelection();
  3326. }
  3327. if ((Qt::Key_Enter == anEvent->key() ||
  3328. Qt::Key_Return == anEvent->key()) &&
  3329. // Qt::NoModifier == anEvent->modifiers() &&
  3330. -1 != image_holder_->focusedSelection()) {
  3331. image_holder_->clearFocusOnArea();
  3332. image_holder_->clearHoveredPoint();
  3333. image_holder_->update();
  3334. }
  3335. if (Qt::Key_Escape == anEvent->key()) {
  3336. image_holder_->clearLast();
  3337. image_holder_->clearFocusOnArea();
  3338. image_holder_->clearHoveredPoint();
  3339. image_holder_->update();
  3340. }
  3341. if (Qt::Key_Z == anEvent->key() &&
  3342. Qt::ControlModifier == anEvent->modifiers()) {
  3343. image_holder_->undo();
  3344. }
  3345. if (Qt::Key_Y == anEvent->key() &&
  3346. Qt::ControlModifier == anEvent->modifiers()) {
  3347. image_holder_->redo();
  3348. }
  3349. if (Qt::Key_Delete == anEvent->key() &&
  3350. list_areas_->hasFocus()) {
  3351. deleteArea();
  3352. }
  3353. if (Qt::Key_Delete == anEvent->key() &&
  3354. list_label_->hasFocus()) {
  3355. removeLabel();
  3356. }
  3357. if (Qt::Key_Delete == anEvent->key() &&
  3358. list_images_widget_->hasFocus()) {
  3359. removeImage();
  3360. }
  3361. if (Qt::Key_Delete == anEvent->key() &&
  3362. image_holder_->hasFocus()) {
  3363. image_holder_->removeSelectedPoint();
  3364. }
  3365. QWidget::keyPressEvent(anEvent);
  3366. }
  3367. //!
  3368. /*!
  3369. *
  3370. */
  3371. void
  3372. ImageLabeler::keyReleaseEvent(QKeyEvent *anEvent)
  3373. {
  3374. keyboard_modifier_ = anEvent->modifiers();
  3375. }
  3376. //! A protected member which is being automatically called on every mouse wheel turn
  3377. /*!
  3378. * implements zooming in and out on ctrl+wheel
  3379. */
  3380. void
  3381. ImageLabeler::wheelEvent(QWheelEvent *anEvent)
  3382. {
  3383. /* zoomin */
  3384. if (0 < anEvent->delta() &&
  3385. Qt::ControlModifier == keyboard_modifier_) {
  3386. image_holder_->scaleImage(ZoomIn, 1.1);
  3387. }
  3388. /* zoomout */
  3389. else if (anEvent->delta() < 0 &&
  3390. Qt::ControlModifier == keyboard_modifier_) {
  3391. image_holder_->scaleImage(ZoomOut, 1.1);
  3392. }
  3393. }
  3394. //!
  3395. /*!
  3396. *
  3397. */
  3398. void
  3399. ImageLabeler::closeEvent(QCloseEvent *anEvent)
  3400. {
  3401. Q_UNUSED(anEvent)
  3402. writeSettings();
  3403. askForUnsavedData();
  3404. }
  3405. /*
  3406. *
  3407. */