Visual Servoing Platform  version 3.0.0
vpDisplayOpenCV.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
34  * Christophe Collewet
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
40 
46 #include <visp3/core/vpConfig.h>
47 
48 #if defined(VISP_HAVE_OPENCV)
49 
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <iostream>
53 #include <cmath> // std::fabs
54 #include <limits> // numeric_limits
55 
56 // Display stuff
57 #include <visp3/core/vpDisplay.h>
58 #include <visp3/gui/vpDisplayOpenCV.h>
59 #include <visp3/core/vpMath.h>
60 #include <visp3/core/vpImageTools.h>
61 
62 //debug / exception
63 #include <visp3/core/vpDebug.h>
64 #include <visp3/core/vpDisplayException.h>
65 
66 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
67 
68 # include <opencv2/imgproc/imgproc.hpp>
69 # include <opencv2/core/core_c.h> // for CV_FILLED versus cv::FILLED
70 
71 # ifndef CV_RGB
72 # define CV_RGB( r, g, b ) cv::Scalar( (b), (g), (r), 0 )
73 # endif
74 #endif
75 
76 std::vector<std::string> vpDisplayOpenCV::m_listTitles = std::vector<std::string>();
77 unsigned int vpDisplayOpenCV::m_nbWindows = 0;
78 
90  int x,
91  int y,
92  const char *title)
93  :
94  vpDisplay(),
95  #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
96  background(NULL), col(NULL), cvcolor(), font(NULL),
97  #else
98  background(), col(NULL), cvcolor(), font(cv::FONT_HERSHEY_PLAIN), fontScale(0.8f),
99  #endif
100  fontHeight(10), x_move(0), y_move(0) , move(false),
101  x_lbuttondown(0), y_lbuttondown(0), lbuttondown(false),
102  x_mbuttondown(0), y_mbuttondown(0), mbuttondown(false),
103  x_rbuttondown(0), y_rbuttondown(0), rbuttondown(false),
104  x_lbuttonup(0), y_lbuttonup(0), lbuttonup(false),
105  x_mbuttonup(0), y_mbuttonup(0), mbuttonup(false),
106  x_rbuttonup(0), y_rbuttonup(0), rbuttonup(false)
107 {
108  init(I, x, y, title) ;
109 }
110 
111 
121  int x,
122  int y,
123  const char *title)
124  :
125  #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
126  background(NULL), col(NULL), cvcolor(), font(NULL),
127  #else
128  background(), col(NULL), cvcolor(), font(cv::FONT_HERSHEY_PLAIN), fontScale(0.8f),
129  #endif
130  fontHeight(10), x_move(0), y_move(0) , move(false),
131  x_lbuttondown(0), y_lbuttondown(0), lbuttondown(false),
132  x_mbuttondown(0), y_mbuttondown(0), mbuttondown(false),
133  x_rbuttondown(0), y_rbuttondown(0), rbuttondown(false),
134  x_lbuttonup(0), y_lbuttonup(0), lbuttonup(false),
135  x_mbuttonup(0), y_mbuttonup(0), mbuttonup(false),
136  x_rbuttonup(0), y_rbuttonup(0), rbuttonup(false)
137 {
138  init(I, x, y, title) ;
139 }
140 
163 vpDisplayOpenCV::vpDisplayOpenCV ( int x, int y, const char *title )
164  :
165  #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
166  background(NULL), col(NULL), cvcolor(), font(NULL),
167  #else
168  background(), col(NULL), cvcolor(), font(cv::FONT_HERSHEY_PLAIN), fontScale(0.8f),
169  #endif
170  fontHeight(10), x_move(0), y_move(0) , move(false),
171  x_lbuttondown(0), y_lbuttondown(0), lbuttondown(false),
172  x_mbuttondown(0), y_mbuttondown(0), mbuttondown(false),
173  x_rbuttondown(0), y_rbuttondown(0), rbuttondown(false),
174  x_lbuttonup(0), y_lbuttonup(0), lbuttonup(false),
175  x_mbuttonup(0), y_mbuttonup(0), mbuttonup(false),
176  x_rbuttonup(0), y_rbuttonup(0), rbuttonup(false)
177 {
178  windowXPosition = x;
179  windowYPosition = y;
180 
181  if(title != NULL){
182  title_ = std::string(title);
183  }
184  else{
185  std::ostringstream s;
186  s << m_nbWindows++;
187  title_ = std::string("Window ") + s.str();
188  }
189 
190  bool isInList;
191  do{
192  isInList = false;
193  for(size_t i = 0 ; i < m_listTitles.size() ; i++){
194  if(m_listTitles[i] == title_){
195  std::ostringstream s;
196  s << m_nbWindows++;
197  title_ = std::string("Window ") + s.str();
198  isInList = true;
199  break;
200  }
201  }
202  }
203  while(isInList);
204 
205  m_listTitles.push_back(title_);
206 }
207 
228  :
229  #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
230  background(NULL), col(NULL), cvcolor(), font(NULL),
231  #else
232  background(), col(NULL), cvcolor(), font(cv::FONT_HERSHEY_PLAIN), fontScale(0.8f),
233  #endif
234  fontHeight(10), x_move(0), y_move(0) , move(false),
235  x_lbuttondown(0), y_lbuttondown(0), lbuttondown(false),
236  x_mbuttondown(0), y_mbuttondown(0), mbuttondown(false),
237  x_rbuttondown(0), y_rbuttondown(0), rbuttondown(false),
238  x_lbuttonup(0), y_lbuttonup(0), lbuttonup(false),
239  x_mbuttonup(0), y_mbuttonup(0), mbuttonup(false),
240  x_rbuttonup(0), y_rbuttonup(0), rbuttonup(false)
241 {
242 }
243 
248 {
249  closeDisplay() ;
250 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
251  cvReleaseImage(&background);
252 #endif
253 }
254 
263 void
265  int x,
266  int y,
267  const char *title)
268 {
269 
270  if ((I.getHeight() == 0) || (I.getWidth()==0))
271  {
272  vpERROR_TRACE("Image not initialized " ) ;
274  "Image not initialized")) ;
275  }
276  init (I.getWidth(), I.getHeight(), x, y, title) ;
277  I.display = this ;
279 }
280 
290 void
292  int x,
293  int y,
294  const char *title)
295 {
296  if ((I.getHeight() == 0) || (I.getWidth()==0))
297  {
298  vpERROR_TRACE("Image not initialized " ) ;
300  "Image not initialized")) ;
301  }
302 
303  init (I.getWidth(), I.getHeight(), x, y, title) ;
304  I.display = this ;
306 }
307 
318 void
319 vpDisplayOpenCV::init(unsigned int w, unsigned int h,
320  int x, int y,
321  const char *title)
322 {
323  this->width = w;
324  this->height = h;
325 
326  if (x != -1)
327  this->windowXPosition = x;
328  if (y != -1)
329  this->windowYPosition = y;
330 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
331  int flags = CV_WINDOW_AUTOSIZE;
332 #else
333  int flags = cv::WINDOW_AUTOSIZE;
334 #endif
335 
336  if(title_.empty()){
337  if(title != NULL){
338  title_ = std::string(title);
339  }
340  else{
341 
342  std::ostringstream s;
343  s << m_nbWindows++;
344  title_ = std::string("Window ") + s.str();
345  }
346 
347  bool isInList;
348  do{
349  isInList = false;
350  for(size_t i = 0 ; i < m_listTitles.size() ; i++){
351  if(m_listTitles[i] == title_){
352  std::ostringstream s;
353  s << m_nbWindows++;
354  title_ = std::string("Window ") + s.str();
355  isInList = true;
356  break;
357  }
358  }
359  }
360  while(isInList);
361 
362  m_listTitles.push_back(title_);
363  }
364 
365  /* Create the window*/
366 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
367  if (cvNamedWindow( this->title_.c_str(), flags ) < 0) {
368  vpERROR_TRACE("OpenCV was not built with a display device");
370  "OpenCV was not built with a display device")) ;
371  }
372 #else
373  cv::namedWindow( this->title_, flags );
374 #endif
375 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
376  cvMoveWindow( this->title_.c_str(), this->windowXPosition, this->windowYPosition );
377 #else
378  cv::moveWindow( this->title_.c_str(), this->windowXPosition, this->windowYPosition );
379 #endif
380  move = false;
381  lbuttondown = false;
382  mbuttondown = false;
383  rbuttondown = false;
384  lbuttonup = false;
385  mbuttonup = false;
386  rbuttonup = false;
387 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
388  cvSetMouseCallback( this->title_.c_str(), on_mouse, this );
389  col = new CvScalar[vpColor::id_unknown] ;
390 #else
391  cv::setMouseCallback( this->title_, on_mouse, this );
392  col = new cv::Scalar[vpColor::id_unknown] ;
393 #endif
394 
395  /* Create color */
396  vpColor pcolor; // Predefined colors
397  pcolor = vpColor::lightBlue;
398  col[vpColor::id_lightBlue] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
399  pcolor = vpColor::blue;
400  col[vpColor::id_blue] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
401  pcolor = vpColor::darkBlue;
402  col[vpColor::id_darkBlue] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
403  pcolor = vpColor::lightRed;
404  col[vpColor::id_lightRed] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
405  pcolor = vpColor::red;
406  col[vpColor::id_red] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
407  pcolor = vpColor::darkRed;
408  col[vpColor::id_darkRed] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
409  pcolor = vpColor::lightGreen;
410  col[vpColor::id_lightGreen] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
411  pcolor = vpColor::green;
412  col[vpColor::id_green] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
413  pcolor = vpColor::darkGreen;
414  col[vpColor::id_darkGreen] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
415  pcolor = vpColor::yellow;
416  col[vpColor::id_yellow] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
417  pcolor = vpColor::cyan;
418  col[vpColor::id_cyan] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
419  pcolor = vpColor::orange;
420  col[vpColor::id_orange] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
421  pcolor = vpColor::purple;
422  col[vpColor::id_purple] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
423  pcolor = vpColor::white;
424  col[vpColor::id_white] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
425  pcolor = vpColor::black;
426  col[vpColor::id_black] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
427  pcolor = vpColor::lightGray;
428  col[vpColor::id_lightGray] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
429  pcolor = vpColor::gray;
430  col[vpColor::id_gray] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
431  pcolor = vpColor::darkGray;
432  col[vpColor::id_darkGray] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
433 
434 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
435  font = new CvFont;
436  cvInitFont( font, CV_FONT_HERSHEY_PLAIN, 0.70f,0.70f);
437  CvSize fontSize;
438  int baseline;
439  cvGetTextSize( "A", font, &fontSize, &baseline );
440 #else
441  int thickness = 1;
442  cv::Size fontSize;
443  int baseline;
444  fontSize = cv::getTextSize( "A", font, fontScale, thickness, &baseline );
445 #endif
446 
447  fontHeight = fontSize.height + baseline;
449 }
450 
451 
467 void
468 vpDisplayOpenCV::setFont(const char * /* font */)
469 {
470  vpERROR_TRACE("Not yet implemented" ) ;
471 }
472 
480 void
481 vpDisplayOpenCV::setTitle(const char * /* title */)
482 {
483 // static bool warn_displayed = false;
484 // if (! warn_displayed) {
485 // vpTRACE("Not implemented");
486 // warn_displayed = true;
487 // }
488 }
489 
490 
499 void vpDisplayOpenCV::setWindowPosition(int winx, int winy)
500 {
502  this->windowXPosition = winx;
503  this->windowYPosition = winy;
504 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
505  cvMoveWindow( this->title_.c_str(), winx, winy );
506 #else
507  cv::moveWindow( this->title_.c_str(), winx, winy );
508 #endif
509  }
510  else
511  {
512  vpERROR_TRACE("OpenCV not initialized " ) ;
514  "OpenCV not initialized")) ;
515  }
516 }
529 {
531  {
532  vpImage<vpRGBa> Ic;
534  vpImageConvert::convert(Ic,background);
535  /* Copie de l'image dans le pixmap fond */
536  width = I.getWidth();
537  height = I.getHeight();
538  /* Le pixmap background devient le fond de la zone de dessin */
539  }
540  else
541  {
542  vpERROR_TRACE("openCV not initialized " ) ;
544  "OpenCV not initialized")) ;
545  }
546 }
547 
564  const unsigned int w, const unsigned int h )
565 {
567  {
569  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),h,w,Itemp);
570  vpImage<vpRGBa> Ic;
571  vpImageConvert::convert(Itemp,Ic);
572 
573 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
574  int depth = 8;
575  int channels = 3;
576  CvSize size = cvSize((int)this->width, (int)this->height);
577  if (background != NULL){
578  if(background->nChannels != channels || background->depth != depth
579  || background->height != (int) I.getHeight() || background->width != (int) I.getWidth()){
580  if(background->nChannels != 0) cvReleaseImage(&background);
581  background = cvCreateImage( size, depth, channels );
582  }
583  }
584  else background = cvCreateImage( size, depth, channels );
585  IplImage* Ip = NULL;
586  vpImageConvert::convert(Ic, Ip);
587  unsigned char * input = (unsigned char*)Ip->imageData;
588  unsigned char * output = (unsigned char*)background->imageData;
589 
590  unsigned int iwidth = Ic.getWidth();
591 
592  output = output + (int)(iP.get_i()*3*this->width+ iP.get_j()*3);
593 
594  unsigned int i = 0;
595  while (i < h)
596  {
597  unsigned int j = 0;
598  while (j < w)
599  {
600  *(output+3*j) = *(input+j*3);
601  *(output+3*j+1) = *(input+j*3+1);
602  *(output+3*j+2) = *(input+j*3+2);
603  j++;
604  }
605  input = input + 3*iwidth;
606  output = output + 3*this->width;
607  i++;
608  }
609 
610  cvReleaseImage(&Ip);
611 #else
612  int depth = CV_8U;
613  int channels = 3;
614  cv::Size size((int)this->width, (int)this->height);
615  if(background.channels() != channels || background.depth() != depth
616  || background.rows != (int) I.getHeight() || background.cols != (int) I.getWidth()){
617  background = cv::Mat( size, CV_MAKETYPE(depth, channels) );
618  }
619 
620  cv::Mat Ip;
621  vpImageConvert::convert(Ic, Ip);
622 
623  unsigned char * input = (unsigned char*)Ip.data;
624  unsigned char * output = (unsigned char*)background.data;
625 
626  unsigned int iwidth = Ic.getWidth();
627 
628  output = output + (int)(iP.get_i()*3*this->width+ iP.get_j()*3);
629 
630  unsigned int i = 0;
631  while (i < h)
632  {
633  unsigned int j = 0;
634  while (j < w)
635  {
636  *(output+3*j) = *(input+j*3);
637  *(output+3*j+1) = *(input+j*3+1);
638  *(output+3*j+2) = *(input+j*3+2);
639  j++;
640  }
641  input = input + 3*iwidth;
642  output = output + 3*this->width;
643  i++;
644  }
645 #endif
646  }
647  else
648  {
649  vpERROR_TRACE("openCV not initialized " ) ;
651  "OpenCV not initialized")) ;
652  }
653 }
654 
655 
668 {
669 
671  {
672  /* Copie de l'image dans le pixmap fond */
673 
674  vpImageConvert::convert(I,background);
675  /* Copie de l'image dans le pixmap fond */
676  width = I.getWidth();
677  height = I.getHeight();
678  }
679  else
680  {
681  vpERROR_TRACE("OpenCV not initialized " ) ;
683  "OpenCV not initialized")) ;
684  }
685 }
686 
703  const unsigned int w, const unsigned int h )
704 {
706  {
707  vpImage<vpRGBa> Ic;
708  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),h,w,Ic);
709 
710 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
711  CvSize size = cvSize((int)this->width, (int)this->height);
712  int depth = 8;
713  int channels = 3;
714  if (background != NULL){
715  if(background->nChannels != channels || background->depth != depth
716  || background->height != (int) I.getHeight() || background->width != (int) I.getWidth()){
717  if(background->nChannels != 0) cvReleaseImage(&background);
718  background = cvCreateImage( size, depth, channels );
719  }
720  }
721  else background = cvCreateImage( size, depth, channels );
722 
723  IplImage* Ip = NULL;
724  vpImageConvert::convert(Ic, Ip);
725 
726  unsigned char * input = (unsigned char*)Ip->imageData;
727  unsigned char * output = (unsigned char*)background->imageData;
728 
729  unsigned int iwidth = Ic.getWidth();
730 
731  output = output + (int)(iP.get_i()*3*this->width+ iP.get_j()*3);
732 
733  unsigned int i = 0;
734  while (i < h)
735  {
736  unsigned int j = 0;
737  while (j < w)
738  {
739  *(output+3*j) = *(input+j*3);
740  *(output+3*j+1) = *(input+j*3+1);
741  *(output+3*j+2) = *(input+j*3+2);
742  j++;
743  }
744  input = input + 3*iwidth;
745  output = output + 3*this->width;
746  i++;
747  }
748 
749  cvReleaseImage(&Ip);
750 #else
751  int depth = CV_8U;
752  int channels = 3;
753  cv::Size size((int)this->width, (int)this->height);
754  if(background.channels() != channels || background.depth() != depth
755  || background.rows != (int) I.getHeight() || background.cols != (int) I.getWidth()){
756  background = cv::Mat( size, CV_MAKETYPE(depth, channels) );
757  }
758  cv::Mat Ip;
759  vpImageConvert::convert(Ic, Ip);
760 
761  unsigned char * input = (unsigned char*)Ip.data;
762  unsigned char * output = (unsigned char*)background.data;
763 
764  unsigned int iwidth = Ic.getWidth();
765 
766  output = output + (int)(iP.get_i()*3*this->width+ iP.get_j()*3);
767 
768  unsigned int i = 0;
769  while (i < h)
770  {
771  unsigned int j = 0;
772  while (j < w)
773  {
774  *(output+3*j) = *(input+j*3);
775  *(output+3*j+1) = *(input+j*3+1);
776  *(output+3*j+2) = *(input+j*3+2);
777  j++;
778  }
779  input = input + 3*iwidth;
780  output = output + 3*this->width;
781  i++;
782  }
783 #endif
784  }
785  else
786  {
787  vpERROR_TRACE("openCV not initialized " ) ;
789  "OpenCV not initialized")) ;
790  }
791 }
792 
793 
799 void vpDisplayOpenCV::displayImage(const unsigned char * /* I */)
800 {
801  vpTRACE(" not implemented ") ;
802 }
803 
812 {
813  if (col != NULL)
814  {
815  delete [] col ; col = NULL ;
816  }
817 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
818  if (font != NULL)
819  {
820  delete font ;
821  font = NULL ;
822  }
823 #endif
825 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
826  cvDestroyWindow( this->title_.c_str() );
827 #else
828  cv::destroyWindow( this->title_ );
829 #endif
830 
831  for(size_t i = 0 ; i < m_listTitles.size() ; i++){
832  if(title_ == m_listTitles[i]){
833  m_listTitles.erase(m_listTitles.begin()+(long int)i);
834  break;
835  }
836  }
837 
838  title_.clear();
839 
841  }
842 }
843 
844 
851 {
853  {
854 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
855  cvShowImage(this->title_.c_str(), background );
856  cvWaitKey(5);
857 #else
858  cv::imshow(this->title_, background );
859  cv::waitKey(5);
860 #endif
861  }
862  else
863  {
864  vpERROR_TRACE("OpenCV not initialized " ) ;
866  "OpenCV not initialized")) ;
867  }
868 }
869 
875 void vpDisplayOpenCV::flushDisplayROI(const vpImagePoint &/*iP*/, const unsigned int /*width*/, const unsigned int /*height*/)
876 {
878  {
879 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
880  cvShowImage(this->title_.c_str(), background );
881  cvWaitKey(5);
882 #else
883  cv::imshow(this->title_.c_str(), background );
884  cv::waitKey(5);
885 #endif
886  }
887  else
888  {
889  vpERROR_TRACE("OpenCV not initialized " ) ;
891  "OpenCV not initialized")) ;
892  }
893 }
894 
895 
899 void vpDisplayOpenCV::clearDisplay(const vpColor & /* color */)
900 {
901  static bool warn_displayed = false;
902  if (! warn_displayed) {
903  vpTRACE("Not implemented");
904  warn_displayed = true;
905  }
906 }
907 
916  const vpImagePoint &ip2,
917  const vpColor &color,
918  unsigned int w, unsigned int h,
919  unsigned int thickness)
920 {
922  {
923  try{
924  double a = ip2.get_i() - ip1.get_i() ;
925  double b = ip2.get_j() - ip1.get_j() ;
926  double lg = sqrt(vpMath::sqr(a)+vpMath::sqr(b)) ;
927 
928  //if ((a==0)&&(b==0))
929  if ((std::fabs(a) <= std::numeric_limits<double>::epsilon())
930  &&(std::fabs(b)<= std::numeric_limits<double>::epsilon()))
931  {
932  // DisplayCrossLarge(i1,j1,3,col) ;
933  }
934  else
935  {
936  a /= lg ;
937  b /= lg ;
938 
939  vpImagePoint ip3;
940  ip3.set_i(ip2.get_i() - w*a);
941  ip3.set_j(ip2.get_j() - w*b);
942 
943  vpImagePoint ip4;
944  ip4.set_i( ip3.get_i() - b*h );
945  ip4.set_j( ip3.get_j() + a*h );
946 
947  if (lg > 2*vpImagePoint::distance(ip2, ip4) )
948  displayLine ( ip2, ip4, color, thickness ) ;
949 
950  ip4.set_i( ip3.get_i() + b*h );
951  ip4.set_j( ip3.get_j() - a*h );
952 
953  if (lg > 2*vpImagePoint::distance(ip2, ip4) )
954  displayLine ( ip2, ip4, color, thickness ) ;
955 
956  displayLine ( ip1, ip2, color, thickness ) ;
957  }
958  }
959  catch (...)
960  {
961  vpERROR_TRACE("Error caught") ;
962  throw ;
963  }
964  }
965  else
966  {
967  vpERROR_TRACE("OpenCV not initialized " ) ;
969  "OpenCV not initialized")) ;
970  }
971 }
972 
985  const char *text,
986  const vpColor &color )
987 {
989  {
990  if (color.id < vpColor::id_unknown) {
991 #if VISP_HAVE_OPENCV_VERSION < 0x020408
992  cvPutText( background, text,
993  cvPoint( vpMath::round( ip.get_u() ),
994  vpMath::round( ip.get_v()+fontHeight ) ),
995  font, col[color.id] );
996 #else
997  cv::putText( background, text,
998  cv::Point( vpMath::round( ip.get_u() ),
999  vpMath::round( ip.get_v()+fontHeight ) ),
1000  font, fontScale, col[color.id] );
1001 #endif
1002  }
1003  else {
1004  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1005 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1006  cvPutText( background, text,
1007  cvPoint( vpMath::round( ip.get_u() ),
1008  vpMath::round( ip.get_v()+fontHeight ) ),
1009  font, cvcolor );
1010 #else
1011  cv::putText( background, text,
1012  cv::Point( vpMath::round( ip.get_u() ),
1013  vpMath::round( ip.get_v()+fontHeight ) ),
1014  font, fontScale, cvcolor );
1015 #endif
1016  }
1017  }
1018  else
1019  {
1020  vpERROR_TRACE("OpenCV not initialized " ) ;
1022  "OpenCV not initialized")) ;
1023  }
1024 }
1035  unsigned int radius,
1036  const vpColor &color,
1037  bool fill ,
1038  unsigned int thickness)
1039 {
1041  {
1042  if (fill == false) {
1043  if (color.id < vpColor::id_unknown) {
1044 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1045  cvCircle( background,
1046  cvPoint( vpMath::round( center.get_u() ),
1047  vpMath::round( center.get_v() ) ),
1048  (int)radius, col[color.id], (int)thickness);
1049 #else
1050  cv::circle( background,
1051  cv::Point( vpMath::round( center.get_u() ),
1052  vpMath::round( center.get_v() ) ),
1053  (int)radius, col[color.id], (int)thickness);
1054 #endif
1055  }
1056  else {
1057  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1058 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1059  cvCircle( background,
1060  cvPoint( vpMath::round( center.get_u() ),
1061  vpMath::round( center.get_v() ) ),
1062  (int)radius, cvcolor, (int)thickness);
1063 #else
1064  cv::circle( background,
1065  cv::Point( vpMath::round( center.get_u() ),
1066  vpMath::round( center.get_v() ) ),
1067  (int)radius, cvcolor, (int)thickness);
1068 #endif
1069  }
1070  }
1071  else {
1072 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
1073  int filled = cv::FILLED;
1074 #else
1075  int filled = CV_FILLED;
1076 #endif
1077  if (color.id < vpColor::id_unknown) {
1078 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1079  cvCircle( background,
1080  cvPoint( vpMath::round( center.get_u() ),
1081  vpMath::round( center.get_v() ) ),
1082  (int)radius, col[color.id], filled);
1083 #else
1084  cv::circle( background,
1085  cv::Point( vpMath::round( center.get_u() ),
1086  vpMath::round( center.get_v() ) ),
1087  (int)radius, col[color.id], filled);
1088 #endif
1089  }
1090  else {
1091  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1092 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1093  cvCircle( background,
1094  cvPoint( vpMath::round( center.get_u() ),
1095  vpMath::round( center.get_v() ) ),
1096  (int)radius, cvcolor, filled);
1097 #else
1098  cv::circle( background,
1099  cv::Point( vpMath::round( center.get_u() ),
1100  vpMath::round( center.get_v() ) ),
1101  (int)radius, cvcolor, filled);
1102 #endif
1103  }
1104  }
1105  }
1106  else
1107  {
1108  vpERROR_TRACE("OpenCV not initialized " ) ;
1110  "OpenCV not initialized")) ;
1111  }
1112 }
1113 
1121 void
1123  unsigned int size,
1124  const vpColor &color,
1125  unsigned int thickness)
1126 {
1128  {
1129  vpImagePoint top,bottom,left,right;
1130  top.set_i(ip.get_i()-size/2);
1131  top.set_j(ip.get_j());
1132  bottom.set_i(ip.get_i()+size/2);
1133  bottom.set_j(ip.get_j());
1134  left.set_i(ip.get_i());
1135  left.set_j(ip.get_j()-size/2);
1136  right.set_i(ip.get_i());
1137  right.set_j(ip.get_j()+size/2);
1138  try{
1139  displayLine(top, bottom, color, thickness) ;
1140  displayLine(left, right, color, thickness) ;
1141  }
1142  catch (...)
1143  {
1144  vpERROR_TRACE("Error caught") ;
1145  throw ;
1146  }
1147  }
1148 
1149  else
1150  {
1151  vpERROR_TRACE("OpenCV not initialized " ) ;
1153  "OpenCV not initialized")) ;
1154  }
1155 
1156 }
1157 
1168 void
1170  const vpImagePoint &ip2,
1171  const vpColor &color,
1172  unsigned int thickness)
1173 {
1174 
1176  {
1177  //vpTRACE("Dot lines are not yet implemented");
1178  if (color.id < vpColor::id_unknown) {
1179 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1180  cvLine( background,
1181  cvPoint( vpMath::round( ip1.get_u() ),
1182  vpMath::round( ip1.get_v() ) ),
1183  cvPoint( vpMath::round( ip2.get_u() ),
1184  vpMath::round( ip2.get_v() ) ),
1185  col[color.id], (int) thickness);
1186 #else
1187  cv::line( background,
1188  cv::Point( vpMath::round( ip1.get_u() ),
1189  vpMath::round( ip1.get_v() ) ),
1190  cv::Point( vpMath::round( ip2.get_u() ),
1191  vpMath::round( ip2.get_v() ) ),
1192  col[color.id], (int) thickness);
1193 #endif
1194  }
1195  else {
1196  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1197 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1198  cvLine( background,
1199  cvPoint( vpMath::round( ip1.get_u() ),
1200  vpMath::round( ip1.get_v() ) ),
1201  cvPoint( vpMath::round( ip2.get_u() ),
1202  vpMath::round( ip2.get_v() ) ),
1203  cvcolor, (int) thickness);
1204 #else
1205  cv::line( background,
1206  cv::Point( vpMath::round( ip1.get_u() ),
1207  vpMath::round( ip1.get_v() ) ),
1208  cv::Point( vpMath::round( ip2.get_u() ),
1209  vpMath::round( ip2.get_v() ) ),
1210  cvcolor, (int) thickness);
1211 #endif
1212  }
1213  }
1214  else
1215  {
1216  vpERROR_TRACE("OpenCV not initialized " ) ;
1218  "OpenCV not initialized")) ;
1219  }
1220 }
1221 
1222 
1229 void
1231  const vpImagePoint &ip2,
1232  const vpColor &color,
1233  unsigned int thickness)
1234 {
1236  {
1237  if (color.id < vpColor::id_unknown) {
1238 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1239  cvLine( background,
1240  cvPoint( vpMath::round( ip1.get_u() ),
1241  vpMath::round( ip1.get_v() ) ),
1242  cvPoint( vpMath::round( ip2.get_u() ),
1243  vpMath::round( ip2.get_v() ) ),
1244  col[color.id], (int) thickness);
1245 #else
1246  cv::line( background,
1247  cv::Point( vpMath::round( ip1.get_u() ),
1248  vpMath::round( ip1.get_v() ) ),
1249  cv::Point( vpMath::round( ip2.get_u() ),
1250  vpMath::round( ip2.get_v() ) ),
1251  col[color.id], (int) thickness);
1252 #endif
1253  }
1254  else {
1255  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1256 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1257  cvLine( background,
1258  cvPoint( vpMath::round( ip1.get_u() ),
1259  vpMath::round( ip1.get_v() ) ),
1260  cvPoint( vpMath::round( ip2.get_u() ),
1261  vpMath::round( ip2.get_v() ) ),
1262  cvcolor, (int) thickness);
1263 #else
1264  cv::line( background,
1265  cv::Point( vpMath::round( ip1.get_u() ),
1266  vpMath::round( ip1.get_v() ) ),
1267  cv::Point( vpMath::round( ip2.get_u() ),
1268  vpMath::round( ip2.get_v() ) ),
1269  cvcolor, (int) thickness);
1270 #endif
1271  }
1272  }
1273  else
1274  {
1275  vpERROR_TRACE("OpenCV not initialized " ) ;
1277  "OpenCV not initialized")) ;
1278  }
1279 }
1280 
1287  const vpColor &color)
1288 {
1290  {
1291  displayLine(ip,ip,color,1);
1292 // if (color.id < vpColor::id_unknown) {
1293 // ((uchar*)(background->imageData
1294 // + background->widthStep*vpMath::round( ip.get_i() )))
1295 // [vpMath::round( ip.get_j()*3 )] = (uchar)col[color.id].val[0];
1296 //
1297 // ((uchar*)(background->imageData
1298 // + background->widthStep*vpMath::round( ip.get_i() )))
1299 // [vpMath::round( ip.get_j()*3+1 )] = (uchar)col[color.id].val[1];
1300 //
1301 // ((uchar*)(background->imageData
1302 // + background->widthStep*vpMath::round( ip.get_i() )))
1303 // [vpMath::round( ip.get_j()*3+2 )] = (uchar)col[color.id].val[2];
1304 // }
1305 // else {
1306 // cvcolor = CV_RGB(color.R, color.G, color.B) ;
1307 // ((uchar*)(background->imageData
1308 // + background->widthStep*vpMath::round( ip.get_i() )))
1309 // [vpMath::round( ip.get_j()*3 )] = (uchar)cvcolor.val[0];
1310 //
1311 // ((uchar*)(background->imageData
1312 // + background->widthStep*vpMath::round( ip.get_i() )))
1313 // [vpMath::round( ip.get_j()*3+1 )] = (uchar)cvcolor.val[1];
1314 //
1315 // ((uchar*)(background->imageData
1316 // + background->widthStep*vpMath::round( ip.get_i() )))
1317 // [vpMath::round( ip.get_j()*3+2 )] = (uchar)cvcolor.val[2];
1318 //
1319 // }
1320  }
1321  else
1322  {
1323  vpERROR_TRACE("OpenCV not initialized " ) ;
1325  "OpenCV not initialized")) ;
1326  }
1327 }
1328 
1342 void
1344  unsigned int w, unsigned int h,
1345  const vpColor &color, bool fill,
1346  unsigned int thickness)
1347 {
1349  {
1350  if (fill == false) {
1351  if (color.id < vpColor::id_unknown) {
1352 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1353  cvRectangle( background,
1354  cvPoint( vpMath::round( topLeft.get_u() ),
1355  vpMath::round( topLeft.get_v() ) ),
1356  cvPoint( vpMath::round( topLeft.get_u()+w ),
1357  vpMath::round( topLeft.get_v()+h ) ),
1358  col[color.id], (int)thickness);
1359 #else
1360  cv::rectangle( background,
1361  cv::Point( vpMath::round( topLeft.get_u() ),
1362  vpMath::round( topLeft.get_v() ) ),
1363  cv::Point( vpMath::round( topLeft.get_u()+w ),
1364  vpMath::round( topLeft.get_v()+h ) ),
1365  col[color.id], (int)thickness);
1366 #endif
1367  }
1368  else {
1369  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1370 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1371  cvRectangle( background,
1372  cvPoint( vpMath::round( topLeft.get_u() ),
1373  vpMath::round( topLeft.get_v() ) ),
1374  cvPoint( vpMath::round( topLeft.get_u()+w ),
1375  vpMath::round( topLeft.get_v()+h ) ),
1376  cvcolor, (int)thickness);
1377 #else
1378  cv::rectangle( background,
1379  cv::Point( vpMath::round( topLeft.get_u() ),
1380  vpMath::round( topLeft.get_v() ) ),
1381  cv::Point( vpMath::round( topLeft.get_u()+w ),
1382  vpMath::round( topLeft.get_v()+h ) ),
1383  cvcolor, (int)thickness);
1384 #endif
1385  }
1386  }
1387  else {
1388 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
1389  int filled = cv::FILLED;
1390 #else
1391  int filled = CV_FILLED;
1392 #endif
1393  if (color.id < vpColor::id_unknown) {
1394 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1395  cvRectangle( background,
1396  cvPoint( vpMath::round( topLeft.get_u() ),
1397  vpMath::round( topLeft.get_v() ) ),
1398  cvPoint( vpMath::round( topLeft.get_u()+w ),
1399  vpMath::round( topLeft.get_v()+h ) ),
1400  col[color.id], filled);
1401 #else
1402  cv::rectangle( background,
1403  cv::Point( vpMath::round( topLeft.get_u() ),
1404  vpMath::round( topLeft.get_v() ) ),
1405  cv::Point( vpMath::round( topLeft.get_u()+w ),
1406  vpMath::round( topLeft.get_v()+h ) ),
1407  col[color.id], filled);
1408 #endif
1409  }
1410  else {
1411  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1412 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1413  cvRectangle( background,
1414  cvPoint( vpMath::round( topLeft.get_u() ),
1415  vpMath::round( topLeft.get_v() ) ),
1416  cvPoint( vpMath::round( topLeft.get_u()+w ),
1417  vpMath::round( topLeft.get_v()+h ) ),
1418  cvcolor, filled);
1419 #else
1420  cv::rectangle( background,
1421  cv::Point( vpMath::round( topLeft.get_u() ),
1422  vpMath::round( topLeft.get_v() ) ),
1423  cv::Point( vpMath::round( topLeft.get_u()+w ),
1424  vpMath::round( topLeft.get_v()+h ) ),
1425  cvcolor, filled);
1426 #endif
1427  }
1428  }
1429  }
1430  else
1431  {
1432  vpERROR_TRACE("OpenCV not initialized " ) ;
1434  "OpenCV not initialized")) ;
1435  }
1436 }
1449 void
1451  const vpImagePoint &bottomRight,
1452  const vpColor &color, bool fill,
1453  unsigned int thickness )
1454 {
1456  {
1457  if (fill == false) {
1458  if (color.id < vpColor::id_unknown) {
1459 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1460  cvRectangle( background,
1461  cvPoint( vpMath::round( topLeft.get_u() ),
1462  vpMath::round( topLeft.get_v() ) ),
1463  cvPoint( vpMath::round( bottomRight.get_u() ),
1464  vpMath::round( bottomRight.get_v() ) ),
1465  col[color.id], (int)thickness);
1466 #else
1467  cv::rectangle( background,
1468  cv::Point( vpMath::round( topLeft.get_u() ),
1469  vpMath::round( topLeft.get_v() ) ),
1470  cv::Point( vpMath::round( bottomRight.get_u() ),
1471  vpMath::round( bottomRight.get_v() ) ),
1472  col[color.id], (int)thickness);
1473 #endif
1474  }
1475  else {
1476  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1477 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1478  cvRectangle( background,
1479  cvPoint( vpMath::round( topLeft.get_u() ),
1480  vpMath::round( topLeft.get_v() ) ),
1481  cvPoint( vpMath::round( bottomRight.get_u() ),
1482  vpMath::round( bottomRight.get_v() ) ),
1483  cvcolor, (int)thickness);
1484 #else
1485  cv::rectangle( background,
1486  cv::Point( vpMath::round( topLeft.get_u() ),
1487  vpMath::round( topLeft.get_v() ) ),
1488  cv::Point( vpMath::round( bottomRight.get_u() ),
1489  vpMath::round( bottomRight.get_v() ) ),
1490  cvcolor, (int)thickness);
1491 #endif
1492  }
1493  }
1494  else {
1495 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
1496  int filled = cv::FILLED;
1497 #else
1498  int filled = CV_FILLED;
1499 #endif
1500  if (color.id < vpColor::id_unknown) {
1501 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1502  cvRectangle( background,
1503  cvPoint( vpMath::round( topLeft.get_u() ),
1504  vpMath::round( topLeft.get_v() ) ),
1505  cvPoint( vpMath::round( bottomRight.get_u() ),
1506  vpMath::round( bottomRight.get_v() ) ),
1507  col[color.id], filled);
1508 #else
1509  cv::rectangle( background,
1510  cv::Point( vpMath::round( topLeft.get_u() ),
1511  vpMath::round( topLeft.get_v() ) ),
1512  cv::Point( vpMath::round( bottomRight.get_u() ),
1513  vpMath::round( bottomRight.get_v() ) ),
1514  col[color.id], filled);
1515 #endif
1516  }
1517  else {
1518  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1519 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1520  cvRectangle( background,
1521  cvPoint( vpMath::round( topLeft.get_u() ),
1522  vpMath::round( topLeft.get_v() ) ),
1523  cvPoint( vpMath::round( bottomRight.get_u() ),
1524  vpMath::round( bottomRight.get_v() ) ),
1525  cvcolor, filled);
1526 #else
1527  cv::rectangle( background,
1528  cv::Point( vpMath::round( topLeft.get_u() ),
1529  vpMath::round( topLeft.get_v() ) ),
1530  cv::Point( vpMath::round( bottomRight.get_u() ),
1531  vpMath::round( bottomRight.get_v() ) ),
1532  cvcolor, filled);
1533 #endif
1534  }
1535  }
1536  }
1537  else
1538  {
1539  vpERROR_TRACE("OpenCV not initialized " ) ;
1541  "OpenCV not initialized")) ;
1542  }
1543 }
1544 
1557 void
1559  const vpColor &color, bool fill,
1560  unsigned int thickness)
1561 {
1563  {
1564  if (fill == false) {
1565  if (color.id < vpColor::id_unknown) {
1566 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1567  cvRectangle( background,
1568  cvPoint( vpMath::round( rectangle.getLeft() ),
1569  vpMath::round( rectangle.getBottom() ) ),
1570  cvPoint( vpMath::round( rectangle.getRight() ),
1571  vpMath::round( rectangle.getTop() ) ),
1572  col[color.id], (int)thickness);
1573 #else
1574  cv::rectangle( background,
1575  cv::Point( vpMath::round( rectangle.getLeft() ),
1576  vpMath::round( rectangle.getBottom() ) ),
1577  cv::Point( vpMath::round( rectangle.getRight() ),
1578  vpMath::round( rectangle.getTop() ) ),
1579  col[color.id], (int)thickness);
1580 #endif
1581  }
1582  else {
1583  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1584 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1585  cvRectangle( background,
1586  cvPoint( vpMath::round( rectangle.getLeft() ),
1587  vpMath::round( rectangle.getBottom() ) ),
1588  cvPoint( vpMath::round( rectangle.getRight() ),
1589  vpMath::round( rectangle.getTop() ) ),
1590  cvcolor, (int)thickness);
1591 
1592 #else
1593  cv::rectangle( background,
1594  cv::Point( vpMath::round( rectangle.getLeft() ),
1595  vpMath::round( rectangle.getBottom() ) ),
1596  cv::Point( vpMath::round( rectangle.getRight() ),
1597  vpMath::round( rectangle.getTop() ) ),
1598  cvcolor, (int)thickness);
1599 
1600 #endif
1601  }
1602  }
1603  else {
1604 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
1605  int filled = cv::FILLED;
1606 #else
1607  int filled = CV_FILLED;
1608 #endif
1609  if (color.id < vpColor::id_unknown) {
1610 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1611  cvRectangle( background,
1612  cvPoint( vpMath::round( rectangle.getLeft() ),
1613  vpMath::round( rectangle.getBottom() ) ),
1614  cvPoint( vpMath::round( rectangle.getRight() ),
1615  vpMath::round( rectangle.getTop() ) ),
1616  col[color.id], filled);
1617 #else
1618  cv::rectangle( background,
1619  cv::Point( vpMath::round( rectangle.getLeft() ),
1620  vpMath::round( rectangle.getBottom() ) ),
1621  cv::Point( vpMath::round( rectangle.getRight() ),
1622  vpMath::round( rectangle.getTop() ) ),
1623  col[color.id], filled);
1624 #endif
1625  }
1626  else {
1627  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1628 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1629  cvRectangle( background,
1630  cvPoint( vpMath::round( rectangle.getLeft() ),
1631  vpMath::round( rectangle.getBottom() ) ),
1632  cvPoint( vpMath::round( rectangle.getRight() ),
1633  vpMath::round( rectangle.getTop() ) ),
1634  cvcolor, filled);
1635 #else
1636  cv::rectangle( background,
1637  cv::Point( vpMath::round( rectangle.getLeft() ),
1638  vpMath::round( rectangle.getBottom() ) ),
1639  cv::Point( vpMath::round( rectangle.getRight() ),
1640  vpMath::round( rectangle.getTop() ) ),
1641  cvcolor, filled);
1642 #endif
1643  }
1644  }
1645  }
1646  else
1647  {
1648  vpERROR_TRACE("OpenCV not initialized " ) ;
1650  "OpenCV not initialized")) ;
1651  }
1652 }
1653 
1654 
1655 
1671 bool
1673 {
1674  bool ret = false;
1676  flushDisplay() ;
1677  if (blocking){
1678  lbuttondown = false;
1679  mbuttondown = false;
1680  rbuttondown = false;
1681  }
1682  do {
1683  if (lbuttondown){
1684  ret = true ;
1685  lbuttondown = false;
1686  }
1687  if (mbuttondown){
1688  ret = true ;
1689  mbuttondown = false;
1690  }
1691  if (rbuttondown){
1692  ret = true ;
1693  rbuttondown = false;
1694  }
1695  if (blocking)
1696 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1697  cvWaitKey(10);
1698 #else
1699  cv::waitKey(10);
1700 #endif
1701  } while ( ret == false && blocking == true);
1702  }
1703  else {
1704  vpERROR_TRACE("OpenCV not initialized " ) ;
1706  "OpenCV not initialized")) ;
1707  }
1708  return ret;
1709 }
1710 
1728 bool
1730 {
1731  bool ret = false;
1732 
1734  flushDisplay() ;
1735 
1736  double u, v;
1737 
1738  if (blocking){
1739  lbuttondown = false;
1740  mbuttondown = false;
1741  rbuttondown = false;
1742  }
1743  do {
1744  if (lbuttondown){
1745  ret = true ;
1746  u = (unsigned int)x_lbuttondown;
1747  v = (unsigned int)y_lbuttondown;
1748  ip.set_u( u );
1749  ip.set_v( v );
1750  lbuttondown = false;
1751  }
1752  if (mbuttondown){
1753  ret = true ;
1754  u = (unsigned int)x_mbuttondown;
1755  v = (unsigned int)y_mbuttondown;
1756  ip.set_u( u );
1757  ip.set_v( v );
1758  mbuttondown = false;
1759  }
1760  if (rbuttondown){
1761  ret = true ;
1762  u = (unsigned int)x_rbuttondown;
1763  v = (unsigned int)y_rbuttondown;
1764  ip.set_u( u );
1765  ip.set_v( v );
1766  rbuttondown = false;
1767  }
1768  if (blocking)
1769 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1770  cvWaitKey(10);
1771 #else
1772  cv::waitKey(10);
1773 #endif
1774  } while ( ret == false && blocking == true);
1775  }
1776  else {
1777  vpERROR_TRACE("OpenCV not initialized " ) ;
1779  "OpenCV not initialized")) ;
1780  }
1781  return ret ;
1782 }
1783 
1784 
1804 bool
1807  bool blocking)
1808 {
1809  bool ret = false;
1810 
1812  //flushDisplay() ;
1813  double u, v;
1814  if (blocking){
1815  lbuttondown = false;
1816  mbuttondown = false;
1817  rbuttondown = false;
1818  }
1819  do {
1820  if (lbuttondown){
1821  ret = true ;
1822  u = (unsigned int)x_lbuttondown;
1823  v = (unsigned int)y_lbuttondown;
1824  ip.set_u( u );
1825  ip.set_v( v );
1826  button = vpMouseButton::button1;
1827  lbuttondown = false;
1828  }
1829  if (mbuttondown){
1830  ret = true ;
1831  u = (unsigned int)x_mbuttondown;
1832  v = (unsigned int)y_mbuttondown;
1833  ip.set_u( u );
1834  ip.set_v( v );
1835  button = vpMouseButton::button2;
1836  mbuttondown = false;
1837  }
1838  if (rbuttondown){
1839  ret = true ;
1840  u = (unsigned int)x_rbuttondown;
1841  v = (unsigned int)y_rbuttondown;
1842  ip.set_u( u );
1843  ip.set_v( v );
1844  button = vpMouseButton::button3;
1845  rbuttondown = false;
1846  }
1847  if (blocking)
1848 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1849  cvWaitKey(10);
1850 #else
1851  cv::waitKey(10);
1852 #endif
1853  } while ( ret == false && blocking == true);
1854  }
1855  else {
1856  vpERROR_TRACE("OpenCV not initialized " ) ;
1858  "OpenCV not initialized")) ;
1859  }
1860  return ret;
1861 }
1862 
1886 bool
1889  bool blocking)
1890 {
1891  bool ret = false;
1893  //flushDisplay() ;
1894  double u, v;
1895  if (blocking){
1896  lbuttonup = false;
1897  mbuttonup = false;
1898  rbuttonup = false;
1899  }
1900  do {
1901  if (lbuttonup){
1902  ret = true ;
1903  u = (unsigned int)x_lbuttonup;
1904  v = (unsigned int)y_lbuttonup;
1905  ip.set_u( u );
1906  ip.set_v( v );
1907  button = vpMouseButton::button1;
1908  lbuttonup = false;
1909  }
1910  if (mbuttonup){
1911  ret = true ;
1912  u = (unsigned int)x_mbuttonup;
1913  v = (unsigned int)y_mbuttonup;
1914  ip.set_u( u );
1915  ip.set_v( v );
1916  button = vpMouseButton::button2;
1917  mbuttonup = false;
1918  }
1919  if (rbuttonup){
1920  ret = true ;
1921  u = (unsigned int)x_rbuttonup;
1922  v = (unsigned int)y_rbuttonup;
1923  ip.set_u( u );
1924  ip.set_v( v );
1925  button = vpMouseButton::button3;
1926  rbuttonup = false;
1927  }
1928  if (blocking)
1929 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1930  cvWaitKey(10);
1931 #else
1932  cv::waitKey(10);
1933 #endif
1934  } while ( ret == false && blocking == true);
1935  }
1936  else {
1937  vpERROR_TRACE ( "OpenCV not initialized " ) ;
1939  "OpenCV not initialized" ) ) ;
1940  }
1941  return ret;
1942 }
1943 
1944 /*
1945  \brief gets the displayed image (including the overlay plane)
1946  and returns an RGBa image
1947 */
1949 {
1950  vpImageConvert::convert(background,I);
1951  // should certainly be optimized.
1952 }
1953 
1954 void vpDisplayOpenCV::on_mouse( int event, int x, int y, int /*flags*/, void* display )
1955 {
1956  vpDisplayOpenCV* disp = (vpDisplayOpenCV*)display;
1957  switch ( event )
1958  {
1959 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1960  case CV_EVENT_MOUSEMOVE:
1961 #else
1962  case cv::EVENT_MOUSEMOVE:
1963 #endif
1964  {
1965  disp->move = true;
1966  disp->x_move = x;
1967  disp->y_move = y;
1968  break;
1969  }
1970 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1971  case CV_EVENT_LBUTTONDOWN:
1972 #else
1973  case cv::EVENT_LBUTTONDOWN:
1974 #endif
1975  {
1976  disp->lbuttondown = true;
1977  disp->x_lbuttondown = x;
1978  disp->y_lbuttondown = y;
1979  break;
1980  }
1981 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1982  case CV_EVENT_MBUTTONDOWN:
1983 #else
1984  case cv::EVENT_MBUTTONDOWN:
1985 #endif
1986  {
1987  disp->mbuttondown = true;
1988  disp->x_mbuttondown = x;
1989  disp->y_mbuttondown = y;
1990  break;
1991  }
1992 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1993  case CV_EVENT_RBUTTONDOWN:
1994 #else
1995  case cv::EVENT_RBUTTONDOWN:
1996 #endif
1997  {
1998  disp->rbuttondown = true;
1999  disp->x_rbuttondown = x;
2000  disp->y_rbuttondown = y;
2001  break;
2002  }
2003 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
2004  case CV_EVENT_LBUTTONUP:
2005 #else
2006  case cv::EVENT_LBUTTONUP:
2007 #endif
2008  {
2009  disp->lbuttonup = true;
2010  disp->x_lbuttonup = x;
2011  disp->y_lbuttonup = y;
2012  break;
2013  }
2014 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
2015  case CV_EVENT_MBUTTONUP:
2016 #else
2017  case cv::EVENT_MBUTTONUP:
2018 #endif
2019  {
2020  disp->mbuttonup = true;
2021  disp->x_mbuttonup = x;
2022  disp->y_mbuttonup = y;
2023  break;
2024  }
2025 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
2026  case CV_EVENT_RBUTTONUP:
2027 #else
2028  case cv::EVENT_RBUTTONUP:
2029 #endif
2030  {
2031  disp->rbuttonup = true;
2032  disp->x_rbuttonup = x;
2033  disp->y_rbuttonup = y;
2034  break;
2035  }
2036 
2037  default :
2038  break;
2039  }
2040 }
2041 
2058 bool
2060 {
2061  int key_pressed;
2062  int delay;
2064  flushDisplay() ;
2065  if (blocking)
2066  delay = 0;
2067  else
2068  delay = 10;
2069 
2070 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
2071  key_pressed = cvWaitKey(delay);
2072 #else
2073  key_pressed = cv::waitKey(delay);
2074 #endif
2075 
2076  if (key_pressed == -1)
2077  return false;
2078  return true;
2079  }
2080  else {
2081  vpERROR_TRACE("OpenCV not initialized " ) ;
2083  "OpenCV not initialized")) ;
2084  }
2085  //return false; // Never reached after throw()
2086 }
2106 bool
2107 vpDisplayOpenCV::getKeyboardEvent(char *string, bool blocking)
2108 {
2109  int key_pressed;
2110  int delay;
2112  flushDisplay() ;
2113  if (blocking)
2114  delay = 0;
2115  else
2116  delay = 10;
2117 
2118 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
2119  key_pressed = cvWaitKey(delay);
2120 #else
2121  key_pressed = cv::waitKey(delay);
2122 #endif
2123  if (key_pressed == -1)
2124  return false;
2125  else {
2126  //std::cout << "Key pressed: \"" << key_pressed << "\"" << std::endl;
2127  sprintf(string, "%c", key_pressed);
2128  }
2129  return true;
2130  }
2131  else {
2132  vpERROR_TRACE("OpenCV not initialized " ) ;
2134  "OpenCV not initialized")) ;
2135  }
2136  //return false; // Never reached after throw()
2137 }
2138 
2151 bool
2153 {
2154  bool ret = false;
2155 
2157  //flushDisplay() ;
2158  double u, v;
2159  if (move){
2160  ret = true ;
2161  u = (unsigned int)x_move;
2162  v = (unsigned int)y_move;
2163  ip.set_u( u );
2164  ip.set_v( v );
2165  move = false;
2166  }
2167  }
2168 
2169  else {
2170  vpERROR_TRACE("OpenCV not initialized " ) ;
2172  "OpenCV not initialized")) ;
2173  }
2174  return ret;
2175 }
2176 
2187 bool
2189 {
2191  //vpTRACE("Not implemented yet");
2192  bool moved = getPointerMotionEvent(ip);
2193  if (!moved)
2194  {
2195  double u, v;
2196  u = (unsigned int)x_move;
2197  v = (unsigned int)y_move;
2198  ip.set_u( u );
2199  ip.set_v( v );
2200  }
2201  return false;
2202  }
2203  else {
2204  vpERROR_TRACE("OpenCV not initialized " ) ;
2206  "OpenCV not initialized")) ;
2207  }
2208  //return false; // Never reached after throw()
2209 }
2210 
2211 #elif !defined(VISP_BUILD_SHARED_LIBS)
2212 // Work arround to avoid warning: libvisp_core.a(vpDisplayOpenCV.cpp.o) has no symbols
2213 void dummy_vpDisplayOpenCV() {};
2214 #endif
void displayCircle(const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)
vpDisplay * display
Definition: vpImage.h:117
void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
double getTop() const
Definition: vpRect.h:176
double get_v() const
Definition: vpImagePoint.h:259
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:170
void clearDisplay(const vpColor &color=vpColor::white)
unsigned int width
Definition: vpDisplay.h:179
double get_i() const
Definition: vpImagePoint.h:190
unsigned int getWidth() const
Definition: vpImage.h:161
void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
void displayPoint(const vpImagePoint &ip, const vpColor &color)
unsigned char B
Blue component.
Definition: vpRGBa.h:144
static const vpColor black
Definition: vpColor.h:157
static const vpColor darkRed
Definition: vpColor.h:164
#define vpERROR_TRACE
Definition: vpDebug.h:391
bool getClick(bool blocking=true)
void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
double get_u() const
Definition: vpImagePoint.h:248
static const vpColor lightGray
Definition: vpColor.h:159
bool getPointerPosition(vpImagePoint &ip)
static const vpColor darkBlue
Definition: vpColor.h:170
bool displayHasBeenInitialized
display has been initialized
Definition: vpDisplay.h:174
unsigned char G
Green component.
Definition: vpRGBa.h:143
double getRight() const
Definition: vpRect.h:163
static const vpColor green
Definition: vpColor.h:166
static int round(const double x)
Definition: vpMath.h:248
double get_j() const
Definition: vpImagePoint.h:201
void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static const vpColor lightRed
Definition: vpColor.h:162
virtual ~vpDisplayOpenCV()
static const vpColor red
Definition: vpColor.h:163
static const vpColor orange
Definition: vpColor.h:173
std::string title_
Definition: vpDisplay.h:181
static void on_mouse(int event, int x, int y, int flags, void *param)
double getBottom() const
Definition: vpRect.h:99
vpColorIdentifier id
Definition: vpColor.h:152
void set_i(const double ii)
Definition: vpImagePoint.h:154
static const vpColor cyan
Definition: vpColor.h:172
static const vpColor lightGreen
Definition: vpColor.h:165
void flushDisplayROI(const vpImagePoint &iP, const unsigned int width, const unsigned int height)
void set_u(const double u)
Definition: vpImagePoint.h:212
#define vpTRACE
Definition: vpDebug.h:414
static double sqr(double x)
Definition: vpMath.h:110
void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
unsigned int height
Definition: vpDisplay.h:180
The vpDisplayOpenCV allows to display image using the opencv library.
void set_v(const double v)
Definition: vpImagePoint.h:223
void displayImage(const vpImage< vpRGBa > &I)
void displayArrow(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color=vpColor::white, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)
void displayImageROI(const vpImage< unsigned char > &I, const vpImagePoint &iP, const unsigned int width, const unsigned int height)
bool getClickUp(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking=true)
static const vpColor gray
Definition: vpColor.h:160
static const vpColor darkGray
Definition: vpColor.h:161
void set_j(const double jj)
Definition: vpImagePoint.h:165
bool getPointerMotionEvent(vpImagePoint &ip)
Error that can be emited by the vpDisplay class and its derivates.
static void createSubImage(const vpImage< Type > &I, unsigned int i_sub, unsigned int j_sub, unsigned int nrow_sub, unsigned int ncol_sub, vpImage< Type > &S)
Definition: vpImageTools.h:132
void getImage(vpImage< vpRGBa > &I)
get the window pixmap and put it in vpRGBa image
int windowXPosition
display position
Definition: vpDisplay.h:176
unsigned char R
Red component.
Definition: vpRGBa.h:142
void setFont(const char *font)
unsigned int getHeight() const
Definition: vpImage.h:152
Defines a rectangle in the plane.
Definition: vpRect.h:81
static const vpColor darkGreen
Definition: vpColor.h:167
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
void setTitle(const char *title)
static const vpColor yellow
Definition: vpColor.h:171
static const vpColor lightBlue
Definition: vpColor.h:168
int windowYPosition
display position
Definition: vpDisplay.h:178
static const vpColor purple
Definition: vpColor.h:174
static const vpColor white
Definition: vpColor.h:158
double getLeft() const
Definition: vpRect.h:157
bool getKeyboardEvent(bool blocking=true)
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
Definition: vpImagePoint.h:270
void setWindowPosition(int winx, int winy)
static const vpColor blue
Definition: vpColor.h:169