SimpleSelector.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 ( Q3PopupMenu *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 ( int id );
  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<int, 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, std::vector<NICE::RectT<double> > & rectangles,
  100. std::vector<int> & colors,
  101. const std::string & title,
  102. const int maxColors)
  103. {
  104. if (qApp == NULL) {
  105. QtFramework::instance();
  106. }
  107. SimpleSelector* display = new SimpleSelector( true, false, maxColors );
  108. display->setCaption(title.c_str());
  109. display->setImage(&image, true);
  110. display->show();
  111. QtFramework::exec ( false );
  112. rectangles = display->getRectangles();
  113. colors = display->getColors();
  114. delete display;
  115. }
  116. /**
  117. * Create a \c SimpleSelector window (which allows rectangle
  118. * and point selection), display the \c image
  119. * and finally give control to Qt.
  120. * @param rectangles list of all selected rectangles
  121. * @param title window title
  122. */
  123. template <class ImageType>
  124. void selectRectangles ( const ImageType & image, std::vector<NICE::RectT<double> > & rectangles,
  125. const std::string & title )
  126. {
  127. std::vector<int> colors;
  128. selectColoredRectangles ( image, rectangles, colors, title, 1 );
  129. }
  130. /**
  131. * Create a \c SimpleSelector window (which allows rectangle
  132. * and point selection), display the \c image
  133. * and finally give control to Qt.
  134. * @param points list of all selected points
  135. * @param colors corresponding colors of all selected points
  136. * @param title window title
  137. * @param maximum number of colors
  138. */
  139. template <class ImageType>
  140. void selectColoredPoints ( const ImageType & image, std::vector<NICE::CoordT<double> > & points,
  141. std::vector<int> & colors,
  142. const std::string & title,
  143. const int maxColors)
  144. {
  145. if (qApp == NULL) {
  146. QtFramework::instance();
  147. }
  148. SimpleSelector* display = new SimpleSelector( false, true, maxColors );
  149. display->setCaption(title.c_str());
  150. display->setImage(&image, true);
  151. display->show();
  152. QtFramework::exec ( false );
  153. const std::vector<NICE::RectT<double> > & rectangles = display->getRectangles();
  154. colors = display->getColors();
  155. points.clear();
  156. for ( std::vector<NICE::RectT<double> >::const_iterator i = rectangles.begin();
  157. i != rectangles.end(); i++ )
  158. points.push_back ( CoordT<double> ( i->left, i->top ) );
  159. delete display;
  160. }
  161. /**
  162. * Create a \c SimpleSelector window (which allows rectangle
  163. * and point selection), display the \c image
  164. * and finally give control to Qt.
  165. * @param points list of all selected points
  166. * @param title window title
  167. */
  168. template <class ImageType>
  169. void selectPoints ( const ImageType & image, std::vector<NICE::CoordT<double> > & points,
  170. const std::string & title)
  171. {
  172. std::vector<int> colors;
  173. selectColoredPoints ( image, points, colors, title, 1 );
  174. }
  175. } // namespace
  176. #endif /* _SIMPLESELECTOR_SIMPLESELECTOR_H */