xvWidgets
ImagePanel.hpp
1 #pragma once
2 
7 #include <wx/wx.h>
8 #include <wx/panel.h>
9 #include <wx/sizer.h>
10 
11 #include <opencv2/core.hpp>
12 #include <opencv2/highgui/highgui.hpp>
13 
14 #include <chrono>
15 #include <list>
16 #include <functional>
17 #include <mutex>
18 
19 #include "Image.hpp"
20 #include "Point.hpp"
21 
22 namespace xv{
23 
24  class VideoCapture;
25  class Widget;
26  class Image;
27 
32 class ImagePanel : public wxPanel
33 {
34  friend Widget;
35 public:
36 
38  ImagePanel();
39 
41  ImagePanel(wxWindow * parent,
42  wxWindowID id = wxID_ANY,
43  const wxPoint &pos = wxDefaultPosition,
44  const wxSize &size = wxDefaultSize,
45  long style = wxTAB_TRAVERSAL,
46  const wxString &name = wxPanelNameStr
47  );
48 
49 
50 #pragma region operators
51 
53  void operator << (Widget &widget);
54 
56  void operator >> (Widget &widget);
57 
59  operator cv::_InputOutputArray() const { return m_image.m_cvMat; }
60 
62  operator cv::Mat() const { return m_image.m_cvMat; }
63 
65  operator cv::Mat&() { return m_image.m_cvMat; }
66 
68  void operator<<(const cv::Mat&);
69 #pragma endregion operators
70 
72  void Refresh(bool eraseBackground = false, const wxRect *rect = NULL);
73 
75  float getScale(){ return m_scale; };
76 
77 private:
78 
79 #if defined(wxUSE_GUI)
80  void paintEvent(wxPaintEvent&);
81  void sizeEvent(wxSizeEvent&);
82  void onEraseBackground(wxEraseEvent&);
83 #endif
84 
85  //the ms threshold since last rendering avoid processing transformation that are not perceptible
86  void render(int ms=16);
88  std::function<void(cv::Mat &)>
89  m_preProcessCallback = [](cv::Mat &){},
90  m_postProcessCallback = [](cv::Mat &){};
91 
92  void onMouseMove(wxMouseEvent& evt);
93  void onMouseDown(wxMouseEvent& evt);
94  void onMouseUp(wxMouseEvent& evt);
95 
96  bool containsWidget(Widget*);
97 
98  //std::list<Widget*> m_widgets;
99  std::chrono::steady_clock::time_point m_lastPaintTime;
100  std::mutex m_mutex;
101  Image m_image;
102  wxSizer* m_sizer;
103  inline void createBitmap();
104 
105  friend void operator>>(cv::VideoCapture &videoCapture, ImagePanel &imagePanel){
106  imagePanel.m_mutex.lock();
107  videoCapture >> imagePanel.m_image.m_cvMat; // to-do: refactor
108  imagePanel.m_mutex.unlock();
109  imagePanel.render();
110  }
111 
113  friend void operator<<(ImagePanel &imagePanel, cv::Mat& mat){
114  imagePanel.m_mutex.lock();
115  imagePanel.m_image.m_cvMat = mat.clone(); // to-do: refactor
116  imagePanel.m_mutex.unlock();
117  imagePanel.render();
118  };
119 
120  double m_scale = 1;
121  int m_hBorder = 0;
122  int m_vBorder=0;
123 
124  DECLARE_EVENT_TABLE()
125 };
126 
127 }
ImagePanel()
Default constructor.
void operator<<(Widget &widget)
Simple rendering/display of widget without user input.
void Refresh(bool eraseBackground=false, const wxRect *rect=NULL)
Redraw an image optionally erasing background first or a limited area.
void operator>>(Widget &widget)
widgets for user input
Class used to display images and as container of widgets.
Definition: ImagePanel.hpp:32
cv::Mat m_cvMat
to-do: refactor
Definition: Image.hpp:59
Definition: Angle.hpp:8
float getScale()
The scale ratio of the image in relation to the original data.
Definition: ImagePanel.hpp:75
Base class for all Widgets.
Definition: Widget.hpp:40
friend void operator<<(ImagePanel &imagePanel, cv::Mat &mat)
use the xv::Image to display a cv::Mat
Definition: ImagePanel.hpp:113