ImageLabeler.cpp 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456
  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. &image_description_form_,
  418. SLOT(show())
  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. qDebug() << "file is: " << file;
  1080. if (ignoredFiles.contains(file) ||
  1081. file.contains("_segmented", Qt::CaseInsensitive) ||
  1082. file.contains(".dat", Qt::CaseInsensitive)) {
  1083. continue;
  1084. }
  1085. Image newImage;
  1086. newImage.image_ = dir.absoluteFilePath(file);
  1087. /* TODO: think about loading pascal files */
  1088. newImage.pas_ = 0;
  1089. QString labeled = alterFileName(file, "_labeled");
  1090. labeled = removePath(labeled);
  1091. labeled.append(".dat");
  1092. if (listImages.contains(labeled, Qt::CaseInsensitive))
  1093. newImage.labeled_ = 1;
  1094. else
  1095. {
  1096. newImage.labeled_ = 0;
  1097. }
  1098. addImage(&newImage);
  1099. }
  1100. QStringList listDir = dir.entryList(QDir::Dirs);
  1101. foreach (QString subdir, listDir) {
  1102. if ("." == subdir || ".." == subdir)
  1103. continue;
  1104. getImagesFromDir(QDir(dir.absoluteFilePath(subdir)));
  1105. }
  1106. }
  1107. void
  1108. ImageLabeler::nextImage()
  1109. {
  1110. if (list_images_->isEmpty()) {
  1111. return;
  1112. /* NOTREACHED */
  1113. }
  1114. if (askForUnsavedData()) {
  1115. return;
  1116. /* NOTREACHED */
  1117. }
  1118. if (list_images_widget_->count() - 1 == image_ID_) {
  1119. //current_image_ = list_images_->at(0).image_;
  1120. image_ID_ = 0;
  1121. }
  1122. else {
  1123. image_ID_ ++;
  1124. //current_image_ = list_images_->at(image_ID_).image_;
  1125. }
  1126. if (!selectImage(image_ID_)) {
  1127. showWarning(tr("Next image is not available"));
  1128. return;
  1129. /* NOTREACHED */
  1130. }
  1131. list_images_widget_->setCurrentRow(image_ID_);
  1132. if (current_image_.isEmpty()) {
  1133. return;
  1134. /* NOTREACHED */
  1135. }
  1136. QString winTitle;
  1137. winTitle.append("ImageLabeler - ");
  1138. winTitle.append(current_image_);
  1139. setWindowTitle(winTitle);
  1140. image_->load(current_image_);
  1141. image_holder_->resize(image_->size());
  1142. image_holder_->setPixmap(*image_);
  1143. list_bounding_box_.clear();
  1144. list_polygon_.clear();
  1145. list_areas_->clear();
  1146. image_holder_->clearAll();
  1147. segmented_image_.clear();
  1148. clearLabelColorList();
  1149. //setWindowTitle(tr("ImageLabeler"));
  1150. }
  1151. void
  1152. ImageLabeler::prevImage()
  1153. {
  1154. if (!list_images_widget_->count()) {
  1155. return;
  1156. /* NOTREACHED */
  1157. }
  1158. if (askForUnsavedData()) {
  1159. return;
  1160. /* NOTREACHED */
  1161. }
  1162. if (!image_ID_) {
  1163. image_ID_ = list_images_widget_->count() - 1;
  1164. //current_image_ = list_images_->at(image_ID_).image_;
  1165. }
  1166. else {
  1167. image_ID_--;
  1168. //current_image_ = list_images_->at(image_ID_).image_;
  1169. }
  1170. list_images_widget_->setCurrentRow(image_ID_);
  1171. if (!selectImage(image_ID_)) {
  1172. showWarning(tr("Next image is not available"));
  1173. return;
  1174. /* NOTREACHED */
  1175. }
  1176. QString winTitle;
  1177. winTitle.append("ImageLabeler - ");
  1178. winTitle.append(current_image_);
  1179. setWindowTitle(winTitle);
  1180. image_->load(current_image_);
  1181. image_holder_->resize(image_->size());
  1182. image_holder_->setPixmap(*image_);
  1183. list_bounding_box_.clear();
  1184. list_polygon_.clear();
  1185. list_areas_->clear();
  1186. image_holder_->clearAll();
  1187. segmented_image_.clear();
  1188. clearLabelColorList();
  1189. //setWindowTitle(tr("ImageLabeler"));
  1190. }
  1191. void
  1192. ImageLabeler::saveAllInfo()
  1193. {
  1194. if (!list_images_widget_->count()) {
  1195. showWarning("You have not opened any image yet");
  1196. return;
  1197. /* NOTREACHED */
  1198. }
  1199. /* ------------------------------------------------------------------------
  1200. * XML part
  1201. */
  1202. QDomDocument doc(tr("ImageLabeler"));
  1203. QDomElement root = doc.createElement(tr("pixelwise_labeling"));
  1204. doc.appendChild(root);
  1205. QDomElement image = doc.createElement(tr("image"));
  1206. root.appendChild(image);
  1207. QDomText pathToImage = doc.createTextNode(current_image_);
  1208. image.appendChild(pathToImage);
  1209. if (!segmented_image_.isEmpty()) {
  1210. QDomElement segmentedImage = doc.createElement(tr("segmented"));
  1211. root.appendChild(segmentedImage);
  1212. QDomText pathToSegmented = doc.createTextNode(segmented_image_);
  1213. segmentedImage.appendChild(pathToSegmented);
  1214. }
  1215. QDomElement description = doc.createElement(tr("description"));
  1216. root.appendChild(description);
  1217. QDomText descriptionText = doc.createTextNode(image_description_);
  1218. description.appendChild(descriptionText);
  1219. legendToXml(&doc, &root);
  1220. objectsToXml(&doc, &root);
  1221. setPureData();
  1222. QDomElement pureData = doc.createElement(tr("pure_data"));
  1223. QSize imageSize = image_->size();
  1224. QString pixelValues;
  1225. for (int i = 0; i < imageSize.height(); i++) {
  1226. for (int j = 0; j < imageSize.width(); j++) {
  1227. pixelValues.append(QString("%1").arg(pure_data_[i][j]));
  1228. //pixelValues.append(QString(""));
  1229. }
  1230. pixelValues.append("\n");
  1231. }
  1232. QDomText pureDataText = doc.createTextNode(pixelValues);
  1233. pureData.appendChild(pureDataText);
  1234. root.appendChild(pureData);
  1235. QString xml = doc.toString();
  1236. /* ------------------------------------------------------------------------
  1237. * XML part ends
  1238. */
  1239. QFileDialog fileDialog(0, tr("Save all info"));
  1240. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1241. fileDialog.setDefaultSuffix("dat");
  1242. fileDialog.setFileMode(QFileDialog::AnyFile);
  1243. QString dir = getDirFromPath(&current_image_);
  1244. /* altering the name of a new file */
  1245. QString newFileName = alterFileName(current_image_, "_labeled");
  1246. newFileName = removePath(newFileName);
  1247. fileDialog.selectFile(newFileName);
  1248. fileDialog.setDirectory(dir);
  1249. QString filename;
  1250. if (fileDialog.exec()) {
  1251. filename = fileDialog.selectedFiles().last();
  1252. }
  1253. else {
  1254. //showWarning(tr("Can not open file dialog"));
  1255. return;
  1256. /* NOTREACHED */
  1257. }
  1258. if (filename.isEmpty()) {
  1259. return;
  1260. /* NOTREACHED */
  1261. }
  1262. QFile file(filename);
  1263. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1264. showWarning(tr("Can not open file for writing"));
  1265. return;
  1266. /* NOTREACHED */
  1267. }
  1268. file.write(xml.toLocal8Bit());
  1269. file.close();
  1270. unsaved_data_ = 0;
  1271. }
  1272. void
  1273. ImageLabeler::saveSegmentedPicture()
  1274. {
  1275. if (list_bounding_box_.isEmpty() && list_polygon_.isEmpty()) {
  1276. return;
  1277. /* NOTREACHED */
  1278. }
  1279. setPureData();
  1280. // QString suffix = *current_image_;
  1281. // int dotPos = suffix.lastIndexOf(".");
  1282. // suffix.mid(dotPos + 1, suffix.size() - dotPos - 1);
  1283. QFileDialog fileDialog(0, tr("Save segmented picture"));
  1284. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1285. fileDialog.setDefaultSuffix("png");
  1286. fileDialog.setFileMode(QFileDialog::AnyFile);
  1287. QString dir = getDirFromPath(&current_image_);
  1288. /* altering the name of a new file */
  1289. QString newFileName = alterFileName(current_image_, "_segmented");
  1290. newFileName = removePath(newFileName);
  1291. fileDialog.selectFile(newFileName);
  1292. fileDialog.setDirectory(dir);
  1293. QString filename;
  1294. if (fileDialog.exec()) {
  1295. filename = fileDialog.selectedFiles().last();
  1296. }
  1297. else {
  1298. return;
  1299. /* NOTREACHED */
  1300. }
  1301. QSize imageSize = image_holder_->pixmap()->size();
  1302. QImage newImage(imageSize, QImage::Format_RGB32);
  1303. bool generateColorsFlag = auto_color_generation_;
  1304. bool flag = 0;
  1305. /* checking if all the colors are are different from white(default) */
  1306. if (!generateColorsFlag) {
  1307. for (int i = 1; i < list_label_->count(); i++) {
  1308. if (list_label_colors_.at(i) == 0xffffffff && !flag)
  1309. flag = 1;
  1310. else if (list_label_colors_.at(i) == 0xffffffff && flag) {
  1311. QMessageBox msgBox;
  1312. msgBox.setText(tr("There are few labels with default white color."));
  1313. msgBox.setInformativeText(tr("Do you want to generate all colors automatically? Otherwise you'll have to do it manually."));
  1314. msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  1315. msgBox.setDefaultButton(QMessageBox::Yes);
  1316. msgBox.setIcon(QMessageBox::Question);
  1317. int ret = msgBox.exec();
  1318. if (QMessageBox::Yes == ret) {
  1319. generateColorsFlag = 1;
  1320. break;
  1321. }
  1322. else {
  1323. return;
  1324. /* NOTREACHED */
  1325. }
  1326. }
  1327. }
  1328. }
  1329. /* generating colors for labels */
  1330. if (list_label_colors_.count() < list_label_->count() ||
  1331. generateColorsFlag) {
  1332. generateColors();
  1333. }
  1334. for (int i = 0; i < imageSize.height(); i++)
  1335. for (int j = 0; j < imageSize.width(); j++) {
  1336. newImage.setPixel(j, i, list_label_colors_.at(pure_data_[i][j]));
  1337. }
  1338. if (!newImage.save(filename, "png", 100)) {
  1339. showWarning(tr("An error occurred while saving the segmented image"));
  1340. return;
  1341. /* NOTREACHED */
  1342. }
  1343. segmented_image_ = filename;
  1344. action_view_segmented_->setEnabled(true);
  1345. }
  1346. void
  1347. ImageLabeler::saveLegend()
  1348. {
  1349. if (!list_label_->count()) {
  1350. showWarning("You have not added any label yet");
  1351. return;
  1352. /* NOTREACHED */
  1353. }
  1354. /* ------------------------------------------------------------------------
  1355. * XML part
  1356. */
  1357. QDomDocument doc(tr("ImageLabeler"));
  1358. QDomElement root = doc.createElement(tr("root"));
  1359. doc.appendChild(root);
  1360. legendToXml(&doc, &root);
  1361. QString xml = doc.toString();
  1362. /* ------------------------------------------------------------------------
  1363. * XML part ends
  1364. */
  1365. QFileDialog fileDialog(0, tr("Save legend"));
  1366. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1367. fileDialog.setDefaultSuffix("dat");
  1368. fileDialog.setFileMode(QFileDialog::AnyFile);
  1369. QString dir = getDirFromPath(&current_image_);
  1370. /* altering the name of a new file */
  1371. QString newFileName = alterFileName(current_image_, "_legend");
  1372. newFileName = removePath(newFileName);
  1373. fileDialog.selectFile(newFileName);
  1374. fileDialog.setDirectory(dir);
  1375. QString filename;
  1376. if (fileDialog.exec()) {
  1377. filename = fileDialog.selectedFiles().last();
  1378. }
  1379. else {
  1380. //showWarning(tr("Can not open file dialog"));
  1381. return;
  1382. /* NOTREACHED */
  1383. }
  1384. if (filename.isEmpty()) {
  1385. return;
  1386. /* NOTREACHED */
  1387. }
  1388. QFile file(filename);
  1389. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1390. showWarning(tr("Can not open file for writing"));
  1391. return;
  1392. /* NOTREACHED */
  1393. }
  1394. file.write(xml.toLocal8Bit());
  1395. file.close();
  1396. }
  1397. void
  1398. ImageLabeler::loadInfo()
  1399. {
  1400. if (askForUnsavedData()) {
  1401. return;
  1402. /* NOTREACHED */
  1403. }
  1404. QFileDialog fileDialog(0, tr("Load file with info"));
  1405. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1406. fileDialog.setDefaultSuffix("dat");
  1407. fileDialog.setFileMode(QFileDialog::AnyFile);
  1408. QString filename;
  1409. if (fileDialog.exec()) {
  1410. filename = fileDialog.selectedFiles().last();
  1411. }
  1412. else {
  1413. //showWarning(tr("Can not open file dialog"));
  1414. return;
  1415. /* NOTREACHED */
  1416. }
  1417. clearAllTool();
  1418. clearLabelList();
  1419. if (loadInfo(filename)) {
  1420. enableTools();
  1421. Image newImage;
  1422. newImage.image_ = current_image_;
  1423. newImage.labeled_ = 1;
  1424. newImage.pas_ = 0;
  1425. addImage(&newImage);
  1426. image_ID_ = list_images_widget_->count() - 1;
  1427. list_images_widget_->setCurrentRow(image_ID_);
  1428. }
  1429. unsaved_data_ = 0;
  1430. }
  1431. bool
  1432. ImageLabeler::loadInfo(QString filename)
  1433. {
  1434. QDomDocument doc("Image Labeler");
  1435. QFile file(filename);
  1436. if (!file.open(QIODevice::ReadOnly)) {
  1437. showWarning(tr("Can not open such file"));
  1438. return false;
  1439. /* NOTREACHED */
  1440. }
  1441. QString errMsg;
  1442. if (!doc.setContent(&file, &errMsg)) {
  1443. showWarning(errMsg);
  1444. file.close();
  1445. return false;
  1446. /* NOTREACHED */
  1447. }
  1448. file.close();
  1449. //clearAll();
  1450. //enableTools();
  1451. /* getting all info */
  1452. QDomElement elements = doc.documentElement();
  1453. QDomNode rootNode = elements.firstChild();
  1454. QString string;
  1455. while(!rootNode.isNull()) {
  1456. QDomElement element = rootNode.toElement();
  1457. if(!element.isNull()) {
  1458. /* path to the image */
  1459. if (element.tagName() == "image") {
  1460. string = element.text();
  1461. if (string.isEmpty()) {
  1462. showWarning(
  1463. tr(
  1464. "The file with data doesn't contain path to the image"
  1465. )
  1466. );
  1467. return false;
  1468. /* NOTREACHED */
  1469. }
  1470. if (!image_->load(string)) {
  1471. return false;
  1472. /* NOTREACHED */
  1473. }
  1474. // Image newImage;
  1475. // newImage.image_ = string;
  1476. // newImage.labeled_ = 1;
  1477. // addImage(&newImage);
  1478. current_image_ = string;
  1479. // image_ID_ = list_images_widget_->count() - 1;
  1480. // list_images_widget_->setCurrentRow(image_ID_);
  1481. QString winTitle;
  1482. winTitle.append("ImageLabeler - ");
  1483. winTitle.append(current_image_);
  1484. setWindowTitle(winTitle);
  1485. image_holder_->resize(image_->size());
  1486. image_holder_->setPixmap(*image_);
  1487. }
  1488. /* path to the segmented image */
  1489. if (element.tagName() == "segmented") {
  1490. string = element.text();
  1491. if (string.isEmpty()) {
  1492. continue;
  1493. }
  1494. segmented_image_ = string;
  1495. action_view_segmented_->setEnabled(true);
  1496. }
  1497. /* image description */
  1498. else if (element.tagName() == "description" &&
  1499. !element.text().isEmpty()) {
  1500. image_description_ = element.text();
  1501. }
  1502. /* legend */
  1503. else if (element.tagName() == "legend") {
  1504. loadLegendFromNode(&element);
  1505. }
  1506. /* objects */
  1507. else if (element.tagName() == "objects") {
  1508. QDomNode subNode = element.firstChild();
  1509. QDomElement subElement;
  1510. while(!subNode.isNull()) {
  1511. subElement = subNode.toElement();
  1512. if (subElement.isNull() || subElement.text().isEmpty()) {
  1513. subNode = subNode.nextSibling();
  1514. continue;
  1515. }
  1516. string = subElement.attribute("id");
  1517. bool ok = 1;
  1518. int id = string.toInt(&ok, 10);
  1519. if (!ok) {
  1520. qDebug() <<
  1521. "loadInfo: "
  1522. "poly id format is corrupted";
  1523. subNode = subNode.nextSibling();
  1524. continue;
  1525. }
  1526. string = subElement.text();
  1527. if (subElement.tagName() == "bbox") {
  1528. addBBoxFromData(&string, &id);
  1529. }
  1530. if (subElement.tagName() == "poly") {
  1531. addPolyFromData(&string, &id);
  1532. }
  1533. subNode = subNode.nextSibling();
  1534. }
  1535. }
  1536. }
  1537. rootNode = rootNode.nextSibling();
  1538. }
  1539. unsaved_data_ = 0;
  1540. return true;
  1541. }
  1542. void
  1543. ImageLabeler::loadPascalFile()
  1544. {
  1545. if (askForUnsavedData()) {
  1546. return;
  1547. /* NOTREACHED */
  1548. }
  1549. QFileDialog fileDialog(0, tr("Load pascal file"));
  1550. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1551. fileDialog.setDefaultSuffix("xml");
  1552. fileDialog.setFileMode(QFileDialog::AnyFile);
  1553. QStringList filters;
  1554. filters << "PASCAL xml data (*.xml)"
  1555. << "Any files (*)";
  1556. fileDialog.setNameFilters(filters);
  1557. QString filename;
  1558. if (fileDialog.exec()) {
  1559. filename = fileDialog.selectedFiles().last();
  1560. }
  1561. else {
  1562. //showWarning(tr("Can not open file dialog"));
  1563. return;
  1564. /* NOTREACHED */
  1565. }
  1566. clearAllTool();
  1567. clearLabelList();
  1568. if (loadPascalFile(filename)) {
  1569. qDebug() << list_bounding_box_.at(0)->label_ID_;
  1570. qDebug() << list_bounding_box_.size();
  1571. enableTools();
  1572. Image newImage;
  1573. newImage.image_ = current_image_;
  1574. newImage.labeled_ = 1;
  1575. newImage.pas_ = 1;
  1576. addImage(&newImage);
  1577. image_ID_ = list_images_widget_->count() - 1;
  1578. list_images_widget_->setCurrentRow(image_ID_);
  1579. }
  1580. unsaved_data_ = 0;
  1581. }
  1582. bool
  1583. ImageLabeler::loadPascalFile(QString aFilename)
  1584. {
  1585. QDomDocument doc;
  1586. QFile file(aFilename);
  1587. if (!file.open(QIODevice::ReadOnly)) {
  1588. showWarning(tr("Can not open such file"));
  1589. return false;
  1590. /* NOTREACHED */
  1591. }
  1592. QString errMsg;
  1593. if (!doc.setContent(&file, &errMsg)) {
  1594. showWarning(errMsg);
  1595. file.close();
  1596. return false;
  1597. /* NOTREACHED */
  1598. }
  1599. file.close();
  1600. //clearAll();
  1601. //enableTools();
  1602. /* getting all info */
  1603. QDomElement elements = doc.documentElement();
  1604. QDomNode rootNode = elements.firstChild();
  1605. QString string;
  1606. QString path = getPathFromFilename(aFilename);
  1607. qDebug() << path;
  1608. QString filename;
  1609. QStringList labels;
  1610. labels << "BACKGROUND";
  1611. int labelID;
  1612. while(!rootNode.isNull()) {
  1613. QDomElement element = rootNode.toElement();
  1614. if(!element.isNull()) {
  1615. /* folder */
  1616. if (element.tagName() == "folder") {
  1617. string = element.text();
  1618. if (!string.isEmpty()) {
  1619. path.append(string);
  1620. }
  1621. }
  1622. /* filename */
  1623. else if (element.tagName() == "filename") {
  1624. string = element.text();
  1625. if (!string.isEmpty()) {
  1626. filename = string;
  1627. }
  1628. }
  1629. /* object */
  1630. else if (element.tagName() == "object") {
  1631. QDomNode subNode = element.firstChild();
  1632. QDomElement subElement;
  1633. while(!subNode.isNull()) {
  1634. subElement = subNode.toElement();
  1635. if (subElement.isNull() || subElement.text().isEmpty()) {
  1636. subNode = subNode.nextSibling();
  1637. continue;
  1638. }
  1639. /* label */
  1640. if (subElement.tagName() == "name") {
  1641. string = subElement.text();
  1642. if (!string.isEmpty() &&
  1643. !labels.contains(string, Qt::CaseInsensitive))
  1644. {
  1645. addLabel(list_label_->count(), 0, string);
  1646. labelID = labels.count();
  1647. labels << string;
  1648. }
  1649. else if (labels.contains(string, Qt::CaseInsensitive)) {
  1650. for (int i = 0; i < labels.count(); i++) {
  1651. if (labels.at(i) == string)
  1652. labelID = i;
  1653. }
  1654. }
  1655. }
  1656. if (subElement.tagName() == "bndbox") {
  1657. /* 2 points */
  1658. QPoint topLeft;
  1659. QPoint bottomRight;
  1660. QDomNode bboxNode = subElement.firstChild();
  1661. QDomElement bboxElement;
  1662. while(!bboxNode.isNull()) {
  1663. bboxElement = bboxNode.toElement();
  1664. string.clear();
  1665. bool ok = 1;
  1666. if (bboxElement.tagName() == "xmin") {
  1667. string = bboxElement.text();
  1668. if (string.isEmpty())
  1669. ok = 0;
  1670. double xmin = string.toDouble(&ok);
  1671. if (ok)
  1672. topLeft.setX(qRound(xmin));
  1673. }
  1674. else if (bboxElement.tagName() == "ymin") {
  1675. string = bboxElement.text();
  1676. if (string.isEmpty())
  1677. ok = 0;
  1678. double ymin = string.toDouble(&ok);
  1679. if (ok)
  1680. topLeft.setY(qRound(ymin));
  1681. }
  1682. else if (bboxElement.tagName() == "xmax") {
  1683. string = bboxElement.text();
  1684. if (string.isEmpty())
  1685. ok = 0;
  1686. double xmax = string.toDouble(&ok);
  1687. if (ok)
  1688. bottomRight.setX(qRound(xmax));
  1689. }
  1690. else if (bboxElement.tagName() == "ymax") {
  1691. string = bboxElement.text();
  1692. if (string.isEmpty())
  1693. ok = 0;
  1694. double ymax = string.toDouble(&ok);
  1695. if (ok)
  1696. bottomRight.setY(qRound(ymax));
  1697. }
  1698. qDebug() << string;
  1699. if (string.isEmpty() || !ok)
  1700. break;
  1701. bboxNode = bboxNode.nextSibling();
  1702. if (bboxNode.isNull()) {
  1703. BoundingBox *bbox = new BoundingBox;
  1704. bbox->rect.setTopLeft(topLeft);
  1705. bbox->rect.setBottomRight(bottomRight);
  1706. bbox->label_ID_ = labelID;
  1707. addBBox(bbox);
  1708. }
  1709. }
  1710. }
  1711. subNode = subNode.nextSibling();
  1712. }
  1713. }
  1714. }
  1715. rootNode = rootNode.nextSibling();
  1716. }
  1717. qDebug() << path + "/" + filename;
  1718. if (!image_->load(path + "/" + filename)) {
  1719. return false;
  1720. /* NOTREACHED */
  1721. }
  1722. current_image_ = path + filename;
  1723. QString winTitle;
  1724. winTitle.append("ImageLabeler - ");
  1725. winTitle.append(current_image_);
  1726. setWindowTitle(winTitle);
  1727. image_holder_->resize(image_->size());
  1728. image_holder_->setPixmap(*image_);
  1729. unsaved_data_ = 0;
  1730. return true;
  1731. }
  1732. void
  1733. ImageLabeler::loadPascalPolys()
  1734. {
  1735. if (current_image_.isEmpty()) {
  1736. return;
  1737. /* NOTREACHED */
  1738. }
  1739. QFileDialog fileDialog(0, tr("Load pascal polygons"));
  1740. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1741. fileDialog.setDefaultSuffix("polygon");
  1742. fileDialog.setFileMode(QFileDialog::AnyFile);
  1743. QStringList filters;
  1744. filters << "PASCAL polygons (*.polygon)"
  1745. << "Any files (*)";
  1746. fileDialog.setNameFilters(filters);
  1747. QString filename;
  1748. if (fileDialog.exec()) {
  1749. filename = fileDialog.selectedFiles().last();
  1750. }
  1751. else {
  1752. //showWarning(tr("Can not open file dialog"));
  1753. return;
  1754. /* NOTREACHED */
  1755. }
  1756. //clearAllTool();
  1757. //clearLabelList();
  1758. if (!loadPascalPolys(filename)) {
  1759. showWarning(tr("File format is corrupted."));
  1760. }
  1761. unsaved_data_ = 0;
  1762. image_holder_->update();
  1763. }
  1764. bool
  1765. ImageLabeler::loadPascalPolys(QString aFilename)
  1766. {
  1767. QFile file(aFilename);
  1768. if (!file.open(QIODevice::ReadOnly)) {
  1769. showWarning(tr("Can not open such file"));
  1770. return false;
  1771. /* NOTREACHED */
  1772. }
  1773. QByteArray data = file.readAll();
  1774. file.close();
  1775. QString label;
  1776. int lastSpace = 0;
  1777. int pointCount = 0;
  1778. Polygon *poly = 0;
  1779. QPoint point;
  1780. bool evenFlag = 0;
  1781. //for (int i = 0; i < data.length(); i++) {
  1782. int i = 0;
  1783. while (i < data.length()) {
  1784. qDebug() << data.at(i);
  1785. if (data.at(i) == ' ' && label.isEmpty() && !pointCount) {
  1786. label = QString(data.mid(lastSpace, i - lastSpace));
  1787. qDebug() << label;
  1788. lastSpace = i + 1;
  1789. }
  1790. else if (data.at(i) == ' ' && !label.isEmpty() && !pointCount) {
  1791. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1792. qDebug() << num;
  1793. bool ok = 1;
  1794. pointCount = num.toInt(&ok, 10);
  1795. if (!ok) {
  1796. return false;
  1797. /* NOTREACHED */
  1798. }
  1799. poly = new Polygon;
  1800. poly->label_ID_ = -1;
  1801. lastSpace = i + 1;
  1802. }
  1803. else if (data.at(i) == ' ' &&
  1804. !label.isEmpty() &&
  1805. pointCount &&
  1806. !evenFlag)
  1807. {
  1808. evenFlag = 1;
  1809. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1810. qDebug() << num;
  1811. bool ok = 1;
  1812. int coor = qRound(num.toDouble(&ok));
  1813. if (!ok) {
  1814. return false;
  1815. /* NOTREACHED */
  1816. }
  1817. point.setX(coor);
  1818. lastSpace = i + 1;
  1819. }
  1820. else if (data.at(i) == ' ' &&
  1821. !label.isEmpty() &&
  1822. pointCount &&
  1823. evenFlag)
  1824. {
  1825. evenFlag = 0;
  1826. pointCount--;
  1827. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1828. qDebug() << num;
  1829. bool ok = 1;
  1830. int coor = qRound(num.toDouble(&ok));
  1831. if (!ok) {
  1832. return false;
  1833. /* NOTREACHED */
  1834. }
  1835. point.setY(coor);
  1836. lastSpace = i + 1;
  1837. poly->poly << point;
  1838. }
  1839. else if (data.at(i) == '\n') {
  1840. if (pointCount || !poly) {
  1841. return false;
  1842. /* NOTREACHED */
  1843. }
  1844. lastSpace = i + 1;
  1845. for (int j = 0; j < list_label_->count(); j++) {
  1846. if (list_label_->item(j)->text().contains(label, Qt::CaseInsensitive)) {
  1847. poly->label_ID_ = j;
  1848. }
  1849. }
  1850. if (-1 == poly->label_ID_) {
  1851. int labelID = list_label_->count();
  1852. addLabel(labelID, 0, label);
  1853. poly->label_ID_ = labelID;
  1854. }
  1855. addPoly(poly);
  1856. label.clear();
  1857. }
  1858. i++;
  1859. }
  1860. //clearAll();
  1861. //enableTools();
  1862. return true;
  1863. }
  1864. void
  1865. ImageLabeler::loadImage()
  1866. {
  1867. if (askForUnsavedData()) {
  1868. return;
  1869. /* NOTREACHED */
  1870. }
  1871. QFileDialog fileDialog(0, tr("Load image"));
  1872. fileDialog.setFileMode(QFileDialog::ExistingFile);
  1873. QStringList filters;
  1874. filters << "Image files (*.jpeg *.jpg *.gif *.png *.bmp *.tiff)"
  1875. << "Any files (*)";
  1876. fileDialog.setNameFilters(filters);
  1877. QString filename;
  1878. if (fileDialog.exec()) {
  1879. filename = fileDialog.selectedFiles().last();
  1880. }
  1881. else {
  1882. //showWarning(tr("Could not open file dialog"));
  1883. return;
  1884. /* NOTREACHED */
  1885. }
  1886. if (filename.isEmpty()) {
  1887. return;
  1888. /* NOTREACHED */
  1889. }
  1890. QDir dir(filename);
  1891. QStringList filter;
  1892. filter << "*.dat";
  1893. QStringList fileList = dir.entryList(filter, QDir::Files);
  1894. QString labeled = alterFileName(filename, "_labeled");
  1895. labeled = removePath(labeled);
  1896. labeled.append(".dat");
  1897. if (fileList.contains(labeled, Qt::CaseInsensitive)) {
  1898. labeled = dir.absoluteFilePath(labeled);
  1899. loadInfo(labeled);
  1900. return;
  1901. /* NOTREACHED */
  1902. }
  1903. clearAllTool();
  1904. QString winTitle;
  1905. winTitle.append("ImageLabeler - ");
  1906. winTitle.append(current_image_);
  1907. setWindowTitle(winTitle);
  1908. if (!image_->load(filename)) {
  1909. return;
  1910. /* NOTREACHED */
  1911. }
  1912. image_holder_->resize(image_->size());
  1913. image_holder_->setPixmap(*image_);
  1914. if (!list_images_widget_->count()) {
  1915. return;
  1916. /* NOTREACHED */
  1917. }
  1918. current_image_ = filename;
  1919. image_ID_ = list_images_widget_->count() - 1;
  1920. list_images_widget_->setCurrentRow(image_ID_);
  1921. Image newImage;
  1922. newImage.image_ = filename;
  1923. newImage.labeled_ = 0;
  1924. newImage.pas_ = 0;
  1925. addImage(&newImage);
  1926. enableTools();
  1927. }
  1928. void
  1929. ImageLabeler::loadImages()
  1930. {
  1931. if (askForUnsavedData()) {
  1932. return;
  1933. /* NOTREACHED */
  1934. }
  1935. QFileDialog fileDialog(0, tr("Load images"));
  1936. fileDialog.setFileMode(QFileDialog::Directory);
  1937. QString dirName("");
  1938. if (fileDialog.exec()) {
  1939. /* TODO: make it possible to select multiple folders */
  1940. dirName = fileDialog.selectedFiles().last();
  1941. }
  1942. else {
  1943. //showWarning(tr("Could not open file dialog"));
  1944. return;
  1945. /* NOTREACHED */
  1946. }
  1947. clearAllTool();
  1948. QWidget *searchInProgress = new QWidget(0);
  1949. searchInProgress->setWindowTitle(tr("Loading images"));
  1950. QLabel *info = new QLabel(searchInProgress);
  1951. info->setText(
  1952. tr("Program is looking for all image files in your directory recursively."));
  1953. QPushButton *cancel = new QPushButton(tr("Cancel"), searchInProgress);
  1954. connect(cancel, SIGNAL(clicked()), this, SLOT(interruptSearch()));
  1955. QVBoxLayout *layout = new QVBoxLayout(searchInProgress);
  1956. searchInProgress->setLayout(layout);
  1957. layout->addWidget(info);
  1958. layout->addWidget(cancel);
  1959. searchInProgress->adjustSize();
  1960. searchInProgress->move(QApplication::desktop()->screen()->rect().center() - rect().center());
  1961. searchInProgress->show();
  1962. getImagesFromDir(QDir(dirName));
  1963. cancel->disconnect();
  1964. searchInProgress->hide();
  1965. delete info;
  1966. delete cancel;
  1967. delete layout;
  1968. delete searchInProgress;
  1969. if (interrupt_search_) {
  1970. interrupt_search_ = 0;
  1971. return;
  1972. /* NOTREACHED */
  1973. }
  1974. if (!list_images_widget_->count()) {
  1975. showWarning(tr("The folder you selected contains no images"));
  1976. return;
  1977. /* NOTREACHED */
  1978. }
  1979. bool ret = 0;
  1980. if (list_images_->at(0).labeled_) {
  1981. QString labeled =
  1982. alterFileName(list_images_->at(0).image_, "_labeled");
  1983. labeled.append(".dat");
  1984. ret = loadInfo(labeled);
  1985. }
  1986. else
  1987. ret = image_->load(list_images_->at(0).image_);
  1988. if (!ret) {
  1989. return;
  1990. /* NOTREACHED */
  1991. }
  1992. current_image_ = list_images_->at(0).image_;
  1993. image_ID_ = 0;
  1994. list_images_widget_->setCurrentRow(image_ID_);
  1995. QString winTitle;
  1996. winTitle.append("ImageLabeler - ");
  1997. winTitle.append(current_image_);
  1998. setWindowTitle(winTitle);
  1999. image_holder_->resize(image_->size());
  2000. image_holder_->setPixmap(*image_);
  2001. enableTools();
  2002. }
  2003. void
  2004. ImageLabeler::loadLegendFromFile()
  2005. {
  2006. QFileDialog fileDialog(0, tr("Load legend"));
  2007. fileDialog.setFileMode(QFileDialog::AnyFile);
  2008. QString filename;
  2009. if (fileDialog.exec()) {
  2010. filename = fileDialog.selectedFiles().last();
  2011. }
  2012. else {
  2013. //showWarning(tr("Could not open file dialog"));
  2014. return;
  2015. /* NOTREACHED */
  2016. }
  2017. QDomDocument doc("Image Labeler");
  2018. QFile file(filename);
  2019. if (!file.open(QIODevice::ReadOnly)) {
  2020. showWarning(tr("Can not open such file"));
  2021. return;
  2022. /* NOTREACHED */
  2023. }
  2024. QString errMsg;
  2025. if (!doc.setContent(&file, &errMsg)) {
  2026. showWarning(errMsg);
  2027. file.close();
  2028. return;
  2029. /* NOTREACHED */
  2030. }
  2031. file.close();
  2032. list_label_->clear();
  2033. /* getting legend */
  2034. QDomElement elements = doc.documentElement();
  2035. QDomNode rootNode = elements.firstChild();
  2036. QString string;
  2037. while(!rootNode.isNull()) {
  2038. QDomElement element = rootNode.toElement();
  2039. if(!element.isNull()) {
  2040. if (element.tagName() == "legend") {
  2041. loadLegendFromNode(&element);
  2042. }
  2043. }
  2044. rootNode = rootNode.nextSibling();
  2045. }
  2046. }
  2047. void
  2048. ImageLabeler::loadLegendFromNode(QDomElement *anElement)
  2049. {
  2050. if (!anElement) {
  2051. return;
  2052. /* NOTREACHED */
  2053. }
  2054. QDomNode subNode = anElement->firstChild();
  2055. QDomElement subElement;
  2056. QString string;
  2057. int id = -1;
  2058. bool isMain;
  2059. uint color = 0xff000000;
  2060. while(!subNode.isNull()) {
  2061. subElement = subNode.toElement();
  2062. if (!subElement.isNull() && !subElement.text().isEmpty()) {
  2063. string = subElement.attribute("id");
  2064. bool ok = 0;
  2065. id = string.toInt(&ok, 10);
  2066. if (!ok) {
  2067. qDebug() <<
  2068. "loadLegendFromNode: "
  2069. "label id format is corrupted";
  2070. subNode = subNode.nextSibling();
  2071. continue;
  2072. }
  2073. string = subElement.attribute("isMain");
  2074. isMain = string.toInt(&ok, 2);
  2075. if (!ok) {
  2076. qDebug() <<
  2077. "loadLegendFromNode: "
  2078. "label isMain flag format is corrupted";
  2079. subNode = subNode.nextSibling();
  2080. continue;
  2081. }
  2082. string = subElement.attribute("color");
  2083. color = string.toUInt(&ok, 16);
  2084. if (!ok) {
  2085. qDebug() <<
  2086. "loadLegendFromNode: "
  2087. "label color format is corrupted";
  2088. subNode = subNode.nextSibling();
  2089. continue;
  2090. }
  2091. string = subElement.text();
  2092. addLabel(id, isMain, string);
  2093. setLabelColor(id, color);
  2094. }
  2095. subNode = subNode.nextSibling();
  2096. }
  2097. }
  2098. void
  2099. ImageLabeler::legendToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2100. {
  2101. QDomElement legend = aDoc->createElement(tr("legend"));
  2102. aRoot->appendChild(legend);
  2103. /* storing all labels made by user */
  2104. int labelCount = list_label_->count();
  2105. for (int i = 0; i < labelCount; i++) {
  2106. QDomElement label = aDoc->createElement(tr("label"));
  2107. label.setAttribute("color", QString("%1").arg(list_label_colors_.at(i), 0, 16));
  2108. label.setAttribute("id", i);
  2109. QString priority;
  2110. if (main_label_ == i)
  2111. priority.append("1");
  2112. else
  2113. priority.append("0");
  2114. label.setAttribute("isMain", priority);
  2115. QString labelText = list_label_->item(i)->text();
  2116. /* removing the number prefix of label */
  2117. if (-1 != labelText.indexOf(QString("%1: ").arg(i))) {
  2118. labelText = labelText.mid(3, labelText.size() - 3);
  2119. }
  2120. QDomText labelName = aDoc->createTextNode(labelText);
  2121. label.appendChild(labelName);
  2122. legend.appendChild(label);
  2123. }
  2124. /* in case we have no labels */
  2125. if (0 == labelCount) {
  2126. QDomElement label = aDoc->createElement(tr("label"));
  2127. label.setAttribute(tr("id"), -1);
  2128. legend.appendChild(label);
  2129. }
  2130. }
  2131. void
  2132. ImageLabeler::objectsToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2133. {
  2134. QDomElement objects = aDoc->createElement(tr("objects"));
  2135. aRoot->appendChild(objects);
  2136. /* rects first */
  2137. for (int i = 0; i < list_bounding_box_.size(); i++) {
  2138. QDomElement rectData = aDoc->createElement(tr("bbox"));
  2139. rectData.setAttribute("id", list_bounding_box_.at(i)->label_ID_);
  2140. QRect rect = list_bounding_box_.at(i)->rect.normalized();
  2141. QString rectDataString =
  2142. QString("%1;%2;%3;%4;").
  2143. arg(rect.x()).
  2144. arg(rect.y()).
  2145. arg(rect.width()).
  2146. arg(rect.height());
  2147. QDomText rectDataText = aDoc->createTextNode(rectDataString);
  2148. rectData.appendChild(rectDataText);
  2149. objects.appendChild(rectData);
  2150. }
  2151. /* polys next */
  2152. for (int i = 0; i < list_polygon_.size(); i++) {
  2153. QDomElement polyData = aDoc->createElement(tr("poly"));
  2154. polyData.setAttribute("id", list_polygon_.at(i)->label_ID_);
  2155. QPolygon poly = list_polygon_.at(i)->poly;
  2156. QString polyDataString;
  2157. for (int j = 0; j < poly.count(); j++)
  2158. polyDataString.append(
  2159. QString("%1;%2;").
  2160. arg(poly.point(j).x()).
  2161. arg(poly.point(j).y())
  2162. );
  2163. QDomText polyDataText = aDoc->createTextNode(polyDataString);
  2164. polyData.appendChild(polyDataText);
  2165. objects.appendChild(polyData);
  2166. }
  2167. }
  2168. void
  2169. ImageLabeler::generateColors()
  2170. {
  2171. int labelCount = list_label_->count();
  2172. if (!labelCount) {
  2173. showWarning(tr("you have not added any labels yet"));
  2174. return;
  2175. /* NOTREACHED */
  2176. }
  2177. int coeff = (0xff / labelCount) * 3;
  2178. //QList< uint > colors;
  2179. list_label_colors_.clear();
  2180. list_label_colors_.append(0);
  2181. uchar red = 0xff;
  2182. uchar green = 0xff;
  2183. uchar blue = 0xff;
  2184. uchar iterationColor = coeff;
  2185. uint color = 0xffffffff;
  2186. int j = 1;
  2187. for (int i = 1; i < labelCount; i++) {
  2188. if (6 == j) {
  2189. iterationColor += coeff;
  2190. j = 1;
  2191. }
  2192. if (5 == j) {
  2193. red = 0xff - iterationColor;
  2194. green = 0xff - iterationColor;
  2195. blue = 0xff;
  2196. }
  2197. else if (4 == j) {
  2198. red = 0xff - iterationColor;
  2199. green = 0xff;
  2200. blue = 0xff - iterationColor;
  2201. }
  2202. else if (3 == j) {
  2203. red = 0xff - iterationColor;
  2204. green = 0xff;
  2205. blue = 0xff;
  2206. }
  2207. else if (2 == j) {
  2208. red = 0xff;
  2209. green = 0xff - iterationColor;
  2210. blue = 0xff;
  2211. }
  2212. else if (1 == j){
  2213. red = 0xff;
  2214. green = 0xff;
  2215. blue = 0xff - iterationColor;
  2216. }
  2217. j++;
  2218. color = red + (green * 0x100) + (blue * 0x10000) + 0xff000000;
  2219. int itemNo = list_label_colors_.count();
  2220. QPixmap iconPix = QPixmap(20, 20);
  2221. iconPix.fill(color);
  2222. QIcon icon(iconPix);
  2223. list_label_->item(itemNo)->setIcon(icon);
  2224. list_label_colors_.append(color);
  2225. }
  2226. image_holder_->update();
  2227. }
  2228. void
  2229. ImageLabeler::addBBoxFromData(
  2230. QString *aBBoxData,
  2231. int *ID
  2232. )
  2233. {
  2234. BoundingBox *bbox = new BoundingBox;
  2235. *bbox = BBoxFromData(aBBoxData);
  2236. if (!bbox->rect.isValid() || !ID) {
  2237. return;
  2238. /* NOTREACHED */
  2239. }
  2240. bbox->label_ID_ = *ID;
  2241. list_bounding_box_.append(bbox);
  2242. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  2243. }
  2244. BoundingBox
  2245. ImageLabeler::BBoxFromData(
  2246. QString *aBBoxData
  2247. )
  2248. {
  2249. qDebug() << *aBBoxData;
  2250. BoundingBox bbox;
  2251. QString buffer;
  2252. bbox.rect.setRect(-1, -1, -1, -1);
  2253. int startPos = 0;
  2254. bool ok = 1;
  2255. for (int i = 0; i < aBBoxData->size(); i++) {
  2256. if (';' != aBBoxData->at(i))
  2257. continue;
  2258. buffer = aBBoxData->mid(startPos, i - startPos);
  2259. int bboxData = buffer.toInt(&ok, 10);
  2260. if (!ok) {
  2261. qDebug() <<
  2262. "BBoxFromData: "
  2263. "bbox format is corrupted";
  2264. break;
  2265. }
  2266. if (-1 == bbox.rect.x()) {
  2267. bbox.rect.setX(bboxData);
  2268. bbox.rect.setWidth(-1);
  2269. }
  2270. else if (-1 == bbox.rect.y()) {
  2271. bbox.rect.setY(bboxData);
  2272. bbox.rect.setHeight(-1);
  2273. }
  2274. else if (-1 == bbox.rect.width()) {
  2275. bbox.rect.setWidth(bboxData);
  2276. }
  2277. else if (-1 == bbox.rect.height()) {
  2278. bbox.rect.setHeight(bboxData);
  2279. }
  2280. startPos = i + 1;
  2281. }
  2282. if (!bbox.rect.isValid()) {
  2283. qDebug() <<
  2284. "BBoxFromData: "
  2285. "bbox format is corrupted";
  2286. bbox.rect.setRect(-1, -1, -1, -1);
  2287. }
  2288. else if (!ok) {
  2289. bbox.rect.setRect(-1, -1, -1, -1);
  2290. }
  2291. return bbox;
  2292. }
  2293. BoundingBox
  2294. ImageLabeler::BBoxFromString(
  2295. QString *aString,
  2296. int *oldID
  2297. )
  2298. {
  2299. BoundingBox bbox;
  2300. bbox.label_ID_ = -1;
  2301. *oldID = -1;
  2302. if (!aString) {
  2303. return bbox;
  2304. /* NOTREACHED */
  2305. }
  2306. if (-1 == aString->indexOf("BBox")) {
  2307. return bbox;
  2308. /* NOTREACHED */
  2309. }
  2310. /* getting bbox id in the list(it cannot be changed) */
  2311. bool ok = 0;
  2312. int bboxID = getNumFromString(aString, "BBox #", ";", &ok);
  2313. if (!ok || bboxID <= -1) {
  2314. qDebug() <<
  2315. "BBoxFromString: bboxID is corrupted";
  2316. return bbox;
  2317. /* NOTREACHED */
  2318. }
  2319. /* getting new label id */
  2320. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  2321. if (!ok || labelID <= -1) {
  2322. showWarning(
  2323. tr("new LabelID is wrong, area can not be changed")
  2324. );
  2325. return bbox;
  2326. /* NOTREACHED */
  2327. }
  2328. /* getting new points */
  2329. int pointsPos = aString->indexOf("data:") + 5;
  2330. int pointsLen = aString->size() - pointsPos;
  2331. if (pointsLen <= 0) {
  2332. showWarning(
  2333. tr("new data is wrong, area can not be changed")
  2334. );
  2335. return bbox;
  2336. /* NOTREACHED */
  2337. }
  2338. QString pointsData = aString->mid(pointsPos, pointsLen);
  2339. bbox = BBoxFromData(&pointsData);
  2340. bbox.label_ID_ = labelID;
  2341. *oldID = bboxID;
  2342. return bbox;
  2343. }
  2344. void
  2345. ImageLabeler::addPoly(Polygon *poly)
  2346. {
  2347. if (poly->poly.isEmpty() || poly->label_ID_ < 0) {
  2348. return;
  2349. /* NOTREACHED */
  2350. }
  2351. list_polygon_.append(poly);
  2352. addPolyArea(list_polygon_.count() - 1, *poly);
  2353. }
  2354. void
  2355. ImageLabeler::addBBox(BoundingBox *bbox)
  2356. {
  2357. if (bbox->rect.isEmpty() || bbox->label_ID_ < 0) {
  2358. return;
  2359. /* NOTREACHED */
  2360. }
  2361. list_bounding_box_.append(bbox);
  2362. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  2363. }
  2364. void
  2365. ImageLabeler::addPolyFromData(
  2366. QString *aPolyData,
  2367. int *labelID
  2368. )
  2369. {
  2370. Polygon *poly = new Polygon;
  2371. *poly = polyFromData(aPolyData);
  2372. if (poly->poly.isEmpty() || !labelID) {
  2373. return;
  2374. /* NOTREACHED */
  2375. }
  2376. poly->label_ID_ = *labelID;
  2377. list_polygon_.append(poly);
  2378. addPolyArea(list_polygon_.count() - 1, *poly);
  2379. }
  2380. Polygon
  2381. ImageLabeler::polyFromData(
  2382. QString *aPolyData
  2383. )
  2384. {
  2385. Polygon poly;
  2386. poly.label_ID_ = -1;
  2387. QPoint point;
  2388. QString buffer;
  2389. int startPos = 0;
  2390. bool ok = 1;
  2391. bool evenFlag = 0;
  2392. for (int i = 0; i < aPolyData->size(); i++) {
  2393. if (';' != aPolyData->at(i))
  2394. continue;
  2395. buffer = aPolyData->mid(startPos, i - startPos);
  2396. int polyCoor = buffer.toInt(&ok, 10);
  2397. if (!ok) {
  2398. qDebug() <<
  2399. "polyFromData: "
  2400. "poly format is corrupted";
  2401. break;
  2402. }
  2403. if (!evenFlag) {
  2404. point.setX(polyCoor);
  2405. evenFlag = 1;
  2406. }
  2407. else {
  2408. point.setY(polyCoor);
  2409. poly.poly.append(point);
  2410. evenFlag = 0;
  2411. }
  2412. startPos = i + 1;
  2413. }
  2414. if (evenFlag) {
  2415. qDebug() <<
  2416. "polyFromData: "
  2417. "poly format is corrupted";
  2418. poly.poly.clear();
  2419. }
  2420. else if (!ok) {
  2421. poly.poly.clear();
  2422. }
  2423. return poly;
  2424. }
  2425. Polygon
  2426. ImageLabeler::polyFromString(
  2427. QString *aString,
  2428. int *oldID
  2429. )
  2430. {
  2431. Polygon poly;
  2432. poly.label_ID_ = -1;
  2433. *oldID = -1;
  2434. if (!aString) {
  2435. return poly;
  2436. /* NOTREACHED */
  2437. }
  2438. if (-1 == aString->indexOf("Poly")) {
  2439. return poly;
  2440. /* NOTREACHED */
  2441. }
  2442. /* getting poly id in the list(it cannot be changed) */
  2443. bool ok = 0;
  2444. int polyID = getNumFromString(aString, "Poly #", ";", &ok);
  2445. if (!ok || polyID <= -1) {
  2446. qDebug() <<
  2447. "polyFromString: poly ID is corrupted";
  2448. return poly;
  2449. /* NOTREACHED */
  2450. }
  2451. /* getting new label id */
  2452. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  2453. if (!ok || labelID <= -1) {
  2454. showWarning(
  2455. tr("new LabelID is wrong, area can not be changed")
  2456. );
  2457. return poly;
  2458. /* NOTREACHED */
  2459. }
  2460. /* getting new points */
  2461. int pointsPos = aString->indexOf("points:") + 7;
  2462. int pointsLen = aString->size() - pointsPos;
  2463. if (pointsLen <= 0) {
  2464. showWarning(
  2465. tr("new points data is wrong, area can not be changed")
  2466. );
  2467. return poly;
  2468. /* NOTREACHED */
  2469. }
  2470. QString pointsData = aString->mid(pointsPos, pointsLen);
  2471. poly = polyFromData(&pointsData);
  2472. poly.label_ID_ = labelID;
  2473. *oldID = polyID;
  2474. return poly;
  2475. }
  2476. void
  2477. ImageLabeler::showWarning(
  2478. const QString text
  2479. )
  2480. {
  2481. if (text.isEmpty()) {
  2482. return;
  2483. /* NOTREACHED */
  2484. }
  2485. QMessageBox msgBox;
  2486. msgBox.setText(text);
  2487. msgBox.setIcon(QMessageBox::Warning);
  2488. msgBox.exec();
  2489. }
  2490. bool
  2491. ImageLabeler::askForUnsavedData()
  2492. {
  2493. if ((!list_bounding_box_.isEmpty() ||
  2494. !list_polygon_.isEmpty()) &&
  2495. unsaved_data_)
  2496. {
  2497. QMessageBox msgBox;
  2498. msgBox.setText(tr("There is some unsaved data"));
  2499. msgBox.setInformativeText(tr("Do you want to save your progress?"));
  2500. msgBox.setStandardButtons(
  2501. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  2502. msgBox.setDefaultButton(QMessageBox::Save);
  2503. msgBox.setIcon(QMessageBox::Question);
  2504. int ret = msgBox.exec();
  2505. if (QMessageBox::Save == ret)
  2506. saveAllInfo();
  2507. else if (QMessageBox::Cancel == ret)
  2508. return true;
  2509. }
  2510. return false;
  2511. }
  2512. void
  2513. ImageLabeler::setBoundingBoxTool(bool aButtonPressed)
  2514. {
  2515. if (aButtonPressed)
  2516. image_holder_->setTool(ImageHolder::BoundingBoxTool);
  2517. else {
  2518. image_holder_->setTool(ImageHolder::NoTool);
  2519. image_holder_->clearLast();
  2520. }
  2521. }
  2522. void
  2523. ImageLabeler::setPolygonTool(bool aButtonPressed)
  2524. {
  2525. if (aButtonPressed) {
  2526. image_holder_->setTool(ImageHolder::PolygonTool);
  2527. image_holder_->setFocus();
  2528. }
  2529. else {
  2530. image_holder_->setTool(ImageHolder::NoTool);
  2531. image_holder_->clearLast();
  2532. }
  2533. }
  2534. void
  2535. ImageLabeler::onSelectionStarted()
  2536. {
  2537. button_confirm_selection_->setEnabled(true);
  2538. }
  2539. void
  2540. ImageLabeler::onImageScaled()
  2541. {
  2542. }
  2543. void
  2544. ImageLabeler::confirmSelection()
  2545. {
  2546. if (!list_label_->count()) {
  2547. showWarning(tr("You haven't added any label"));
  2548. return;
  2549. /* NOTREACHED */
  2550. }
  2551. image_holder_->confirmSelection();
  2552. if (label_ID_ < 0)
  2553. label_ID_ = 0;
  2554. ImageHolder::Tool tool = image_holder_->tool();
  2555. switch (tool) {
  2556. case ImageHolder::BoundingBoxTool:
  2557. list_bounding_box_.last()->label_ID_ = label_ID_;
  2558. addBBoxArea(
  2559. list_bounding_box_.count() - 1,
  2560. *(list_bounding_box_.last())
  2561. );
  2562. break;
  2563. case ImageHolder::PolygonTool:
  2564. list_polygon_.last()->label_ID_ = label_ID_;
  2565. addPolyArea(
  2566. list_polygon_.count() - 1,
  2567. *(list_polygon_.last())
  2568. );
  2569. break;
  2570. default:
  2571. break;
  2572. }
  2573. button_confirm_selection_->setEnabled(false);
  2574. unsaved_data_ = 1;
  2575. }
  2576. void
  2577. ImageLabeler::clearAll()
  2578. {
  2579. clearLabelList();
  2580. list_areas_->clear();
  2581. list_bounding_box_.clear();
  2582. list_polygon_.clear();
  2583. list_images_->clear();
  2584. list_images_widget_->clear();
  2585. main_label_ = -1;
  2586. image_holder_->clearAll();
  2587. segmented_image_.clear();
  2588. action_view_normal_->setEnabled(false);
  2589. action_view_segmented_->setEnabled(false);
  2590. }
  2591. void
  2592. ImageLabeler::clearAllTool()
  2593. {
  2594. list_areas_->clear();
  2595. list_bounding_box_.clear();
  2596. list_polygon_.clear();
  2597. main_label_ = -1;
  2598. image_holder_->clearAll();
  2599. }
  2600. void ImageLabeler::clearLabelList()
  2601. {
  2602. list_label_->clear();
  2603. addLabel(0, false, "BACKGROUND");
  2604. list_label_->item(0)->setFlags(
  2605. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  2606. Qt::ItemIsEnabled
  2607. );
  2608. }
  2609. void ImageLabeler::clearLabelColorList()
  2610. {
  2611. list_label_colors_.clear();
  2612. list_label_colors_.append(0x0);
  2613. }
  2614. void
  2615. ImageLabeler::enableTools()
  2616. {
  2617. action_save_labels_->setEnabled(true);
  2618. action_save_segmented_->setEnabled(true);
  2619. action_save_legend_->setEnabled(true);
  2620. action_bound_box_tool_->setEnabled(true);
  2621. action_polygon_tool_->setEnabled(true);
  2622. action_add_description_->setEnabled(true);
  2623. action_undo_->setEnabled(true);
  2624. action_redo_->setEnabled(true);
  2625. button_bound_box_tool_->setEnabled(true);
  2626. button_polygon_tool_->setEnabled(true);
  2627. button_add_label_->setEnabled(true);
  2628. button_remove_label_->setEnabled(true);
  2629. button_prev_image_->setEnabled(true);
  2630. button_next_image_->setEnabled(true);
  2631. button_clear_selection_tool_->setEnabled(true);
  2632. button_generate_colors_->setEnabled(true);
  2633. button_delete_all_labels_->setEnabled(true);
  2634. }
  2635. void
  2636. ImageLabeler::disableTools()
  2637. {
  2638. action_save_labels_->setEnabled(false);
  2639. action_save_segmented_->setEnabled(false);
  2640. action_save_legend_->setEnabled(false);
  2641. action_bound_box_tool_->setEnabled(false);
  2642. action_polygon_tool_->setEnabled(false);
  2643. action_add_description_->setEnabled(false);
  2644. action_undo_->setEnabled(false);
  2645. action_redo_->setEnabled(false);
  2646. button_bound_box_tool_->setEnabled(false);
  2647. button_polygon_tool_->setEnabled(false);
  2648. button_add_label_->setEnabled(false);
  2649. button_remove_label_->setEnabled(false);
  2650. button_prev_image_->setEnabled(false);
  2651. button_next_image_->setEnabled(false);
  2652. button_clear_selection_tool_->setEnabled(false);
  2653. button_generate_colors_->setEnabled(false);
  2654. button_delete_all_labels_->setEnabled(false);
  2655. }
  2656. void
  2657. ImageLabeler::areaListPopupMenu(const QPoint &aPos)
  2658. {
  2659. QPoint globalPos = list_areas_->mapToGlobal(aPos);
  2660. QModelIndex index = list_areas_->indexAt(aPos);
  2661. if (-1 == index.row()) {
  2662. return;
  2663. /* NOTREACHED */
  2664. }
  2665. list_areas_->item(index.row())->setSelected(true);
  2666. popup_area_list_->exec(globalPos);
  2667. }
  2668. void
  2669. ImageLabeler::labelListPopupMenu(const QPoint &aPos)
  2670. {
  2671. QPoint globalPos = list_label_->mapToGlobal(aPos);
  2672. QModelIndex index = list_label_->indexAt(aPos);
  2673. if (-1 == index.row() || !index.row()) {
  2674. return;
  2675. /* NOTREACHED */
  2676. }
  2677. list_label_->item(index.row())->setSelected(true);
  2678. popup_label_list_->exec(globalPos);
  2679. }
  2680. void
  2681. ImageLabeler::setDescription(QString aDescription)
  2682. {
  2683. image_description_ = aDescription;
  2684. setWindowTitle(image_description_);
  2685. }
  2686. void
  2687. ImageLabeler::setPureData()
  2688. {
  2689. /* initializing array */
  2690. if (pure_data_) {
  2691. delete[] *pure_data_;
  2692. delete pure_data_;
  2693. }
  2694. QSize imageSize = image_->size();
  2695. pure_data_ = new int *[imageSize.height()];
  2696. if (!pure_data_) {
  2697. return;
  2698. /* NOTREACHED */
  2699. }
  2700. for (int i = 0; i < imageSize.height(); i++) {
  2701. pure_data_[i] = new int[imageSize.width()];
  2702. if (!pure_data_[i]) {
  2703. return;
  2704. /* NOTREACHED */
  2705. }
  2706. }
  2707. int bboxCnt = list_bounding_box_.count();
  2708. int polyCnt = list_polygon_.count();
  2709. for (int i = 0; i < imageSize.height(); i++)
  2710. for (int j = 0; j < imageSize.width(); j++) {
  2711. pure_data_[i][j] = 0;
  2712. /* bboxes first */
  2713. for (int cnt = 0; cnt < bboxCnt; cnt++) {
  2714. BoundingBox *bbox = list_bounding_box_.at(cnt);
  2715. if (bbox->rect.contains(j, i)) {
  2716. pure_data_[i][j] = bbox->label_ID_;
  2717. }
  2718. }
  2719. /* polys next */
  2720. for (int cnt = 0; cnt < polyCnt; cnt++) {
  2721. Polygon *poly = list_polygon_.at(cnt);
  2722. if (poly->poly.containsPoint(QPoint(j, i), Qt::OddEvenFill)) {
  2723. pure_data_[i][j] = poly->label_ID_;
  2724. }
  2725. }
  2726. }
  2727. }
  2728. void
  2729. ImageLabeler::setLabelColor()
  2730. {
  2731. if (list_label_colors_.count() < list_label_->count()) {
  2732. generateColors();
  2733. }
  2734. QListWidgetItem *current = list_label_->currentItem();
  2735. int labelID = list_label_->row(current);
  2736. QColor defaultColor;
  2737. defaultColor.setRgb(list_label_colors_.at(labelID));
  2738. QColor newColor = QColorDialog::getColor(
  2739. defaultColor,
  2740. list_label_
  2741. );
  2742. if (!newColor.isValid()) {
  2743. return;
  2744. /* NOTREACHED */
  2745. }
  2746. list_label_colors_.takeAt(labelID);
  2747. list_label_colors_.insert(labelID, newColor.rgb());
  2748. QPixmap iconPix = QPixmap(20, 20);
  2749. iconPix.fill(newColor);
  2750. QIcon icon(iconPix);
  2751. current->setIcon(icon);
  2752. image_holder_->update();
  2753. }
  2754. void
  2755. ImageLabeler::setLabelColor(int anID, QColor aColor)
  2756. {
  2757. if (anID < 0) {
  2758. return;
  2759. /* NOTREACHED */
  2760. }
  2761. if (list_label_colors_.count() < list_label_->count()) {
  2762. generateColors();
  2763. }
  2764. QListWidgetItem *item = list_label_->item(anID);
  2765. list_label_colors_.takeAt(anID);
  2766. list_label_colors_.insert(anID, aColor.rgb());
  2767. QPixmap iconPix = QPixmap(20, 20);
  2768. iconPix.fill(aColor);
  2769. QIcon icon(iconPix);
  2770. item->setIcon(icon);
  2771. image_holder_->update();
  2772. }
  2773. void
  2774. ImageLabeler::onOptionsSet()
  2775. {
  2776. auto_color_generation_ = options_form_.autoColorGeneration();
  2777. }
  2778. void
  2779. ImageLabeler::viewNormal()
  2780. {
  2781. if (current_image_.isEmpty()) {
  2782. return;
  2783. /* NOTREACHED */
  2784. }
  2785. image_->load(current_image_);
  2786. image_holder_->setPixmap(*image_);
  2787. action_view_segmented_->setEnabled(true);
  2788. action_view_normal_->setEnabled(false);
  2789. }
  2790. void
  2791. ImageLabeler::viewSegmented()
  2792. {
  2793. if (segmented_image_.isEmpty()) {
  2794. return;
  2795. /* NOTREACHED */
  2796. }
  2797. image_->load(segmented_image_);
  2798. image_holder_->setPixmap(*image_);
  2799. action_view_segmented_->setEnabled(false);
  2800. action_view_normal_->setEnabled(true);
  2801. }
  2802. void
  2803. ImageLabeler::interruptSearch()
  2804. {
  2805. interrupt_search_ = 1;
  2806. }
  2807. void
  2808. ImageLabeler::selectImage(QListWidgetItem *anItem)
  2809. {
  2810. if (!anItem || list_images_widget_->row(anItem) < 0 ||
  2811. list_images_->isEmpty()) {
  2812. return;
  2813. /* NOTREACHED */
  2814. }
  2815. clearAllTool();
  2816. clearLabelList();
  2817. clearLabelColorList();
  2818. image_ID_ = list_images_widget_->row(anItem);
  2819. selectImage(image_ID_);
  2820. // if (list_images_->at(image_ID_).labeled_ &&
  2821. // !list_images_->at(image_ID_).pas_)
  2822. // {
  2823. // list_label_->clear();
  2824. // QString labeled =
  2825. // alterFileName(list_images_->at(image_ID_).image_, "_labeled");
  2826. // labeled.append(".dat");
  2827. // loadInfo(labeled);
  2828. // }
  2829. // else if (list_images_->at(image_ID_).labeled_ &&
  2830. // list_images_->at(image_ID_).pas_)
  2831. // {
  2832. // /* TODO: do the pascal file selecting */
  2833. // showWarning("this function doesn't work at the moment, sorry.");
  2834. // }
  2835. // else {
  2836. // current_image_ = list_images_->at(image_ID_).image_;
  2837. // image_->load(current_image_);
  2838. // image_holder_->setPixmap(*image_);
  2839. // image_holder_->resize(image_->size());
  2840. // }
  2841. }
  2842. bool
  2843. ImageLabeler::selectImage(int anImageID)
  2844. {
  2845. if (anImageID < 0 || list_images_->isEmpty() ||
  2846. list_images_->count() <= anImageID)
  2847. {
  2848. return false;
  2849. /* NOTREACHED */
  2850. }
  2851. if (list_images_->at(anImageID).labeled_ &&
  2852. !list_images_->at(anImageID).pas_)
  2853. {
  2854. list_label_->clear();
  2855. QString labeled =
  2856. alterFileName(list_images_->at(anImageID).image_, "_labeled");
  2857. labeled.append(".dat");
  2858. loadInfo(labeled);
  2859. }
  2860. else if (list_images_->at(anImageID).labeled_ &&
  2861. list_images_->at(anImageID).pas_)
  2862. {
  2863. /* TODO: do the pascal file selecting */
  2864. showWarning("this function doesn't work at the moment, sorry.");
  2865. }
  2866. else {
  2867. current_image_ = list_images_->at(anImageID).image_;
  2868. image_->load(current_image_);
  2869. image_holder_->setPixmap(*image_);
  2870. image_holder_->resize(image_->size());
  2871. }
  2872. return true;
  2873. }
  2874. void
  2875. ImageLabeler::imageListPopupMenu(const QPoint &aPos)
  2876. {
  2877. QPoint globalPos = list_images_widget_->mapToGlobal(aPos);
  2878. QModelIndex index = list_images_widget_->indexAt(aPos);
  2879. if (-1 == index.row()) {
  2880. return;
  2881. /* NOTREACHED */
  2882. }
  2883. list_images_widget_->item(index.row())->setSelected(true);
  2884. popup_images_list_->exec(globalPos);
  2885. }
  2886. void
  2887. ImageLabeler::removeImage()
  2888. {
  2889. int num = list_images_widget_->currentRow();
  2890. list_images_widget_->takeItem(num);
  2891. list_images_->takeAt(num);
  2892. for (int i = num ; i < list_images_widget_->count(); i++) {
  2893. QString newStr = getFilenameFromPath(&(list_images_->at(i).image_));
  2894. newStr.prepend(QString("%1: ").arg(i));
  2895. list_images_widget_->item(i)->setText(newStr);
  2896. }
  2897. }
  2898. void
  2899. ImageLabeler::resizeEvent (QResizeEvent *anEvent)
  2900. {
  2901. QWidget::resizeEvent(anEvent);
  2902. }
  2903. void
  2904. ImageLabeler::mousePressEvent(QMouseEvent *anEvent)
  2905. {
  2906. QWidget::mousePressEvent(anEvent);
  2907. }
  2908. void
  2909. ImageLabeler::keyPressEvent(QKeyEvent *anEvent)
  2910. {
  2911. keyboard_modifier_ = anEvent->modifiers();
  2912. if (Qt::Key_Left == anEvent->key() &&
  2913. Qt::ControlModifier == keyboard_modifier_) {
  2914. prevImage();
  2915. }
  2916. if (Qt::Key_Right == anEvent->key() &&
  2917. Qt::ControlModifier == keyboard_modifier_) {
  2918. nextImage();
  2919. }
  2920. if ((Qt::Key_Enter == anEvent->key() ||
  2921. Qt::Key_Return == anEvent->key()) &&
  2922. // Qt::NoModifier == anEvent->modifiers() &&
  2923. ImageHolder::NewSelection == image_holder_->state()) {
  2924. confirmSelection();
  2925. }
  2926. if ((Qt::Key_Enter == anEvent->key() ||
  2927. Qt::Key_Return == anEvent->key()) &&
  2928. // Qt::NoModifier == anEvent->modifiers() &&
  2929. -1 != image_holder_->focusedSelection()) {
  2930. image_holder_->clearFocusOnArea();
  2931. image_holder_->clearEdition();
  2932. image_holder_->update();
  2933. }
  2934. if (Qt::Key_Escape == anEvent->key()) {
  2935. image_holder_->clearLast();
  2936. image_holder_->clearFocusOnArea();
  2937. image_holder_->clearEdition();
  2938. image_holder_->update();
  2939. }
  2940. if (Qt::Key_Z == anEvent->key() &&
  2941. Qt::ControlModifier == anEvent->modifiers()) {
  2942. image_holder_->undo();
  2943. }
  2944. if (Qt::Key_Y == anEvent->key() &&
  2945. Qt::ControlModifier == anEvent->modifiers()) {
  2946. image_holder_->redo();
  2947. }
  2948. QWidget::keyPressEvent(anEvent);
  2949. }
  2950. void
  2951. ImageLabeler::keyReleaseEvent(QKeyEvent *anEvent)
  2952. {
  2953. keyboard_modifier_ = anEvent->modifiers();
  2954. }
  2955. void
  2956. ImageLabeler::wheelEvent(QWheelEvent *anEvent)
  2957. {
  2958. /* zoomin */
  2959. if (0 < anEvent->delta() &&
  2960. Qt::ControlModifier == keyboard_modifier_) {
  2961. image_holder_->scaleImage(ZoomIn, 1.1);
  2962. }
  2963. /* zoomout */
  2964. else if (anEvent->delta() < 0 &&
  2965. Qt::ControlModifier == keyboard_modifier_) {
  2966. image_holder_->scaleImage(ZoomOut, 1.1);
  2967. }
  2968. }
  2969. /*
  2970. *
  2971. */