MatViewer.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #ifndef XYZ_MAT_VIEWER_HPP_
  2. #define XYZ_MAT_VIEWER_HPP_
  3. #include <opencv2/opencv.hpp>
  4. #include <string>
  5. #include "DepthRender.hpp"
  6. class GraphicItem
  7. {
  8. public:
  9. GraphicItem(
  10. const cv::Scalar& color = cv::Scalar(255,255,255)
  11. )
  12. : _id(++globalID), _color(color) {}
  13. virtual ~GraphicItem() {}
  14. int id() const { return _id; }
  15. cv::Scalar color() const { return _color; }
  16. void setColor(const cv::Scalar& color) { _color = color; }
  17. virtual void draw(cv::Mat& img) = 0;
  18. protected:
  19. int _id;
  20. cv::Scalar _color;
  21. private:
  22. static int globalID;
  23. };
  24. class GraphicRectangleItem : public GraphicItem
  25. {
  26. public:
  27. cv::Rect _rect;
  28. GraphicRectangleItem(
  29. const cv::Scalar& color = cv::Scalar(255,255,255),
  30. const cv::Rect& rect = cv::Rect()
  31. )
  32. : GraphicItem(color), _rect(rect) {}
  33. virtual ~GraphicRectangleItem() {}
  34. void set(const cv::Rect& rect) { _rect = rect; }
  35. virtual void draw(cv::Mat& img){ cv::rectangle(img, _rect, color()); }
  36. };
  37. class GraphicStringItem : public GraphicItem
  38. {
  39. public:
  40. std::string _str;
  41. cv::Point _loc;
  42. double _scale;
  43. int _thick;
  44. GraphicStringItem(
  45. const std::string& str = std::string(),
  46. const cv::Point loc = cv::Point(),
  47. double scale = 0,
  48. const cv::Scalar& color = cv::Scalar(),
  49. int thick = 0
  50. )
  51. : GraphicItem(color), _str(str), _loc(loc), _scale(scale), _thick(thick) {}
  52. virtual ~GraphicStringItem() {}
  53. void set(const std::string& str) { _str = str; }
  54. virtual void draw(cv::Mat& img){
  55. cv::putText(img, _str, _loc, cv::FONT_HERSHEY_SIMPLEX, _scale, _color, _thick);
  56. }
  57. };
  58. class OpencvViewer
  59. {
  60. public:
  61. OpencvViewer(const std::string& win)
  62. : _win(win)
  63. {
  64. _has_win = 0;
  65. //cv::namedWindow(_win);
  66. //cv::setMouseCallback(_win, _onMouseCallback, this);
  67. }
  68. ~OpencvViewer()
  69. {
  70. if (_has_win)
  71. {
  72. //cv::setMouseCallback(_win, NULL, NULL);
  73. cv::destroyWindow(_win);
  74. }
  75. }
  76. const std::string& name() const {return _win;}
  77. virtual void show(const cv::Mat& img)
  78. {
  79. _has_win = 1;
  80. _orgImg = img.clone();
  81. showImage();
  82. }
  83. virtual void onMouseCallback(cv::Mat& /*img*/, int /*event*/, const cv::Point /*pnt*/
  84. , bool& repaint) {repaint = false;}
  85. void addGraphicItem(GraphicItem* item) {
  86. _items.insert(std::make_pair(item->id(), item));}
  87. void delGraphicItem(GraphicItem* item) { _items.erase(item->id()); }
  88. private:
  89. static void _onMouseCallback(int event, int x, int y, int flags, void* ustc);
  90. void showImage();
  91. cv::Mat _orgImg;
  92. cv::Mat _showImg;
  93. int _has_win;
  94. std::string _win;
  95. std::map<int, GraphicItem*> _items;
  96. };
  97. //////////////////////////////////////////////////////////////////////////////////
  98. class DepthViewer : public OpencvViewer
  99. {
  100. public:
  101. DepthViewer(const std::string& win);
  102. virtual void show(const cv::Mat& depthImage);
  103. virtual void onMouseCallback(cv::Mat& img, int event, const cv::Point pnt
  104. , bool& repaint);
  105. float depth_scale_unit;
  106. private:
  107. cv::Mat _depth;
  108. cv::Mat _renderedDepth;
  109. DepthRender _render;
  110. GraphicStringItem _centerDepthItem;
  111. GraphicStringItem _pickedDepthItem;
  112. cv::Point _fixLoc;
  113. };
  114. #endif