ImageLabeler.cpp 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451
  1. /*
  2. * ImageLabeler.cpp
  3. *
  4. * Created on: Oct 4, 2011
  5. * Author: Gapchich Vladislav
  6. */
  7. #include "ImageLabeler.h"
  8. #include "functions.h"
  9. #include <QApplication>
  10. #include <QFrame>
  11. #include <QMenuBar>
  12. #include <QMenu>
  13. #include <QAction>
  14. #include <QBoxLayout>
  15. #include <QGridLayout>
  16. #include <QPixmap>
  17. #include <QLabel>
  18. #include <QScrollArea>
  19. #include <QPushButton>
  20. #include <QButtonGroup>
  21. #include <QListWidget>
  22. #include <QListWidgetItem>
  23. #include <QDesktopWidget>
  24. #include <QFileDialog>
  25. #include <QColorDialog>
  26. #include <QDir>
  27. #include <QMessageBox>
  28. #include <QListIterator>
  29. #include <QDomDocument>
  30. #include <QFile>
  31. #include <QKeyEvent>
  32. #include <QDebug>
  33. ImageLabeler::ImageLabeler(QWidget *aParent) :
  34. QMainWindow(aParent)
  35. {
  36. /*
  37. * Variables
  38. */
  39. list_images_ = new QList< Image >;
  40. main_label_ = -1;
  41. pure_data_ = 0;
  42. //label_ID_ = -1;
  43. /* options */
  44. auto_color_generation_ = 0;
  45. /* flags */
  46. interrupt_search_ = 0;
  47. unsaved_data_ = 0;
  48. /*
  49. * menu bar part begins
  50. */
  51. menu_bar_ = new QMenuBar(this);
  52. setMenuBar(menu_bar_);
  53. menu_file_ = new QMenu(menu_bar_);
  54. menu_file_->setTitle(tr("&File"));
  55. menu_pascal_ = new QMenu(menu_bar_);
  56. menu_pascal_->setTitle(tr("&Pascal"));
  57. menu_view_ = new QMenu(menu_bar_);
  58. menu_view_->setTitle(tr("&View"));
  59. menu_edit_ = new QMenu(menu_bar_);
  60. menu_edit_->setTitle(tr("&Edit"));
  61. menu_help_ = new QMenu(menu_bar_);
  62. menu_help_->setTitle(tr("&Help"));
  63. /* menu file */
  64. action_open_image_ = new QAction(this);
  65. action_open_image_->setText(tr("&Load image"));
  66. action_open_images_ = new QAction(this);
  67. action_open_images_->setText(tr("&Load images(recursively)"));
  68. action_open_labeled_image_ = new QAction(this);
  69. action_open_labeled_image_->setText(tr("&Load labeled image"));
  70. action_load_legend_ = new QAction(this);
  71. action_load_legend_->setText(tr("&Load legend"));
  72. action_save_labels_ = new QAction(this);
  73. action_save_labels_->setText(tr("&Save all info"));
  74. action_save_labels_->setEnabled(false);
  75. action_save_segmented_ = new QAction(this);
  76. action_save_segmented_->setText(tr("Save segmented &picture"));
  77. action_save_segmented_->setEnabled(false);
  78. action_save_legend_ = new QAction(this);
  79. action_save_legend_->setText(tr("Save &legend"));
  80. action_save_legend_->setEnabled(false);
  81. action_quit_ = new QAction(this);
  82. action_quit_->setText(tr("&Quit"));
  83. /* menu pascal */
  84. action_load_pascal_file_ = new QAction(this);
  85. action_load_pascal_file_->setText(tr("&Load pascal file"));
  86. action_load_pascal_poly_ = new QAction(this);
  87. action_load_pascal_poly_->setText(tr("&Load poly info"));
  88. /* menu view */
  89. action_view_normal_ = new QAction(this);
  90. action_view_normal_->setText(tr("&Normal"));
  91. action_view_normal_->setEnabled(false);
  92. action_view_segmented_ = new QAction(this);
  93. action_view_segmented_->setText(tr("&Segmented"));
  94. action_view_segmented_->setEnabled(false);
  95. /* menu edit */
  96. action_undo_ = new QAction(this);
  97. action_undo_->setText(tr("&Undo"));
  98. action_undo_->setEnabled(false);
  99. action_redo_ = new QAction(this);
  100. action_redo_->setText(tr("&Redo"));
  101. action_redo_->setEnabled(false);
  102. action_bound_box_tool_ = new QAction(this);
  103. action_bound_box_tool_->setText(tr("Bounding box tool"));
  104. action_bound_box_tool_->setEnabled(false);
  105. action_polygon_tool_ = new QAction(this);
  106. action_polygon_tool_->setText(tr("&Polygon tool"));
  107. action_polygon_tool_->setEnabled(false);
  108. action_tagging_tool_ = new QAction(this);
  109. action_tagging_tool_->setText(tr("&Tagging tool"));
  110. action_tagging_tool_->setEnabled(false);
  111. action_add_description_ = new QAction(this);
  112. action_add_description_->setText(tr("&Add image description"));
  113. action_add_description_->setEnabled(false);
  114. action_options_ = new QAction(this);
  115. action_options_->setText(tr("&Options"));
  116. /* menu help */
  117. action_help_content_ = new QAction(this);
  118. action_help_content_->setText(tr("&Help content"));
  119. action_help_content_->setEnabled(false);
  120. action_about_ = new QAction(this);
  121. action_about_->setText(tr("&About"));
  122. action_about_->setEnabled(false);
  123. /* ------------------ */
  124. menu_file_->addAction(action_open_image_);
  125. menu_file_->addAction(action_open_images_);
  126. menu_file_->addAction(action_open_labeled_image_);
  127. menu_file_->addAction(action_load_legend_);
  128. menu_file_->addAction(menu_pascal_->menuAction());
  129. menu_file_->addSeparator();
  130. menu_file_->addAction(action_save_segmented_);
  131. menu_file_->addAction(action_save_legend_);
  132. menu_file_->addAction(action_save_labels_);
  133. menu_file_->addSeparator();
  134. menu_file_->addAction(action_quit_);
  135. menu_pascal_->addAction(action_load_pascal_file_);
  136. menu_pascal_->addAction(action_load_pascal_poly_);
  137. menu_view_->addAction(action_view_normal_);
  138. menu_view_->addAction(action_view_segmented_);
  139. menu_edit_->addAction(action_undo_);
  140. menu_edit_->addAction(action_redo_);
  141. menu_edit_->addSeparator();
  142. menu_edit_->addAction(action_bound_box_tool_);
  143. menu_edit_->addAction(action_polygon_tool_);
  144. menu_edit_->addAction(action_tagging_tool_);
  145. menu_edit_->addSeparator();
  146. menu_edit_->addAction(action_add_description_);
  147. menu_edit_->addAction(action_options_);
  148. menu_help_->addAction(action_help_content_);
  149. menu_help_->addAction(action_about_);
  150. menu_bar_->addAction(menu_file_->menuAction());
  151. menu_bar_->addAction(menu_view_->menuAction());
  152. menu_bar_->addAction(menu_edit_->menuAction());
  153. menu_bar_->addAction(menu_help_->menuAction());
  154. /*
  155. * popup menu part begins
  156. */
  157. popup_area_list_ = new QMenu;
  158. action_delete_area_ = new QAction(this);
  159. action_delete_area_->setText(tr("&Delete area"));
  160. action_edit_area_ = new QAction(this);
  161. action_edit_area_->setText(tr("&Change area"));
  162. popup_area_list_->addAction(action_delete_area_);
  163. popup_area_list_->addAction(action_edit_area_);
  164. popup_label_list_ = new QMenu;
  165. action_set_color_ = new QAction(this);
  166. action_set_color_->setText(tr("Set &color"));
  167. action_toggle_priority_ = new QAction(this);
  168. action_toggle_priority_->setText(tr("Toggle &priority"));
  169. action_delete_label_ = new QAction(this);
  170. action_delete_label_->setText(tr("&Delete"));
  171. popup_label_list_->addAction(action_set_color_);
  172. popup_label_list_->addAction(action_toggle_priority_);
  173. popup_label_list_->addAction(action_delete_label_);
  174. popup_images_list_ = new QMenu;
  175. action_remove_image_ = new QAction(this);
  176. action_remove_image_->setText(tr("&remove image"));
  177. popup_images_list_->addAction(action_remove_image_);
  178. /*
  179. * widgets part begins
  180. */
  181. central_widget_ = new QWidget(this);
  182. setCentralWidget(central_widget_);
  183. frame_toolbox_ = new QFrame(central_widget_);
  184. frame_toolbox_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  185. frame_toolbox_->setLineWidth(0);
  186. frame_toolbox_->setMidLineWidth(0);
  187. frame_center_ = new QFrame(central_widget_);
  188. //frame_image_->setFrameStyle(QFrame::Box | QFrame::Plain);
  189. //frame_image_->setLineWidth(1);
  190. frame_image_ = new QScrollArea(frame_center_);
  191. //frame_image_->setStyleSheet("QWidget {background: #888888;}");
  192. frame_image_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  193. frame_image_->setLineWidth(0);
  194. frame_image_->setMidLineWidth(0);
  195. frame_image_->setWidgetResizable(false);
  196. frame_image_->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
  197. frame_image_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  198. frame_labelbox_ = new QFrame(central_widget_);
  199. frame_labelbox_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  200. frame_labelbox_->setLineWidth(0);
  201. frame_labelbox_->setMidLineWidth(0);
  202. image_ = new QPixmap(500, 500);
  203. image_->fill(QColor(Qt::white));
  204. //image_holder_ = new ImageHolder(frame_image_);
  205. image_holder_ = new ImageHolder;//(frame_image_);
  206. image_holder_->setPixmap(*image_);
  207. //image_holder_->setStyleSheet("QLabel {background: #ffffff;}");
  208. image_holder_->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
  209. image_holder_->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
  210. image_holder_->setScaledContents(true);
  211. image_holder_->setFrameStyle(QFrame::Box | QFrame::Plain);
  212. image_holder_->setLineWidth(0);
  213. frame_image_->setWidget(image_holder_);
  214. list_label_ = new QListWidget(central_widget_);
  215. list_label_->setContextMenuPolicy(Qt::CustomContextMenu);
  216. list_areas_ = new QListWidget(central_widget_);
  217. list_areas_->setContextMenuPolicy(Qt::CustomContextMenu);
  218. list_images_widget_ = new QListWidget(central_widget_);
  219. list_images_widget_->setContextMenuPolicy(Qt::CustomContextMenu);
  220. // addLabel(0, false, "BACKGROUND");
  221. // list_label_->item(0)->setFlags(Qt::ItemIsSelectable);
  222. //QListWidgetItem *background = list_label_->item(0);
  223. //qDebug() << background;
  224. //background->setFlags(Qt::ItemIsSelectable);
  225. label_toolbox_ = new QLabel(tr("Tool box"), frame_toolbox_);
  226. label_list_areas_ = new QLabel(tr("Selected areas:"), central_widget_);
  227. label_list_images_ = new QLabel(tr("Loaded images:"), central_widget_);
  228. /* buttons */
  229. button_bound_box_tool_ = new QPushButton(frame_toolbox_);
  230. button_bound_box_tool_->setText(tr("bbox"));
  231. button_bound_box_tool_->setEnabled(false);
  232. button_bound_box_tool_->setCheckable(true);
  233. button_polygon_tool_ = new QPushButton(frame_toolbox_);
  234. button_polygon_tool_->setText(tr("poly tool"));
  235. button_polygon_tool_->setEnabled(false);
  236. button_polygon_tool_->setCheckable(true);
  237. button_tagging_tool_ = new QPushButton(frame_toolbox_);
  238. button_tagging_tool_->setText(tr("tagging"));
  239. button_tagging_tool_->setEnabled(false);
  240. button_tagging_tool_->setCheckable(true);
  241. button_clear_selection_tool_ = new QPushButton(frame_toolbox_);
  242. button_clear_selection_tool_->setText(tr("clear selection"));
  243. button_clear_selection_tool_->setEnabled(false);
  244. button_generate_colors_ = new QPushButton(frame_toolbox_);
  245. button_generate_colors_->setText(tr("generate label colors"));
  246. button_generate_colors_->setEnabled(false);
  247. button_delete_all_labels_ = new QPushButton(frame_toolbox_);
  248. button_delete_all_labels_->setText(tr("delete all labels"));
  249. button_delete_all_labels_->setEnabled(false);
  250. group_tools_ = new QButtonGroup;
  251. group_tools_->addButton(button_bound_box_tool_);
  252. group_tools_->addButton(button_polygon_tool_);
  253. group_tools_->addButton(button_tagging_tool_);
  254. button_confirm_selection_ = new QPushButton(central_widget_);
  255. button_confirm_selection_->setText(tr("Confirm selection"));
  256. button_confirm_selection_->setEnabled(false);
  257. button_add_label_ = new QPushButton(frame_labelbox_);
  258. button_add_label_->setText(tr("Add label"));
  259. button_add_label_->setEnabled(false);
  260. button_remove_label_ = new QPushButton(frame_labelbox_);
  261. button_remove_label_->setText(tr("Remove label"));
  262. button_remove_label_->setEnabled(false);
  263. button_prev_image_ = new QPushButton(central_widget_);
  264. button_prev_image_->setText("←");
  265. button_next_image_ = new QPushButton(central_widget_);
  266. button_next_image_->setText("→");
  267. /*
  268. * layouts part begins
  269. */
  270. layout_main_ = new QHBoxLayout(central_widget_);
  271. layout_left_ = new QVBoxLayout();
  272. layout_toolbox_ = new QVBoxLayout();
  273. layout_center_ = new QVBoxLayout();
  274. layout_frame_image_ = new QVBoxLayout();
  275. layout_image_widget_ = new QGridLayout();
  276. layout_center_buttons_ = new QHBoxLayout();
  277. layout_right_ = new QVBoxLayout();
  278. layout_labelbox_ = new QVBoxLayout();
  279. layout_labelbox_buttons_ = new QHBoxLayout();
  280. layout_main_->addLayout(layout_left_);
  281. layout_main_->addLayout(layout_center_);
  282. layout_main_->addLayout(layout_right_);
  283. /* making the center part stretchable */
  284. layout_main_->setStretch(1, 1);
  285. /* left part */
  286. layout_left_->addWidget(frame_toolbox_);
  287. frame_toolbox_->setLayout(layout_toolbox_);
  288. layout_toolbox_->addWidget(label_toolbox_);
  289. layout_toolbox_->addWidget(button_bound_box_tool_);
  290. layout_toolbox_->addWidget(button_polygon_tool_);
  291. layout_toolbox_->addWidget(button_tagging_tool_);
  292. layout_toolbox_->addSpacing(10);
  293. layout_toolbox_->addWidget(button_clear_selection_tool_);
  294. layout_toolbox_->addWidget(button_delete_all_labels_);
  295. layout_toolbox_->addSpacing(10);
  296. layout_toolbox_->addWidget(button_generate_colors_);
  297. /* stretch is for making toolbox as small as it can be */
  298. layout_toolbox_->addSpacing(10);
  299. layout_left_->addWidget(label_list_images_);
  300. layout_left_->addWidget(list_images_widget_);
  301. list_images_widget_->setFixedWidth(200);
  302. layout_left_->addStretch(1);
  303. layout_left_->addWidget(button_confirm_selection_);
  304. /* central part */
  305. layout_center_->addWidget(frame_center_);
  306. frame_center_->setLayout(layout_frame_image_);
  307. //layout_frame_image_->addWidget(image_holder_);
  308. layout_frame_image_->setContentsMargins(0, 0, 0, 0);
  309. layout_frame_image_->addWidget(frame_image_);
  310. //frame_image_->setLayout(layout_image_widget_);
  311. // layout_image_widget_->setRowStretch(0, 1);
  312. // layout_image_widget_->setColumnStretch(0, 1);
  313. // layout_image_widget_->addWidget(image_holder_, 1, 1);
  314. // layout_image_widget_->setRowStretch(2, 1);
  315. // layout_image_widget_->setColumnStretch(2, 1);
  316. //layout_frame_image_->addStretch(1);
  317. layout_frame_image_->addLayout(layout_center_buttons_);
  318. layout_center_buttons_->addWidget(button_prev_image_);
  319. layout_center_buttons_->addWidget(button_next_image_);
  320. /* right part */
  321. layout_right_->addWidget(frame_labelbox_);
  322. frame_labelbox_->setFixedWidth(200);
  323. frame_labelbox_->setLayout(layout_labelbox_);
  324. layout_labelbox_->addLayout(layout_labelbox_buttons_);
  325. layout_labelbox_buttons_->addWidget(button_add_label_);
  326. layout_labelbox_buttons_->addWidget(button_remove_label_);
  327. layout_labelbox_->addWidget(list_label_);
  328. layout_labelbox_->addWidget(label_list_areas_);
  329. layout_labelbox_->addWidget(list_areas_);
  330. connect(
  331. action_quit_,
  332. SIGNAL(triggered()),
  333. this,
  334. SLOT(close())
  335. );
  336. connect(
  337. action_open_images_,
  338. SIGNAL(triggered()),
  339. this,
  340. SLOT(loadImages())
  341. );
  342. connect(
  343. action_open_image_,
  344. SIGNAL(triggered()),
  345. this,
  346. SLOT(loadImage())
  347. );
  348. connect(
  349. action_open_labeled_image_,
  350. SIGNAL(triggered()),
  351. this,
  352. SLOT(loadInfo())
  353. );
  354. connect(
  355. action_load_legend_,
  356. SIGNAL(triggered()),
  357. this,
  358. SLOT(loadLegendFromFile())
  359. );
  360. connect(
  361. action_load_pascal_file_,
  362. SIGNAL(triggered()),
  363. this,
  364. SLOT(loadPascalFile())
  365. );
  366. connect(
  367. action_load_pascal_poly_,
  368. SIGNAL(triggered()),
  369. this,
  370. SLOT(loadPascalPolys())
  371. );
  372. connect(
  373. action_save_legend_,
  374. SIGNAL(triggered()),
  375. this,
  376. SLOT(saveLegend())
  377. );
  378. connect(
  379. action_save_segmented_,
  380. SIGNAL(triggered()),
  381. this,
  382. SLOT(saveSegmentedPicture())
  383. );
  384. connect(
  385. action_save_labels_,
  386. SIGNAL(triggered()),
  387. this,
  388. SLOT(saveAllInfo())
  389. );
  390. connect(
  391. action_view_normal_,
  392. SIGNAL(triggered()),
  393. this,
  394. SLOT(viewNormal())
  395. );
  396. connect(
  397. action_view_segmented_,
  398. SIGNAL(triggered()),
  399. this,
  400. SLOT(viewSegmented())
  401. );
  402. connect(
  403. action_undo_,
  404. SIGNAL(triggered()),
  405. image_holder_,
  406. SLOT(undo())
  407. );
  408. connect(
  409. action_redo_,
  410. SIGNAL(triggered()),
  411. image_holder_,
  412. SLOT(redo())
  413. );
  414. connect(
  415. action_add_description_,
  416. SIGNAL(triggered()),
  417. &line_edit_form_,
  418. SLOT(setDescription())
  419. );
  420. connect(
  421. action_options_,
  422. SIGNAL(triggered()),
  423. &options_form_,
  424. SLOT(show())
  425. );
  426. connect(
  427. button_add_label_,
  428. SIGNAL(clicked()),
  429. this,
  430. SLOT(addLabel())
  431. );
  432. connect(
  433. button_remove_label_,
  434. SIGNAL(clicked()),
  435. this, SLOT(removeLabel())
  436. );
  437. connect(
  438. button_next_image_,
  439. SIGNAL(clicked()),
  440. this,
  441. SLOT(nextImage())
  442. );
  443. connect(
  444. button_prev_image_,
  445. SIGNAL(clicked()),
  446. this,
  447. SLOT(prevImage())
  448. );
  449. connect(
  450. button_bound_box_tool_,
  451. SIGNAL(toggled(bool)),
  452. this,
  453. SLOT(setBoundingBoxTool(bool))
  454. );
  455. connect(
  456. button_polygon_tool_,
  457. SIGNAL(toggled(bool)),
  458. this,
  459. SLOT(setPolygonTool(bool))
  460. );
  461. connect(
  462. button_clear_selection_tool_,
  463. SIGNAL(clicked()),
  464. this,
  465. SLOT(clearAllTool())
  466. );
  467. connect(
  468. button_generate_colors_,
  469. SIGNAL(clicked()),
  470. this,
  471. SLOT(generateColors())
  472. );
  473. connect(
  474. button_delete_all_labels_,
  475. SIGNAL(clicked()),
  476. this,
  477. SLOT(clearLabelList())
  478. );
  479. connect(
  480. button_confirm_selection_,
  481. SIGNAL(clicked()),
  482. this,
  483. SLOT(confirmSelection())
  484. );
  485. connect(
  486. list_label_,
  487. SIGNAL(itemChanged(QListWidgetItem *)),
  488. this,
  489. SLOT(editLabel(QListWidgetItem *))
  490. );
  491. connect(
  492. list_label_,
  493. SIGNAL(itemClicked(QListWidgetItem *)),
  494. this,
  495. SLOT(setLabelID(QListWidgetItem *))
  496. );
  497. connect(
  498. list_areas_,
  499. SIGNAL(itemDoubleClicked(QListWidgetItem *)),
  500. image_holder_,
  501. SLOT(focusOnArea(QListWidgetItem *))
  502. );
  503. connect(
  504. list_areas_,
  505. SIGNAL(customContextMenuRequested(const QPoint &)),
  506. this,
  507. SLOT(areaListPopupMenu(const QPoint &))
  508. );
  509. connect(
  510. list_areas_,
  511. SIGNAL(itemChanged(QListWidgetItem *)),
  512. this,
  513. SLOT(onAreaItemChange(QListWidgetItem *))
  514. );
  515. connect(
  516. list_label_,
  517. SIGNAL(customContextMenuRequested(const QPoint &)),
  518. this,
  519. SLOT(labelListPopupMenu(const QPoint &))
  520. );
  521. connect(
  522. list_images_widget_,
  523. SIGNAL(itemDoubleClicked(QListWidgetItem *)),
  524. this,
  525. SLOT(selectImage(QListWidgetItem *))
  526. );
  527. connect(
  528. list_images_widget_,
  529. SIGNAL(customContextMenuRequested(const QPoint &)),
  530. this,
  531. SLOT(imageListPopupMenu(const QPoint &))
  532. );
  533. connect(
  534. action_delete_area_,
  535. SIGNAL(triggered()),
  536. this,
  537. SLOT(deleteArea())
  538. );
  539. connect(
  540. action_edit_area_,
  541. SIGNAL(triggered()),
  542. this,
  543. SLOT(editArea())
  544. );
  545. connect(
  546. action_set_color_,
  547. SIGNAL(triggered()),
  548. this,
  549. SLOT(setLabelColor())
  550. );
  551. connect(
  552. action_toggle_priority_,
  553. SIGNAL(triggered()),
  554. this,
  555. SLOT(toggleLabelPriority())
  556. );
  557. connect(
  558. action_delete_label_,
  559. SIGNAL(triggered()),
  560. this,
  561. SLOT(removeLabel())
  562. );
  563. connect(
  564. action_remove_image_,
  565. SIGNAL(triggered()),
  566. this,
  567. SLOT(removeImage())
  568. );
  569. connect(
  570. image_holder_,
  571. SIGNAL(selectionStarted()),
  572. this,
  573. SLOT(onSelectionStarted())
  574. );
  575. // connect(
  576. // &image_description_form_,
  577. // SIGNAL(descriptionSet(QString)),
  578. // this,
  579. // SLOT(setDescription(QString))
  580. // );
  581. connect(
  582. image_holder_,
  583. SIGNAL(imageScaled()),
  584. this,
  585. SLOT(onImageScaled())
  586. );
  587. connect(
  588. image_holder_,
  589. SIGNAL(areaEdited()),
  590. this,
  591. SLOT(onAreaEdit())
  592. );
  593. connect(
  594. &options_form_,
  595. SIGNAL(optionsSet()),
  596. this,
  597. SLOT(onOptionsSet())
  598. );
  599. image_holder_->setBoundingBoxList(&list_bounding_box_);
  600. image_holder_->setPolygonList(&list_polygon_);
  601. image_holder_->setLabelColorList(&list_label_colors_);
  602. image_holder_->setMainLabelNum(&main_label_);
  603. image_holder_->setImage(image_);
  604. }
  605. ImageLabeler::~ImageLabeler()
  606. {
  607. delete action_quit_;
  608. delete action_open_labeled_image_;
  609. delete action_open_image_;
  610. delete action_open_images_;
  611. delete action_load_legend_;
  612. delete action_load_pascal_file_;
  613. delete action_load_pascal_poly_;
  614. delete action_save_legend_;
  615. delete action_save_segmented_;
  616. delete action_save_labels_;
  617. delete action_view_normal_;
  618. delete action_view_segmented_;
  619. delete action_undo_;
  620. delete action_redo_;
  621. delete action_bound_box_tool_;
  622. delete action_polygon_tool_;
  623. delete action_tagging_tool_;
  624. delete action_add_description_;
  625. delete action_options_;
  626. delete action_about_;
  627. delete action_help_content_;
  628. delete menu_pascal_;
  629. delete menu_file_;
  630. delete menu_view_;
  631. delete menu_edit_;
  632. delete menu_help_;
  633. delete menu_bar_;
  634. delete action_delete_area_;
  635. delete action_edit_area_;
  636. delete popup_area_list_;
  637. delete action_toggle_priority_;
  638. delete action_set_color_;
  639. delete action_delete_label_;
  640. delete popup_label_list_;
  641. delete action_remove_image_;
  642. delete popup_images_list_;
  643. delete image_;
  644. delete image_holder_;
  645. delete button_add_label_;
  646. delete button_remove_label_;
  647. delete button_bound_box_tool_;
  648. delete button_polygon_tool_;
  649. delete button_tagging_tool_;
  650. delete button_clear_selection_tool_;
  651. delete button_generate_colors_;
  652. delete button_delete_all_labels_;
  653. delete button_next_image_;
  654. delete button_prev_image_;
  655. delete button_confirm_selection_;
  656. delete label_list_areas_;
  657. delete label_toolbox_;
  658. delete label_list_images_;
  659. delete list_areas_;
  660. delete list_label_;
  661. delete list_images_widget_;
  662. delete layout_toolbox_;
  663. delete layout_right_;
  664. delete layout_center_buttons_;
  665. delete layout_frame_image_;
  666. delete layout_center_;
  667. delete layout_labelbox_buttons_;
  668. delete layout_labelbox_;
  669. delete layout_left_;
  670. delete layout_main_;
  671. delete frame_labelbox_;
  672. delete frame_toolbox_;
  673. delete frame_image_;
  674. delete frame_center_;
  675. delete central_widget_;
  676. //delete current_image_;
  677. if (pure_data_) {
  678. delete[] *pure_data_;
  679. delete pure_data_;
  680. }
  681. delete list_images_;
  682. }
  683. void
  684. ImageLabeler::addImage(Image *anImage)
  685. {
  686. QListWidgetItem *newItem = new QListWidgetItem;
  687. QString itemText = QString("%1: %2").
  688. arg(list_images_widget_->count()).
  689. arg(getFilenameFromPath(&(anImage->image_)));
  690. if (anImage->labeled_)
  691. itemText.append(" #labeled");
  692. if (anImage->pas_)
  693. itemText.append(" #pas");
  694. newItem->setText(itemText);
  695. list_images_widget_->addItem(newItem);
  696. list_images_->append(*anImage);
  697. }
  698. void
  699. ImageLabeler::addLabel()
  700. {
  701. QListWidgetItem *newItem = new QListWidgetItem;
  702. QString label;
  703. int itemNum = list_label_->count();
  704. label.append(QString("%1: ").arg(itemNum));
  705. if (itemNum) {
  706. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  707. label.append("New label");
  708. }
  709. else {
  710. newItem->setFlags(
  711. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  712. Qt::ItemIsEnabled
  713. );
  714. label.append("BACKGROUND");
  715. }
  716. newItem->setText(label);
  717. QPixmap iconPix = QPixmap(20, 20);
  718. QColor color;
  719. if (0 != itemNum)
  720. color = Qt::white;
  721. else
  722. color = Qt::black;
  723. iconPix.fill(color);
  724. QIcon icon(iconPix);
  725. list_label_colors_.append(color.rgb());
  726. newItem->setIcon(icon);
  727. list_label_->addItem(newItem);
  728. list_label_->setItemSelected(newItem, true);
  729. label_ID_ = list_label_->count() - 1;
  730. unsaved_data_ = 1;
  731. }
  732. void
  733. ImageLabeler::addLabel(
  734. int aLabelID,
  735. bool isMain,
  736. QString aLabel
  737. )
  738. {
  739. QListWidgetItem *newItem = new QListWidgetItem;
  740. if (0 == aLabelID) {
  741. aLabel = QString("BACKGROUND");
  742. newItem->setFlags(
  743. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  744. Qt::ItemIsEnabled
  745. );
  746. }
  747. QString label;
  748. label.append(QString("%1: %2").
  749. arg(aLabelID).
  750. arg(aLabel)
  751. );
  752. /* TODO: check if there is another main label and make it common */
  753. if (isMain) {
  754. main_label_ = aLabelID;
  755. }
  756. QPixmap iconPix = QPixmap(20, 20);
  757. QColor color;
  758. if (0 != aLabelID)
  759. color = Qt::white;
  760. else
  761. color = Qt::black;
  762. iconPix.fill(color);
  763. QIcon icon(iconPix);
  764. list_label_colors_.append(color.rgb());
  765. newItem->setIcon(icon);
  766. newItem->setText(label);
  767. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  768. list_label_->addItem(newItem);
  769. list_label_->setItemSelected(newItem, true);
  770. unsaved_data_ = 1;
  771. }
  772. void
  773. ImageLabeler::editLabel(QListWidgetItem *anItem)
  774. {
  775. QString label = anItem->text();
  776. int itemRow = list_label_->row(anItem);
  777. QString prefix = QString("%1: ").arg(itemRow);
  778. if (-1 != label.indexOf(prefix)) {
  779. return;
  780. /* NOTREACHED */
  781. }
  782. label.prepend(QString("%1: ").arg(itemRow));
  783. if (main_label_ == itemRow) {
  784. label.append(" #main");
  785. }
  786. list_label_->blockSignals(true);
  787. anItem->setText(label);
  788. list_label_->blockSignals(false);
  789. unsaved_data_ = 1;
  790. }
  791. void
  792. ImageLabeler::removeLabel()
  793. {
  794. if (0 == list_label_->count()) {
  795. return;
  796. /* NOTREACHED */
  797. }
  798. //QListWidgetItem *current = list_label_->currentItem();
  799. /* we need to keep BACKGROUND category */
  800. if (label_ID_ < 1) {
  801. return;
  802. /* NOTREACHED */
  803. }
  804. if (list_label_->count() <= label_ID_ ||
  805. list_label_colors_.count() <= label_ID_) {
  806. return;
  807. /* NOTREACHED */
  808. }
  809. list_label_->takeItem(label_ID_);
  810. list_label_colors_.takeAt(label_ID_);
  811. unsaved_data_ = 1;
  812. }
  813. void
  814. ImageLabeler::setLabelID(
  815. QListWidgetItem *anItem
  816. )
  817. {
  818. Q_UNUSED(anItem)
  819. if (!list_label_->count()) {
  820. return;
  821. /* NOTREACHED */
  822. }
  823. label_ID_ = list_label_->row(anItem);
  824. }
  825. void
  826. ImageLabeler::addBBoxArea(
  827. int anID,
  828. BoundingBox aBBox,
  829. int itemID
  830. )
  831. {
  832. QListWidgetItem *newItem = new QListWidgetItem;
  833. if (-1 == itemID)
  834. itemID = list_areas_->count();
  835. QString label;
  836. label.append(QString("%1: ").arg(itemID));
  837. label.append(QString("BBox #%1; ").arg(anID));
  838. label.append(QString("LabelID: %1; ").arg(aBBox.label_ID_));
  839. label.append(
  840. QString("data:%1;%2;%3;%4; ").
  841. arg(aBBox.rect.topLeft().x()).
  842. arg(aBBox.rect.topLeft().y()).
  843. arg(aBBox.rect.width()).
  844. arg(aBBox.rect.height())
  845. );
  846. newItem->setText(label);
  847. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  848. list_areas_->insertItem(itemID, newItem);
  849. list_areas_->setItemSelected(newItem, true);
  850. }
  851. void
  852. ImageLabeler::addPolyArea(
  853. int aPolyID,
  854. Polygon aPoly,
  855. int itemID
  856. )
  857. {
  858. QListWidgetItem *newItem = new QListWidgetItem;
  859. if (-1 == itemID)
  860. itemID = list_areas_->count();
  861. QString label;
  862. label.append(QString("%1: ").arg(itemID));
  863. label.append(QString("Poly #%1; ").arg(aPolyID));
  864. label.append(QString("LabelID: %1; ").arg(aPoly.label_ID_));
  865. label.append("points:");
  866. for (int i = 0; i < aPoly.poly.count(); i++) {
  867. label.append(
  868. QString("%1;%2;").
  869. arg(aPoly.poly.point(i).x()).
  870. arg(aPoly.poly.point(i).y())
  871. );
  872. }
  873. newItem->setText(label);
  874. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  875. list_areas_->insertItem(itemID, newItem);
  876. list_areas_->setItemSelected(newItem, true);
  877. }
  878. void
  879. ImageLabeler::editArea()
  880. {
  881. bool oldState = list_areas_->blockSignals(true);
  882. if (!list_areas_->currentItem() || !list_areas_->count()) {
  883. return;
  884. /* NOTREACHED */
  885. }
  886. QListWidgetItem *current = list_areas_->currentItem();
  887. old_area_string_ = current->text();
  888. current->setFlags(current->flags() | Qt::ItemIsEditable);
  889. list_areas_->editItem(current);
  890. list_areas_->blockSignals(oldState);
  891. }
  892. void
  893. ImageLabeler::onAreaItemChange(QListWidgetItem *anItem)
  894. {
  895. list_areas_->blockSignals(true);
  896. QString areaString = anItem->text();
  897. int oldID = -1;
  898. if (-1 != areaString.indexOf("Poly")) {
  899. Polygon *poly = new Polygon;
  900. *poly = polyFromString(&areaString, &oldID);
  901. if (-1 < poly->label_ID_ && !poly->poly.isEmpty() &&
  902. -1 < oldID) {
  903. list_polygon_.takeAt(oldID);
  904. list_polygon_.insert(oldID, poly);
  905. }
  906. else
  907. anItem->setText(old_area_string_);
  908. }
  909. else if (-1 != areaString.indexOf("BBox")) {
  910. BoundingBox *bbox = new BoundingBox;
  911. *bbox = BBoxFromString(&areaString, &oldID);
  912. if (-1 < bbox->label_ID_ && -1 < oldID) {
  913. list_bounding_box_.takeAt(oldID);
  914. list_bounding_box_.insert(oldID, bbox);
  915. }
  916. else
  917. anItem->setText(old_area_string_);
  918. }
  919. else {
  920. showWarning(tr("record format is corrupted, try again"));
  921. anItem->setText(old_area_string_);
  922. }
  923. anItem->setFlags(anItem->flags() ^ Qt::ItemIsEditable);
  924. list_areas_->blockSignals(false);
  925. image_holder_->update();
  926. unsaved_data_ = 1;
  927. }
  928. void
  929. ImageLabeler::onAreaEdit()
  930. {
  931. if (!list_areas_->count() ||
  932. -1 == image_holder_->focusedSelection()) {
  933. //showWarning(tr("You haven't added any label"));
  934. return;
  935. /* NOTREACHED */
  936. }
  937. int figureID = image_holder_->focusedSelection();
  938. Figure figure = image_holder_->focusedSelectionType();
  939. for (int i = 0; i < list_areas_->count(); i++) {
  940. QListWidgetItem *item = list_areas_->item(i);
  941. QString text = item->text();
  942. if (RectFigure == figure && -1 != text.indexOf("BBox")) {
  943. bool ok = 0;
  944. int num = getNumFromString(&text, "BBox #", ";", &ok);
  945. if (ok && num == figureID) {
  946. list_areas_->takeItem(i);
  947. addBBoxArea(num, *list_bounding_box_.at(num), i);
  948. }
  949. }
  950. else if (PolyFigure == figure && -1 != text.indexOf("Poly")) {
  951. bool ok = 0;
  952. int num = getNumFromString(&text, "Poly #", ";", &ok);
  953. if (ok && num == figureID) {
  954. list_areas_->takeItem(i);
  955. addPolyArea(num, *list_polygon_.at(num), i);
  956. }
  957. }
  958. }
  959. unsaved_data_ = 1;
  960. }
  961. void
  962. ImageLabeler::deleteArea()
  963. {
  964. if (!list_areas_->currentItem() || !list_areas_->count()) {
  965. return;
  966. /* NOTREACHED */
  967. }
  968. QString text = list_areas_->currentItem()->text();
  969. bool ok = 0;
  970. //int labelID = getNumFromString(&text, "LabelID:", ";", &ok);
  971. // if (!ok) {
  972. // return;
  973. // /* NOTREACHED */
  974. // }
  975. /*
  976. * 0 - bbox
  977. * 1 - poly
  978. */
  979. QString shape;
  980. if (-1 != text.indexOf("BBox"))
  981. shape = QString("BBox");
  982. else if (-1 != text.indexOf("Poly"))
  983. shape = QString("Poly");
  984. else {
  985. return;
  986. /* NOTREACHED */
  987. }
  988. int areaNum = getNumFromString(&text, "#", ";", &ok);
  989. if (!ok) {
  990. return;
  991. /* NOTREACHED */
  992. }
  993. int currentItemRow = list_areas_->row(list_areas_->currentItem());
  994. /* changing all shapes(depends on current) which are next in the list */
  995. for (int i = list_areas_->count() - 1; i > currentItemRow; i--) {
  996. QListWidgetItem item = *(list_areas_->item(i));
  997. QString newText = item.text();
  998. if (-1 == newText.indexOf(shape))
  999. continue;
  1000. int num = getNumFromString(&newText, "#", ";", &ok);
  1001. num--;
  1002. QString numString = QString("%1").arg(num);
  1003. int numPos = newText.indexOf("#") + 1;
  1004. newText.replace(numPos, numString.size(), numString);
  1005. list_areas_->takeItem(i);
  1006. list_areas_->insertItem(i, newText);
  1007. }
  1008. list_areas_->takeItem(currentItemRow);
  1009. if (shape == "BBox")
  1010. list_bounding_box_.removeAt(areaNum);
  1011. else
  1012. list_polygon_.removeAt(areaNum);
  1013. image_holder_->update();
  1014. unsaved_data_ = 1;
  1015. }
  1016. void
  1017. ImageLabeler::toggleLabelPriority()
  1018. {
  1019. if (!list_label_->count()) {
  1020. return;
  1021. /* NOTREACHED */
  1022. }
  1023. QListWidgetItem *item = list_label_->currentItem();
  1024. /* because we need to keep BACKGROUND category */
  1025. if (0 == list_label_->row(item)) {
  1026. return;
  1027. /* NOTREACHED */
  1028. }
  1029. int itemRow = list_label_->row(item);
  1030. QString text = item->text();
  1031. /* cleaning previous " #main" mark */
  1032. if (-1 != main_label_) {
  1033. QListWidgetItem *lastMain = list_label_->item(main_label_);
  1034. QString lastMainText = lastMain->text();
  1035. int mainPos = lastMainText.indexOf(" #main");
  1036. lastMainText = lastMainText.mid(0, mainPos);
  1037. list_label_->blockSignals(true);
  1038. lastMain->setText(lastMainText);
  1039. list_label_->blockSignals(false);
  1040. }
  1041. if (main_label_ == itemRow) {
  1042. int mainPos = text.indexOf(" #main");
  1043. text = text.mid(0, mainPos);
  1044. main_label_ = -1;
  1045. }
  1046. else {
  1047. text.append(" #main");
  1048. main_label_ = list_label_->row(item);
  1049. }
  1050. list_label_->blockSignals(true);
  1051. item->setText(text);
  1052. list_label_->blockSignals(false);
  1053. image_holder_->update();
  1054. unsaved_data_ = 1;
  1055. }
  1056. void
  1057. ImageLabeler::getImagesFromDir(const QDir &dir)
  1058. {
  1059. /* avoiding freezing during recursive search for files */
  1060. QApplication::processEvents();
  1061. if (interrupt_search_) {
  1062. clearAll();
  1063. return;
  1064. }
  1065. QStringList filenameFilter;
  1066. filenameFilter <<
  1067. "*.jpeg" <<
  1068. "*.jpg" <<
  1069. "*.gif" <<
  1070. "*.png" <<
  1071. "*.bmp" <<
  1072. "*.tiff" <<
  1073. "*.dat"
  1074. ;
  1075. QStringList listImages =
  1076. dir.entryList(filenameFilter, QDir::Files);
  1077. QStringList ignoredFiles;
  1078. foreach (QString file, listImages) {
  1079. if (ignoredFiles.contains(file) ||
  1080. file.contains("_segmented", Qt::CaseInsensitive) ||
  1081. file.contains(".dat", Qt::CaseInsensitive)) {
  1082. continue;
  1083. }
  1084. Image newImage;
  1085. newImage.image_ = dir.absoluteFilePath(file);
  1086. /* TODO: think about loading pascal files */
  1087. newImage.pas_ = 0;
  1088. QString labeled = alterFileName(file, "_labeled");
  1089. labeled = removePath(labeled);
  1090. labeled.append(".dat");
  1091. if (listImages.contains(labeled, Qt::CaseInsensitive))
  1092. newImage.labeled_ = 1;
  1093. else
  1094. {
  1095. newImage.labeled_ = 0;
  1096. }
  1097. addImage(&newImage);
  1098. }
  1099. QStringList listDir = dir.entryList(QDir::Dirs);
  1100. foreach (QString subdir, listDir) {
  1101. if ("." == subdir || ".." == subdir)
  1102. continue;
  1103. getImagesFromDir(QDir(dir.absoluteFilePath(subdir)));
  1104. }
  1105. }
  1106. void
  1107. ImageLabeler::nextImage()
  1108. {
  1109. if (list_images_->isEmpty()) {
  1110. return;
  1111. /* NOTREACHED */
  1112. }
  1113. if (askForUnsavedData()) {
  1114. return;
  1115. /* NOTREACHED */
  1116. }
  1117. if (list_images_widget_->count() - 1 == image_ID_) {
  1118. //current_image_ = list_images_->at(0).image_;
  1119. image_ID_ = 0;
  1120. }
  1121. else {
  1122. image_ID_ ++;
  1123. //current_image_ = list_images_->at(image_ID_).image_;
  1124. }
  1125. if (!selectImage(image_ID_)) {
  1126. showWarning(tr("Next image is not available"));
  1127. return;
  1128. /* NOTREACHED */
  1129. }
  1130. list_images_widget_->setCurrentRow(image_ID_);
  1131. if (current_image_.isEmpty()) {
  1132. return;
  1133. /* NOTREACHED */
  1134. }
  1135. QString winTitle;
  1136. winTitle.append("ImageLabeler - ");
  1137. winTitle.append(current_image_);
  1138. setWindowTitle(winTitle);
  1139. image_->load(current_image_);
  1140. image_holder_->resize(image_->size());
  1141. image_holder_->setPixmap(*image_);
  1142. list_bounding_box_.clear();
  1143. list_polygon_.clear();
  1144. list_areas_->clear();
  1145. image_holder_->clearAll();
  1146. segmented_image_.clear();
  1147. clearLabelColorList();
  1148. //setWindowTitle(tr("ImageLabeler"));
  1149. }
  1150. void
  1151. ImageLabeler::prevImage()
  1152. {
  1153. if (!list_images_widget_->count()) {
  1154. return;
  1155. /* NOTREACHED */
  1156. }
  1157. if (askForUnsavedData()) {
  1158. return;
  1159. /* NOTREACHED */
  1160. }
  1161. if (!image_ID_) {
  1162. image_ID_ = list_images_widget_->count() - 1;
  1163. //current_image_ = list_images_->at(image_ID_).image_;
  1164. }
  1165. else {
  1166. image_ID_--;
  1167. //current_image_ = list_images_->at(image_ID_).image_;
  1168. }
  1169. list_images_widget_->setCurrentRow(image_ID_);
  1170. if (!selectImage(image_ID_)) {
  1171. showWarning(tr("Next image is not available"));
  1172. return;
  1173. /* NOTREACHED */
  1174. }
  1175. QString winTitle;
  1176. winTitle.append("ImageLabeler - ");
  1177. winTitle.append(current_image_);
  1178. setWindowTitle(winTitle);
  1179. image_->load(current_image_);
  1180. image_holder_->resize(image_->size());
  1181. image_holder_->setPixmap(*image_);
  1182. list_bounding_box_.clear();
  1183. list_polygon_.clear();
  1184. list_areas_->clear();
  1185. image_holder_->clearAll();
  1186. segmented_image_.clear();
  1187. clearLabelColorList();
  1188. //setWindowTitle(tr("ImageLabeler"));
  1189. }
  1190. void
  1191. ImageLabeler::saveAllInfo()
  1192. {
  1193. if (!list_images_widget_->count()) {
  1194. showWarning("You have not opened any image yet");
  1195. return;
  1196. /* NOTREACHED */
  1197. }
  1198. /* ------------------------------------------------------------------------
  1199. * XML part
  1200. */
  1201. QDomDocument doc(tr("ImageLabeler"));
  1202. QDomElement root = doc.createElement(tr("pixelwise_labeling"));
  1203. doc.appendChild(root);
  1204. QDomElement image = doc.createElement(tr("image"));
  1205. root.appendChild(image);
  1206. QDomText pathToImage = doc.createTextNode(current_image_);
  1207. image.appendChild(pathToImage);
  1208. if (!segmented_image_.isEmpty()) {
  1209. QDomElement segmentedImage = doc.createElement(tr("segmented"));
  1210. root.appendChild(segmentedImage);
  1211. QDomText pathToSegmented = doc.createTextNode(segmented_image_);
  1212. segmentedImage.appendChild(pathToSegmented);
  1213. }
  1214. QDomElement description = doc.createElement(tr("description"));
  1215. root.appendChild(description);
  1216. QDomText descriptionText = doc.createTextNode(image_description_);
  1217. description.appendChild(descriptionText);
  1218. legendToXml(&doc, &root);
  1219. objectsToXml(&doc, &root);
  1220. setPureData();
  1221. QDomElement pureData = doc.createElement(tr("pure_data"));
  1222. QSize imageSize = image_->size();
  1223. QString pixelValues;
  1224. for (int i = 0; i < imageSize.height(); i++) {
  1225. for (int j = 0; j < imageSize.width(); j++) {
  1226. pixelValues.append(QString("%1").arg(pure_data_[i][j]));
  1227. //pixelValues.append(QString(""));
  1228. }
  1229. pixelValues.append("\n");
  1230. }
  1231. QDomText pureDataText = doc.createTextNode(pixelValues);
  1232. pureData.appendChild(pureDataText);
  1233. root.appendChild(pureData);
  1234. QString xml = doc.toString();
  1235. /* ------------------------------------------------------------------------
  1236. * XML part ends
  1237. */
  1238. QFileDialog fileDialog(0, tr("Save all info"));
  1239. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1240. fileDialog.setDefaultSuffix("dat");
  1241. fileDialog.setFileMode(QFileDialog::AnyFile);
  1242. QString dir = getDirFromPath(&current_image_);
  1243. /* altering the name of a new file */
  1244. QString newFileName = alterFileName(current_image_, "_labeled");
  1245. newFileName = removePath(newFileName);
  1246. fileDialog.selectFile(newFileName);
  1247. fileDialog.setDirectory(dir);
  1248. QString filename;
  1249. if (fileDialog.exec()) {
  1250. filename = fileDialog.selectedFiles().last();
  1251. }
  1252. else {
  1253. //showWarning(tr("Can not open file dialog"));
  1254. return;
  1255. /* NOTREACHED */
  1256. }
  1257. if (filename.isEmpty()) {
  1258. return;
  1259. /* NOTREACHED */
  1260. }
  1261. QFile file(filename);
  1262. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1263. showWarning(tr("Can not open file for writing"));
  1264. return;
  1265. /* NOTREACHED */
  1266. }
  1267. file.write(xml.toLocal8Bit());
  1268. file.close();
  1269. unsaved_data_ = 0;
  1270. }
  1271. void
  1272. ImageLabeler::saveSegmentedPicture()
  1273. {
  1274. if (list_bounding_box_.isEmpty() && list_polygon_.isEmpty()) {
  1275. return;
  1276. /* NOTREACHED */
  1277. }
  1278. setPureData();
  1279. // QString suffix = *current_image_;
  1280. // int dotPos = suffix.lastIndexOf(".");
  1281. // suffix.mid(dotPos + 1, suffix.size() - dotPos - 1);
  1282. QFileDialog fileDialog(0, tr("Save segmented picture"));
  1283. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1284. fileDialog.setDefaultSuffix("png");
  1285. fileDialog.setFileMode(QFileDialog::AnyFile);
  1286. QString dir = getDirFromPath(&current_image_);
  1287. /* altering the name of a new file */
  1288. QString newFileName = alterFileName(current_image_, "_segmented");
  1289. newFileName = removePath(newFileName);
  1290. fileDialog.selectFile(newFileName);
  1291. fileDialog.setDirectory(dir);
  1292. QString filename;
  1293. if (fileDialog.exec()) {
  1294. filename = fileDialog.selectedFiles().last();
  1295. }
  1296. else {
  1297. return;
  1298. /* NOTREACHED */
  1299. }
  1300. QSize imageSize = image_holder_->pixmap()->size();
  1301. QImage newImage(imageSize, QImage::Format_RGB32);
  1302. bool generateColorsFlag = auto_color_generation_;
  1303. bool flag = 0;
  1304. /* checking if all the colors are are different from white(default) */
  1305. if (!generateColorsFlag) {
  1306. for (int i = 1; i < list_label_->count(); i++) {
  1307. if (list_label_colors_.at(i) == 0xffffffff && !flag)
  1308. flag = 1;
  1309. else if (list_label_colors_.at(i) == 0xffffffff && flag) {
  1310. QMessageBox msgBox;
  1311. msgBox.setText(tr("There are few labels with default white color."));
  1312. msgBox.setInformativeText(tr("Do you want to generate all colors automatically? Otherwise you'll have to do it manually."));
  1313. msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  1314. msgBox.setDefaultButton(QMessageBox::Yes);
  1315. msgBox.setIcon(QMessageBox::Question);
  1316. int ret = msgBox.exec();
  1317. if (QMessageBox::Yes == ret) {
  1318. generateColorsFlag = 1;
  1319. break;
  1320. }
  1321. else {
  1322. return;
  1323. /* NOTREACHED */
  1324. }
  1325. }
  1326. }
  1327. }
  1328. /* generating colors for labels */
  1329. if (list_label_colors_.count() < list_label_->count() ||
  1330. generateColorsFlag) {
  1331. generateColors();
  1332. }
  1333. for (int i = 0; i < imageSize.height(); i++)
  1334. for (int j = 0; j < imageSize.width(); j++) {
  1335. newImage.setPixel(j, i, list_label_colors_.at(pure_data_[i][j]));
  1336. }
  1337. if (!newImage.save(filename, "png", 100)) {
  1338. showWarning(tr("An error occurred while saving the segmented image"));
  1339. return;
  1340. /* NOTREACHED */
  1341. }
  1342. segmented_image_ = filename;
  1343. action_view_segmented_->setEnabled(true);
  1344. }
  1345. void
  1346. ImageLabeler::saveLegend()
  1347. {
  1348. if (!list_label_->count()) {
  1349. showWarning("You have not added any label yet");
  1350. return;
  1351. /* NOTREACHED */
  1352. }
  1353. /* ------------------------------------------------------------------------
  1354. * XML part
  1355. */
  1356. QDomDocument doc(tr("ImageLabeler"));
  1357. QDomElement root = doc.createElement(tr("root"));
  1358. doc.appendChild(root);
  1359. legendToXml(&doc, &root);
  1360. QString xml = doc.toString();
  1361. /* ------------------------------------------------------------------------
  1362. * XML part ends
  1363. */
  1364. QFileDialog fileDialog(0, tr("Save legend"));
  1365. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1366. fileDialog.setDefaultSuffix("dat");
  1367. fileDialog.setFileMode(QFileDialog::AnyFile);
  1368. QString dir = getDirFromPath(&current_image_);
  1369. /* altering the name of a new file */
  1370. QString newFileName = alterFileName(current_image_, "_legend");
  1371. newFileName = removePath(newFileName);
  1372. fileDialog.selectFile(newFileName);
  1373. fileDialog.setDirectory(dir);
  1374. QString filename;
  1375. if (fileDialog.exec()) {
  1376. filename = fileDialog.selectedFiles().last();
  1377. }
  1378. else {
  1379. //showWarning(tr("Can not open file dialog"));
  1380. return;
  1381. /* NOTREACHED */
  1382. }
  1383. if (filename.isEmpty()) {
  1384. return;
  1385. /* NOTREACHED */
  1386. }
  1387. QFile file(filename);
  1388. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1389. showWarning(tr("Can not open file for writing"));
  1390. return;
  1391. /* NOTREACHED */
  1392. }
  1393. file.write(xml.toLocal8Bit());
  1394. file.close();
  1395. }
  1396. void
  1397. ImageLabeler::loadInfo()
  1398. {
  1399. if (askForUnsavedData()) {
  1400. return;
  1401. /* NOTREACHED */
  1402. }
  1403. QFileDialog fileDialog(0, tr("Load file with info"));
  1404. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1405. fileDialog.setDefaultSuffix("dat");
  1406. fileDialog.setFileMode(QFileDialog::AnyFile);
  1407. QString filename;
  1408. if (fileDialog.exec()) {
  1409. filename = fileDialog.selectedFiles().last();
  1410. }
  1411. else {
  1412. //showWarning(tr("Can not open file dialog"));
  1413. return;
  1414. /* NOTREACHED */
  1415. }
  1416. clearAllTool();
  1417. clearLabelList();
  1418. if (loadInfo(filename)) {
  1419. enableTools();
  1420. Image newImage;
  1421. newImage.image_ = current_image_;
  1422. newImage.labeled_ = 1;
  1423. newImage.pas_ = 0;
  1424. addImage(&newImage);
  1425. image_ID_ = list_images_widget_->count() - 1;
  1426. list_images_widget_->setCurrentRow(image_ID_);
  1427. }
  1428. unsaved_data_ = 0;
  1429. }
  1430. bool
  1431. ImageLabeler::loadInfo(QString filename)
  1432. {
  1433. QDomDocument doc("Image Labeler");
  1434. QFile file(filename);
  1435. if (!file.open(QIODevice::ReadOnly)) {
  1436. showWarning(tr("Can not open such file"));
  1437. return false;
  1438. /* NOTREACHED */
  1439. }
  1440. QString errMsg;
  1441. if (!doc.setContent(&file, &errMsg)) {
  1442. showWarning(errMsg);
  1443. file.close();
  1444. return false;
  1445. /* NOTREACHED */
  1446. }
  1447. file.close();
  1448. //clearAll();
  1449. //enableTools();
  1450. /* getting all info */
  1451. QDomElement elements = doc.documentElement();
  1452. QDomNode rootNode = elements.firstChild();
  1453. QString string;
  1454. while(!rootNode.isNull()) {
  1455. QDomElement element = rootNode.toElement();
  1456. if(!element.isNull()) {
  1457. /* path to the image */
  1458. if (element.tagName() == "image") {
  1459. string = element.text();
  1460. if (string.isEmpty()) {
  1461. showWarning(
  1462. tr(
  1463. "The file with data doesn't contain path to the image"
  1464. )
  1465. );
  1466. return false;
  1467. /* NOTREACHED */
  1468. }
  1469. if (!image_->load(string)) {
  1470. return false;
  1471. /* NOTREACHED */
  1472. }
  1473. // Image newImage;
  1474. // newImage.image_ = string;
  1475. // newImage.labeled_ = 1;
  1476. // addImage(&newImage);
  1477. current_image_ = string;
  1478. // image_ID_ = list_images_widget_->count() - 1;
  1479. // list_images_widget_->setCurrentRow(image_ID_);
  1480. QString winTitle;
  1481. winTitle.append("ImageLabeler - ");
  1482. winTitle.append(current_image_);
  1483. setWindowTitle(winTitle);
  1484. image_holder_->resize(image_->size());
  1485. image_holder_->setPixmap(*image_);
  1486. }
  1487. /* path to the segmented image */
  1488. if (element.tagName() == "segmented") {
  1489. string = element.text();
  1490. if (string.isEmpty()) {
  1491. continue;
  1492. }
  1493. segmented_image_ = string;
  1494. action_view_segmented_->setEnabled(true);
  1495. }
  1496. /* image description */
  1497. else if (element.tagName() == "description" &&
  1498. !element.text().isEmpty()) {
  1499. image_description_ = element.text();
  1500. }
  1501. /* legend */
  1502. else if (element.tagName() == "legend") {
  1503. loadLegendFromNode(&element);
  1504. }
  1505. /* objects */
  1506. else if (element.tagName() == "objects") {
  1507. QDomNode subNode = element.firstChild();
  1508. QDomElement subElement;
  1509. while(!subNode.isNull()) {
  1510. subElement = subNode.toElement();
  1511. if (subElement.isNull() || subElement.text().isEmpty()) {
  1512. subNode = subNode.nextSibling();
  1513. continue;
  1514. }
  1515. string = subElement.attribute("id");
  1516. bool ok = 1;
  1517. int id = string.toInt(&ok, 10);
  1518. if (!ok) {
  1519. qDebug() <<
  1520. "loadInfo: "
  1521. "poly id format is corrupted";
  1522. subNode = subNode.nextSibling();
  1523. continue;
  1524. }
  1525. string = subElement.text();
  1526. if (subElement.tagName() == "bbox") {
  1527. addBBoxFromData(&string, &id);
  1528. }
  1529. if (subElement.tagName() == "poly") {
  1530. addPolyFromData(&string, &id);
  1531. }
  1532. subNode = subNode.nextSibling();
  1533. }
  1534. }
  1535. }
  1536. rootNode = rootNode.nextSibling();
  1537. }
  1538. unsaved_data_ = 0;
  1539. return true;
  1540. }
  1541. void
  1542. ImageLabeler::loadPascalFile()
  1543. {
  1544. if (askForUnsavedData()) {
  1545. return;
  1546. /* NOTREACHED */
  1547. }
  1548. QFileDialog fileDialog(0, tr("Load pascal file"));
  1549. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1550. fileDialog.setDefaultSuffix("xml");
  1551. fileDialog.setFileMode(QFileDialog::AnyFile);
  1552. QStringList filters;
  1553. filters << "PASCAL xml data (*.xml)"
  1554. << "Any files (*)";
  1555. fileDialog.setNameFilters(filters);
  1556. QString filename;
  1557. if (fileDialog.exec()) {
  1558. filename = fileDialog.selectedFiles().last();
  1559. }
  1560. else {
  1561. //showWarning(tr("Can not open file dialog"));
  1562. return;
  1563. /* NOTREACHED */
  1564. }
  1565. clearAllTool();
  1566. clearLabelList();
  1567. if (loadPascalFile(filename)) {
  1568. enableTools();
  1569. Image newImage;
  1570. newImage.image_ = current_image_;
  1571. newImage.labeled_ = 1;
  1572. newImage.pas_ = 1;
  1573. addImage(&newImage);
  1574. image_ID_ = list_images_widget_->count() - 1;
  1575. list_images_widget_->setCurrentRow(image_ID_);
  1576. }
  1577. unsaved_data_ = 0;
  1578. }
  1579. bool
  1580. ImageLabeler::loadPascalFile(QString aFilename)
  1581. {
  1582. QDomDocument doc;
  1583. QFile file(aFilename);
  1584. if (!file.open(QIODevice::ReadOnly)) {
  1585. showWarning(tr("Can not open such file"));
  1586. return false;
  1587. /* NOTREACHED */
  1588. }
  1589. QString errMsg;
  1590. if (!doc.setContent(&file, &errMsg)) {
  1591. showWarning(errMsg);
  1592. file.close();
  1593. return false;
  1594. /* NOTREACHED */
  1595. }
  1596. file.close();
  1597. //clearAll();
  1598. //enableTools();
  1599. /* getting all info */
  1600. QDomElement elements = doc.documentElement();
  1601. QDomNode rootNode = elements.firstChild();
  1602. QString string;
  1603. QString path = getPathFromFilename(aFilename);
  1604. QString filename;
  1605. QStringList labels;
  1606. labels << "BACKGROUND";
  1607. int labelID;
  1608. while(!rootNode.isNull()) {
  1609. QDomElement element = rootNode.toElement();
  1610. if(!element.isNull()) {
  1611. /* folder */
  1612. if (element.tagName() == "folder") {
  1613. string = element.text();
  1614. if (!string.isEmpty()) {
  1615. path.append(string);
  1616. }
  1617. }
  1618. /* filename */
  1619. else if (element.tagName() == "filename") {
  1620. string = element.text();
  1621. if (!string.isEmpty()) {
  1622. filename = string;
  1623. }
  1624. }
  1625. /* object */
  1626. else if (element.tagName() == "object") {
  1627. QDomNode subNode = element.firstChild();
  1628. QDomElement subElement;
  1629. while(!subNode.isNull()) {
  1630. subElement = subNode.toElement();
  1631. if (subElement.isNull() || subElement.text().isEmpty()) {
  1632. subNode = subNode.nextSibling();
  1633. continue;
  1634. }
  1635. /* label */
  1636. if (subElement.tagName() == "name") {
  1637. string = subElement.text();
  1638. if (!string.isEmpty() &&
  1639. !labels.contains(string, Qt::CaseInsensitive))
  1640. {
  1641. addLabel(list_label_->count(), 0, string);
  1642. labelID = labels.count();
  1643. labels << string;
  1644. }
  1645. else if (labels.contains(string, Qt::CaseInsensitive)) {
  1646. for (int i = 0; i < labels.count(); i++) {
  1647. if (labels.at(i) == string)
  1648. labelID = i;
  1649. }
  1650. }
  1651. }
  1652. if (subElement.tagName() == "bndbox") {
  1653. /* 2 points */
  1654. QPoint topLeft;
  1655. QPoint bottomRight;
  1656. QDomNode bboxNode = subElement.firstChild();
  1657. QDomElement bboxElement;
  1658. while(!bboxNode.isNull()) {
  1659. bboxElement = bboxNode.toElement();
  1660. string.clear();
  1661. bool ok = 1;
  1662. if (bboxElement.tagName() == "xmin") {
  1663. string = bboxElement.text();
  1664. if (string.isEmpty())
  1665. ok = 0;
  1666. double xmin = string.toDouble(&ok);
  1667. if (ok)
  1668. topLeft.setX(qRound(xmin));
  1669. }
  1670. else if (bboxElement.tagName() == "ymin") {
  1671. string = bboxElement.text();
  1672. if (string.isEmpty())
  1673. ok = 0;
  1674. double ymin = string.toDouble(&ok);
  1675. if (ok)
  1676. topLeft.setY(qRound(ymin));
  1677. }
  1678. else if (bboxElement.tagName() == "xmax") {
  1679. string = bboxElement.text();
  1680. if (string.isEmpty())
  1681. ok = 0;
  1682. double xmax = string.toDouble(&ok);
  1683. if (ok)
  1684. bottomRight.setX(qRound(xmax));
  1685. }
  1686. else if (bboxElement.tagName() == "ymax") {
  1687. string = bboxElement.text();
  1688. if (string.isEmpty())
  1689. ok = 0;
  1690. double ymax = string.toDouble(&ok);
  1691. if (ok)
  1692. bottomRight.setY(qRound(ymax));
  1693. }
  1694. if (string.isEmpty() || !ok)
  1695. break;
  1696. bboxNode = bboxNode.nextSibling();
  1697. if (bboxNode.isNull()) {
  1698. BoundingBox *bbox = new BoundingBox;
  1699. bbox->rect.setTopLeft(topLeft);
  1700. bbox->rect.setBottomRight(bottomRight);
  1701. bbox->label_ID_ = labelID;
  1702. addBBox(bbox);
  1703. }
  1704. }
  1705. }
  1706. subNode = subNode.nextSibling();
  1707. }
  1708. }
  1709. }
  1710. rootNode = rootNode.nextSibling();
  1711. }
  1712. if (!image_->load(path + "/" + filename)) {
  1713. return false;
  1714. /* NOTREACHED */
  1715. }
  1716. current_image_ = path + filename;
  1717. QString winTitle;
  1718. winTitle.append("ImageLabeler - ");
  1719. winTitle.append(current_image_);
  1720. setWindowTitle(winTitle);
  1721. image_holder_->resize(image_->size());
  1722. image_holder_->setPixmap(*image_);
  1723. unsaved_data_ = 0;
  1724. return true;
  1725. }
  1726. void
  1727. ImageLabeler::loadPascalPolys()
  1728. {
  1729. if (current_image_.isEmpty()) {
  1730. return;
  1731. /* NOTREACHED */
  1732. }
  1733. QFileDialog fileDialog(0, tr("Load pascal polygons"));
  1734. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1735. fileDialog.setDefaultSuffix("polygon");
  1736. fileDialog.setFileMode(QFileDialog::AnyFile);
  1737. QStringList filters;
  1738. filters << "PASCAL polygons (*.polygon)"
  1739. << "Any files (*)";
  1740. fileDialog.setNameFilters(filters);
  1741. QString filename;
  1742. if (fileDialog.exec()) {
  1743. filename = fileDialog.selectedFiles().last();
  1744. }
  1745. else {
  1746. //showWarning(tr("Can not open file dialog"));
  1747. return;
  1748. /* NOTREACHED */
  1749. }
  1750. //clearAllTool();
  1751. //clearLabelList();
  1752. if (!loadPascalPolys(filename)) {
  1753. showWarning(tr("File format is corrupted."));
  1754. }
  1755. unsaved_data_ = 0;
  1756. image_holder_->update();
  1757. }
  1758. bool
  1759. ImageLabeler::loadPascalPolys(QString aFilename)
  1760. {
  1761. QFile file(aFilename);
  1762. if (!file.open(QIODevice::ReadOnly)) {
  1763. showWarning(tr("Can not open such file"));
  1764. return false;
  1765. /* NOTREACHED */
  1766. }
  1767. QByteArray data = file.readAll();
  1768. file.close();
  1769. QString label;
  1770. int lastSpace = 0;
  1771. int pointCount = 0;
  1772. Polygon *poly = 0;
  1773. QPoint point;
  1774. bool evenFlag = 0;
  1775. //for (int i = 0; i < data.length(); i++) {
  1776. int i = 0;
  1777. while (i < data.length()) {
  1778. if (data.at(i) == ' ' && label.isEmpty() && !pointCount) {
  1779. label = QString(data.mid(lastSpace, i - lastSpace));
  1780. lastSpace = i + 1;
  1781. }
  1782. else if (data.at(i) == ' ' && !label.isEmpty() && !pointCount) {
  1783. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1784. bool ok = 1;
  1785. pointCount = num.toInt(&ok, 10);
  1786. if (!ok) {
  1787. return false;
  1788. /* NOTREACHED */
  1789. }
  1790. poly = new Polygon;
  1791. poly->label_ID_ = -1;
  1792. lastSpace = i + 1;
  1793. }
  1794. else if (data.at(i) == ' ' &&
  1795. !label.isEmpty() &&
  1796. pointCount &&
  1797. !evenFlag)
  1798. {
  1799. evenFlag = 1;
  1800. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1801. bool ok = 1;
  1802. int coor = qRound(num.toDouble(&ok));
  1803. if (!ok) {
  1804. return false;
  1805. /* NOTREACHED */
  1806. }
  1807. point.setX(coor);
  1808. lastSpace = i + 1;
  1809. }
  1810. else if (data.at(i) == ' ' &&
  1811. !label.isEmpty() &&
  1812. pointCount &&
  1813. evenFlag)
  1814. {
  1815. evenFlag = 0;
  1816. pointCount--;
  1817. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1818. bool ok = 1;
  1819. int coor = qRound(num.toDouble(&ok));
  1820. if (!ok) {
  1821. return false;
  1822. /* NOTREACHED */
  1823. }
  1824. point.setY(coor);
  1825. lastSpace = i + 1;
  1826. poly->poly << point;
  1827. }
  1828. else if (data.at(i) == '\n') {
  1829. if (pointCount || !poly) {
  1830. return false;
  1831. /* NOTREACHED */
  1832. }
  1833. lastSpace = i + 1;
  1834. for (int j = 0; j < list_label_->count(); j++) {
  1835. if (list_label_->item(j)->text().contains(label, Qt::CaseInsensitive)) {
  1836. poly->label_ID_ = j;
  1837. }
  1838. }
  1839. if (-1 == poly->label_ID_) {
  1840. int labelID = list_label_->count();
  1841. addLabel(labelID, 0, label);
  1842. poly->label_ID_ = labelID;
  1843. }
  1844. addPoly(poly);
  1845. label.clear();
  1846. }
  1847. i++;
  1848. }
  1849. //clearAll();
  1850. //enableTools();
  1851. return true;
  1852. }
  1853. void
  1854. ImageLabeler::loadImage()
  1855. {
  1856. if (askForUnsavedData()) {
  1857. return;
  1858. /* NOTREACHED */
  1859. }
  1860. QFileDialog fileDialog(0, tr("Load image"));
  1861. fileDialog.setFileMode(QFileDialog::ExistingFile);
  1862. QStringList filters;
  1863. filters << "Image files (*.jpeg *.jpg *.gif *.png *.bmp *.tiff)"
  1864. << "Any files (*)";
  1865. fileDialog.setNameFilters(filters);
  1866. QString filename;
  1867. if (fileDialog.exec()) {
  1868. filename = fileDialog.selectedFiles().last();
  1869. }
  1870. else {
  1871. //showWarning(tr("Could not open file dialog"));
  1872. return;
  1873. /* NOTREACHED */
  1874. }
  1875. if (filename.isEmpty()) {
  1876. return;
  1877. /* NOTREACHED */
  1878. }
  1879. QString dirPath = getPathFromFilename(filename);
  1880. QDir dir(dirPath);
  1881. QStringList filter;
  1882. filter << "*.dat";
  1883. QStringList fileList = dir.entryList(filter, QDir::Files);
  1884. QString labeled = alterFileName(filename, "_labeled");
  1885. labeled = removePath(labeled);
  1886. labeled.append(".dat");
  1887. if (fileList.contains(labeled, Qt::CaseInsensitive)) {
  1888. labeled = dir.absoluteFilePath(labeled);
  1889. loadInfo(labeled);
  1890. Image *newImage = new Image;
  1891. newImage->image_ = filename;
  1892. newImage->labeled_ = 1;
  1893. newImage->pas_ = 0;
  1894. addImage(newImage);
  1895. return;
  1896. /* NOTREACHED */
  1897. }
  1898. clearAllTool();
  1899. QString winTitle;
  1900. winTitle.append("ImageLabeler - ");
  1901. winTitle.append(current_image_);
  1902. setWindowTitle(winTitle);
  1903. if (!image_->load(filename)) {
  1904. return;
  1905. /* NOTREACHED */
  1906. }
  1907. image_holder_->resize(image_->size());
  1908. image_holder_->setPixmap(*image_);
  1909. current_image_ = filename;
  1910. image_ID_ = list_images_widget_->count() - 1;
  1911. list_images_widget_->setCurrentRow(image_ID_);
  1912. Image *newImage = new Image;
  1913. newImage->image_ = filename;
  1914. newImage->labeled_ = 0;
  1915. newImage->pas_ = 0;
  1916. addImage(newImage);
  1917. if (!list_images_widget_->count()) {
  1918. return;
  1919. /* NOTREACHED */
  1920. }
  1921. enableTools();
  1922. }
  1923. void
  1924. ImageLabeler::loadImages()
  1925. {
  1926. if (askForUnsavedData()) {
  1927. return;
  1928. /* NOTREACHED */
  1929. }
  1930. QFileDialog fileDialog(0, tr("Load images"));
  1931. fileDialog.setFileMode(QFileDialog::Directory);
  1932. QString dirName("");
  1933. if (fileDialog.exec()) {
  1934. /* TODO: make it possible to select multiple folders */
  1935. dirName = fileDialog.selectedFiles().last();
  1936. }
  1937. else {
  1938. //showWarning(tr("Could not open file dialog"));
  1939. return;
  1940. /* NOTREACHED */
  1941. }
  1942. clearAllTool();
  1943. QWidget *searchInProgress = new QWidget(0);
  1944. searchInProgress->setWindowTitle(tr("Loading images"));
  1945. QLabel *info = new QLabel(searchInProgress);
  1946. info->setText(
  1947. tr("Program is looking for all image files in your directory recursively."));
  1948. QPushButton *cancel = new QPushButton(tr("Cancel"), searchInProgress);
  1949. connect(cancel, SIGNAL(clicked()), this, SLOT(interruptSearch()));
  1950. QVBoxLayout *layout = new QVBoxLayout(searchInProgress);
  1951. searchInProgress->setLayout(layout);
  1952. layout->addWidget(info);
  1953. layout->addWidget(cancel);
  1954. searchInProgress->adjustSize();
  1955. searchInProgress->move(QApplication::desktop()->screen()->rect().center() - rect().center());
  1956. searchInProgress->show();
  1957. getImagesFromDir(QDir(dirName));
  1958. cancel->disconnect();
  1959. searchInProgress->hide();
  1960. delete info;
  1961. delete cancel;
  1962. delete layout;
  1963. delete searchInProgress;
  1964. if (interrupt_search_) {
  1965. interrupt_search_ = 0;
  1966. return;
  1967. /* NOTREACHED */
  1968. }
  1969. if (!list_images_widget_->count()) {
  1970. showWarning(tr("The folder you selected contains no images"));
  1971. return;
  1972. /* NOTREACHED */
  1973. }
  1974. bool ret = 0;
  1975. if (list_images_->at(0).labeled_) {
  1976. QString labeled =
  1977. alterFileName(list_images_->at(0).image_, "_labeled");
  1978. labeled.append(".dat");
  1979. ret = loadInfo(labeled);
  1980. }
  1981. else
  1982. ret = image_->load(list_images_->at(0).image_);
  1983. if (!ret) {
  1984. return;
  1985. /* NOTREACHED */
  1986. }
  1987. current_image_ = list_images_->at(0).image_;
  1988. image_ID_ = 0;
  1989. list_images_widget_->setCurrentRow(image_ID_);
  1990. QString winTitle;
  1991. winTitle.append("ImageLabeler - ");
  1992. winTitle.append(current_image_);
  1993. setWindowTitle(winTitle);
  1994. image_holder_->resize(image_->size());
  1995. image_holder_->setPixmap(*image_);
  1996. enableTools();
  1997. }
  1998. void
  1999. ImageLabeler::loadLegendFromFile()
  2000. {
  2001. QFileDialog fileDialog(0, tr("Load legend"));
  2002. fileDialog.setFileMode(QFileDialog::AnyFile);
  2003. QString filename;
  2004. if (fileDialog.exec()) {
  2005. filename = fileDialog.selectedFiles().last();
  2006. }
  2007. else {
  2008. //showWarning(tr("Could not open file dialog"));
  2009. return;
  2010. /* NOTREACHED */
  2011. }
  2012. QDomDocument doc("Image Labeler");
  2013. QFile file(filename);
  2014. if (!file.open(QIODevice::ReadOnly)) {
  2015. showWarning(tr("Can not open such file"));
  2016. return;
  2017. /* NOTREACHED */
  2018. }
  2019. QString errMsg;
  2020. if (!doc.setContent(&file, &errMsg)) {
  2021. showWarning(errMsg);
  2022. file.close();
  2023. return;
  2024. /* NOTREACHED */
  2025. }
  2026. file.close();
  2027. list_label_->clear();
  2028. /* getting legend */
  2029. QDomElement elements = doc.documentElement();
  2030. QDomNode rootNode = elements.firstChild();
  2031. QString string;
  2032. while(!rootNode.isNull()) {
  2033. QDomElement element = rootNode.toElement();
  2034. if(!element.isNull()) {
  2035. if (element.tagName() == "legend") {
  2036. loadLegendFromNode(&element);
  2037. }
  2038. }
  2039. rootNode = rootNode.nextSibling();
  2040. }
  2041. }
  2042. void
  2043. ImageLabeler::loadLegendFromNode(QDomElement *anElement)
  2044. {
  2045. if (!anElement) {
  2046. return;
  2047. /* NOTREACHED */
  2048. }
  2049. QDomNode subNode = anElement->firstChild();
  2050. QDomElement subElement;
  2051. QString string;
  2052. int id = -1;
  2053. bool isMain;
  2054. uint color = 0xff000000;
  2055. while(!subNode.isNull()) {
  2056. subElement = subNode.toElement();
  2057. if (!subElement.isNull() && !subElement.text().isEmpty()) {
  2058. string = subElement.attribute("id");
  2059. bool ok = 0;
  2060. id = string.toInt(&ok, 10);
  2061. if (!ok) {
  2062. qDebug() <<
  2063. "loadLegendFromNode: "
  2064. "label id format is corrupted";
  2065. subNode = subNode.nextSibling();
  2066. continue;
  2067. }
  2068. string = subElement.attribute("isMain");
  2069. isMain = string.toInt(&ok, 2);
  2070. if (!ok) {
  2071. qDebug() <<
  2072. "loadLegendFromNode: "
  2073. "label isMain flag format is corrupted";
  2074. subNode = subNode.nextSibling();
  2075. continue;
  2076. }
  2077. string = subElement.attribute("color");
  2078. color = string.toUInt(&ok, 16);
  2079. if (!ok) {
  2080. qDebug() <<
  2081. "loadLegendFromNode: "
  2082. "label color format is corrupted";
  2083. subNode = subNode.nextSibling();
  2084. continue;
  2085. }
  2086. string = subElement.text();
  2087. addLabel(id, isMain, string);
  2088. setLabelColor(id, color);
  2089. }
  2090. subNode = subNode.nextSibling();
  2091. }
  2092. }
  2093. void
  2094. ImageLabeler::legendToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2095. {
  2096. QDomElement legend = aDoc->createElement(tr("legend"));
  2097. aRoot->appendChild(legend);
  2098. /* storing all labels made by user */
  2099. int labelCount = list_label_->count();
  2100. for (int i = 0; i < labelCount; i++) {
  2101. QDomElement label = aDoc->createElement(tr("label"));
  2102. label.setAttribute("color", QString("%1").arg(list_label_colors_.at(i), 0, 16));
  2103. label.setAttribute("id", i);
  2104. QString priority;
  2105. if (main_label_ == i)
  2106. priority.append("1");
  2107. else
  2108. priority.append("0");
  2109. label.setAttribute("isMain", priority);
  2110. QString labelText = list_label_->item(i)->text();
  2111. /* removing the number prefix of label */
  2112. if (-1 != labelText.indexOf(QString("%1: ").arg(i))) {
  2113. labelText = labelText.mid(3, labelText.size() - 3);
  2114. }
  2115. QDomText labelName = aDoc->createTextNode(labelText);
  2116. label.appendChild(labelName);
  2117. legend.appendChild(label);
  2118. }
  2119. /* in case we have no labels */
  2120. if (0 == labelCount) {
  2121. QDomElement label = aDoc->createElement(tr("label"));
  2122. label.setAttribute(tr("id"), -1);
  2123. legend.appendChild(label);
  2124. }
  2125. }
  2126. void
  2127. ImageLabeler::objectsToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2128. {
  2129. QDomElement objects = aDoc->createElement(tr("objects"));
  2130. aRoot->appendChild(objects);
  2131. /* rects first */
  2132. for (int i = 0; i < list_bounding_box_.size(); i++) {
  2133. QDomElement rectData = aDoc->createElement(tr("bbox"));
  2134. rectData.setAttribute("id", list_bounding_box_.at(i)->label_ID_);
  2135. QRect rect = list_bounding_box_.at(i)->rect.normalized();
  2136. QString rectDataString =
  2137. QString("%1;%2;%3;%4;").
  2138. arg(rect.x()).
  2139. arg(rect.y()).
  2140. arg(rect.width()).
  2141. arg(rect.height());
  2142. QDomText rectDataText = aDoc->createTextNode(rectDataString);
  2143. rectData.appendChild(rectDataText);
  2144. objects.appendChild(rectData);
  2145. }
  2146. /* polys next */
  2147. for (int i = 0; i < list_polygon_.size(); i++) {
  2148. QDomElement polyData = aDoc->createElement(tr("poly"));
  2149. polyData.setAttribute("id", list_polygon_.at(i)->label_ID_);
  2150. QPolygon poly = list_polygon_.at(i)->poly;
  2151. QString polyDataString;
  2152. for (int j = 0; j < poly.count(); j++)
  2153. polyDataString.append(
  2154. QString("%1;%2;").
  2155. arg(poly.point(j).x()).
  2156. arg(poly.point(j).y())
  2157. );
  2158. QDomText polyDataText = aDoc->createTextNode(polyDataString);
  2159. polyData.appendChild(polyDataText);
  2160. objects.appendChild(polyData);
  2161. }
  2162. }
  2163. void
  2164. ImageLabeler::generateColors()
  2165. {
  2166. int labelCount = list_label_->count();
  2167. if (!labelCount) {
  2168. showWarning(tr("you have not added any labels yet"));
  2169. return;
  2170. /* NOTREACHED */
  2171. }
  2172. int coeff = (0xff / labelCount) * 3;
  2173. //QList< uint > colors;
  2174. list_label_colors_.clear();
  2175. list_label_colors_.append(0);
  2176. uchar red = 0xff;
  2177. uchar green = 0xff;
  2178. uchar blue = 0xff;
  2179. uchar iterationColor = coeff;
  2180. uint color = 0xffffffff;
  2181. int j = 1;
  2182. for (int i = 1; i < labelCount; i++) {
  2183. if (6 == j) {
  2184. iterationColor += coeff;
  2185. j = 1;
  2186. }
  2187. if (5 == j) {
  2188. red = 0xff - iterationColor;
  2189. green = 0xff - iterationColor;
  2190. blue = 0xff;
  2191. }
  2192. else if (4 == j) {
  2193. red = 0xff - iterationColor;
  2194. green = 0xff;
  2195. blue = 0xff - iterationColor;
  2196. }
  2197. else if (3 == j) {
  2198. red = 0xff - iterationColor;
  2199. green = 0xff;
  2200. blue = 0xff;
  2201. }
  2202. else if (2 == j) {
  2203. red = 0xff;
  2204. green = 0xff - iterationColor;
  2205. blue = 0xff;
  2206. }
  2207. else if (1 == j){
  2208. red = 0xff;
  2209. green = 0xff;
  2210. blue = 0xff - iterationColor;
  2211. }
  2212. j++;
  2213. color = red + (green * 0x100) + (blue * 0x10000) + 0xff000000;
  2214. int itemNo = list_label_colors_.count();
  2215. QPixmap iconPix = QPixmap(20, 20);
  2216. iconPix.fill(color);
  2217. QIcon icon(iconPix);
  2218. list_label_->item(itemNo)->setIcon(icon);
  2219. list_label_colors_.append(color);
  2220. }
  2221. image_holder_->update();
  2222. }
  2223. void
  2224. ImageLabeler::addBBoxFromData(
  2225. QString *aBBoxData,
  2226. int *ID
  2227. )
  2228. {
  2229. BoundingBox *bbox = new BoundingBox;
  2230. *bbox = BBoxFromData(aBBoxData);
  2231. if (!bbox->rect.isValid() || !ID) {
  2232. return;
  2233. /* NOTREACHED */
  2234. }
  2235. bbox->label_ID_ = *ID;
  2236. list_bounding_box_.append(bbox);
  2237. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  2238. }
  2239. BoundingBox
  2240. ImageLabeler::BBoxFromData(
  2241. QString *aBBoxData
  2242. )
  2243. {
  2244. BoundingBox bbox;
  2245. QString buffer;
  2246. bbox.rect.setRect(-1, -1, -1, -1);
  2247. int startPos = 0;
  2248. bool ok = 1;
  2249. for (int i = 0; i < aBBoxData->size(); i++) {
  2250. if (';' != aBBoxData->at(i))
  2251. continue;
  2252. buffer = aBBoxData->mid(startPos, i - startPos);
  2253. int bboxData = buffer.toInt(&ok, 10);
  2254. if (!ok) {
  2255. qDebug() <<
  2256. "BBoxFromData: "
  2257. "bbox format is corrupted";
  2258. break;
  2259. }
  2260. if (-1 == bbox.rect.x()) {
  2261. bbox.rect.setX(bboxData);
  2262. bbox.rect.setWidth(-1);
  2263. }
  2264. else if (-1 == bbox.rect.y()) {
  2265. bbox.rect.setY(bboxData);
  2266. bbox.rect.setHeight(-1);
  2267. }
  2268. else if (-1 == bbox.rect.width()) {
  2269. bbox.rect.setWidth(bboxData);
  2270. }
  2271. else if (-1 == bbox.rect.height()) {
  2272. bbox.rect.setHeight(bboxData);
  2273. }
  2274. startPos = i + 1;
  2275. }
  2276. if (!bbox.rect.isValid()) {
  2277. qDebug() <<
  2278. "BBoxFromData: "
  2279. "bbox format is corrupted";
  2280. bbox.rect.setRect(-1, -1, -1, -1);
  2281. }
  2282. else if (!ok) {
  2283. bbox.rect.setRect(-1, -1, -1, -1);
  2284. }
  2285. return bbox;
  2286. }
  2287. BoundingBox
  2288. ImageLabeler::BBoxFromString(
  2289. QString *aString,
  2290. int *oldID
  2291. )
  2292. {
  2293. BoundingBox bbox;
  2294. bbox.label_ID_ = -1;
  2295. *oldID = -1;
  2296. if (!aString) {
  2297. return bbox;
  2298. /* NOTREACHED */
  2299. }
  2300. if (-1 == aString->indexOf("BBox")) {
  2301. return bbox;
  2302. /* NOTREACHED */
  2303. }
  2304. /* getting bbox id in the list(it cannot be changed) */
  2305. bool ok = 0;
  2306. int bboxID = getNumFromString(aString, "BBox #", ";", &ok);
  2307. if (!ok || bboxID <= -1) {
  2308. qDebug() <<
  2309. "BBoxFromString: bboxID is corrupted";
  2310. return bbox;
  2311. /* NOTREACHED */
  2312. }
  2313. /* getting new label id */
  2314. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  2315. if (!ok || labelID <= -1) {
  2316. showWarning(
  2317. tr("new LabelID is wrong, area can not be changed")
  2318. );
  2319. return bbox;
  2320. /* NOTREACHED */
  2321. }
  2322. /* getting new points */
  2323. int pointsPos = aString->indexOf("data:") + 5;
  2324. int pointsLen = aString->size() - pointsPos;
  2325. if (pointsLen <= 0) {
  2326. showWarning(
  2327. tr("new data is wrong, area can not be changed")
  2328. );
  2329. return bbox;
  2330. /* NOTREACHED */
  2331. }
  2332. QString pointsData = aString->mid(pointsPos, pointsLen);
  2333. bbox = BBoxFromData(&pointsData);
  2334. bbox.label_ID_ = labelID;
  2335. *oldID = bboxID;
  2336. return bbox;
  2337. }
  2338. void
  2339. ImageLabeler::addPoly(Polygon *poly)
  2340. {
  2341. if (poly->poly.isEmpty() || poly->label_ID_ < 0) {
  2342. return;
  2343. /* NOTREACHED */
  2344. }
  2345. list_polygon_.append(poly);
  2346. addPolyArea(list_polygon_.count() - 1, *poly);
  2347. }
  2348. void
  2349. ImageLabeler::addBBox(BoundingBox *bbox)
  2350. {
  2351. if (bbox->rect.isEmpty() || bbox->label_ID_ < 0) {
  2352. return;
  2353. /* NOTREACHED */
  2354. }
  2355. list_bounding_box_.append(bbox);
  2356. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  2357. }
  2358. void
  2359. ImageLabeler::addPolyFromData(
  2360. QString *aPolyData,
  2361. int *labelID
  2362. )
  2363. {
  2364. Polygon *poly = new Polygon;
  2365. *poly = polyFromData(aPolyData);
  2366. if (poly->poly.isEmpty() || !labelID) {
  2367. return;
  2368. /* NOTREACHED */
  2369. }
  2370. poly->label_ID_ = *labelID;
  2371. list_polygon_.append(poly);
  2372. addPolyArea(list_polygon_.count() - 1, *poly);
  2373. }
  2374. Polygon
  2375. ImageLabeler::polyFromData(
  2376. QString *aPolyData
  2377. )
  2378. {
  2379. Polygon poly;
  2380. poly.label_ID_ = -1;
  2381. QPoint point;
  2382. QString buffer;
  2383. int startPos = 0;
  2384. bool ok = 1;
  2385. bool evenFlag = 0;
  2386. for (int i = 0; i < aPolyData->size(); i++) {
  2387. if (';' != aPolyData->at(i))
  2388. continue;
  2389. buffer = aPolyData->mid(startPos, i - startPos);
  2390. int polyCoor = buffer.toInt(&ok, 10);
  2391. if (!ok) {
  2392. qDebug() <<
  2393. "polyFromData: "
  2394. "poly format is corrupted";
  2395. break;
  2396. }
  2397. if (!evenFlag) {
  2398. point.setX(polyCoor);
  2399. evenFlag = 1;
  2400. }
  2401. else {
  2402. point.setY(polyCoor);
  2403. poly.poly.append(point);
  2404. evenFlag = 0;
  2405. }
  2406. startPos = i + 1;
  2407. }
  2408. if (evenFlag) {
  2409. qDebug() <<
  2410. "polyFromData: "
  2411. "poly format is corrupted";
  2412. poly.poly.clear();
  2413. }
  2414. else if (!ok) {
  2415. poly.poly.clear();
  2416. }
  2417. return poly;
  2418. }
  2419. Polygon
  2420. ImageLabeler::polyFromString(
  2421. QString *aString,
  2422. int *oldID
  2423. )
  2424. {
  2425. Polygon poly;
  2426. poly.label_ID_ = -1;
  2427. *oldID = -1;
  2428. if (!aString) {
  2429. return poly;
  2430. /* NOTREACHED */
  2431. }
  2432. if (-1 == aString->indexOf("Poly")) {
  2433. return poly;
  2434. /* NOTREACHED */
  2435. }
  2436. /* getting poly id in the list(it cannot be changed) */
  2437. bool ok = 0;
  2438. int polyID = getNumFromString(aString, "Poly #", ";", &ok);
  2439. if (!ok || polyID <= -1) {
  2440. qDebug() <<
  2441. "polyFromString: poly ID is corrupted";
  2442. return poly;
  2443. /* NOTREACHED */
  2444. }
  2445. /* getting new label id */
  2446. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  2447. if (!ok || labelID <= -1) {
  2448. showWarning(
  2449. tr("new LabelID is wrong, area can not be changed")
  2450. );
  2451. return poly;
  2452. /* NOTREACHED */
  2453. }
  2454. /* getting new points */
  2455. int pointsPos = aString->indexOf("points:") + 7;
  2456. int pointsLen = aString->size() - pointsPos;
  2457. if (pointsLen <= 0) {
  2458. showWarning(
  2459. tr("new points data is wrong, area can not be changed")
  2460. );
  2461. return poly;
  2462. /* NOTREACHED */
  2463. }
  2464. QString pointsData = aString->mid(pointsPos, pointsLen);
  2465. poly = polyFromData(&pointsData);
  2466. poly.label_ID_ = labelID;
  2467. *oldID = polyID;
  2468. return poly;
  2469. }
  2470. void
  2471. ImageLabeler::showWarning(
  2472. const QString text
  2473. )
  2474. {
  2475. if (text.isEmpty()) {
  2476. return;
  2477. /* NOTREACHED */
  2478. }
  2479. QMessageBox msgBox;
  2480. msgBox.setText(text);
  2481. msgBox.setIcon(QMessageBox::Warning);
  2482. msgBox.exec();
  2483. }
  2484. bool
  2485. ImageLabeler::askForUnsavedData()
  2486. {
  2487. if ((!list_bounding_box_.isEmpty() ||
  2488. !list_polygon_.isEmpty()) &&
  2489. unsaved_data_)
  2490. {
  2491. QMessageBox msgBox;
  2492. msgBox.setText(tr("There is some unsaved data"));
  2493. msgBox.setInformativeText(tr("Do you want to save your progress?"));
  2494. msgBox.setStandardButtons(
  2495. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  2496. msgBox.setDefaultButton(QMessageBox::Save);
  2497. msgBox.setIcon(QMessageBox::Question);
  2498. int ret = msgBox.exec();
  2499. if (QMessageBox::Save == ret)
  2500. saveAllInfo();
  2501. else if (QMessageBox::Cancel == ret)
  2502. return true;
  2503. }
  2504. return false;
  2505. }
  2506. void
  2507. ImageLabeler::setBoundingBoxTool(bool aButtonPressed)
  2508. {
  2509. if (aButtonPressed)
  2510. image_holder_->setTool(ImageHolder::BoundingBoxTool);
  2511. else {
  2512. image_holder_->setTool(ImageHolder::NoTool);
  2513. image_holder_->clearLast();
  2514. }
  2515. }
  2516. void
  2517. ImageLabeler::setPolygonTool(bool aButtonPressed)
  2518. {
  2519. if (aButtonPressed) {
  2520. image_holder_->setTool(ImageHolder::PolygonTool);
  2521. image_holder_->setFocus();
  2522. }
  2523. else {
  2524. image_holder_->setTool(ImageHolder::NoTool);
  2525. image_holder_->clearLast();
  2526. }
  2527. }
  2528. void
  2529. ImageLabeler::onSelectionStarted()
  2530. {
  2531. button_confirm_selection_->setEnabled(true);
  2532. }
  2533. void
  2534. ImageLabeler::onImageScaled()
  2535. {
  2536. }
  2537. void
  2538. ImageLabeler::confirmSelection()
  2539. {
  2540. if (!list_label_->count()) {
  2541. showWarning(tr("You haven't added any label"));
  2542. return;
  2543. /* NOTREACHED */
  2544. }
  2545. image_holder_->confirmSelection();
  2546. if (label_ID_ < 0)
  2547. label_ID_ = 0;
  2548. ImageHolder::Tool tool = image_holder_->tool();
  2549. switch (tool) {
  2550. case ImageHolder::BoundingBoxTool:
  2551. list_bounding_box_.last()->label_ID_ = label_ID_;
  2552. addBBoxArea(
  2553. list_bounding_box_.count() - 1,
  2554. *(list_bounding_box_.last())
  2555. );
  2556. break;
  2557. case ImageHolder::PolygonTool:
  2558. list_polygon_.last()->label_ID_ = label_ID_;
  2559. addPolyArea(
  2560. list_polygon_.count() - 1,
  2561. *(list_polygon_.last())
  2562. );
  2563. break;
  2564. default:
  2565. break;
  2566. }
  2567. button_confirm_selection_->setEnabled(false);
  2568. unsaved_data_ = 1;
  2569. }
  2570. void
  2571. ImageLabeler::clearAll()
  2572. {
  2573. clearLabelList();
  2574. list_areas_->clear();
  2575. list_bounding_box_.clear();
  2576. list_polygon_.clear();
  2577. list_images_->clear();
  2578. list_images_widget_->clear();
  2579. main_label_ = -1;
  2580. image_holder_->clearAll();
  2581. segmented_image_.clear();
  2582. action_view_normal_->setEnabled(false);
  2583. action_view_segmented_->setEnabled(false);
  2584. }
  2585. void
  2586. ImageLabeler::clearAllTool()
  2587. {
  2588. list_areas_->clear();
  2589. list_bounding_box_.clear();
  2590. list_polygon_.clear();
  2591. main_label_ = -1;
  2592. image_holder_->clearAll();
  2593. }
  2594. void ImageLabeler::clearLabelList()
  2595. {
  2596. list_label_->clear();
  2597. addLabel(0, false, "BACKGROUND");
  2598. list_label_->item(0)->setFlags(
  2599. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  2600. Qt::ItemIsEnabled
  2601. );
  2602. }
  2603. void ImageLabeler::clearLabelColorList()
  2604. {
  2605. list_label_colors_.clear();
  2606. list_label_colors_.append(0x0);
  2607. }
  2608. void
  2609. ImageLabeler::enableTools()
  2610. {
  2611. action_save_labels_->setEnabled(true);
  2612. action_save_segmented_->setEnabled(true);
  2613. action_save_legend_->setEnabled(true);
  2614. action_bound_box_tool_->setEnabled(true);
  2615. action_polygon_tool_->setEnabled(true);
  2616. action_add_description_->setEnabled(true);
  2617. action_undo_->setEnabled(true);
  2618. action_redo_->setEnabled(true);
  2619. button_bound_box_tool_->setEnabled(true);
  2620. button_polygon_tool_->setEnabled(true);
  2621. button_add_label_->setEnabled(true);
  2622. button_remove_label_->setEnabled(true);
  2623. button_prev_image_->setEnabled(true);
  2624. button_next_image_->setEnabled(true);
  2625. button_clear_selection_tool_->setEnabled(true);
  2626. button_generate_colors_->setEnabled(true);
  2627. button_delete_all_labels_->setEnabled(true);
  2628. }
  2629. void
  2630. ImageLabeler::disableTools()
  2631. {
  2632. action_save_labels_->setEnabled(false);
  2633. action_save_segmented_->setEnabled(false);
  2634. action_save_legend_->setEnabled(false);
  2635. action_bound_box_tool_->setEnabled(false);
  2636. action_polygon_tool_->setEnabled(false);
  2637. action_add_description_->setEnabled(false);
  2638. action_undo_->setEnabled(false);
  2639. action_redo_->setEnabled(false);
  2640. button_bound_box_tool_->setEnabled(false);
  2641. button_polygon_tool_->setEnabled(false);
  2642. button_add_label_->setEnabled(false);
  2643. button_remove_label_->setEnabled(false);
  2644. button_prev_image_->setEnabled(false);
  2645. button_next_image_->setEnabled(false);
  2646. button_clear_selection_tool_->setEnabled(false);
  2647. button_generate_colors_->setEnabled(false);
  2648. button_delete_all_labels_->setEnabled(false);
  2649. }
  2650. void
  2651. ImageLabeler::areaListPopupMenu(const QPoint &aPos)
  2652. {
  2653. QPoint globalPos = list_areas_->mapToGlobal(aPos);
  2654. QModelIndex index = list_areas_->indexAt(aPos);
  2655. if (-1 == index.row()) {
  2656. return;
  2657. /* NOTREACHED */
  2658. }
  2659. list_areas_->item(index.row())->setSelected(true);
  2660. popup_area_list_->exec(globalPos);
  2661. }
  2662. void
  2663. ImageLabeler::labelListPopupMenu(const QPoint &aPos)
  2664. {
  2665. QPoint globalPos = list_label_->mapToGlobal(aPos);
  2666. QModelIndex index = list_label_->indexAt(aPos);
  2667. if (-1 == index.row() || !index.row()) {
  2668. return;
  2669. /* NOTREACHED */
  2670. }
  2671. list_label_->item(index.row())->setSelected(true);
  2672. popup_label_list_->exec(globalPos);
  2673. }
  2674. void
  2675. ImageLabeler::setDescription(QString aDescription)
  2676. {
  2677. image_description_ = aDescription;
  2678. setWindowTitle(image_description_);
  2679. }
  2680. void
  2681. ImageLabeler::setPureData()
  2682. {
  2683. /* initializing array */
  2684. if (pure_data_) {
  2685. delete[] *pure_data_;
  2686. delete pure_data_;
  2687. }
  2688. QSize imageSize = image_->size();
  2689. pure_data_ = new int *[imageSize.height()];
  2690. if (!pure_data_) {
  2691. return;
  2692. /* NOTREACHED */
  2693. }
  2694. for (int i = 0; i < imageSize.height(); i++) {
  2695. pure_data_[i] = new int[imageSize.width()];
  2696. if (!pure_data_[i]) {
  2697. return;
  2698. /* NOTREACHED */
  2699. }
  2700. }
  2701. int bboxCnt = list_bounding_box_.count();
  2702. int polyCnt = list_polygon_.count();
  2703. for (int i = 0; i < imageSize.height(); i++)
  2704. for (int j = 0; j < imageSize.width(); j++) {
  2705. pure_data_[i][j] = 0;
  2706. /* bboxes first */
  2707. for (int cnt = 0; cnt < bboxCnt; cnt++) {
  2708. BoundingBox *bbox = list_bounding_box_.at(cnt);
  2709. if (bbox->rect.contains(j, i)) {
  2710. pure_data_[i][j] = bbox->label_ID_;
  2711. }
  2712. }
  2713. /* polys next */
  2714. for (int cnt = 0; cnt < polyCnt; cnt++) {
  2715. Polygon *poly = list_polygon_.at(cnt);
  2716. if (poly->poly.containsPoint(QPoint(j, i), Qt::OddEvenFill)) {
  2717. pure_data_[i][j] = poly->label_ID_;
  2718. }
  2719. }
  2720. }
  2721. }
  2722. void
  2723. ImageLabeler::setLabelColor()
  2724. {
  2725. if (list_label_colors_.count() < list_label_->count()) {
  2726. generateColors();
  2727. }
  2728. QListWidgetItem *current = list_label_->currentItem();
  2729. int labelID = list_label_->row(current);
  2730. QColor defaultColor;
  2731. defaultColor.setRgb(list_label_colors_.at(labelID));
  2732. QColor newColor = QColorDialog::getColor(
  2733. defaultColor,
  2734. list_label_
  2735. );
  2736. if (!newColor.isValid()) {
  2737. return;
  2738. /* NOTREACHED */
  2739. }
  2740. list_label_colors_.takeAt(labelID);
  2741. list_label_colors_.insert(labelID, newColor.rgb());
  2742. QPixmap iconPix = QPixmap(20, 20);
  2743. iconPix.fill(newColor);
  2744. QIcon icon(iconPix);
  2745. current->setIcon(icon);
  2746. image_holder_->update();
  2747. }
  2748. void
  2749. ImageLabeler::setLabelColor(int anID, QColor aColor)
  2750. {
  2751. if (anID < 0) {
  2752. return;
  2753. /* NOTREACHED */
  2754. }
  2755. if (list_label_colors_.count() < list_label_->count()) {
  2756. generateColors();
  2757. }
  2758. QListWidgetItem *item = list_label_->item(anID);
  2759. list_label_colors_.takeAt(anID);
  2760. list_label_colors_.insert(anID, aColor.rgb());
  2761. QPixmap iconPix = QPixmap(20, 20);
  2762. iconPix.fill(aColor);
  2763. QIcon icon(iconPix);
  2764. item->setIcon(icon);
  2765. image_holder_->update();
  2766. }
  2767. void
  2768. ImageLabeler::onOptionsSet()
  2769. {
  2770. auto_color_generation_ = options_form_.autoColorGeneration();
  2771. }
  2772. void
  2773. ImageLabeler::viewNormal()
  2774. {
  2775. if (current_image_.isEmpty()) {
  2776. return;
  2777. /* NOTREACHED */
  2778. }
  2779. image_->load(current_image_);
  2780. image_holder_->setPixmap(*image_);
  2781. action_view_segmented_->setEnabled(true);
  2782. action_view_normal_->setEnabled(false);
  2783. }
  2784. void
  2785. ImageLabeler::viewSegmented()
  2786. {
  2787. if (segmented_image_.isEmpty()) {
  2788. return;
  2789. /* NOTREACHED */
  2790. }
  2791. image_->load(segmented_image_);
  2792. image_holder_->setPixmap(*image_);
  2793. action_view_segmented_->setEnabled(false);
  2794. action_view_normal_->setEnabled(true);
  2795. }
  2796. void
  2797. ImageLabeler::interruptSearch()
  2798. {
  2799. interrupt_search_ = 1;
  2800. }
  2801. void
  2802. ImageLabeler::selectImage(QListWidgetItem *anItem)
  2803. {
  2804. if (!anItem || list_images_widget_->row(anItem) < 0 ||
  2805. list_images_->isEmpty())
  2806. {
  2807. return;
  2808. /* NOTREACHED */
  2809. }
  2810. clearAllTool();
  2811. clearLabelList();
  2812. clearLabelColorList();
  2813. image_ID_ = list_images_widget_->row(anItem);
  2814. selectImage(image_ID_);
  2815. // if (list_images_->at(image_ID_).labeled_ &&
  2816. // !list_images_->at(image_ID_).pas_)
  2817. // {
  2818. // list_label_->clear();
  2819. // QString labeled =
  2820. // alterFileName(list_images_->at(image_ID_).image_, "_labeled");
  2821. // labeled.append(".dat");
  2822. // loadInfo(labeled);
  2823. // }
  2824. // else if (list_images_->at(image_ID_).labeled_ &&
  2825. // list_images_->at(image_ID_).pas_)
  2826. // {
  2827. // /* TODO: do the pascal file selecting */
  2828. // showWarning("this function doesn't work at the moment, sorry.");
  2829. // }
  2830. // else {
  2831. // current_image_ = list_images_->at(image_ID_).image_;
  2832. // image_->load(current_image_);
  2833. // image_holder_->setPixmap(*image_);
  2834. // image_holder_->resize(image_->size());
  2835. // }
  2836. }
  2837. bool
  2838. ImageLabeler::selectImage(int anImageID)
  2839. {
  2840. if (anImageID < 0 || list_images_->isEmpty() ||
  2841. list_images_->count() <= anImageID)
  2842. {
  2843. return false;
  2844. /* NOTREACHED */
  2845. }
  2846. if (list_images_->at(anImageID).labeled_ &&
  2847. !list_images_->at(anImageID).pas_)
  2848. {
  2849. list_label_->clear();
  2850. QString labeled =
  2851. alterFileName(list_images_->at(anImageID).image_, "_labeled");
  2852. labeled.append(".dat");
  2853. loadInfo(labeled);
  2854. }
  2855. else if (list_images_->at(anImageID).labeled_ &&
  2856. list_images_->at(anImageID).pas_)
  2857. {
  2858. /* TODO: do the pascal file selecting */
  2859. showWarning("this function doesn't work at the moment, sorry.");
  2860. }
  2861. else {
  2862. current_image_ = list_images_->at(anImageID).image_;
  2863. image_->load(current_image_);
  2864. image_holder_->setPixmap(*image_);
  2865. image_holder_->resize(image_->size());
  2866. }
  2867. return true;
  2868. }
  2869. void
  2870. ImageLabeler::imageListPopupMenu(const QPoint &aPos)
  2871. {
  2872. QPoint globalPos = list_images_widget_->mapToGlobal(aPos);
  2873. QModelIndex index = list_images_widget_->indexAt(aPos);
  2874. if (-1 == index.row()) {
  2875. return;
  2876. /* NOTREACHED */
  2877. }
  2878. list_images_widget_->item(index.row())->setSelected(true);
  2879. popup_images_list_->exec(globalPos);
  2880. }
  2881. void
  2882. ImageLabeler::removeImage()
  2883. {
  2884. int num = list_images_widget_->currentRow();
  2885. list_images_widget_->takeItem(num);
  2886. list_images_->takeAt(num);
  2887. for (int i = num ; i < list_images_widget_->count(); i++) {
  2888. QString newStr = getFilenameFromPath(&(list_images_->at(i).image_));
  2889. newStr.prepend(QString("%1: ").arg(i));
  2890. list_images_widget_->item(i)->setText(newStr);
  2891. }
  2892. }
  2893. void
  2894. ImageLabeler::resizeEvent (QResizeEvent *anEvent)
  2895. {
  2896. QWidget::resizeEvent(anEvent);
  2897. }
  2898. void
  2899. ImageLabeler::mousePressEvent(QMouseEvent *anEvent)
  2900. {
  2901. QWidget::mousePressEvent(anEvent);
  2902. }
  2903. void
  2904. ImageLabeler::keyPressEvent(QKeyEvent *anEvent)
  2905. {
  2906. keyboard_modifier_ = anEvent->modifiers();
  2907. if (Qt::Key_Left == anEvent->key() &&
  2908. Qt::ControlModifier == keyboard_modifier_) {
  2909. prevImage();
  2910. }
  2911. if (Qt::Key_Right == anEvent->key() &&
  2912. Qt::ControlModifier == keyboard_modifier_) {
  2913. nextImage();
  2914. }
  2915. if ((Qt::Key_Enter == anEvent->key() ||
  2916. Qt::Key_Return == anEvent->key()) &&
  2917. // Qt::NoModifier == anEvent->modifiers() &&
  2918. ImageHolder::NewSelection == image_holder_->state()) {
  2919. confirmSelection();
  2920. }
  2921. if ((Qt::Key_Enter == anEvent->key() ||
  2922. Qt::Key_Return == anEvent->key()) &&
  2923. // Qt::NoModifier == anEvent->modifiers() &&
  2924. -1 != image_holder_->focusedSelection()) {
  2925. image_holder_->clearFocusOnArea();
  2926. image_holder_->clearEdition();
  2927. image_holder_->update();
  2928. }
  2929. if (Qt::Key_Escape == anEvent->key()) {
  2930. image_holder_->clearLast();
  2931. image_holder_->clearFocusOnArea();
  2932. image_holder_->clearEdition();
  2933. image_holder_->update();
  2934. }
  2935. if (Qt::Key_Z == anEvent->key() &&
  2936. Qt::ControlModifier == anEvent->modifiers()) {
  2937. image_holder_->undo();
  2938. }
  2939. if (Qt::Key_Y == anEvent->key() &&
  2940. Qt::ControlModifier == anEvent->modifiers()) {
  2941. image_holder_->redo();
  2942. }
  2943. QWidget::keyPressEvent(anEvent);
  2944. }
  2945. void
  2946. ImageLabeler::keyReleaseEvent(QKeyEvent *anEvent)
  2947. {
  2948. keyboard_modifier_ = anEvent->modifiers();
  2949. }
  2950. void
  2951. ImageLabeler::wheelEvent(QWheelEvent *anEvent)
  2952. {
  2953. /* zoomin */
  2954. if (0 < anEvent->delta() &&
  2955. Qt::ControlModifier == keyboard_modifier_) {
  2956. image_holder_->scaleImage(ZoomIn, 1.1);
  2957. }
  2958. /* zoomout */
  2959. else if (anEvent->delta() < 0 &&
  2960. Qt::ControlModifier == keyboard_modifier_) {
  2961. image_holder_->scaleImage(ZoomOut, 1.1);
  2962. }
  2963. }
  2964. /*
  2965. *
  2966. */