ViSP  2.8.0
vpDisplayOpenCV.cpp
1 /****************************************************************************
2  *
3  * $Id: vpDisplayOpenCV.cpp 4317 2013-07-17 09:40:17Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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) : vpDisplay()
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) : vpDisplay()
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 ) : vpDisplay()
134 {
135  col = NULL;
136  background = NULL;
137  font = NULL;
138  windowXPosition = x;
139  windowYPosition = y;
140 
141  if (title != NULL)
142  strcpy (this->title, title);
143 }
144 
165 {
166  col = NULL;
167  background = NULL;
168  font = NULL;
169 }
170 
175 {
176  closeDisplay() ;
177  cvReleaseImage(&background);
178 }
179 
188 void
190  int x,
191  int y,
192  const char *title)
193 {
194 
195  if ((I.getHeight() == 0) || (I.getWidth()==0))
196  {
197  vpERROR_TRACE("Image not initialized " ) ;
199  "Image not initialized")) ;
200  }
201  init (I.getWidth(), I.getHeight(), x, y, title) ;
202  I.display = this ;
204 }
205 
215 void
217  int x,
218  int y,
219  const char *title)
220 {
221  if ((I.getHeight() == 0) || (I.getWidth()==0))
222  {
223  vpERROR_TRACE("Image not initialized " ) ;
225  "Image not initialized")) ;
226  }
227 
228  init (I.getWidth(), I.getHeight(), x, y, title) ;
229  I.display = this ;
231 }
232 
243 void
244 vpDisplayOpenCV::init(unsigned int width, unsigned int height,
245  int x, int y,
246  const char *title)
247 {
248  this->width = width;
249  this->height = height;
250 
251  if (x != -1)
252  this->windowXPosition = x;
253  if (y != -1)
254  this->windowYPosition = y;
255  int flags = CV_WINDOW_AUTOSIZE;
256 
257  if (title != NULL)
258  strcpy(this->title, title) ;
259 
260  /* Create the window*/
261  if (cvNamedWindow( this->title, flags ) < 0) {
262  vpERROR_TRACE("OpenCV was not built with a display device");
264  "OpenCV was not built with a display device")) ;
265  }
266  cvMoveWindow( this->title, this->windowXPosition, this->windowYPosition );
267  move = false;
268  lbuttondown = false;
269  mbuttondown = false;
270  rbuttondown = false;
271  lbuttonup = false;
272  mbuttonup = false;
273  rbuttonup = false;
274  cvSetMouseCallback( this->title, on_mouse, this );
275  /* Create background pixmap */
276 // background = cvCreateImage(cvSize((int)width,(int)height),IPL_DEPTH_8U,3);
277 //
278 // cvShowImage( this->title,background);
279 
280  col = new CvScalar[vpColor::id_unknown] ;
281 
282  /* Create color */
283  vpColor pcolor; // Predefined colors
284  pcolor = vpColor::lightBlue;
285  col[vpColor::id_lightBlue] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
286  pcolor = vpColor::blue;
287  col[vpColor::id_blue] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
288  pcolor = vpColor::darkBlue;
289  col[vpColor::id_darkBlue] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
290  pcolor = vpColor::lightRed;
291  col[vpColor::id_lightRed] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
292  pcolor = vpColor::red;
293  col[vpColor::id_red] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
294  pcolor = vpColor::darkRed;
295  col[vpColor::id_darkRed] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
296  pcolor = vpColor::lightGreen;
297  col[vpColor::id_lightGreen] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
298  pcolor = vpColor::green;
299  col[vpColor::id_green] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
300  pcolor = vpColor::darkGreen;
301  col[vpColor::id_darkGreen] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
302  pcolor = vpColor::yellow;
303  col[vpColor::id_yellow] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
304  pcolor = vpColor::cyan;
305  col[vpColor::id_cyan] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
306  pcolor = vpColor::orange;
307  col[vpColor::id_orange] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
308  pcolor = vpColor::purple;
309  col[vpColor::id_purple] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
310  pcolor = vpColor::white;
311  col[vpColor::id_white] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
312  pcolor = vpColor::black;
313  col[vpColor::id_black] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
314  pcolor = vpColor::lightGray;
315  col[vpColor::id_lightGray] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
316  pcolor = vpColor::gray;
317  col[vpColor::id_gray] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
318  pcolor = vpColor::darkGray;
319  col[vpColor::id_darkGray] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
320 
321  font = new CvFont;
322  cvInitFont( font, CV_FONT_HERSHEY_PLAIN, 0.70f,0.70f);
323 
324  CvSize fontSize;
325  int baseline;
326  cvGetTextSize( "A", font, &fontSize, &baseline );
327  fontHeight = fontSize.height + baseline;
329 }
330 
331 
347 void
348 vpDisplayOpenCV::setFont(const char * /* font */)
349 {
350  vpERROR_TRACE("Not yet implemented" ) ;
351 }
352 
360 void
361 vpDisplayOpenCV::setTitle(const char * /* title */)
362 {
363  static bool warn_displayed = false;
364  if (! warn_displayed) {
365  vpTRACE("Not implemented");
366  warn_displayed = true;
367  }
368 }
369 
370 
379 void vpDisplayOpenCV::setWindowPosition(int winx, int winy)
380 {
382  this->windowXPosition = winx;
383  this->windowYPosition = winy;
384  cvMoveWindow( this->title, winx, winy );
385  }
386  else
387  {
388  vpERROR_TRACE("OpenCV not initialized " ) ;
390  "OpenCV not initialized")) ;
391  }
392 }
405 {
407  {
408  vpImage<vpRGBa> Ic;
410  vpImageConvert::convert(Ic,background);
411  /* Copie de l'image dans le pixmap fond */
412  width = I.getWidth();
413  height = I.getHeight();
414  /* Le pixmap background devient le fond de la zone de dessin */
415 
416  /* Affichage */
417  //gdk_window_clear(window);
418  //gdk_flush();
419  }
420  else
421  {
422  vpERROR_TRACE("openCV not initialized " ) ;
424  "OpenCV not initialized")) ;
425  }
426 }
427 
445 void vpDisplayOpenCV::displayImageROI ( const vpImage<unsigned char> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
446 {
448  {
450  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),height,width,Itemp);
451  vpImage<vpRGBa> Ic;
452  vpImageConvert::convert(Itemp,Ic);
453 
454  CvSize size = cvSize((int)this->width, (int)this->height);
455  int depth = 8;
456  int channels = 3;
457  if (background != NULL){
458  if(background->nChannels != channels || background->depth != depth
459  || background->height != (int) I.getHeight() || background->width != (int) I.getWidth()){
460  if(background->nChannels != 0) cvReleaseImage(&background);
461  background = cvCreateImage( size, depth, channels );
462  }
463  }
464  else background = cvCreateImage( size, depth, channels );
465 
466  IplImage* Ip = NULL;
467  vpImageConvert::convert(Ic, Ip);
468 
469  unsigned char * input = (unsigned char*)Ip->imageData;
470  unsigned char * output = (unsigned char*)background->imageData;
471 
472  unsigned int iwidth = Ic.getWidth();
473 
474  output = output + (int)(iP.get_i()*3*this->width+ iP.get_j()*3);
475 
476  unsigned int i = 0;
477  while (i < height)
478  {
479  unsigned int j = 0;
480  while (j < width)
481  {
482  *(output+3*j) = *(input+j*3);
483  *(output+3*j+1) = *(input+j*3+1);
484  *(output+3*j+2) = *(input+j*3+2);
485  j++;
486  }
487  input = input + 3*iwidth;
488  output = output + 3*this->width;
489  i++;
490  }
491 
492  cvReleaseImage(&Ip);
493  }
494  else
495  {
496  vpERROR_TRACE("openCV not initialized " ) ;
498  "OpenCV not initialized")) ;
499  }
500 }
501 
502 
515 {
516 
518  {
519  /* Copie de l'image dans le pixmap fond */
520 
521  vpImageConvert::convert(I,background);
522  /* Copie de l'image dans le pixmap fond */
523  width = I.getWidth();
524  height = I.getHeight();
525 
526  }
527  else
528  {
529  vpERROR_TRACE("OpenCV not initialized " ) ;
531  "OpenCV not initialized")) ;
532  }
533 }
534 
552 void vpDisplayOpenCV::displayImageROI ( const vpImage<vpRGBa> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
553 {
555  {
556  vpImage<vpRGBa> Ic;
557  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),height,width,Ic);
558 
559  CvSize size = cvSize((int)this->width, (int)this->height);
560  int depth = 8;
561  int channels = 3;
562  if (background != NULL){
563  if(background->nChannels != channels || background->depth != depth
564  || background->height != (int) I.getHeight() || background->width != (int) I.getWidth()){
565  if(background->nChannels != 0) cvReleaseImage(&background);
566  background = cvCreateImage( size, depth, channels );
567  }
568  }
569  else background = cvCreateImage( size, depth, channels );
570 
571  IplImage* Ip = NULL;
572  vpImageConvert::convert(Ic, Ip);
573 
574  unsigned char * input = (unsigned char*)Ip->imageData;
575  unsigned char * output = (unsigned char*)background->imageData;
576 
577  unsigned int iwidth = Ic.getWidth();
578 
579  output = output + (int)(iP.get_i()*3*this->width+ iP.get_j()*3);
580 
581  unsigned int i = 0;
582  while (i < height)
583  {
584  unsigned int j = 0;
585  while (j < width)
586  {
587  *(output+3*j) = *(input+j*3);
588  *(output+3*j+1) = *(input+j*3+1);
589  *(output+3*j+2) = *(input+j*3+2);
590  j++;
591  }
592  input = input + 3*iwidth;
593  output = output + 3*this->width;
594  i++;
595  }
596 
597  cvReleaseImage(&Ip);
598  }
599  else
600  {
601  vpERROR_TRACE("openCV not initialized " ) ;
603  "OpenCV not initialized")) ;
604  }
605 }
606 
607 
613 void vpDisplayOpenCV::displayImage(const unsigned char * /* I */)
614 {
615  vpTRACE(" not implemented ") ;
616 }
617 
626 {
627  if (col != NULL)
628  {
629  delete [] col ; col = NULL ;
630  }
631  if (font != NULL)
632  {
633  delete font ;
634  font = NULL ;
635  }
636 
638  cvDestroyWindow( this->title );
639 
641  }
642 }
643 
644 
651 {
653  {
654  cvShowImage(this->title, background );
655  cvWaitKey(5);
656  }
657  else
658  {
659  vpERROR_TRACE("OpenCV not initialized " ) ;
661  "OpenCV not initialized")) ;
662  }
663 }
664 
670 void vpDisplayOpenCV::flushDisplayROI(const vpImagePoint &/*iP*/, const unsigned int /*width*/, const unsigned int /*height*/)
671 {
673  {
674  cvShowImage(this->title, background );
675  cvWaitKey(5);
676  }
677  else
678  {
679  vpERROR_TRACE("OpenCV not initialized " ) ;
681  "OpenCV not initialized")) ;
682  }
683 }
684 
685 
689 void vpDisplayOpenCV::clearDisplay(const vpColor & /* color */)
690 {
691  static bool warn_displayed = false;
692  if (! warn_displayed) {
693  vpTRACE("Not implemented");
694  warn_displayed = true;
695  }
696 }
697 
706  const vpImagePoint &ip2,
707  const vpColor &color,
708  unsigned int w, unsigned int h,
709  unsigned int thickness)
710 {
712  {
713  try{
714  double a = ip2.get_i() - ip1.get_i() ;
715  double b = ip2.get_j() - ip1.get_j() ;
716  double lg = sqrt(vpMath::sqr(a)+vpMath::sqr(b)) ;
717 
718  //if ((a==0)&&(b==0))
719  if ((std::fabs(a) <= std::numeric_limits<double>::epsilon())
720  &&(std::fabs(b)<= std::numeric_limits<double>::epsilon()))
721  {
722  // DisplayCrossLarge(i1,j1,3,col) ;
723  }
724  else
725  {
726  a /= lg ;
727  b /= lg ;
728 
729  vpImagePoint ip3;
730  ip3.set_i(ip2.get_i() - w*a);
731  ip3.set_j(ip2.get_j() - w*b);
732 
733  vpImagePoint ip4;
734  ip4.set_i( ip3.get_i() - b*h );
735  ip4.set_j( ip3.get_j() + a*h );
736 
737  displayLine ( ip2, ip4, color, thickness ) ;
738 
739  ip4.set_i( ip3.get_i() + b*h );
740  ip4.set_j( ip3.get_j() - a*h );
741 
742  displayLine ( ip2, ip4, color, thickness ) ;
743  displayLine ( ip1, ip2, color, thickness ) ;
744  }
745  }
746  catch (...)
747  {
748  vpERROR_TRACE("Error caught") ;
749  throw ;
750  }
751  }
752  else
753  {
754  vpERROR_TRACE("OpenCV not initialized " ) ;
756  "OpenCV not initialized")) ;
757  }
758 }
759 
772  const char *text,
773  const vpColor &color )
774 {
776  {
777  if (color.id < vpColor::id_unknown) {
778  cvPutText( background, text,
779  cvPoint( vpMath::round( ip.get_u() ),
780  vpMath::round( ip.get_v()+fontHeight ) ),
781  font, col[color.id] );
782  }
783  else {
784  cvcolor = CV_RGB(color.R, color.G, color.B) ;
785  cvPutText( background, text,
786  cvPoint( vpMath::round( ip.get_u() ),
787  vpMath::round( ip.get_v()+fontHeight ) ),
788  font, cvcolor );
789  }
790  }
791  else
792  {
793  vpERROR_TRACE("OpenCV not initialized " ) ;
795  "OpenCV not initialized")) ;
796  }
797 }
808  unsigned int radius,
809  const vpColor &color,
810  bool fill ,
811  unsigned int thickness)
812 {
814  {
815  if (fill == false) {
816  if (color.id < vpColor::id_unknown) {
817  cvCircle( background,
818  cvPoint( vpMath::round( center.get_u() ),
819  vpMath::round( center.get_v() ) ),
820  (int)radius, col[color.id], (int)thickness);
821  }
822  else {
823  cvcolor = CV_RGB(color.R, color.G, color.B) ;
824  cvCircle( background,
825  cvPoint( vpMath::round( center.get_u() ),
826  vpMath::round( center.get_v() ) ),
827  (int)radius, cvcolor, (int)thickness);
828  }
829  }
830  else {
831  if (color.id < vpColor::id_unknown) {
832  cvCircle( background,
833  cvPoint( vpMath::round( center.get_u() ),
834  vpMath::round( center.get_v() ) ),
835  (int)radius, col[color.id], CV_FILLED);
836  }
837  else {
838  cvcolor = CV_RGB(color.R, color.G, color.B) ;
839  cvCircle( background,
840  cvPoint( vpMath::round( center.get_u() ),
841  vpMath::round( center.get_v() ) ),
842  (int)radius, cvcolor, CV_FILLED);
843  }
844  }
845  }
846  else
847  {
848  vpERROR_TRACE("OpenCV not initialized " ) ;
850  "OpenCV not initialized")) ;
851  }
852 }
853 
861 void
863  unsigned int size,
864  const vpColor &color,
865  unsigned int thickness)
866 {
868  {
869  vpImagePoint top,bottom,left,right;
870  top.set_i(ip.get_i()-size/2);
871  top.set_j(ip.get_j());
872  bottom.set_i(ip.get_i()+size/2);
873  bottom.set_j(ip.get_j());
874  left.set_i(ip.get_i());
875  left.set_j(ip.get_j()-size/2);
876  right.set_i(ip.get_i());
877  right.set_j(ip.get_j()+size/2);
878  try{
879  displayLine(top, bottom, color, thickness) ;
880  displayLine(left, right, color, thickness) ;
881  }
882  catch (...)
883  {
884  vpERROR_TRACE("Error caught") ;
885  throw ;
886  }
887  }
888 
889  else
890  {
891  vpERROR_TRACE("OpenCV not initialized " ) ;
893  "OpenCV not initialized")) ;
894  }
895 
896 }
897 
908 void
910  const vpImagePoint &ip2,
911  const vpColor &color,
912  unsigned int thickness)
913 {
914 
916  {
917  vpTRACE("Dot lines are not yet implemented");
918  if (color.id < vpColor::id_unknown) {
919  cvLine( background,
920  cvPoint( vpMath::round( ip1.get_u() ),
921  vpMath::round( ip1.get_v() ) ),
922  cvPoint( vpMath::round( ip2.get_u() ),
923  vpMath::round( ip2.get_v() ) ),
924  col[color.id], (int) thickness);
925  }
926  else {
927  cvcolor = CV_RGB(color.R, color.G, color.B) ;
928  cvLine( background,
929  cvPoint( vpMath::round( ip1.get_u() ),
930  vpMath::round( ip1.get_v() ) ),
931  cvPoint( vpMath::round( ip2.get_u() ),
932  vpMath::round( ip2.get_v() ) ),
933  cvcolor, (int) thickness);
934  }
935  }
936  else
937  {
938  vpERROR_TRACE("OpenCV not initialized " ) ;
940  "OpenCV not initialized")) ;
941  }
942 }
943 
944 
951 void
953  const vpImagePoint &ip2,
954  const vpColor &color,
955  unsigned int thickness)
956 {
958  {
959  if (color.id < vpColor::id_unknown) {
960  cvLine( background,
961  cvPoint( vpMath::round( ip1.get_u() ),
962  vpMath::round( ip1.get_v() ) ),
963  cvPoint( vpMath::round( ip2.get_u() ),
964  vpMath::round( ip2.get_v() ) ),
965  col[color.id], (int) thickness);
966  }
967  else {
968  cvcolor = CV_RGB(color.R, color.G, color.B) ;
969  cvLine( background,
970  cvPoint( vpMath::round( ip1.get_u() ),
971  vpMath::round( ip1.get_v() ) ),
972  cvPoint( vpMath::round( ip2.get_u() ),
973  vpMath::round( ip2.get_v() ) ),
974  cvcolor, (int) thickness);
975  }
976  }
977  else
978  {
979  vpERROR_TRACE("OpenCV not initialized " ) ;
981  "OpenCV not initialized")) ;
982  }
983 }
984 
991  const vpColor &color)
992 {
994  {
995  displayLine(ip,ip,color,1);
996 // if (color.id < vpColor::id_unknown) {
997 // ((uchar*)(background->imageData
998 // + background->widthStep*vpMath::round( ip.get_i() )))
999 // [vpMath::round( ip.get_j()*3 )] = (uchar)col[color.id].val[0];
1000 //
1001 // ((uchar*)(background->imageData
1002 // + background->widthStep*vpMath::round( ip.get_i() )))
1003 // [vpMath::round( ip.get_j()*3+1 )] = (uchar)col[color.id].val[1];
1004 //
1005 // ((uchar*)(background->imageData
1006 // + background->widthStep*vpMath::round( ip.get_i() )))
1007 // [vpMath::round( ip.get_j()*3+2 )] = (uchar)col[color.id].val[2];
1008 // }
1009 // else {
1010 // cvcolor = CV_RGB(color.R, color.G, color.B) ;
1011 // ((uchar*)(background->imageData
1012 // + background->widthStep*vpMath::round( ip.get_i() )))
1013 // [vpMath::round( ip.get_j()*3 )] = (uchar)cvcolor.val[0];
1014 //
1015 // ((uchar*)(background->imageData
1016 // + background->widthStep*vpMath::round( ip.get_i() )))
1017 // [vpMath::round( ip.get_j()*3+1 )] = (uchar)cvcolor.val[1];
1018 //
1019 // ((uchar*)(background->imageData
1020 // + background->widthStep*vpMath::round( ip.get_i() )))
1021 // [vpMath::round( ip.get_j()*3+2 )] = (uchar)cvcolor.val[2];
1022 //
1023 // }
1024  }
1025  else
1026  {
1027  vpERROR_TRACE("OpenCV not initialized " ) ;
1029  "OpenCV not initialized")) ;
1030  }
1031 }
1032 
1046 void
1048  unsigned int width, unsigned int height,
1049  const vpColor &color, bool fill,
1050  unsigned int thickness)
1051 {
1053  {
1054  if (fill == false) {
1055  if (color.id < vpColor::id_unknown) {
1056  cvRectangle( background,
1057  cvPoint( vpMath::round( topLeft.get_u() ),
1058  vpMath::round( topLeft.get_v() ) ),
1059  cvPoint( vpMath::round( topLeft.get_u()+width ),
1060  vpMath::round( topLeft.get_v()+height ) ),
1061  col[color.id], (int)thickness);
1062  }
1063  else {
1064  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1065  cvRectangle( background,
1066  cvPoint( vpMath::round( topLeft.get_u() ),
1067  vpMath::round( topLeft.get_v() ) ),
1068  cvPoint( vpMath::round( topLeft.get_u()+width ),
1069  vpMath::round( topLeft.get_v()+height ) ),
1070  cvcolor, (int)thickness);
1071  }
1072  }
1073  else {
1074  if (color.id < vpColor::id_unknown) {
1075  cvRectangle( background,
1076  cvPoint( vpMath::round( topLeft.get_u() ),
1077  vpMath::round( topLeft.get_v() ) ),
1078  cvPoint( vpMath::round( topLeft.get_u()+width ),
1079  vpMath::round( topLeft.get_v()+height ) ),
1080  col[color.id], CV_FILLED);
1081  }
1082  else {
1083  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1084  cvRectangle( background,
1085  cvPoint( vpMath::round( topLeft.get_u() ),
1086  vpMath::round( topLeft.get_v() ) ),
1087  cvPoint( vpMath::round( topLeft.get_u()+width ),
1088  vpMath::round( topLeft.get_v()+height ) ),
1089  cvcolor, CV_FILLED);
1090 
1091  }
1092  }
1093  }
1094  else
1095  {
1096  vpERROR_TRACE("OpenCV not initialized " ) ;
1098  "OpenCV not initialized")) ;
1099  }
1100 }
1113 void
1115  const vpImagePoint &bottomRight,
1116  const vpColor &color, bool fill,
1117  unsigned int thickness )
1118 {
1120  {
1121  if (fill == false) {
1122  if (color.id < vpColor::id_unknown) {
1123  cvRectangle( background,
1124  cvPoint( vpMath::round( topLeft.get_u() ),
1125  vpMath::round( topLeft.get_v() ) ),
1126  cvPoint( vpMath::round( bottomRight.get_u() ),
1127  vpMath::round( bottomRight.get_v() ) ),
1128  col[color.id], (int)thickness);
1129  }
1130  else {
1131  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1132  cvRectangle( background,
1133  cvPoint( vpMath::round( topLeft.get_u() ),
1134  vpMath::round( topLeft.get_v() ) ),
1135  cvPoint( vpMath::round( bottomRight.get_u() ),
1136  vpMath::round( bottomRight.get_v() ) ),
1137  cvcolor, (int)thickness);
1138  }
1139  }
1140  else {
1141  if (color.id < vpColor::id_unknown) {
1142  cvRectangle( background,
1143  cvPoint( vpMath::round( topLeft.get_u() ),
1144  vpMath::round( topLeft.get_v() ) ),
1145  cvPoint( vpMath::round( bottomRight.get_u() ),
1146  vpMath::round( bottomRight.get_v() ) ),
1147  col[color.id], CV_FILLED);
1148  }
1149  else {
1150  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1151  cvRectangle( background,
1152  cvPoint( vpMath::round( topLeft.get_u() ),
1153  vpMath::round( topLeft.get_v() ) ),
1154  cvPoint( vpMath::round( bottomRight.get_u() ),
1155  vpMath::round( bottomRight.get_v() ) ),
1156  cvcolor, CV_FILLED);
1157 
1158  }
1159  }
1160  }
1161  else
1162  {
1163  vpERROR_TRACE("OpenCV not initialized " ) ;
1165  "OpenCV not initialized")) ;
1166  }
1167 }
1168 
1181 void
1183  const vpColor &color, bool fill,
1184  unsigned int thickness)
1185 {
1187  {
1188  if (fill == false) {
1189  if (color.id < vpColor::id_unknown) {
1190  cvRectangle( background,
1191  cvPoint( vpMath::round( rectangle.getLeft() ),
1192  vpMath::round( rectangle.getBottom() ) ),
1193  cvPoint( vpMath::round( rectangle.getRight() ),
1194  vpMath::round( rectangle.getTop() ) ),
1195  col[color.id], (int)thickness);
1196  }
1197  else {
1198  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1199  cvRectangle( background,
1200  cvPoint( vpMath::round( rectangle.getLeft() ),
1201  vpMath::round( rectangle.getBottom() ) ),
1202  cvPoint( vpMath::round( rectangle.getRight() ),
1203  vpMath::round( rectangle.getTop() ) ),
1204  cvcolor, (int)thickness);
1205 
1206  }
1207  }
1208  else {
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], CV_FILLED);
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, CV_FILLED);
1225  }
1226  }
1227  }
1228  else
1229  {
1230  vpERROR_TRACE("OpenCV not initialized " ) ;
1232  "OpenCV not initialized")) ;
1233  }
1234 }
1235 
1236 
1237 
1253 bool
1255 {
1256  bool ret = false;
1258  flushDisplay() ;
1259  if (blocking){
1260  lbuttondown = false;
1261  mbuttondown = false;
1262  rbuttondown = false;
1263  }
1264  do {
1265  if (lbuttondown){
1266  ret = true ;
1267  lbuttondown = false;
1268  }
1269  if (mbuttondown){
1270  ret = true ;
1271  mbuttondown = false;
1272  }
1273  if (rbuttondown){
1274  ret = true ;
1275  rbuttondown = false;
1276  }
1277  if (blocking) cvWaitKey(10);
1278  } while ( ret == false && blocking == true);
1279  }
1280  else {
1281  vpERROR_TRACE("OpenCV not initialized " ) ;
1283  "OpenCV not initialized")) ;
1284  }
1285  return ret;
1286 }
1287 
1305 bool
1307 {
1308  bool ret = false;
1309 
1311  flushDisplay() ;
1312 
1313  double u, v;
1314 
1315  if (blocking){
1316  lbuttondown = false;
1317  mbuttondown = false;
1318  rbuttondown = false;
1319  }
1320  do {
1321  if (lbuttondown){
1322  ret = true ;
1323  u = (unsigned int)x_lbuttondown;
1324  v = (unsigned int)y_lbuttondown;
1325  ip.set_u( u );
1326  ip.set_v( v );
1327  lbuttondown = false;
1328  }
1329  if (mbuttondown){
1330  ret = true ;
1331  u = (unsigned int)x_mbuttondown;
1332  v = (unsigned int)y_mbuttondown;
1333  ip.set_u( u );
1334  ip.set_v( v );
1335  mbuttondown = false;
1336  }
1337  if (rbuttondown){
1338  ret = true ;
1339  u = (unsigned int)x_rbuttondown;
1340  v = (unsigned int)y_rbuttondown;
1341  ip.set_u( u );
1342  ip.set_v( v );
1343  rbuttondown = false;
1344  }
1345  if (blocking) cvWaitKey(10);
1346  } while ( ret == false && blocking == true);
1347  }
1348  else {
1349  vpERROR_TRACE("OpenCV not initialized " ) ;
1351  "OpenCV not initialized")) ;
1352  }
1353  return ret ;
1354 }
1355 
1356 
1376 bool
1379  bool blocking)
1380 {
1381  bool ret = false;
1382 
1384  //flushDisplay() ;
1385  double u, v;
1386  if (blocking){
1387  lbuttondown = false;
1388  mbuttondown = false;
1389  rbuttondown = false;
1390  }
1391  do {
1392  if (lbuttondown){
1393  ret = true ;
1394  u = (unsigned int)x_lbuttondown;
1395  v = (unsigned int)y_lbuttondown;
1396  ip.set_u( u );
1397  ip.set_v( v );
1398  button = vpMouseButton::button1;
1399  lbuttondown = false;
1400  }
1401  if (mbuttondown){
1402  ret = true ;
1403  u = (unsigned int)x_mbuttondown;
1404  v = (unsigned int)y_mbuttondown;
1405  ip.set_u( u );
1406  ip.set_v( v );
1407  button = vpMouseButton::button2;
1408  mbuttondown = false;
1409  }
1410  if (rbuttondown){
1411  ret = true ;
1412  u = (unsigned int)x_rbuttondown;
1413  v = (unsigned int)y_rbuttondown;
1414  ip.set_u( u );
1415  ip.set_v( v );
1416  button = vpMouseButton::button3;
1417  rbuttondown = false;
1418  }
1419  if (blocking) cvWaitKey(10);
1420  } while ( ret == false && blocking == true);
1421  }
1422  else {
1423  vpERROR_TRACE("OpenCV not initialized " ) ;
1425  "OpenCV not initialized")) ;
1426  }
1427  return ret;
1428 }
1429 
1453 bool
1456  bool blocking)
1457 {
1458  bool ret = false;
1460  //flushDisplay() ;
1461  double u, v;
1462  if (blocking){
1463  lbuttonup = false;
1464  mbuttonup = false;
1465  rbuttonup = false;
1466  }
1467  do {
1468  if (lbuttonup){
1469  ret = true ;
1470  u = (unsigned int)x_lbuttonup;
1471  v = (unsigned int)y_lbuttonup;
1472  ip.set_u( u );
1473  ip.set_v( v );
1474  button = vpMouseButton::button1;
1475  lbuttonup = false;
1476  }
1477  if (mbuttonup){
1478  ret = true ;
1479  u = (unsigned int)x_mbuttonup;
1480  v = (unsigned int)y_mbuttonup;
1481  ip.set_u( u );
1482  ip.set_v( v );
1483  button = vpMouseButton::button2;
1484  mbuttonup = false;
1485  }
1486  if (rbuttonup){
1487  ret = true ;
1488  u = (unsigned int)x_rbuttonup;
1489  v = (unsigned int)y_rbuttonup;
1490  ip.set_u( u );
1491  ip.set_v( v );
1492  button = vpMouseButton::button3;
1493  rbuttonup = false;
1494  }
1495  if (blocking) cvWaitKey(10);
1496  } while ( ret == false && blocking == true);
1497  }
1498  else {
1499  vpERROR_TRACE ( "OpenCV not initialized " ) ;
1501  "OpenCV not initialized" ) ) ;
1502  }
1503  return ret;
1504 }
1505 
1506 /*
1507  \brief gets the displayed image (including the overlay plane)
1508  and returns an RGBa image
1509 */
1511 {
1512  vpImageConvert::convert(background,I);
1513  // should certainly be optimized.
1514 }
1515 
1516 void vpDisplayOpenCV::on_mouse( int event, int x, int y, int /*flags*/, void* display )
1517 {
1518  vpDisplayOpenCV* disp = (vpDisplayOpenCV*)display;
1519  switch ( event )
1520  {
1521  case CV_EVENT_MOUSEMOVE:
1522  {
1523  disp->move = true;
1524  disp->x_move = x;
1525  disp->y_move = y;
1526  break;
1527  }
1528  case CV_EVENT_LBUTTONDOWN:
1529  {
1530  disp->lbuttondown = true;
1531  disp->x_lbuttondown = x;
1532  disp->y_lbuttondown = y;
1533  break;
1534  }
1535  case CV_EVENT_MBUTTONDOWN:
1536  {
1537  disp->mbuttondown = true;
1538  disp->x_mbuttondown = x;
1539  disp->y_mbuttondown = y;
1540  break;
1541  }
1542  case CV_EVENT_RBUTTONDOWN:
1543  {
1544  disp->rbuttondown = true;
1545  disp->x_rbuttondown = x;
1546  disp->y_rbuttondown = y;
1547  break;
1548  }
1549  case CV_EVENT_LBUTTONUP:
1550  {
1551  disp->lbuttonup = true;
1552  disp->x_lbuttonup = x;
1553  disp->y_lbuttonup = y;
1554  break;
1555  }
1556  case CV_EVENT_MBUTTONUP:
1557  {
1558  disp->mbuttonup = true;
1559  disp->x_mbuttonup = x;
1560  disp->y_mbuttonup = y;
1561  break;
1562  }
1563  case CV_EVENT_RBUTTONUP:
1564  {
1565  disp->rbuttonup = true;
1566  disp->x_rbuttonup = x;
1567  disp->y_rbuttonup = y;
1568  break;
1569  }
1570 
1571  default :
1572  break;
1573  }
1574 }
1575 
1592 bool
1594 {
1595  int key_pressed;
1596  int delay;
1598  flushDisplay() ;
1599  if (blocking)
1600  delay = 0;
1601  else
1602  delay = 10;
1603 
1604  key_pressed = cvWaitKey(delay);
1605  if (key_pressed == -1)
1606  return false;
1607  return true;
1608  }
1609  else {
1610  vpERROR_TRACE("OpenCV not initialized " ) ;
1612  "OpenCV not initialized")) ;
1613  }
1614  //return false; // Never reached after throw()
1615 }
1635 bool
1636 vpDisplayOpenCV::getKeyboardEvent(char *string, bool blocking)
1637 {
1638  int key_pressed;
1639  int delay;
1641  flushDisplay() ;
1642  if (blocking)
1643  delay = 0;
1644  else
1645  delay = 10;
1646 
1647  key_pressed = cvWaitKey(delay);
1648  if (key_pressed == -1)
1649  return false;
1650  else {
1651  //std::cout << "Key pressed: \"" << key_pressed << "\"" << std::endl;
1652  sprintf(string, "%c", key_pressed);
1653  }
1654  return true;
1655  }
1656  else {
1657  vpERROR_TRACE("OpenCV not initialized " ) ;
1659  "OpenCV not initialized")) ;
1660  }
1661  //return false; // Never reached after throw()
1662 }
1663 
1676 bool
1678 {
1679  bool ret = false;
1680 
1682  //flushDisplay() ;
1683  double u, v;
1684  if (move){
1685  ret = true ;
1686  u = (unsigned int)x_move;
1687  v = (unsigned int)y_move;
1688  ip.set_u( u );
1689  ip.set_v( v );
1690  move = false;
1691  }
1692  }
1693 
1694  else {
1695  vpERROR_TRACE("OpenCV not initialized " ) ;
1697  "OpenCV not initialized")) ;
1698  }
1699  return ret;
1700 }
1701 
1712 bool
1714 {
1716  //vpTRACE("Not implemented yet");
1717  bool moved = getPointerMotionEvent(ip);
1718  if (!moved)
1719  {
1720  double u, v;
1721  u = (unsigned int)x_move;
1722  v = (unsigned int)y_move;
1723  ip.set_u( u );
1724  ip.set_v( v );
1725  }
1726  return false;
1727  }
1728  else {
1729  vpERROR_TRACE("OpenCV not initialized " ) ;
1731  "OpenCV not initialized")) ;
1732  }
1733  //return false; // Never reached after throw()
1734 }
1735 
1736 #endif
1737 
1738 /*
1739  * Local variables:
1740  * c-basic-offset: 2
1741  * End:
1742  */
void displayCircle(const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)
void set_j(const double j)
Definition: vpImagePoint.h:156
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:169
double get_v() const
Definition: vpImagePoint.h:250
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:175
void clearDisplay(const vpColor &color=vpColor::white)
unsigned int width
Definition: vpDisplay.h:187
double get_i() const
Definition: vpImagePoint.h:181
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:379
unsigned char B
Blue component.
Definition: vpRGBa.h:155
#define vpTRACE
Definition: vpDebug.h:401
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:239
void set_i(const double i)
Definition: vpImagePoint.h:145
static const vpColor lightGray
Definition: vpColor.h:163
bool getPointerPosition(vpImagePoint &ip)
char * title
display title
Definition: vpDisplay.h:185
static const vpColor darkBlue
Definition: vpColor.h:174
bool displayHasBeenInitialized
display has been initialized
Definition: vpDisplay.h:179
unsigned char G
Green component.
Definition: vpRGBa.h:154
double getRight() const
Definition: vpRect.h:162
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:192
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
static void on_mouse(int event, int x, int y, int flags, void *param)
double getBottom() const
Definition: vpRect.h:98
vpColorIdentifier id
Definition: vpColor.h:156
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:203
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:188
The vpDisplayOpenCV allows to display image using the opencv library.
void set_v(const double v)
Definition: vpImagePoint.h:214
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
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:181
unsigned char R
Red component.
Definition: vpRGBa.h:153
void setFont(const char *font)
unsigned int getHeight() const
Definition: vpImage.h:150
Defines a rectangle in the plane.
Definition: vpRect.h:82
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:183
static const vpColor purple
Definition: vpColor.h:178
static const vpColor white
Definition: vpColor.h:162
double getLeft() const
Definition: vpRect.h:156
bool getKeyboardEvent(bool blocking=true)
void setWindowPosition(int winx, int winy)
static const vpColor blue
Definition: vpColor.h:173