ViSP  2.7.0
vpDisplayOpenCV.cpp
1 /****************************************************************************
2  *
3  * $Id: vpDisplayOpenCV.cpp 4137 2013-02-14 06:56:53Z 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 
70 int vpDisplayOpenCV::count = 1;
82  int x,
83  int y,
84  const char *title) : vpDisplay()
85 {
86  col = NULL;
87  title = NULL ;
88  window = 0 ;
89  background = NULL;
90  font = NULL;
91  init(I, x, y, title) ;
92 }
93 
94 
104  int x,
105  int y,
106  const char *title)
107 {
108  col = NULL;
109  title = NULL ;
110  window = 0 ;
111  background = NULL;
112  font = NULL;
113  init(I, x, y, title) ;
114 }
115 
138 vpDisplayOpenCV::vpDisplayOpenCV ( int x, int y, const char *title )
139 {
140  col = NULL;
141  title = NULL ;
142  window = 0 ;
143  background = NULL;
144  font = NULL;
145  init(0, 0, x, y, title) ;
146 }
147 
168 {
169  windowXPosition = windowYPosition = -1 ;
170 
171  col = NULL;
172  title = NULL ;
173  window = 0 ;
174  background = NULL;
175  if (title != NULL)
176  {
177  delete [] title ;
178  title = NULL ;
179  }
180  font = NULL;
181  displayHasBeenInitialized = false ;
182 }
183 
188 {
189  closeDisplay() ;
190  cvReleaseImage(&background);
191 }
192 
201 void
203  int x,
204  int y,
205  const char *title)
206 {
207 
208  if ((I.getHeight() == 0) || (I.getWidth()==0))
209  {
210  vpERROR_TRACE("Image not initialized " ) ;
212  "Image not initialized")) ;
213  }
214  init (I.getWidth(), I.getHeight(), x, y, title) ;
215  I.display = this ;
217 }
218 
228 void
230  int x,
231  int y,
232  const char *title)
233 {
234  if ((I.getHeight() == 0) || (I.getWidth()==0))
235  {
236  vpERROR_TRACE("Image not initialized " ) ;
238  "Image not initialized")) ;
239  }
240 
241  init (I.getWidth(), I.getHeight(), x, y, title) ;
242  I.display = this ;
244 }
245 
254 void
255 vpDisplayOpenCV::init(unsigned int width, unsigned int height,
256  int x, int y,
257  const char *title)
258 {
259  this->width = width;
260  this->height = height;
261  this->windowXPosition = x;
262  this->windowYPosition = y;
263  int flags = CV_WINDOW_AUTOSIZE;
264  if (title != NULL)
265  {
266  if (this->title != NULL)
267  {
268  delete [] this->title ;
269  this->title = NULL ;
270  }
271  this->title = new char[strlen(title) + 1] ;
272  strcpy(this->title, title) ;
273  }
274  else{
275  if (this->title != NULL)
276  {
277  delete [] this->title ;
278  this->title = NULL ;
279  }
280  this->title = new char[50] ;
281  sprintf(this->title,"Unnamed ViSP display <%02d>",count) ;
282  }
283  count++;
284  /* Create the window*/
285  window = cvNamedWindow( this->title, flags );
286  cvMoveWindow( this->title, x, y );
287  move = false;
288  lbuttondown = false;
289  mbuttondown = false;
290  rbuttondown = false;
291  lbuttonup = false;
292  mbuttonup = false;
293  rbuttonup = false;
294  cvSetMouseCallback( this->title, on_mouse, this );
295  /* Create background pixmap */
296 // background = cvCreateImage(cvSize((int)width,(int)height),IPL_DEPTH_8U,3);
297 //
298 // cvShowImage( this->title,background);
299 
300  col = new CvScalar[vpColor::id_unknown] ;
301 
302  /* Create color */
303  vpColor pcolor; // Predefined colors
304  pcolor = vpColor::lightBlue;
305  col[vpColor::id_lightBlue] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
306  pcolor = vpColor::blue;
307  col[vpColor::id_blue] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
308  pcolor = vpColor::darkBlue;
309  col[vpColor::id_darkBlue] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
310  pcolor = vpColor::lightRed;
311  col[vpColor::id_lightRed] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
312  pcolor = vpColor::red;
313  col[vpColor::id_red] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
314  pcolor = vpColor::darkRed;
315  col[vpColor::id_darkRed] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
316  pcolor = vpColor::lightGreen;
317  col[vpColor::id_lightGreen] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
318  pcolor = vpColor::green;
319  col[vpColor::id_green] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
320  pcolor = vpColor::darkGreen;
321  col[vpColor::id_darkGreen] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
322  pcolor = vpColor::yellow;
323  col[vpColor::id_yellow] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
324  pcolor = vpColor::cyan;
325  col[vpColor::id_cyan] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
326  pcolor = vpColor::orange;
327  col[vpColor::id_orange] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
328  pcolor = vpColor::purple;
329  col[vpColor::id_purple] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
330  pcolor = vpColor::white;
331  col[vpColor::id_white] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
332  pcolor = vpColor::black;
333  col[vpColor::id_black] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
334  pcolor = vpColor::lightGray;
335  col[vpColor::id_lightGray] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
336  pcolor = vpColor::gray;
337  col[vpColor::id_gray] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
338  pcolor = vpColor::darkGray;
339  col[vpColor::id_darkGray] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
340 
341  font = new CvFont;
342  cvInitFont( font, CV_FONT_HERSHEY_PLAIN, 0.70f,0.70f);
343 
344  CvSize fontSize;
345  int baseline;
346  cvGetTextSize( "A", font, &fontSize, &baseline );
347  fontHeight = fontSize.height + baseline;
349 }
350 
351 
367 void
368 vpDisplayOpenCV::setFont(const char * /* font */)
369 {
370  vpERROR_TRACE("Not yet implemented" ) ;
371 }
372 
380 void
381 vpDisplayOpenCV::setTitle(const char * /* title */)
382 {
383  static bool warn_displayed = false;
384  if (! warn_displayed) {
385  vpTRACE("Not implemented");
386  warn_displayed = true;
387  }
388 #if 0
390  {
391  if (this->title != NULL) {
392  delete [] this->title ;
393  this->title = NULL ;
394  }
395  // fprintf(stdout, "len: %d\n", strlen(windowtitle)); fflush(stdout);
396  this->title = new char[strlen(windowtitle) + 1] ;
397  strcpy(this->title, windowtitle) ;
398  //cvMoveWindow( this->title, windowXPosition, windowYPosition);
399  cvMoveWindow(windowtitle , windowXPosition, windowYPosition);
400 
401  }
402  else
403  {
404  vpERROR_TRACE("OpenCV not initialized " ) ;
406  "OpenCV not initialized")) ;
407  }
408 #endif
409 }
410 
411 
420 void vpDisplayOpenCV::setWindowPosition(int winx, int winy)
421 {
423  this->windowXPosition = winx;
424  this->windowYPosition = winy;
425  cvMoveWindow( this->title, winx, winy );
426  }
427  else
428  {
429  vpERROR_TRACE("OpenCV not initialized " ) ;
431  "OpenCV not initialized")) ;
432  }
433 }
446 {
448  {
449  vpImage<vpRGBa> Ic;
451  vpImageConvert::convert(Ic,background);
452  /* Copie de l'image dans le pixmap fond */
453  width = I.getWidth();
454  height = I.getHeight();
455  /* Le pixmap background devient le fond de la zone de dessin */
456 
457  /* Affichage */
458  //gdk_window_clear(window);
459  //gdk_flush();
460  }
461  else
462  {
463  vpERROR_TRACE("openCV not initialized " ) ;
465  "OpenCV not initialized")) ;
466  }
467 }
468 
486 void vpDisplayOpenCV::displayImageROI ( const vpImage<unsigned char> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
487 {
489  {
491  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),height,width,Itemp);
492  vpImage<vpRGBa> Ic;
493  vpImageConvert::convert(Itemp,Ic);
494 
495  CvSize size = cvSize((int)this->width, (int)this->height);
496  int depth = 8;
497  int channels = 3;
498  if (background != NULL){
499  if(background->nChannels != channels || background->depth != depth
500  || background->height != (int) I.getHeight() || background->width != (int) I.getWidth()){
501  if(background->nChannels != 0) cvReleaseImage(&background);
502  background = cvCreateImage( size, depth, channels );
503  }
504  }
505  else background = cvCreateImage( size, depth, channels );
506 
507  IplImage* Ip = NULL;
508  vpImageConvert::convert(Ic, Ip);
509 
510  unsigned char * input = (unsigned char*)Ip->imageData;
511  unsigned char * output = (unsigned char*)background->imageData;
512 
513  unsigned int iwidth = Ic.getWidth();
514 
515  output = output + (int)(iP.get_i()*3*this->width+ iP.get_j()*3);
516 
517  unsigned int i = 0;
518  while (i < height)
519  {
520  unsigned int j = 0;
521  while (j < width)
522  {
523  *(output+3*j) = *(input+j*3);
524  *(output+3*j+1) = *(input+j*3+1);
525  *(output+3*j+2) = *(input+j*3+2);
526  j++;
527  }
528  input = input + 3*iwidth;
529  output = output + 3*this->width;
530  i++;
531  }
532 
533  cvReleaseImage(&Ip);
534  }
535  else
536  {
537  vpERROR_TRACE("openCV not initialized " ) ;
539  "OpenCV not initialized")) ;
540  }
541 }
542 
543 
556 {
557 
559  {
560  /* Copie de l'image dans le pixmap fond */
561 
562  vpImageConvert::convert(I,background);
563  /* Copie de l'image dans le pixmap fond */
564  width = I.getWidth();
565  height = I.getHeight();
566 
567  }
568  else
569  {
570  vpERROR_TRACE("OpenCV not initialized " ) ;
572  "OpenCV not initialized")) ;
573  }
574 }
575 
593 void vpDisplayOpenCV::displayImageROI ( const vpImage<vpRGBa> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
594 {
596  {
597  vpImage<vpRGBa> Ic;
598  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),height,width,Ic);
599 
600  CvSize size = cvSize((int)this->width, (int)this->height);
601  int depth = 8;
602  int channels = 3;
603  if (background != NULL){
604  if(background->nChannels != channels || background->depth != depth
605  || background->height != (int) I.getHeight() || background->width != (int) I.getWidth()){
606  if(background->nChannels != 0) cvReleaseImage(&background);
607  background = cvCreateImage( size, depth, channels );
608  }
609  }
610  else background = cvCreateImage( size, depth, channels );
611 
612  IplImage* Ip = NULL;
613  vpImageConvert::convert(Ic, Ip);
614 
615  unsigned char * input = (unsigned char*)Ip->imageData;
616  unsigned char * output = (unsigned char*)background->imageData;
617 
618  unsigned int iwidth = Ic.getWidth();
619 
620  output = output + (int)(iP.get_i()*3*this->width+ iP.get_j()*3);
621 
622  unsigned int i = 0;
623  while (i < height)
624  {
625  unsigned int j = 0;
626  while (j < width)
627  {
628  *(output+3*j) = *(input+j*3);
629  *(output+3*j+1) = *(input+j*3+1);
630  *(output+3*j+2) = *(input+j*3+2);
631  j++;
632  }
633  input = input + 3*iwidth;
634  output = output + 3*this->width;
635  i++;
636  }
637 
638  cvReleaseImage(&Ip);
639  }
640  else
641  {
642  vpERROR_TRACE("openCV not initialized " ) ;
644  "OpenCV not initialized")) ;
645  }
646 }
647 
648 
654 void vpDisplayOpenCV::displayImage(const unsigned char * /* I */)
655 {
656  vpTRACE(" not implemented ") ;
657 }
658 
667 {
668  if (col != NULL)
669  {
670  delete [] col ; col = NULL ;
671  }
672  if (font != NULL)
673  {
674  delete font ;
675  font = NULL ;
676  }
677 
678  if (window != 0)
679  {
680  cvDestroyWindow( title );
681  count--;
682  window = 0;
683  }
684  if (title != NULL)
685  {
686  delete [] title ;
687  title = NULL ;
688  }
689 
691 }
692 
693 
700 {
702  {
703  cvShowImage(title, background );
704  cvWaitKey(5);
705  }
706  else
707  {
708  vpERROR_TRACE("OpenCV not initialized " ) ;
710  "OpenCV not initialized")) ;
711  }
712 }
713 
719 void vpDisplayOpenCV::flushDisplayROI(const vpImagePoint &/*iP*/, const unsigned int /*width*/, const unsigned int /*height*/)
720 {
722  {
723  cvShowImage(title, background );
724  cvWaitKey(5);
725  }
726  else
727  {
728  vpERROR_TRACE("OpenCV not initialized " ) ;
730  "OpenCV not initialized")) ;
731  }
732 }
733 
734 
738 void vpDisplayOpenCV::clearDisplay(const vpColor & /* color */)
739 {
740  static bool warn_displayed = false;
741  if (! warn_displayed) {
742  vpTRACE("Not implemented");
743  warn_displayed = true;
744  }
745 }
746 
755  const vpImagePoint &ip2,
756  const vpColor &color,
757  unsigned int w, unsigned int h,
758  unsigned int thickness)
759 {
761  {
762  try{
763  double a = ip2.get_i() - ip1.get_i() ;
764  double b = ip2.get_j() - ip1.get_j() ;
765  double lg = sqrt(vpMath::sqr(a)+vpMath::sqr(b)) ;
766 
767  //if ((a==0)&&(b==0))
768  if ((std::fabs(a) <= std::numeric_limits<double>::epsilon())
769  &&(std::fabs(b)<= std::numeric_limits<double>::epsilon()))
770  {
771  // DisplayCrossLarge(i1,j1,3,col) ;
772  }
773  else
774  {
775  a /= lg ;
776  b /= lg ;
777 
778  vpImagePoint ip3;
779  ip3.set_i(ip2.get_i() - w*a);
780  ip3.set_j(ip2.get_j() - w*b);
781 
782  vpImagePoint ip4;
783  ip4.set_i( ip3.get_i() - b*h );
784  ip4.set_j( ip3.get_j() + a*h );
785 
786  displayLine ( ip2, ip4, color, thickness ) ;
787 
788  ip4.set_i( ip3.get_i() + b*h );
789  ip4.set_j( ip3.get_j() - a*h );
790 
791  displayLine ( ip2, ip4, color, thickness ) ;
792  displayLine ( ip1, ip2, color, thickness ) ;
793  }
794  }
795  catch (...)
796  {
797  vpERROR_TRACE("Error caught") ;
798  throw ;
799  }
800  }
801  else
802  {
803  vpERROR_TRACE("OpenCV not initialized " ) ;
805  "OpenCV not initialized")) ;
806  }
807 }
808 
821  const char *text,
822  const vpColor &color )
823 {
825  {
826  if (color.id < vpColor::id_unknown) {
827  cvPutText( background, text,
828  cvPoint( vpMath::round( ip.get_u() ),
829  vpMath::round( ip.get_v()+fontHeight ) ),
830  font, col[color.id] );
831  }
832  else {
833  cvcolor = CV_RGB(color.R, color.G, color.B) ;
834  cvPutText( background, text,
835  cvPoint( vpMath::round( ip.get_u() ),
836  vpMath::round( ip.get_v()+fontHeight ) ),
837  font, cvcolor );
838  }
839  }
840  else
841  {
842  vpERROR_TRACE("OpenCV not initialized " ) ;
844  "OpenCV not initialized")) ;
845  }
846 }
857  unsigned int radius,
858  const vpColor &color,
859  bool fill ,
860  unsigned int thickness)
861 {
863  {
864  if (fill == false) {
865  if (color.id < vpColor::id_unknown) {
866  cvCircle( background,
867  cvPoint( vpMath::round( center.get_u() ),
868  vpMath::round( center.get_v() ) ),
869  (int)radius, col[color.id], (int)thickness);
870  }
871  else {
872  cvcolor = CV_RGB(color.R, color.G, color.B) ;
873  cvCircle( background,
874  cvPoint( vpMath::round( center.get_u() ),
875  vpMath::round( center.get_v() ) ),
876  (int)radius, cvcolor, (int)thickness);
877  }
878  }
879  else {
880  if (color.id < vpColor::id_unknown) {
881  cvCircle( background,
882  cvPoint( vpMath::round( center.get_u() ),
883  vpMath::round( center.get_v() ) ),
884  (int)radius, col[color.id], CV_FILLED);
885  }
886  else {
887  cvcolor = CV_RGB(color.R, color.G, color.B) ;
888  cvCircle( background,
889  cvPoint( vpMath::round( center.get_u() ),
890  vpMath::round( center.get_v() ) ),
891  (int)radius, cvcolor, CV_FILLED);
892  }
893  }
894  }
895  else
896  {
897  vpERROR_TRACE("OpenCV not initialized " ) ;
899  "OpenCV not initialized")) ;
900  }
901 }
902 
910 void
912  unsigned int size,
913  const vpColor &color,
914  unsigned int thickness)
915 {
917  {
918  vpImagePoint top,bottom,left,right;
919  top.set_i(ip.get_i()-size/2);
920  top.set_j(ip.get_j());
921  bottom.set_i(ip.get_i()+size/2);
922  bottom.set_j(ip.get_j());
923  left.set_i(ip.get_i());
924  left.set_j(ip.get_j()-size/2);
925  right.set_i(ip.get_i());
926  right.set_j(ip.get_j()+size/2);
927  try{
928  displayLine(top, bottom, color, thickness) ;
929  displayLine(left, right, color, thickness) ;
930  }
931  catch (...)
932  {
933  vpERROR_TRACE("Error caught") ;
934  throw ;
935  }
936  }
937 
938  else
939  {
940  vpERROR_TRACE("OpenCV not initialized " ) ;
942  "OpenCV not initialized")) ;
943  }
944 
945 }
946 
957 void
959  const vpImagePoint &ip2,
960  const vpColor &color,
961  unsigned int thickness)
962 {
963 
965  {
966  vpTRACE("Dot lines are not yet implemented");
967  if (color.id < vpColor::id_unknown) {
968  cvLine( background,
969  cvPoint( vpMath::round( ip1.get_u() ),
970  vpMath::round( ip1.get_v() ) ),
971  cvPoint( vpMath::round( ip2.get_u() ),
972  vpMath::round( ip2.get_v() ) ),
973  col[color.id], (int) thickness);
974  }
975  else {
976  cvcolor = CV_RGB(color.R, color.G, color.B) ;
977  cvLine( background,
978  cvPoint( vpMath::round( ip1.get_u() ),
979  vpMath::round( ip1.get_v() ) ),
980  cvPoint( vpMath::round( ip2.get_u() ),
981  vpMath::round( ip2.get_v() ) ),
982  cvcolor, (int) thickness);
983  }
984  }
985  else
986  {
987  vpERROR_TRACE("OpenCV not initialized " ) ;
989  "OpenCV not initialized")) ;
990  }
991 }
992 
993 
1000 void
1002  const vpImagePoint &ip2,
1003  const vpColor &color,
1004  unsigned int thickness)
1005 {
1007  {
1008  if (color.id < vpColor::id_unknown) {
1009  cvLine( background,
1010  cvPoint( vpMath::round( ip1.get_u() ),
1011  vpMath::round( ip1.get_v() ) ),
1012  cvPoint( vpMath::round( ip2.get_u() ),
1013  vpMath::round( ip2.get_v() ) ),
1014  col[color.id], (int) thickness);
1015  }
1016  else {
1017  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1018  cvLine( background,
1019  cvPoint( vpMath::round( ip1.get_u() ),
1020  vpMath::round( ip1.get_v() ) ),
1021  cvPoint( vpMath::round( ip2.get_u() ),
1022  vpMath::round( ip2.get_v() ) ),
1023  cvcolor, (int) thickness);
1024  }
1025  }
1026  else
1027  {
1028  vpERROR_TRACE("OpenCV not initialized " ) ;
1030  "OpenCV not initialized")) ;
1031  }
1032 }
1033 
1040  const vpColor &color)
1041 {
1043  {
1044  displayLine(ip,ip,color,1);
1045 // if (color.id < vpColor::id_unknown) {
1046 // ((uchar*)(background->imageData
1047 // + background->widthStep*vpMath::round( ip.get_i() )))
1048 // [vpMath::round( ip.get_j()*3 )] = (uchar)col[color.id].val[0];
1049 //
1050 // ((uchar*)(background->imageData
1051 // + background->widthStep*vpMath::round( ip.get_i() )))
1052 // [vpMath::round( ip.get_j()*3+1 )] = (uchar)col[color.id].val[1];
1053 //
1054 // ((uchar*)(background->imageData
1055 // + background->widthStep*vpMath::round( ip.get_i() )))
1056 // [vpMath::round( ip.get_j()*3+2 )] = (uchar)col[color.id].val[2];
1057 // }
1058 // else {
1059 // cvcolor = CV_RGB(color.R, color.G, color.B) ;
1060 // ((uchar*)(background->imageData
1061 // + background->widthStep*vpMath::round( ip.get_i() )))
1062 // [vpMath::round( ip.get_j()*3 )] = (uchar)cvcolor.val[0];
1063 //
1064 // ((uchar*)(background->imageData
1065 // + background->widthStep*vpMath::round( ip.get_i() )))
1066 // [vpMath::round( ip.get_j()*3+1 )] = (uchar)cvcolor.val[1];
1067 //
1068 // ((uchar*)(background->imageData
1069 // + background->widthStep*vpMath::round( ip.get_i() )))
1070 // [vpMath::round( ip.get_j()*3+2 )] = (uchar)cvcolor.val[2];
1071 //
1072 // }
1073  }
1074  else
1075  {
1076  vpERROR_TRACE("OpenCV not initialized " ) ;
1078  "OpenCV not initialized")) ;
1079  }
1080 }
1081 
1095 void
1097  unsigned int width, unsigned int height,
1098  const vpColor &color, bool fill,
1099  unsigned int thickness)
1100 {
1102  {
1103  if (fill == false) {
1104  if (color.id < vpColor::id_unknown) {
1105  cvRectangle( background,
1106  cvPoint( vpMath::round( topLeft.get_u() ),
1107  vpMath::round( topLeft.get_v() ) ),
1108  cvPoint( vpMath::round( topLeft.get_u()+width ),
1109  vpMath::round( topLeft.get_v()+height ) ),
1110  col[color.id], (int)thickness);
1111  }
1112  else {
1113  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1114  cvRectangle( background,
1115  cvPoint( vpMath::round( topLeft.get_u() ),
1116  vpMath::round( topLeft.get_v() ) ),
1117  cvPoint( vpMath::round( topLeft.get_u()+width ),
1118  vpMath::round( topLeft.get_v()+height ) ),
1119  cvcolor, (int)thickness);
1120  }
1121  }
1122  else {
1123  if (color.id < vpColor::id_unknown) {
1124  cvRectangle( background,
1125  cvPoint( vpMath::round( topLeft.get_u() ),
1126  vpMath::round( topLeft.get_v() ) ),
1127  cvPoint( vpMath::round( topLeft.get_u()+width ),
1128  vpMath::round( topLeft.get_v()+height ) ),
1129  col[color.id], CV_FILLED);
1130  }
1131  else {
1132  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1133  cvRectangle( background,
1134  cvPoint( vpMath::round( topLeft.get_u() ),
1135  vpMath::round( topLeft.get_v() ) ),
1136  cvPoint( vpMath::round( topLeft.get_u()+width ),
1137  vpMath::round( topLeft.get_v()+height ) ),
1138  cvcolor, CV_FILLED);
1139 
1140  }
1141  }
1142  }
1143  else
1144  {
1145  vpERROR_TRACE("OpenCV not initialized " ) ;
1147  "OpenCV not initialized")) ;
1148  }
1149 }
1162 void
1164  const vpImagePoint &bottomRight,
1165  const vpColor &color, bool fill,
1166  unsigned int thickness )
1167 {
1169  {
1170  if (fill == false) {
1171  if (color.id < vpColor::id_unknown) {
1172  cvRectangle( background,
1173  cvPoint( vpMath::round( topLeft.get_u() ),
1174  vpMath::round( topLeft.get_v() ) ),
1175  cvPoint( vpMath::round( bottomRight.get_u() ),
1176  vpMath::round( bottomRight.get_v() ) ),
1177  col[color.id], (int)thickness);
1178  }
1179  else {
1180  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1181  cvRectangle( background,
1182  cvPoint( vpMath::round( topLeft.get_u() ),
1183  vpMath::round( topLeft.get_v() ) ),
1184  cvPoint( vpMath::round( bottomRight.get_u() ),
1185  vpMath::round( bottomRight.get_v() ) ),
1186  cvcolor, (int)thickness);
1187  }
1188  }
1189  else {
1190  if (color.id < vpColor::id_unknown) {
1191  cvRectangle( background,
1192  cvPoint( vpMath::round( topLeft.get_u() ),
1193  vpMath::round( topLeft.get_v() ) ),
1194  cvPoint( vpMath::round( bottomRight.get_u() ),
1195  vpMath::round( bottomRight.get_v() ) ),
1196  col[color.id], CV_FILLED);
1197  }
1198  else {
1199  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1200  cvRectangle( background,
1201  cvPoint( vpMath::round( topLeft.get_u() ),
1202  vpMath::round( topLeft.get_v() ) ),
1203  cvPoint( vpMath::round( bottomRight.get_u() ),
1204  vpMath::round( bottomRight.get_v() ) ),
1205  cvcolor, CV_FILLED);
1206 
1207  }
1208  }
1209  }
1210  else
1211  {
1212  vpERROR_TRACE("OpenCV not initialized " ) ;
1214  "OpenCV not initialized")) ;
1215  }
1216 }
1217 
1230 void
1232  const vpColor &color, bool fill,
1233  unsigned int thickness)
1234 {
1236  {
1237  if (fill == false) {
1238  if (color.id < vpColor::id_unknown) {
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  col[color.id], (int)thickness);
1245  }
1246  else {
1247  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1248  cvRectangle( background,
1249  cvPoint( vpMath::round( rectangle.getLeft() ),
1250  vpMath::round( rectangle.getBottom() ) ),
1251  cvPoint( vpMath::round( rectangle.getRight() ),
1252  vpMath::round( rectangle.getTop() ) ),
1253  cvcolor, (int)thickness);
1254 
1255  }
1256  }
1257  else {
1258  if (color.id < vpColor::id_unknown) {
1259  cvRectangle( background,
1260  cvPoint( vpMath::round( rectangle.getLeft() ),
1261  vpMath::round( rectangle.getBottom() ) ),
1262  cvPoint( vpMath::round( rectangle.getRight() ),
1263  vpMath::round( rectangle.getTop() ) ),
1264  col[color.id], CV_FILLED);
1265  }
1266  else {
1267  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1268  cvRectangle( background,
1269  cvPoint( vpMath::round( rectangle.getLeft() ),
1270  vpMath::round( rectangle.getBottom() ) ),
1271  cvPoint( vpMath::round( rectangle.getRight() ),
1272  vpMath::round( rectangle.getTop() ) ),
1273  cvcolor, CV_FILLED);
1274  }
1275  }
1276  }
1277  else
1278  {
1279  vpERROR_TRACE("OpenCV not initialized " ) ;
1281  "OpenCV not initialized")) ;
1282  }
1283 }
1284 
1285 
1286 
1302 bool
1304 {
1305  bool ret = false;
1307  flushDisplay() ;
1308  if (blocking){
1309  lbuttondown = false;
1310  mbuttondown = false;
1311  rbuttondown = false;
1312  }
1313  do {
1314  if (lbuttondown){
1315  ret = true ;
1316  lbuttondown = false;
1317  }
1318  if (mbuttondown){
1319  ret = true ;
1320  mbuttondown = false;
1321  }
1322  if (rbuttondown){
1323  ret = true ;
1324  rbuttondown = false;
1325  }
1326  if (blocking) cvWaitKey(10);
1327  } while ( ret == false && blocking == true);
1328  }
1329  else {
1330  vpERROR_TRACE("OpenCV not initialized " ) ;
1332  "OpenCV not initialized")) ;
1333  }
1334  return ret;
1335 }
1336 
1354 bool
1356 {
1357  bool ret = false;
1358 
1360  flushDisplay() ;
1361 
1362  double u, v;
1363 
1364  if (blocking){
1365  lbuttondown = false;
1366  mbuttondown = false;
1367  rbuttondown = false;
1368  }
1369  do {
1370  if (lbuttondown){
1371  ret = true ;
1372  u = (unsigned int)x_lbuttondown;
1373  v = (unsigned int)y_lbuttondown;
1374  ip.set_u( u );
1375  ip.set_v( v );
1376  lbuttondown = false;
1377  }
1378  if (mbuttondown){
1379  ret = true ;
1380  u = (unsigned int)x_mbuttondown;
1381  v = (unsigned int)y_mbuttondown;
1382  ip.set_u( u );
1383  ip.set_v( v );
1384  mbuttondown = false;
1385  }
1386  if (rbuttondown){
1387  ret = true ;
1388  u = (unsigned int)x_rbuttondown;
1389  v = (unsigned int)y_rbuttondown;
1390  ip.set_u( u );
1391  ip.set_v( v );
1392  rbuttondown = false;
1393  }
1394  if (blocking) cvWaitKey(10);
1395  } while ( ret == false && blocking == true);
1396  }
1397  else {
1398  vpERROR_TRACE("OpenCV not initialized " ) ;
1400  "OpenCV not initialized")) ;
1401  }
1402  return ret ;
1403 }
1404 
1405 
1425 bool
1428  bool blocking)
1429 {
1430  bool ret = false;
1431 
1433  //flushDisplay() ;
1434  double u, v;
1435  if (blocking){
1436  lbuttondown = false;
1437  mbuttondown = false;
1438  rbuttondown = false;
1439  }
1440  do {
1441  if (lbuttondown){
1442  ret = true ;
1443  u = (unsigned int)x_lbuttondown;
1444  v = (unsigned int)y_lbuttondown;
1445  ip.set_u( u );
1446  ip.set_v( v );
1447  button = vpMouseButton::button1;
1448  lbuttondown = false;
1449  }
1450  if (mbuttondown){
1451  ret = true ;
1452  u = (unsigned int)x_mbuttondown;
1453  v = (unsigned int)y_mbuttondown;
1454  ip.set_u( u );
1455  ip.set_v( v );
1456  button = vpMouseButton::button2;
1457  mbuttondown = false;
1458  }
1459  if (rbuttondown){
1460  ret = true ;
1461  u = (unsigned int)x_rbuttondown;
1462  v = (unsigned int)y_rbuttondown;
1463  ip.set_u( u );
1464  ip.set_v( v );
1465  button = vpMouseButton::button3;
1466  rbuttondown = false;
1467  }
1468  if (blocking) cvWaitKey(10);
1469  } while ( ret == false && blocking == true);
1470  }
1471  else {
1472  vpERROR_TRACE("OpenCV not initialized " ) ;
1474  "OpenCV not initialized")) ;
1475  }
1476  return ret;
1477 }
1478 
1502 bool
1505  bool blocking)
1506 {
1507  bool ret = false;
1509  //flushDisplay() ;
1510  double u, v;
1511  if (blocking){
1512  lbuttonup = false;
1513  mbuttonup = false;
1514  rbuttonup = false;
1515  }
1516  do {
1517  if (lbuttonup){
1518  ret = true ;
1519  u = (unsigned int)x_lbuttonup;
1520  v = (unsigned int)y_lbuttonup;
1521  ip.set_u( u );
1522  ip.set_v( v );
1523  button = vpMouseButton::button1;
1524  lbuttonup = false;
1525  }
1526  if (mbuttonup){
1527  ret = true ;
1528  u = (unsigned int)x_mbuttonup;
1529  v = (unsigned int)y_mbuttonup;
1530  ip.set_u( u );
1531  ip.set_v( v );
1532  button = vpMouseButton::button2;
1533  mbuttonup = false;
1534  }
1535  if (rbuttonup){
1536  ret = true ;
1537  u = (unsigned int)x_rbuttonup;
1538  v = (unsigned int)y_rbuttonup;
1539  ip.set_u( u );
1540  ip.set_v( v );
1541  button = vpMouseButton::button3;
1542  rbuttonup = false;
1543  }
1544  if (blocking) cvWaitKey(10);
1545  } while ( ret == false && blocking == true);
1546  }
1547  else {
1548  vpERROR_TRACE ( "OpenCV not initialized " ) ;
1550  "OpenCV not initialized" ) ) ;
1551  }
1552  return ret;
1553 }
1554 
1555 /*
1556  \brief gets the displayed image (including the overlay plane)
1557  and returns an RGBa image
1558 */
1560 {
1561  vpImageConvert::convert(background,I);
1562  // shoudl certainly be optimized.
1563 }
1564 
1565 void vpDisplayOpenCV::on_mouse( int event, int x, int y, int /*flags*/, void* display )
1566 {
1567  vpDisplayOpenCV* disp = (vpDisplayOpenCV*)display;
1568  switch ( event )
1569  {
1570  case CV_EVENT_MOUSEMOVE:
1571  {
1572  disp->move = true;
1573  disp->x_move = x;
1574  disp->y_move = y;
1575  break;
1576  }
1577  case CV_EVENT_LBUTTONDOWN:
1578  {
1579  disp->lbuttondown = true;
1580  disp->x_lbuttondown = x;
1581  disp->y_lbuttondown = y;
1582  break;
1583  }
1584  case CV_EVENT_MBUTTONDOWN:
1585  {
1586  disp->mbuttondown = true;
1587  disp->x_mbuttondown = x;
1588  disp->y_mbuttondown = y;
1589  break;
1590  }
1591  case CV_EVENT_RBUTTONDOWN:
1592  {
1593  disp->rbuttondown = true;
1594  disp->x_rbuttondown = x;
1595  disp->y_rbuttondown = y;
1596  break;
1597  }
1598  case CV_EVENT_LBUTTONUP:
1599  {
1600  disp->lbuttonup = true;
1601  disp->x_lbuttonup = x;
1602  disp->y_lbuttonup = y;
1603  break;
1604  }
1605  case CV_EVENT_MBUTTONUP:
1606  {
1607  disp->mbuttonup = true;
1608  disp->x_mbuttonup = x;
1609  disp->y_mbuttonup = y;
1610  break;
1611  }
1612  case CV_EVENT_RBUTTONUP:
1613  {
1614  disp->rbuttonup = true;
1615  disp->x_rbuttonup = x;
1616  disp->y_rbuttonup = y;
1617  break;
1618  }
1619 
1620  default :
1621  break;
1622  }
1623 }
1624 
1641 bool
1643 {
1644  int key_pressed;
1645  int delay;
1647  flushDisplay() ;
1648  if (blocking)
1649  delay = 0;
1650  else
1651  delay = 10;
1652 
1653  key_pressed = cvWaitKey(delay);
1654  if (key_pressed == -1)
1655  return false;
1656  return true;
1657  }
1658  else {
1659  vpERROR_TRACE("OpenCV not initialized " ) ;
1661  "OpenCV not initialized")) ;
1662  }
1663  //return false; // Never reached after throw()
1664 }
1684 bool
1685 vpDisplayOpenCV::getKeyboardEvent(char *string, bool blocking)
1686 {
1687  int key_pressed;
1688  int delay;
1690  flushDisplay() ;
1691  if (blocking)
1692  delay = 0;
1693  else
1694  delay = 10;
1695 
1696  key_pressed = cvWaitKey(delay);
1697  if (key_pressed == -1)
1698  return false;
1699  else {
1700  //std::cout << "Key pressed: \"" << key_pressed << "\"" << std::endl;
1701  sprintf(string, "%c", key_pressed);
1702  }
1703  return true;
1704  }
1705  else {
1706  vpERROR_TRACE("OpenCV not initialized " ) ;
1708  "OpenCV not initialized")) ;
1709  }
1710  //return false; // Never reached after throw()
1711 }
1712 
1725 bool
1727 {
1728  bool ret = false;
1729 
1731  //flushDisplay() ;
1732  double u, v;
1733  if (move){
1734  ret = true ;
1735  u = (unsigned int)x_move;
1736  v = (unsigned int)y_move;
1737  ip.set_u( u );
1738  ip.set_v( v );
1739  move = false;
1740  }
1741  }
1742 
1743  else {
1744  vpERROR_TRACE("OpenCV not initialized " ) ;
1746  "OpenCV not initialized")) ;
1747  }
1748  return ret;
1749 }
1750 
1761 bool
1763 {
1765  //vpTRACE("Not implemented yet");
1766  bool moved = getPointerMotionEvent(ip);
1767  if (!moved)
1768  {
1769  double u, v;
1770  u = (unsigned int)x_move;
1771  v = (unsigned int)y_move;
1772  ip.set_u( u );
1773  ip.set_v( v );
1774  }
1775  return false;
1776  }
1777  else {
1778  vpERROR_TRACE("OpenCV not initialized " ) ;
1780  "OpenCV not initialized")) ;
1781  }
1782  //return false; // Never reached after throw()
1783 }
1784 
1785 #endif
1786 
1787 /*
1788  * Local variables:
1789  * c-basic-offset: 2
1790  * End:
1791  */
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:116
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:167
void clearDisplay(const vpColor &color=vpColor::white)
unsigned int width
Definition: vpDisplay.h:179
double get_i() const
Definition: vpImagePoint.h:181
unsigned int getWidth() const
Definition: vpImage.h:154
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:159
static const vpColor darkRed
Definition: vpColor.h:166
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:123
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:161
bool getPointerPosition(vpImagePoint &ip)
char * title
display title
Definition: vpDisplay.h:177
static const vpColor darkBlue
Definition: vpColor.h:172
bool displayHasBeenInitialized
display has been initialized
Definition: vpDisplay.h:171
unsigned char G
Green component.
Definition: vpRGBa.h:154
double getRight() const
Definition: vpRect.h:162
static const vpColor green
Definition: vpColor.h:168
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:164
virtual ~vpDisplayOpenCV()
static const vpColor red
Definition: vpColor.h:165
static const vpColor orange
Definition: vpColor.h:175
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:154
static const vpColor cyan
Definition: vpColor.h:174
static const vpColor lightGreen
Definition: vpColor.h:167
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:180
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:162
static const vpColor darkGray
Definition: vpColor.h:163
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
unsigned char R
Red component.
Definition: vpRGBa.h:153
void setFont(const char *font)
unsigned int getHeight() const
Definition: vpImage.h:145
Defines a rectangle in the plane.
Definition: vpRect.h:82
static const vpColor darkGreen
Definition: vpColor.h:169
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:173
static const vpColor lightBlue
Definition: vpColor.h:170
static const vpColor purple
Definition: vpColor.h:176
static const vpColor white
Definition: vpColor.h:160
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:171