SimpleSelector.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libimagedisplay - A library for image and video display
  4. * See file License for license information.
  5. */
  6. #ifndef _SIMPLESELECTOR_SIMPLESELECTOR_H
  7. #define _SIMPLESELECTOR_SIMPLESELECTOR_H
  8. #include <vector>
  9. #include <memory>
  10. #include <map>
  11. #include <core/image/ColorImageT.h>
  12. #include <core/image/ImageT.h>
  13. #include <core/image/RectT.h>
  14. #include <core/image/ColorT.h>
  15. #include <core/imagedisplay/ImageDisplay.h>
  16. #include <core/imagedisplay/QtFramework.h>
  17. namespace NICE {
  18. /**
  19. * SimpleSelector is a simple image display widget which
  20. * supports the selection (and display) of labeled rectangles
  21. * and points.
  22. *
  23. * @author Erik Rodner (Erik [dot] Rodner [at] uni-jena [dot] de)
  24. *
  25. * @note
  26. * This class is experimental and might change in the future.
  27. */
  28. class SimpleSelector : public ImageDisplay {
  29. Q_OBJECT;
  30. public:
  31. //! Default constructor
  32. SimpleSelector ( bool drawRectangle = false,
  33. bool drawCross = true,
  34. int m_maxColors = 1,
  35. QWidget* parent = NULL,
  36. const char* name = NULL,
  37. Qt::WFlags flags = 0);
  38. //! Destructor of SimpleSelector
  39. virtual ~SimpleSelector();
  40. //! get selected rectangles
  41. const std::vector< NICE::RectT<double> > & getRectangles(void) const
  42. { return m_rectangles; }
  43. //! add a rectangle
  44. void addRectangle ( const NICE::RectT<double> & rectangle, int color );
  45. //! get colors of all selected rectangles
  46. const std::vector< int > & getColors(void) const
  47. { return m_rectanglesColor; }
  48. protected:
  49. /** print markers and rectangle boundaries */
  50. virtual void paintGLObjects(void);
  51. /**
  52. * called by contextMenuEvent(), use this to include additional
  53. * menu items
  54. */
  55. void addExtraMenuItems ( QMenu *popupMenu );
  56. //! set current marker color
  57. void setCurrentColor ( int color );
  58. private slots:
  59. /**
  60. * A rectangle (\c left, \c top, \c right, \c bottom) has been drawn
  61. * by the user.
  62. * @note A single click results in a degenerate rectangle.
  63. * Thus, single clicks can also be handled:
  64. * use point (\c left, \c top).
  65. */
  66. void rectSelect(float left, float top, float right, float bottom);
  67. //! slot needed for the context menu
  68. void menuActivated ( QAction* action );
  69. //! dummy slot for menu item insertion
  70. void dummy(void);
  71. private:
  72. //! current color
  73. int m_markColor;
  74. //! maximum number of colors
  75. int m_maxColors;
  76. //! draw selected rectangles
  77. bool m_drawRectangle;
  78. //! draw selected points (left, top)
  79. bool m_drawCross;
  80. //! size of crosses
  81. int m_crossSize;
  82. //! selected rectangles
  83. std::vector< NICE::RectT<double> > m_rectangles;
  84. //! corresponding colors of selected rectangles
  85. std::vector<int> m_rectanglesColor;
  86. //! this map maps menu item ids to colors
  87. std::map<QAction*, int> colorMenuMap;
  88. };
  89. /**
  90. * Create a \c SimpleSelector window (which allows rectangle
  91. * and point selection), display the \c image
  92. * and finally give control to Qt.
  93. * @param rectangles list of all selected rectangles
  94. * @param colors corresponding colors of all selected points
  95. * @param title window title
  96. * @param maximum number of colors
  97. */
  98. template <class ImageType>
  99. void selectColoredRectangles ( const ImageType & image,
  100. std::vector<NICE::RectT<double> > & rectangles,
  101. std::vector<int> & colors,
  102. const std::string & title,
  103. const int maxColors)
  104. {
  105. if (qApp == NULL) {
  106. QtFramework::instance();
  107. }
  108. SimpleSelector* display = new SimpleSelector( true, false, maxColors );
  109. display->setCaption(title.c_str());
  110. display->setImage(&image, true);
  111. display->show();
  112. QtFramework::exec ( false );
  113. rectangles = display->getRectangles();
  114. colors = display->getColors();
  115. delete display;
  116. }
  117. /**
  118. * Create a \c SimpleSelector window (which allows rectangle
  119. * and point selection), display the \c image
  120. * and finally give control to Qt.
  121. * @param rectangles list of all selected rectangles
  122. * @param title window title
  123. */
  124. template <class ImageType>
  125. void selectRectangles ( const ImageType & image, std::vector<NICE::RectT<double> > & rectangles,
  126. const std::string & title )
  127. {
  128. std::vector<int> colors;
  129. selectColoredRectangles ( image, rectangles, colors, title, 1 );
  130. }
  131. /**
  132. * Create a \c SimpleSelector window (which allows rectangle
  133. * and point selection), display the \c image
  134. * and finally give control to Qt.
  135. * @param points list of all selected points
  136. * @param colors corresponding colors of all selected points
  137. * @param title window title
  138. * @param maximum number of colors
  139. */
  140. template <class ImageType>
  141. void selectColoredPoints ( const ImageType & image,
  142. std::vector<NICE::CoordT<double> > & points,
  143. std::vector<int> & colors,
  144. const std::string & title,
  145. const int maxColors)
  146. {
  147. if (qApp == NULL) {
  148. QtFramework::instance();
  149. }
  150. SimpleSelector* display = new SimpleSelector( false, true, maxColors );
  151. display->setCaption(title.c_str());
  152. display->setImage(&image, true);
  153. display->show();
  154. QtFramework::exec ( false );
  155. const std::vector<NICE::RectT<double> > & rectangles = display->getRectangles();
  156. colors = display->getColors();
  157. points.clear();
  158. for ( std::vector<NICE::RectT<double> >::const_iterator i = rectangles.begin(); i != rectangles.end(); i++ )
  159. {
  160. points.push_back ( NICE::CoordT<double> ( i->left, i->top ) );
  161. }
  162. //don't waste memory
  163. delete display;
  164. }
  165. /**
  166. * Create a \c SimpleSelector window (which allows rectangle
  167. * and point selection), display the \c image
  168. * and finally give control to Qt.
  169. * @param points list of all selected points
  170. * @param title window title
  171. */
  172. template <class ImageType>
  173. void selectPoints ( const ImageType & image,
  174. std::vector<NICE::CoordT<double> > & points,
  175. const std::string & title)
  176. {
  177. std::vector<int> colors;
  178. selectColoredPoints ( image, points, colors, title, 1 );
  179. }
  180. } // namespace
  181. #endif /* _SIMPLESELECTOR_SIMPLESELECTOR_H */