Visual Servoing Platform  version 3.6.1 under development (2024-11-21)
vpDisplayOpenCV.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Image display.
32  */
33 
34 #ifndef VP_DISPLAY_OPENCV_H
35 #define VP_DISPLAY_OPENCV_H
36 
37 #include <visp3/core/vpConfig.h>
38 #include <visp3/core/vpDisplay.h>
39 
40 #if defined(HAVE_OPENCV_HIGHGUI)
41 
42 #include <functional>
43 
44 #include <visp3/core/vpImage.h>
45 #include <visp3/core/vpImageConvert.h>
46 
47 #include <opencv2/core/core.hpp>
48 #include <opencv2/highgui/highgui.hpp>
49 
50 BEGIN_VISP_NAMESPACE
51 
141 class VISP_EXPORT vpDisplayOpenCV : public vpDisplay
142 {
143 private:
144  cv::Mat m_background;
145  cv::Scalar *col;
146  cv::Scalar cvcolor;
147  int font;
148  float fontScale;
149  static std::vector<std::string> m_listTitles;
150  static unsigned int m_nbWindows;
151  int fontHeight;
152  int x_move;
153  int y_move;
154  bool move;
155  int x_lbuttondown;
156  int y_lbuttondown;
157  bool lbuttondown;
158  int x_mbuttondown;
159  int y_mbuttondown;
160  bool mbuttondown;
161  int x_rbuttondown;
162  int y_rbuttondown;
163  bool rbuttondown;
164  int x_lbuttonup;
165  int y_lbuttonup;
166  bool lbuttonup;
167  int x_mbuttonup;
168  int y_mbuttonup;
169  bool mbuttonup;
170  int x_rbuttonup;
171  int y_rbuttonup;
172  bool rbuttonup;
173 
174  // private:
175  //#ifndef DOXYGEN_SHOULD_SKIP_THIS
176  // vpDisplayOpenCV(const vpDisplayOpenCV &)
177  // : vpDisplay(), background(), col(nullptr), cvcolor(), font(cv::FONT_HERSHEY_PLAIN),
178  // fontScale(0.8f), fontHeight(10), x_move(0), y_move(0) , move(false),
179  // x_lbuttondown(0), y_lbuttondown(0), lbuttondown(false),
180  // x_mbuttondown(0), y_mbuttondown(0), mbuttondown(false),
181  // x_rbuttondown(0), y_rbuttondown(0), rbuttondown(false),
182  // x_lbuttonup(0), y_lbuttonup(0), lbuttonup(false),
183  // x_mbuttonup(0), y_mbuttonup(0), mbuttonup(false),
184  // x_rbuttonup(0), y_rbuttonup(0), rbuttonup(false)
185  // {
186  // throw vpException(vpException::functionNotImplementedError, "Not
187  // implemented!");
188  // }
189  // vpDisplayOpenCV &operator=(const vpDisplayOpenCV &){
190  // throw vpException(vpException::functionNotImplementedError, "Not
191  // implemented!"); return *this;
192  // }
193  //#endif
194 
195 public:
196  vpDisplayOpenCV();
197  vpDisplayOpenCV(int winx, int winy, const std::string &title = "");
199  vpDisplayOpenCV(vpImage<unsigned char> &I, int winx = -1, int winy = -1, const std::string &title = "",
200  vpScaleType type = SCALE_DEFAULT);
202  vpDisplayOpenCV(vpImage<vpRGBa> &I, int winx = -1, int winy = -1, const std::string &title = "",
203  vpScaleType type = SCALE_DEFAULT);
204 
205  virtual ~vpDisplayOpenCV() VP_OVERRIDE;
206 
207  void getImage(vpImage<vpRGBa> &I) VP_OVERRIDE;
208  unsigned int getScreenHeight() VP_OVERRIDE;
209  void getScreenSize(unsigned int &width, unsigned int &height) VP_OVERRIDE;
210  unsigned int getScreenWidth() VP_OVERRIDE;
211 
212  void init(vpImage<unsigned char> &I, int winx = -1, int winy = -1, const std::string &title = "") VP_OVERRIDE;
213  void init(vpImage<vpRGBa> &I, int winx = -1, int winy = -1, const std::string &title = "") VP_OVERRIDE;
214  void init(unsigned int width, unsigned int height, int winx = -1, int winy = -1, const std::string &title = "") VP_OVERRIDE;
215 
216 protected:
217  void setFont(const std::string &font) VP_OVERRIDE;
218  void setTitle(const std::string &title) VP_OVERRIDE;
219  void setWindowPosition(int winx, int winy) VP_OVERRIDE;
220 
221  void clearDisplay(const vpColor &color = vpColor::white) VP_OVERRIDE;
222 
223  void closeDisplay() VP_OVERRIDE;
224 
225  void displayArrow(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color = vpColor::white,
226  unsigned int w = 4, unsigned int h = 2, unsigned int thickness = 1) VP_OVERRIDE;
227 
228  void displayCircle(const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill = false,
229  unsigned int thickness = 1) VP_OVERRIDE;
230  void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness = 1) VP_OVERRIDE;
231  void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color,
232  unsigned int thickness = 1) VP_OVERRIDE;
233 
234  void displayImage(const vpImage<unsigned char> &I) VP_OVERRIDE;
235  void displayImage(const vpImage<vpRGBa> &I) VP_OVERRIDE;
236  void displayImage(const unsigned char *I);
237 
238  void displayImageROI(const vpImage<unsigned char> &I, const vpImagePoint &iP, unsigned int width,
239  unsigned int height) VP_OVERRIDE;
240  void displayImageROI(const vpImage<vpRGBa> &I, const vpImagePoint &iP, unsigned int width, unsigned int height) VP_OVERRIDE;
241 
242  void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness = 1) VP_OVERRIDE;
243  void displayPoint(const vpImagePoint &ip, const vpColor &color, unsigned int thickness = 1) VP_OVERRIDE;
244 
245  void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color,
246  bool fill = false, unsigned int thickness = 1) VP_OVERRIDE;
247  void displayRectangle(const vpImagePoint &topLeft, const vpImagePoint &bottomRight, const vpColor &color,
248  bool fill = false, unsigned int thickness = 1) VP_OVERRIDE;
249  void displayRectangle(const vpRect &rectangle, const vpColor &color, bool fill = false, unsigned int thickness = 1) VP_OVERRIDE;
250 
251  void displayText(const vpImagePoint &ip, const std::string &text, const vpColor &color = vpColor::green) VP_OVERRIDE;
252 
253  void flushDisplay() VP_OVERRIDE;
254  void flushDisplayROI(const vpImagePoint &iP, unsigned int width, unsigned int height) VP_OVERRIDE;
255 
256  bool getClick(bool blocking = true) VP_OVERRIDE;
257  bool getClick(vpImagePoint &ip, bool blocking = true) VP_OVERRIDE;
258  bool getClick(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking = true) VP_OVERRIDE;
259  bool getClickUp(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking = true) VP_OVERRIDE;
260 
261  bool getKeyboardEvent(bool blocking = true) VP_OVERRIDE;
262  bool getKeyboardEvent(std::string &key, bool blocking = true) VP_OVERRIDE;
263  bool getPointerMotionEvent(vpImagePoint &ip) VP_OVERRIDE;
264  bool getPointerPosition(vpImagePoint &ip) VP_OVERRIDE;
265 
266  static void on_mouse(int event, int x, int y, int flags, void *param);
267 
268  void overlay(std::function<void(cv::Mat &)> overlay_function, double opacity);
269 };
270 
271 END_VISP_NAMESPACE
272 #endif
273 #endif
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:157
static const vpColor white
Definition: vpColor.h:212
static const vpColor green
Definition: vpColor.h:220
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void displayCircle(const vpImage< unsigned char > &I, const vpImageCircle &circle, const vpColor &color, bool fill=false, unsigned int thickness=1)
static bool getKeyboardEvent(const vpImage< unsigned char > &I, bool blocking=true)
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
static void getImage(const vpImage< unsigned char > &Is, vpImage< vpRGBa > &Id)
Definition: vpDisplay.cpp:140
static bool getClickUp(const vpImage< unsigned char > &I, vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking=true)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void setTitle(const vpImage< unsigned char > &I, const std::string &windowtitle)
static void displayArrow(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color=vpColor::white, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)
static void displayPoint(const vpImage< unsigned char > &I, const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
static void setFont(const vpImage< unsigned char > &I, const std::string &font)
static void displayDotLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
static bool getPointerPosition(const vpImage< unsigned char > &I, vpImagePoint &ip)
static bool getPointerMotionEvent(const vpImage< unsigned char > &I, vpImagePoint &ip)
static void displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void setWindowPosition(const vpImage< unsigned char > &I, int winx, int winy)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
Defines a rectangle in the plane.
Definition: vpRect.h:79