ViSP  2.9.0
vpDisplayOpenCV.cpp
1 /****************************************************************************
2  *
3  * $Id: vpDisplayOpenCV.cpp 4649 2014-02-07 14:57:11Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Image display.
36  *
37  * Authors:
38  * Christophe Collewet
39  * Eric Marchand
40  * Fabien Spindler
41  *
42  *****************************************************************************/
43 
44 
50 #include <visp/vpConfig.h>
51 
52 #if ( defined(VISP_HAVE_OPENCV) )
53 
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <iostream>
57 #include <cmath> // std::fabs
58 #include <limits> // numeric_limits
59 
60 // Display stuff
61 #include <visp/vpDisplay.h>
62 #include <visp/vpDisplayOpenCV.h>
63 #include <visp/vpMath.h>
64 #include <visp/vpImageTools.h>
65 
66 //debug / exception
67 #include <visp/vpDebug.h>
68 #include <visp/vpDisplayException.h>
69 
81  int x,
82  int y,
83  const char *title)
84 {
85  col = NULL;
86  background = NULL;
87  font = NULL;
88  init(I, x, y, title) ;
89 }
90 
91 
101  int x,
102  int y,
103  const char *title)
104 {
105  col = NULL;
106  background = NULL;
107  font = NULL;
108  init(I, x, y, title) ;
109 }
110 
133 vpDisplayOpenCV::vpDisplayOpenCV ( int x, int y, const char *title )
134 {
135  col = NULL;
136  background = NULL;
137  font = NULL;
138  windowXPosition = x;
139  windowYPosition = y;
140 
141  if(title != NULL)
142  title_ = std::string(title);
143  else
144  title_ = std::string(" ");
145 
146  fontHeight = 10;
147  ncol = nrow = 0;
148  x_move = y_move = 0; move = false;
149  x_lbuttondown = y_lbuttondown = 0; lbuttondown = false;
150  x_mbuttondown = y_mbuttondown = 0; mbuttondown = false;
151  x_rbuttondown = y_rbuttondown = 0; rbuttondown = false;
152  x_lbuttonup = y_lbuttonup = 0; lbuttonup = false;
153  x_mbuttonup = y_mbuttonup = 0; mbuttonup = false;
154  x_rbuttonup = y_rbuttonup = 0; rbuttonup = false;
155 }
156 
177 {
178  col = NULL;
179  background = NULL;
180  font = NULL;
181  fontHeight = 10;
182  ncol = nrow = 0;
183  x_move = y_move = 0; move = false;
184  x_lbuttondown = y_lbuttondown = 0; lbuttondown = false;
185  x_mbuttondown = y_mbuttondown = 0; mbuttondown = false;
186  x_rbuttondown = y_rbuttondown = 0; rbuttondown = false;
187  x_lbuttonup = y_lbuttonup = 0; lbuttonup = false;
188  x_mbuttonup = y_mbuttonup = 0; mbuttonup = false;
189  x_rbuttonup = y_rbuttonup = 0; rbuttonup = false;
190 }
191 
196 {
197  closeDisplay() ;
198  cvReleaseImage(&background);
199 }
200 
209 void
211  int x,
212  int y,
213  const char *title)
214 {
215 
216  if ((I.getHeight() == 0) || (I.getWidth()==0))
217  {
218  vpERROR_TRACE("Image not initialized " ) ;
220  "Image not initialized")) ;
221  }
222  init (I.getWidth(), I.getHeight(), x, y, title) ;
223  I.display = this ;
225 }
226 
236 void
238  int x,
239  int y,
240  const char *title)
241 {
242  if ((I.getHeight() == 0) || (I.getWidth()==0))
243  {
244  vpERROR_TRACE("Image not initialized " ) ;
246  "Image not initialized")) ;
247  }
248 
249  init (I.getWidth(), I.getHeight(), x, y, title) ;
250  I.display = this ;
252 }
253 
264 void
265 vpDisplayOpenCV::init(unsigned int w, unsigned int h,
266  int x, int y,
267  const char *title)
268 {
269  this->width = w;
270  this->height = h;
271 
272  if (x != -1)
273  this->windowXPosition = x;
274  if (y != -1)
275  this->windowYPosition = y;
276  int flags = CV_WINDOW_AUTOSIZE;
277 
278  if(title != NULL)
279  title_ = std::string(title);
280  else
281  title_ = std::string(" ");
282 
283  /* Create the window*/
284  if (cvNamedWindow( this->title_.c_str(), flags ) < 0) {
285  vpERROR_TRACE("OpenCV was not built with a display device");
287  "OpenCV was not built with a display device")) ;
288  }
289  cvMoveWindow( this->title_.c_str(), this->windowXPosition, this->windowYPosition );
290  move = false;
291  lbuttondown = false;
292  mbuttondown = false;
293  rbuttondown = false;
294  lbuttonup = false;
295  mbuttonup = false;
296  rbuttonup = false;
297  cvSetMouseCallback( this->title_.c_str(), on_mouse, this );
298  /* Create background pixmap */
299 // background = cvCreateImage(cvSize((int)width,(int)height),IPL_DEPTH_8U,3);
300 //
301 // cvShowImage( this->title_.c_str(),background);
302 
303  col = new CvScalar[vpColor::id_unknown] ;
304 
305  /* Create color */
306  vpColor pcolor; // Predefined colors
307  pcolor = vpColor::lightBlue;
308  col[vpColor::id_lightBlue] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
309  pcolor = vpColor::blue;
310  col[vpColor::id_blue] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
311  pcolor = vpColor::darkBlue;
312  col[vpColor::id_darkBlue] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
313  pcolor = vpColor::lightRed;
314  col[vpColor::id_lightRed] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
315  pcolor = vpColor::red;
316  col[vpColor::id_red] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
317  pcolor = vpColor::darkRed;
318  col[vpColor::id_darkRed] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
319  pcolor = vpColor::lightGreen;
320  col[vpColor::id_lightGreen] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
321  pcolor = vpColor::green;
322  col[vpColor::id_green] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
323  pcolor = vpColor::darkGreen;
324  col[vpColor::id_darkGreen] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
325  pcolor = vpColor::yellow;
326  col[vpColor::id_yellow] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
327  pcolor = vpColor::cyan;
328  col[vpColor::id_cyan] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
329  pcolor = vpColor::orange;
330  col[vpColor::id_orange] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
331  pcolor = vpColor::purple;
332  col[vpColor::id_purple] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
333  pcolor = vpColor::white;
334  col[vpColor::id_white] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
335  pcolor = vpColor::black;
336  col[vpColor::id_black] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
337  pcolor = vpColor::lightGray;
338  col[vpColor::id_lightGray] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
339  pcolor = vpColor::gray;
340  col[vpColor::id_gray] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
341  pcolor = vpColor::darkGray;
342  col[vpColor::id_darkGray] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
343 
344  font = new CvFont;
345  cvInitFont( font, CV_FONT_HERSHEY_PLAIN, 0.70f,0.70f);
346 
347  CvSize fontSize;
348  int baseline;
349  cvGetTextSize( "A", font, &fontSize, &baseline );
350  fontHeight = fontSize.height + baseline;
352 }
353 
354 
370 void
371 vpDisplayOpenCV::setFont(const char * /* font */)
372 {
373  vpERROR_TRACE("Not yet implemented" ) ;
374 }
375 
383 void
384 vpDisplayOpenCV::setTitle(const char * /* title */)
385 {
386  static bool warn_displayed = false;
387  if (! warn_displayed) {
388  vpTRACE("Not implemented");
389  warn_displayed = true;
390  }
391 }
392 
393 
402 void vpDisplayOpenCV::setWindowPosition(int winx, int winy)
403 {
405  this->windowXPosition = winx;
406  this->windowYPosition = winy;
407  cvMoveWindow( this->title_.c_str(), winx, winy );
408  }
409  else
410  {
411  vpERROR_TRACE("OpenCV not initialized " ) ;
413  "OpenCV not initialized")) ;
414  }
415 }
428 {
430  {
431  vpImage<vpRGBa> Ic;
433  vpImageConvert::convert(Ic,background);
434  /* Copie de l'image dans le pixmap fond */
435  width = I.getWidth();
436  height = I.getHeight();
437  /* Le pixmap background devient le fond de la zone de dessin */
438 
439  /* Affichage */
440  //gdk_window_clear(window);
441  //gdk_flush();
442  }
443  else
444  {
445  vpERROR_TRACE("openCV not initialized " ) ;
447  "OpenCV not initialized")) ;
448  }
449 }
450 
467  const unsigned int w, const unsigned int h )
468 {
470  {
472  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),h,w,Itemp);
473  vpImage<vpRGBa> Ic;
474  vpImageConvert::convert(Itemp,Ic);
475 
476  CvSize size = cvSize((int)this->width, (int)this->height);
477  int depth = 8;
478  int channels = 3;
479  if (background != NULL){
480  if(background->nChannels != channels || background->depth != depth
481  || background->height != (int) I.getHeight() || background->width != (int) I.getWidth()){
482  if(background->nChannels != 0) cvReleaseImage(&background);
483  background = cvCreateImage( size, depth, channels );
484  }
485  }
486  else background = cvCreateImage( size, depth, channels );
487 
488  IplImage* Ip = NULL;
489  vpImageConvert::convert(Ic, Ip);
490 
491  unsigned char * input = (unsigned char*)Ip->imageData;
492  unsigned char * output = (unsigned char*)background->imageData;
493 
494  unsigned int iwidth = Ic.getWidth();
495 
496  output = output + (int)(iP.get_i()*3*this->width+ iP.get_j()*3);
497 
498  unsigned int i = 0;
499  while (i < h)
500  {
501  unsigned int j = 0;
502  while (j < w)
503  {
504  *(output+3*j) = *(input+j*3);
505  *(output+3*j+1) = *(input+j*3+1);
506  *(output+3*j+2) = *(input+j*3+2);
507  j++;
508  }
509  input = input + 3*iwidth;
510  output = output + 3*this->width;
511  i++;
512  }
513 
514  cvReleaseImage(&Ip);
515  }
516  else
517  {
518  vpERROR_TRACE("openCV not initialized " ) ;
520  "OpenCV not initialized")) ;
521  }
522 }
523 
524 
537 {
538 
540  {
541  /* Copie de l'image dans le pixmap fond */
542 
543  vpImageConvert::convert(I,background);
544  /* Copie de l'image dans le pixmap fond */
545  width = I.getWidth();
546  height = I.getHeight();
547 
548  }
549  else
550  {
551  vpERROR_TRACE("OpenCV not initialized " ) ;
553  "OpenCV not initialized")) ;
554  }
555 }
556 
573  const unsigned int w, const unsigned int h )
574 {
576  {
577  vpImage<vpRGBa> Ic;
578  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),h,w,Ic);
579 
580  CvSize size = cvSize((int)this->width, (int)this->height);
581  int depth = 8;
582  int channels = 3;
583  if (background != NULL){
584  if(background->nChannels != channels || background->depth != depth
585  || background->height != (int) I.getHeight() || background->width != (int) I.getWidth()){
586  if(background->nChannels != 0) cvReleaseImage(&background);
587  background = cvCreateImage( size, depth, channels );
588  }
589  }
590  else background = cvCreateImage( size, depth, channels );
591 
592  IplImage* Ip = NULL;
593  vpImageConvert::convert(Ic, Ip);
594 
595  unsigned char * input = (unsigned char*)Ip->imageData;
596  unsigned char * output = (unsigned char*)background->imageData;
597 
598  unsigned int iwidth = Ic.getWidth();
599 
600  output = output + (int)(iP.get_i()*3*this->width+ iP.get_j()*3);
601 
602  unsigned int i = 0;
603  while (i < h)
604  {
605  unsigned int j = 0;
606  while (j < w)
607  {
608  *(output+3*j) = *(input+j*3);
609  *(output+3*j+1) = *(input+j*3+1);
610  *(output+3*j+2) = *(input+j*3+2);
611  j++;
612  }
613  input = input + 3*iwidth;
614  output = output + 3*this->width;
615  i++;
616  }
617 
618  cvReleaseImage(&Ip);
619  }
620  else
621  {
622  vpERROR_TRACE("openCV not initialized " ) ;
624  "OpenCV not initialized")) ;
625  }
626 }
627 
628 
634 void vpDisplayOpenCV::displayImage(const unsigned char * /* I */)
635 {
636  vpTRACE(" not implemented ") ;
637 }
638 
647 {
648  if (col != NULL)
649  {
650  delete [] col ; col = NULL ;
651  }
652  if (font != NULL)
653  {
654  delete font ;
655  font = NULL ;
656  }
657 
659  cvDestroyWindow( this->title_.c_str() );
660 
662  }
663 }
664 
665 
672 {
674  {
675  cvShowImage(this->title_.c_str(), background );
676  cvWaitKey(5);
677  }
678  else
679  {
680  vpERROR_TRACE("OpenCV not initialized " ) ;
682  "OpenCV not initialized")) ;
683  }
684 }
685 
691 void vpDisplayOpenCV::flushDisplayROI(const vpImagePoint &/*iP*/, const unsigned int /*width*/, const unsigned int /*height*/)
692 {
694  {
695  cvShowImage(this->title_.c_str(), background );
696  cvWaitKey(5);
697  }
698  else
699  {
700  vpERROR_TRACE("OpenCV not initialized " ) ;
702  "OpenCV not initialized")) ;
703  }
704 }
705 
706 
710 void vpDisplayOpenCV::clearDisplay(const vpColor & /* color */)
711 {
712  static bool warn_displayed = false;
713  if (! warn_displayed) {
714  vpTRACE("Not implemented");
715  warn_displayed = true;
716  }
717 }
718 
727  const vpImagePoint &ip2,
728  const vpColor &color,
729  unsigned int w, unsigned int h,
730  unsigned int thickness)
731 {
733  {
734  try{
735  double a = ip2.get_i() - ip1.get_i() ;
736  double b = ip2.get_j() - ip1.get_j() ;
737  double lg = sqrt(vpMath::sqr(a)+vpMath::sqr(b)) ;
738 
739  //if ((a==0)&&(b==0))
740  if ((std::fabs(a) <= std::numeric_limits<double>::epsilon())
741  &&(std::fabs(b)<= std::numeric_limits<double>::epsilon()))
742  {
743  // DisplayCrossLarge(i1,j1,3,col) ;
744  }
745  else
746  {
747  a /= lg ;
748  b /= lg ;
749 
750  vpImagePoint ip3;
751  ip3.set_i(ip2.get_i() - w*a);
752  ip3.set_j(ip2.get_j() - w*b);
753 
754  vpImagePoint ip4;
755  ip4.set_i( ip3.get_i() - b*h );
756  ip4.set_j( ip3.get_j() + a*h );
757 
758  displayLine ( ip2, ip4, color, thickness ) ;
759 
760  ip4.set_i( ip3.get_i() + b*h );
761  ip4.set_j( ip3.get_j() - a*h );
762 
763  displayLine ( ip2, ip4, color, thickness ) ;
764  displayLine ( ip1, ip2, color, thickness ) ;
765  }
766  }
767  catch (...)
768  {
769  vpERROR_TRACE("Error caught") ;
770  throw ;
771  }
772  }
773  else
774  {
775  vpERROR_TRACE("OpenCV not initialized " ) ;
777  "OpenCV not initialized")) ;
778  }
779 }
780 
793  const char *text,
794  const vpColor &color )
795 {
797  {
798  if (color.id < vpColor::id_unknown) {
799  cvPutText( background, text,
800  cvPoint( vpMath::round( ip.get_u() ),
801  vpMath::round( ip.get_v()+fontHeight ) ),
802  font, col[color.id] );
803  }
804  else {
805  cvcolor = CV_RGB(color.R, color.G, color.B) ;
806  cvPutText( background, text,
807  cvPoint( vpMath::round( ip.get_u() ),
808  vpMath::round( ip.get_v()+fontHeight ) ),
809  font, cvcolor );
810  }
811  }
812  else
813  {
814  vpERROR_TRACE("OpenCV not initialized " ) ;
816  "OpenCV not initialized")) ;
817  }
818 }
829  unsigned int radius,
830  const vpColor &color,
831  bool fill ,
832  unsigned int thickness)
833 {
835  {
836  if (fill == false) {
837  if (color.id < vpColor::id_unknown) {
838  cvCircle( background,
839  cvPoint( vpMath::round( center.get_u() ),
840  vpMath::round( center.get_v() ) ),
841  (int)radius, col[color.id], (int)thickness);
842  }
843  else {
844  cvcolor = CV_RGB(color.R, color.G, color.B) ;
845  cvCircle( background,
846  cvPoint( vpMath::round( center.get_u() ),
847  vpMath::round( center.get_v() ) ),
848  (int)radius, cvcolor, (int)thickness);
849  }
850  }
851  else {
852  if (color.id < vpColor::id_unknown) {
853  cvCircle( background,
854  cvPoint( vpMath::round( center.get_u() ),
855  vpMath::round( center.get_v() ) ),
856  (int)radius, col[color.id], CV_FILLED);
857  }
858  else {
859  cvcolor = CV_RGB(color.R, color.G, color.B) ;
860  cvCircle( background,
861  cvPoint( vpMath::round( center.get_u() ),
862  vpMath::round( center.get_v() ) ),
863  (int)radius, cvcolor, CV_FILLED);
864  }
865  }
866  }
867  else
868  {
869  vpERROR_TRACE("OpenCV not initialized " ) ;
871  "OpenCV not initialized")) ;
872  }
873 }
874 
882 void
884  unsigned int size,
885  const vpColor &color,
886  unsigned int thickness)
887 {
889  {
890  vpImagePoint top,bottom,left,right;
891  top.set_i(ip.get_i()-size/2);
892  top.set_j(ip.get_j());
893  bottom.set_i(ip.get_i()+size/2);
894  bottom.set_j(ip.get_j());
895  left.set_i(ip.get_i());
896  left.set_j(ip.get_j()-size/2);
897  right.set_i(ip.get_i());
898  right.set_j(ip.get_j()+size/2);
899  try{
900  displayLine(top, bottom, color, thickness) ;
901  displayLine(left, right, color, thickness) ;
902  }
903  catch (...)
904  {
905  vpERROR_TRACE("Error caught") ;
906  throw ;
907  }
908  }
909 
910  else
911  {
912  vpERROR_TRACE("OpenCV not initialized " ) ;
914  "OpenCV not initialized")) ;
915  }
916 
917 }
918 
929 void
931  const vpImagePoint &ip2,
932  const vpColor &color,
933  unsigned int thickness)
934 {
935 
937  {
938  vpTRACE("Dot lines are not yet implemented");
939  if (color.id < vpColor::id_unknown) {
940  cvLine( background,
941  cvPoint( vpMath::round( ip1.get_u() ),
942  vpMath::round( ip1.get_v() ) ),
943  cvPoint( vpMath::round( ip2.get_u() ),
944  vpMath::round( ip2.get_v() ) ),
945  col[color.id], (int) thickness);
946  }
947  else {
948  cvcolor = CV_RGB(color.R, color.G, color.B) ;
949  cvLine( background,
950  cvPoint( vpMath::round( ip1.get_u() ),
951  vpMath::round( ip1.get_v() ) ),
952  cvPoint( vpMath::round( ip2.get_u() ),
953  vpMath::round( ip2.get_v() ) ),
954  cvcolor, (int) thickness);
955  }
956  }
957  else
958  {
959  vpERROR_TRACE("OpenCV not initialized " ) ;
961  "OpenCV not initialized")) ;
962  }
963 }
964 
965 
972 void
974  const vpImagePoint &ip2,
975  const vpColor &color,
976  unsigned int thickness)
977 {
979  {
980  if (color.id < vpColor::id_unknown) {
981  cvLine( background,
982  cvPoint( vpMath::round( ip1.get_u() ),
983  vpMath::round( ip1.get_v() ) ),
984  cvPoint( vpMath::round( ip2.get_u() ),
985  vpMath::round( ip2.get_v() ) ),
986  col[color.id], (int) thickness);
987  }
988  else {
989  cvcolor = CV_RGB(color.R, color.G, color.B) ;
990  cvLine( background,
991  cvPoint( vpMath::round( ip1.get_u() ),
992  vpMath::round( ip1.get_v() ) ),
993  cvPoint( vpMath::round( ip2.get_u() ),
994  vpMath::round( ip2.get_v() ) ),
995  cvcolor, (int) thickness);
996  }
997  }
998  else
999  {
1000  vpERROR_TRACE("OpenCV not initialized " ) ;
1002  "OpenCV not initialized")) ;
1003  }
1004 }
1005 
1012  const vpColor &color)
1013 {
1015  {
1016  displayLine(ip,ip,color,1);
1017 // if (color.id < vpColor::id_unknown) {
1018 // ((uchar*)(background->imageData
1019 // + background->widthStep*vpMath::round( ip.get_i() )))
1020 // [vpMath::round( ip.get_j()*3 )] = (uchar)col[color.id].val[0];
1021 //
1022 // ((uchar*)(background->imageData
1023 // + background->widthStep*vpMath::round( ip.get_i() )))
1024 // [vpMath::round( ip.get_j()*3+1 )] = (uchar)col[color.id].val[1];
1025 //
1026 // ((uchar*)(background->imageData
1027 // + background->widthStep*vpMath::round( ip.get_i() )))
1028 // [vpMath::round( ip.get_j()*3+2 )] = (uchar)col[color.id].val[2];
1029 // }
1030 // else {
1031 // cvcolor = CV_RGB(color.R, color.G, color.B) ;
1032 // ((uchar*)(background->imageData
1033 // + background->widthStep*vpMath::round( ip.get_i() )))
1034 // [vpMath::round( ip.get_j()*3 )] = (uchar)cvcolor.val[0];
1035 //
1036 // ((uchar*)(background->imageData
1037 // + background->widthStep*vpMath::round( ip.get_i() )))
1038 // [vpMath::round( ip.get_j()*3+1 )] = (uchar)cvcolor.val[1];
1039 //
1040 // ((uchar*)(background->imageData
1041 // + background->widthStep*vpMath::round( ip.get_i() )))
1042 // [vpMath::round( ip.get_j()*3+2 )] = (uchar)cvcolor.val[2];
1043 //
1044 // }
1045  }
1046  else
1047  {
1048  vpERROR_TRACE("OpenCV not initialized " ) ;
1050  "OpenCV not initialized")) ;
1051  }
1052 }
1053 
1067 void
1069  unsigned int w, unsigned int h,
1070  const vpColor &color, bool fill,
1071  unsigned int thickness)
1072 {
1074  {
1075  if (fill == false) {
1076  if (color.id < vpColor::id_unknown) {
1077  cvRectangle( background,
1078  cvPoint( vpMath::round( topLeft.get_u() ),
1079  vpMath::round( topLeft.get_v() ) ),
1080  cvPoint( vpMath::round( topLeft.get_u()+w ),
1081  vpMath::round( topLeft.get_v()+h ) ),
1082  col[color.id], (int)thickness);
1083  }
1084  else {
1085  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1086  cvRectangle( background,
1087  cvPoint( vpMath::round( topLeft.get_u() ),
1088  vpMath::round( topLeft.get_v() ) ),
1089  cvPoint( vpMath::round( topLeft.get_u()+w ),
1090  vpMath::round( topLeft.get_v()+h ) ),
1091  cvcolor, (int)thickness);
1092  }
1093  }
1094  else {
1095  if (color.id < vpColor::id_unknown) {
1096  cvRectangle( background,
1097  cvPoint( vpMath::round( topLeft.get_u() ),
1098  vpMath::round( topLeft.get_v() ) ),
1099  cvPoint( vpMath::round( topLeft.get_u()+w ),
1100  vpMath::round( topLeft.get_v()+h ) ),
1101  col[color.id], CV_FILLED);
1102  }
1103  else {
1104  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1105  cvRectangle( background,
1106  cvPoint( vpMath::round( topLeft.get_u() ),
1107  vpMath::round( topLeft.get_v() ) ),
1108  cvPoint( vpMath::round( topLeft.get_u()+w ),
1109  vpMath::round( topLeft.get_v()+h ) ),
1110  cvcolor, CV_FILLED);
1111  }
1112  }
1113  }
1114  else
1115  {
1116  vpERROR_TRACE("OpenCV not initialized " ) ;
1118  "OpenCV not initialized")) ;
1119  }
1120 }
1133 void
1135  const vpImagePoint &bottomRight,
1136  const vpColor &color, bool fill,
1137  unsigned int thickness )
1138 {
1140  {
1141  if (fill == false) {
1142  if (color.id < vpColor::id_unknown) {
1143  cvRectangle( background,
1144  cvPoint( vpMath::round( topLeft.get_u() ),
1145  vpMath::round( topLeft.get_v() ) ),
1146  cvPoint( vpMath::round( bottomRight.get_u() ),
1147  vpMath::round( bottomRight.get_v() ) ),
1148  col[color.id], (int)thickness);
1149  }
1150  else {
1151  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1152  cvRectangle( background,
1153  cvPoint( vpMath::round( topLeft.get_u() ),
1154  vpMath::round( topLeft.get_v() ) ),
1155  cvPoint( vpMath::round( bottomRight.get_u() ),
1156  vpMath::round( bottomRight.get_v() ) ),
1157  cvcolor, (int)thickness);
1158  }
1159  }
1160  else {
1161  if (color.id < vpColor::id_unknown) {
1162  cvRectangle( background,
1163  cvPoint( vpMath::round( topLeft.get_u() ),
1164  vpMath::round( topLeft.get_v() ) ),
1165  cvPoint( vpMath::round( bottomRight.get_u() ),
1166  vpMath::round( bottomRight.get_v() ) ),
1167  col[color.id], CV_FILLED);
1168  }
1169  else {
1170  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1171  cvRectangle( background,
1172  cvPoint( vpMath::round( topLeft.get_u() ),
1173  vpMath::round( topLeft.get_v() ) ),
1174  cvPoint( vpMath::round( bottomRight.get_u() ),
1175  vpMath::round( bottomRight.get_v() ) ),
1176  cvcolor, CV_FILLED);
1177 
1178  }
1179  }
1180  }
1181  else
1182  {
1183  vpERROR_TRACE("OpenCV not initialized " ) ;
1185  "OpenCV not initialized")) ;
1186  }
1187 }
1188 
1201 void
1203  const vpColor &color, bool fill,
1204  unsigned int thickness)
1205 {
1207  {
1208  if (fill == false) {
1209  if (color.id < vpColor::id_unknown) {
1210  cvRectangle( background,
1211  cvPoint( vpMath::round( rectangle.getLeft() ),
1212  vpMath::round( rectangle.getBottom() ) ),
1213  cvPoint( vpMath::round( rectangle.getRight() ),
1214  vpMath::round( rectangle.getTop() ) ),
1215  col[color.id], (int)thickness);
1216  }
1217  else {
1218  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1219  cvRectangle( background,
1220  cvPoint( vpMath::round( rectangle.getLeft() ),
1221  vpMath::round( rectangle.getBottom() ) ),
1222  cvPoint( vpMath::round( rectangle.getRight() ),
1223  vpMath::round( rectangle.getTop() ) ),
1224  cvcolor, (int)thickness);
1225 
1226  }
1227  }
1228  else {
1229  if (color.id < vpColor::id_unknown) {
1230  cvRectangle( background,
1231  cvPoint( vpMath::round( rectangle.getLeft() ),
1232  vpMath::round( rectangle.getBottom() ) ),
1233  cvPoint( vpMath::round( rectangle.getRight() ),
1234  vpMath::round( rectangle.getTop() ) ),
1235  col[color.id], CV_FILLED);
1236  }
1237  else {
1238  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1239  cvRectangle( background,
1240  cvPoint( vpMath::round( rectangle.getLeft() ),
1241  vpMath::round( rectangle.getBottom() ) ),
1242  cvPoint( vpMath::round( rectangle.getRight() ),
1243  vpMath::round( rectangle.getTop() ) ),
1244  cvcolor, CV_FILLED);
1245  }
1246  }
1247  }
1248  else
1249  {
1250  vpERROR_TRACE("OpenCV not initialized " ) ;
1252  "OpenCV not initialized")) ;
1253  }
1254 }
1255 
1256 
1257 
1273 bool
1275 {
1276  bool ret = false;
1278  flushDisplay() ;
1279  if (blocking){
1280  lbuttondown = false;
1281  mbuttondown = false;
1282  rbuttondown = false;
1283  }
1284  do {
1285  if (lbuttondown){
1286  ret = true ;
1287  lbuttondown = false;
1288  }
1289  if (mbuttondown){
1290  ret = true ;
1291  mbuttondown = false;
1292  }
1293  if (rbuttondown){
1294  ret = true ;
1295  rbuttondown = false;
1296  }
1297  if (blocking) cvWaitKey(10);
1298  } while ( ret == false && blocking == true);
1299  }
1300  else {
1301  vpERROR_TRACE("OpenCV not initialized " ) ;
1303  "OpenCV not initialized")) ;
1304  }
1305  return ret;
1306 }
1307 
1325 bool
1327 {
1328  bool ret = false;
1329 
1331  flushDisplay() ;
1332 
1333  double u, v;
1334 
1335  if (blocking){
1336  lbuttondown = false;
1337  mbuttondown = false;
1338  rbuttondown = false;
1339  }
1340  do {
1341  if (lbuttondown){
1342  ret = true ;
1343  u = (unsigned int)x_lbuttondown;
1344  v = (unsigned int)y_lbuttondown;
1345  ip.set_u( u );
1346  ip.set_v( v );
1347  lbuttondown = false;
1348  }
1349  if (mbuttondown){
1350  ret = true ;
1351  u = (unsigned int)x_mbuttondown;
1352  v = (unsigned int)y_mbuttondown;
1353  ip.set_u( u );
1354  ip.set_v( v );
1355  mbuttondown = false;
1356  }
1357  if (rbuttondown){
1358  ret = true ;
1359  u = (unsigned int)x_rbuttondown;
1360  v = (unsigned int)y_rbuttondown;
1361  ip.set_u( u );
1362  ip.set_v( v );
1363  rbuttondown = false;
1364  }
1365  if (blocking) cvWaitKey(10);
1366  } while ( ret == false && blocking == true);
1367  }
1368  else {
1369  vpERROR_TRACE("OpenCV not initialized " ) ;
1371  "OpenCV not initialized")) ;
1372  }
1373  return ret ;
1374 }
1375 
1376 
1396 bool
1399  bool blocking)
1400 {
1401  bool ret = false;
1402 
1404  //flushDisplay() ;
1405  double u, v;
1406  if (blocking){
1407  lbuttondown = false;
1408  mbuttondown = false;
1409  rbuttondown = false;
1410  }
1411  do {
1412  if (lbuttondown){
1413  ret = true ;
1414  u = (unsigned int)x_lbuttondown;
1415  v = (unsigned int)y_lbuttondown;
1416  ip.set_u( u );
1417  ip.set_v( v );
1418  button = vpMouseButton::button1;
1419  lbuttondown = false;
1420  }
1421  if (mbuttondown){
1422  ret = true ;
1423  u = (unsigned int)x_mbuttondown;
1424  v = (unsigned int)y_mbuttondown;
1425  ip.set_u( u );
1426  ip.set_v( v );
1427  button = vpMouseButton::button2;
1428  mbuttondown = false;
1429  }
1430  if (rbuttondown){
1431  ret = true ;
1432  u = (unsigned int)x_rbuttondown;
1433  v = (unsigned int)y_rbuttondown;
1434  ip.set_u( u );
1435  ip.set_v( v );
1436  button = vpMouseButton::button3;
1437  rbuttondown = false;
1438  }
1439  if (blocking) cvWaitKey(10);
1440  } while ( ret == false && blocking == true);
1441  }
1442  else {
1443  vpERROR_TRACE("OpenCV not initialized " ) ;
1445  "OpenCV not initialized")) ;
1446  }
1447  return ret;
1448 }
1449 
1473 bool
1476  bool blocking)
1477 {
1478  bool ret = false;
1480  //flushDisplay() ;
1481  double u, v;
1482  if (blocking){
1483  lbuttonup = false;
1484  mbuttonup = false;
1485  rbuttonup = false;
1486  }
1487  do {
1488  if (lbuttonup){
1489  ret = true ;
1490  u = (unsigned int)x_lbuttonup;
1491  v = (unsigned int)y_lbuttonup;
1492  ip.set_u( u );
1493  ip.set_v( v );
1494  button = vpMouseButton::button1;
1495  lbuttonup = false;
1496  }
1497  if (mbuttonup){
1498  ret = true ;
1499  u = (unsigned int)x_mbuttonup;
1500  v = (unsigned int)y_mbuttonup;
1501  ip.set_u( u );
1502  ip.set_v( v );
1503  button = vpMouseButton::button2;
1504  mbuttonup = false;
1505  }
1506  if (rbuttonup){
1507  ret = true ;
1508  u = (unsigned int)x_rbuttonup;
1509  v = (unsigned int)y_rbuttonup;
1510  ip.set_u( u );
1511  ip.set_v( v );
1512  button = vpMouseButton::button3;
1513  rbuttonup = false;
1514  }
1515  if (blocking) cvWaitKey(10);
1516  } while ( ret == false && blocking == true);
1517  }
1518  else {
1519  vpERROR_TRACE ( "OpenCV not initialized " ) ;
1521  "OpenCV not initialized" ) ) ;
1522  }
1523  return ret;
1524 }
1525 
1526 /*
1527  \brief gets the displayed image (including the overlay plane)
1528  and returns an RGBa image
1529 */
1531 {
1532  vpImageConvert::convert(background,I);
1533  // should certainly be optimized.
1534 }
1535 
1536 void vpDisplayOpenCV::on_mouse( int event, int x, int y, int /*flags*/, void* display )
1537 {
1538  vpDisplayOpenCV* disp = (vpDisplayOpenCV*)display;
1539  switch ( event )
1540  {
1541  case CV_EVENT_MOUSEMOVE:
1542  {
1543  disp->move = true;
1544  disp->x_move = x;
1545  disp->y_move = y;
1546  break;
1547  }
1548  case CV_EVENT_LBUTTONDOWN:
1549  {
1550  disp->lbuttondown = true;
1551  disp->x_lbuttondown = x;
1552  disp->y_lbuttondown = y;
1553  break;
1554  }
1555  case CV_EVENT_MBUTTONDOWN:
1556  {
1557  disp->mbuttondown = true;
1558  disp->x_mbuttondown = x;
1559  disp->y_mbuttondown = y;
1560  break;
1561  }
1562  case CV_EVENT_RBUTTONDOWN:
1563  {
1564  disp->rbuttondown = true;
1565  disp->x_rbuttondown = x;
1566  disp->y_rbuttondown = y;
1567  break;
1568  }
1569  case CV_EVENT_LBUTTONUP:
1570  {
1571  disp->lbuttonup = true;
1572  disp->x_lbuttonup = x;
1573  disp->y_lbuttonup = y;
1574  break;
1575  }
1576  case CV_EVENT_MBUTTONUP:
1577  {
1578  disp->mbuttonup = true;
1579  disp->x_mbuttonup = x;
1580  disp->y_mbuttonup = y;
1581  break;
1582  }
1583  case CV_EVENT_RBUTTONUP:
1584  {
1585  disp->rbuttonup = true;
1586  disp->x_rbuttonup = x;
1587  disp->y_rbuttonup = y;
1588  break;
1589  }
1590 
1591  default :
1592  break;
1593  }
1594 }
1595 
1612 bool
1614 {
1615  int key_pressed;
1616  int delay;
1618  flushDisplay() ;
1619  if (blocking)
1620  delay = 0;
1621  else
1622  delay = 10;
1623 
1624  key_pressed = cvWaitKey(delay);
1625  if (key_pressed == -1)
1626  return false;
1627  return true;
1628  }
1629  else {
1630  vpERROR_TRACE("OpenCV not initialized " ) ;
1632  "OpenCV not initialized")) ;
1633  }
1634  //return false; // Never reached after throw()
1635 }
1655 bool
1656 vpDisplayOpenCV::getKeyboardEvent(char *string, bool blocking)
1657 {
1658  int key_pressed;
1659  int delay;
1661  flushDisplay() ;
1662  if (blocking)
1663  delay = 0;
1664  else
1665  delay = 10;
1666 
1667  key_pressed = cvWaitKey(delay);
1668  if (key_pressed == -1)
1669  return false;
1670  else {
1671  //std::cout << "Key pressed: \"" << key_pressed << "\"" << std::endl;
1672  sprintf(string, "%c", key_pressed);
1673  }
1674  return true;
1675  }
1676  else {
1677  vpERROR_TRACE("OpenCV not initialized " ) ;
1679  "OpenCV not initialized")) ;
1680  }
1681  //return false; // Never reached after throw()
1682 }
1683 
1696 bool
1698 {
1699  bool ret = false;
1700 
1702  //flushDisplay() ;
1703  double u, v;
1704  if (move){
1705  ret = true ;
1706  u = (unsigned int)x_move;
1707  v = (unsigned int)y_move;
1708  ip.set_u( u );
1709  ip.set_v( v );
1710  move = false;
1711  }
1712  }
1713 
1714  else {
1715  vpERROR_TRACE("OpenCV not initialized " ) ;
1717  "OpenCV not initialized")) ;
1718  }
1719  return ret;
1720 }
1721 
1732 bool
1734 {
1736  //vpTRACE("Not implemented yet");
1737  bool moved = getPointerMotionEvent(ip);
1738  if (!moved)
1739  {
1740  double u, v;
1741  u = (unsigned int)x_move;
1742  v = (unsigned int)y_move;
1743  ip.set_u( u );
1744  ip.set_v( v );
1745  }
1746  return false;
1747  }
1748  else {
1749  vpERROR_TRACE("OpenCV not initialized " ) ;
1751  "OpenCV not initialized")) ;
1752  }
1753  //return false; // Never reached after throw()
1754 }
1755 
1756 #endif
1757 
1758 /*
1759  * Local variables:
1760  * c-basic-offset: 2
1761  * End:
1762  */
void displayCircle(const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)
vpDisplay * display
Definition: vpImage.h:121
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:174
double get_v() const
Definition: vpImagePoint.h:263
void clearDisplay(const vpColor &color=vpColor::white)
unsigned int width
Definition: vpDisplay.h:185
double get_i() const
Definition: vpImagePoint.h:194
unsigned int getWidth() const
Definition: vpImage.h:159
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)
#define vpERROR_TRACE
Definition: vpDebug.h:395
unsigned char B
Blue component.
Definition: vpRGBa.h:148
#define vpTRACE
Definition: vpDebug.h:418
static const vpColor black
Definition: vpColor.h:161
static const vpColor darkRed
Definition: vpColor.h:168
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:125
double get_u() const
Definition: vpImagePoint.h:252
static const vpColor lightGray
Definition: vpColor.h:163
bool getPointerPosition(vpImagePoint &ip)
static const vpColor darkBlue
Definition: vpColor.h:174
bool displayHasBeenInitialized
display has been initialized
Definition: vpDisplay.h:180
unsigned char G
Green component.
Definition: vpRGBa.h:147
double getRight() const
Definition: vpRect.h:167
static const vpColor green
Definition: vpColor.h:170
static int round(const double x)
Definition: vpMath.h:228
double get_j() const
Definition: vpImagePoint.h:205
void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static const vpColor lightRed
Definition: vpColor.h:166
virtual ~vpDisplayOpenCV()
static const vpColor red
Definition: vpColor.h:167
static const vpColor orange
Definition: vpColor.h:177
std::string title_
Definition: vpDisplay.h:187
static void on_mouse(int event, int x, int y, int flags, void *param)
double getBottom() const
Definition: vpRect.h:103
vpColorIdentifier id
Definition: vpColor.h:156
void set_i(const double ii)
Definition: vpImagePoint.h:158
static const vpColor cyan
Definition: vpColor.h:176
static const vpColor lightGreen
Definition: vpColor.h:169
void flushDisplayROI(const vpImagePoint &iP, const unsigned int width, const unsigned int height)
void set_u(const double u)
Definition: vpImagePoint.h:216
static double sqr(double x)
Definition: vpMath.h:106
void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
unsigned int height
Definition: vpDisplay.h:186
The vpDisplayOpenCV allows to display image using the opencv library.
void set_v(const double v)
Definition: vpImagePoint.h:227
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:164
static const vpColor darkGray
Definition: vpColor.h:165
void set_j(const double jj)
Definition: vpImagePoint.h:169
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:133
void getImage(vpImage< vpRGBa > &I)
get the window pixmap and put it in vpRGBa image
int windowXPosition
display position
Definition: vpDisplay.h:182
unsigned char R
Red component.
Definition: vpRGBa.h:146
void setFont(const char *font)
unsigned int getHeight() const
Definition: vpImage.h:150
Defines a rectangle in the plane.
Definition: vpRect.h:85
static const vpColor darkGreen
Definition: vpColor.h:171
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:92
void setTitle(const char *title)
static const vpColor yellow
Definition: vpColor.h:175
static const vpColor lightBlue
Definition: vpColor.h:172
int windowYPosition
display position
Definition: vpDisplay.h:184
static const vpColor purple
Definition: vpColor.h:178
static const vpColor white
Definition: vpColor.h:162
double getLeft() const
Definition: vpRect.h:161
bool getKeyboardEvent(bool blocking=true)
void setWindowPosition(int winx, int winy)
static const vpColor blue
Definition: vpColor.h:173