ViSP  2.6.2
vpDisplayOpenCV.cpp
1 /****************************************************************************
2  *
3  * $Id: vpDisplayOpenCV.cpp 3795 2012-06-18 12:22:23Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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  input = input;
516  output = output + (int)(iP.get_i()*3*this->width+ iP.get_j()*3);
517 
518  unsigned int i = 0;
519  while (i < height)
520  {
521  unsigned int j = 0;
522  while (j < width)
523  {
524  *(output+3*j) = *(input+j*3);
525  *(output+3*j+1) = *(input+j*3+1);
526  *(output+3*j+2) = *(input+j*3+2);
527  j++;
528  }
529  input = input + 3*iwidth;
530  output = output + 3*this->width;
531  i++;
532  }
533 
534  cvReleaseImage(&Ip);
535  }
536  else
537  {
538  vpERROR_TRACE("openCV not initialized " ) ;
540  "OpenCV not initialized")) ;
541  }
542 }
543 
544 
557 {
558 
560  {
561  /* Copie de l'image dans le pixmap fond */
562 
563  vpImageConvert::convert(I,background);
564  /* Copie de l'image dans le pixmap fond */
565  width = I.getWidth();
566  height = I.getHeight();
567 
568  }
569  else
570  {
571  vpERROR_TRACE("OpenCV not initialized " ) ;
573  "OpenCV not initialized")) ;
574  }
575 }
576 
594 void vpDisplayOpenCV::displayImageROI ( const vpImage<vpRGBa> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
595 {
597  {
598  vpImage<vpRGBa> Ic;
599  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),height,width,Ic);
600 
601  CvSize size = cvSize((int)this->width, (int)this->height);
602  int depth = 8;
603  int channels = 3;
604  if (background != NULL){
605  if(background->nChannels != channels || background->depth != depth
606  || background->height != (int) I.getHeight() || background->width != (int) I.getWidth()){
607  if(background->nChannels != 0) cvReleaseImage(&background);
608  background = cvCreateImage( size, depth, channels );
609  }
610  }
611  else background = cvCreateImage( size, depth, channels );
612 
613  IplImage* Ip = NULL;
614  vpImageConvert::convert(Ic, Ip);
615 
616  unsigned char * input = (unsigned char*)Ip->imageData;
617  unsigned char * output = (unsigned char*)background->imageData;
618 
619  unsigned int iwidth = Ic.getWidth();
620 
621  input = input;
622  output = output + (int)(iP.get_i()*3*this->width+ iP.get_j()*3);
623 
624  unsigned int i = 0;
625  while (i < height)
626  {
627  unsigned int j = 0;
628  while (j < width)
629  {
630  *(output+3*j) = *(input+j*3);
631  *(output+3*j+1) = *(input+j*3+1);
632  *(output+3*j+2) = *(input+j*3+2);
633  j++;
634  }
635  input = input + 3*iwidth;
636  output = output + 3*this->width;
637  i++;
638  }
639 
640  cvReleaseImage(&Ip);
641  }
642  else
643  {
644  vpERROR_TRACE("openCV not initialized " ) ;
646  "OpenCV not initialized")) ;
647  }
648 }
649 
650 
656 void vpDisplayOpenCV::displayImage(const unsigned char * /* I */)
657 {
658  vpTRACE(" not implemented ") ;
659 }
660 
669 {
670  if (col != NULL)
671  {
672  delete [] col ; col = NULL ;
673  }
674  if (font != NULL)
675  {
676  delete font ;
677  font = NULL ;
678  }
679 
680  if (window != 0)
681  {
682  cvDestroyWindow( title );
683  count--;
684  window = 0;
685  }
686  if (title != NULL)
687  {
688  delete [] title ;
689  title = NULL ;
690  }
691 
693 }
694 
695 
702 {
704  {
705  cvShowImage(title, background );
706  cvWaitKey(5);
707  }
708  else
709  {
710  vpERROR_TRACE("OpenCV not initialized " ) ;
712  "OpenCV not initialized")) ;
713  }
714 }
715 
721 void vpDisplayOpenCV::flushDisplayROI(const vpImagePoint &/*iP*/, const unsigned int /*width*/, const unsigned int /*height*/)
722 {
724  {
725  cvShowImage(title, background );
726  cvWaitKey(5);
727  }
728  else
729  {
730  vpERROR_TRACE("OpenCV not initialized " ) ;
732  "OpenCV not initialized")) ;
733  }
734 }
735 
736 
740 void vpDisplayOpenCV::clearDisplay(const vpColor & /* color */)
741 {
742  static bool warn_displayed = false;
743  if (! warn_displayed) {
744  vpTRACE("Not implemented");
745  warn_displayed = true;
746  }
747 }
748 
757  const vpImagePoint &ip2,
758  const vpColor &color,
759  unsigned int w, unsigned int h,
760  unsigned int thickness)
761 {
763  {
764  try{
765  double a = ip2.get_i() - ip1.get_i() ;
766  double b = ip2.get_j() - ip1.get_j() ;
767  double lg = sqrt(vpMath::sqr(a)+vpMath::sqr(b)) ;
768 
769  //if ((a==0)&&(b==0))
770  if ((std::fabs(a) <= std::numeric_limits<double>::epsilon())
771  &&(std::fabs(b)<= std::numeric_limits<double>::epsilon()))
772  {
773  // DisplayCrossLarge(i1,j1,3,col) ;
774  }
775  else
776  {
777  a /= lg ;
778  b /= lg ;
779 
780  vpImagePoint ip3;
781  ip3.set_i(ip2.get_i() - w*a);
782  ip3.set_j(ip2.get_j() - w*b);
783 
784  vpImagePoint ip4;
785  ip4.set_i( ip3.get_i() - b*h );
786  ip4.set_j( ip3.get_j() + a*h );
787 
788  displayLine ( ip2, ip4, color, thickness ) ;
789 
790  ip4.set_i( ip3.get_i() + b*h );
791  ip4.set_j( ip3.get_j() - a*h );
792 
793  displayLine ( ip2, ip4, color, thickness ) ;
794  displayLine ( ip1, ip2, color, thickness ) ;
795  }
796  }
797  catch (...)
798  {
799  vpERROR_TRACE("Error caught") ;
800  throw ;
801  }
802  }
803  else
804  {
805  vpERROR_TRACE("OpenCV not initialized " ) ;
807  "OpenCV not initialized")) ;
808  }
809 }
810 
823  const char *text,
824  const vpColor &color )
825 {
827  {
828  if (color.id < vpColor::id_unknown) {
829  cvPutText( background, text,
830  cvPoint( vpMath::round( ip.get_u() ),
831  vpMath::round( ip.get_v()+fontHeight ) ),
832  font, col[color.id] );
833  }
834  else {
835  cvcolor = CV_RGB(color.R, color.G, color.B) ;
836  cvPutText( background, text,
837  cvPoint( vpMath::round( ip.get_u() ),
838  vpMath::round( ip.get_v()+fontHeight ) ),
839  font, cvcolor );
840  }
841  }
842  else
843  {
844  vpERROR_TRACE("OpenCV not initialized " ) ;
846  "OpenCV not initialized")) ;
847  }
848 }
859  unsigned int radius,
860  const vpColor &color,
861  bool fill ,
862  unsigned int thickness)
863 {
865  {
866  if (fill == false) {
867  if (color.id < vpColor::id_unknown) {
868  cvCircle( background,
869  cvPoint( vpMath::round( center.get_u() ),
870  vpMath::round( center.get_v() ) ),
871  (int)radius, col[color.id], (int)thickness);
872  }
873  else {
874  cvcolor = CV_RGB(color.R, color.G, color.B) ;
875  cvCircle( background,
876  cvPoint( vpMath::round( center.get_u() ),
877  vpMath::round( center.get_v() ) ),
878  (int)radius, cvcolor, (int)thickness);
879  }
880  }
881  else {
882  if (color.id < vpColor::id_unknown) {
883  cvCircle( background,
884  cvPoint( vpMath::round( center.get_u() ),
885  vpMath::round( center.get_v() ) ),
886  (int)radius, col[color.id], CV_FILLED);
887  }
888  else {
889  cvcolor = CV_RGB(color.R, color.G, color.B) ;
890  cvCircle( background,
891  cvPoint( vpMath::round( center.get_u() ),
892  vpMath::round( center.get_v() ) ),
893  (int)radius, cvcolor, CV_FILLED);
894  }
895  }
896  }
897  else
898  {
899  vpERROR_TRACE("OpenCV not initialized " ) ;
901  "OpenCV not initialized")) ;
902  }
903 }
904 
912 void
914  unsigned int size,
915  const vpColor &color,
916  unsigned int thickness)
917 {
919  {
920  vpImagePoint top,bottom,left,right;
921  top.set_i(ip.get_i()-size/2);
922  top.set_j(ip.get_j());
923  bottom.set_i(ip.get_i()+size/2);
924  bottom.set_j(ip.get_j());
925  left.set_i(ip.get_i());
926  left.set_j(ip.get_j()-size/2);
927  right.set_i(ip.get_i());
928  right.set_j(ip.get_j()+size/2);
929  try{
930  displayLine(top, bottom, color, thickness) ;
931  displayLine(left, right, color, thickness) ;
932  }
933  catch (...)
934  {
935  vpERROR_TRACE("Error caught") ;
936  throw ;
937  }
938  }
939 
940  else
941  {
942  vpERROR_TRACE("OpenCV not initialized " ) ;
944  "OpenCV not initialized")) ;
945  }
946 
947 }
948 
959 void
961  const vpImagePoint &ip2,
962  const vpColor &color,
963  unsigned int thickness)
964 {
965 
967  {
968  vpTRACE("Dot lines are not yet implemented");
969  if (color.id < vpColor::id_unknown) {
970  cvLine( background,
971  cvPoint( vpMath::round( ip1.get_u() ),
972  vpMath::round( ip1.get_v() ) ),
973  cvPoint( vpMath::round( ip2.get_u() ),
974  vpMath::round( ip2.get_v() ) ),
975  col[color.id], (int) thickness);
976  }
977  else {
978  cvcolor = CV_RGB(color.R, color.G, color.B) ;
979  cvLine( background,
980  cvPoint( vpMath::round( ip1.get_u() ),
981  vpMath::round( ip1.get_v() ) ),
982  cvPoint( vpMath::round( ip2.get_u() ),
983  vpMath::round( ip2.get_v() ) ),
984  cvcolor, (int) thickness);
985  }
986  }
987  else
988  {
989  vpERROR_TRACE("OpenCV not initialized " ) ;
991  "OpenCV not initialized")) ;
992  }
993 }
994 
995 
1002 void
1004  const vpImagePoint &ip2,
1005  const vpColor &color,
1006  unsigned int thickness)
1007 {
1009  {
1010  if (color.id < vpColor::id_unknown) {
1011  cvLine( background,
1012  cvPoint( vpMath::round( ip1.get_u() ),
1013  vpMath::round( ip1.get_v() ) ),
1014  cvPoint( vpMath::round( ip2.get_u() ),
1015  vpMath::round( ip2.get_v() ) ),
1016  col[color.id], (int) thickness);
1017  }
1018  else {
1019  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1020  cvLine( background,
1021  cvPoint( vpMath::round( ip1.get_u() ),
1022  vpMath::round( ip1.get_v() ) ),
1023  cvPoint( vpMath::round( ip2.get_u() ),
1024  vpMath::round( ip2.get_v() ) ),
1025  cvcolor, (int) thickness);
1026  }
1027  }
1028  else
1029  {
1030  vpERROR_TRACE("OpenCV not initialized " ) ;
1032  "OpenCV not initialized")) ;
1033  }
1034 }
1035 
1042  const vpColor &color)
1043 {
1045  {
1046  displayLine(ip,ip,color,1);
1047 // if (color.id < vpColor::id_unknown) {
1048 // ((uchar*)(background->imageData
1049 // + background->widthStep*vpMath::round( ip.get_i() )))
1050 // [vpMath::round( ip.get_j()*3 )] = (uchar)col[color.id].val[0];
1051 //
1052 // ((uchar*)(background->imageData
1053 // + background->widthStep*vpMath::round( ip.get_i() )))
1054 // [vpMath::round( ip.get_j()*3+1 )] = (uchar)col[color.id].val[1];
1055 //
1056 // ((uchar*)(background->imageData
1057 // + background->widthStep*vpMath::round( ip.get_i() )))
1058 // [vpMath::round( ip.get_j()*3+2 )] = (uchar)col[color.id].val[2];
1059 // }
1060 // else {
1061 // cvcolor = CV_RGB(color.R, color.G, color.B) ;
1062 // ((uchar*)(background->imageData
1063 // + background->widthStep*vpMath::round( ip.get_i() )))
1064 // [vpMath::round( ip.get_j()*3 )] = (uchar)cvcolor.val[0];
1065 //
1066 // ((uchar*)(background->imageData
1067 // + background->widthStep*vpMath::round( ip.get_i() )))
1068 // [vpMath::round( ip.get_j()*3+1 )] = (uchar)cvcolor.val[1];
1069 //
1070 // ((uchar*)(background->imageData
1071 // + background->widthStep*vpMath::round( ip.get_i() )))
1072 // [vpMath::round( ip.get_j()*3+2 )] = (uchar)cvcolor.val[2];
1073 //
1074 // }
1075  }
1076  else
1077  {
1078  vpERROR_TRACE("OpenCV not initialized " ) ;
1080  "OpenCV not initialized")) ;
1081  }
1082 }
1083 
1097 void
1099  unsigned int width, unsigned int height,
1100  const vpColor &color, bool fill,
1101  unsigned int thickness)
1102 {
1104  {
1105  if (fill == false) {
1106  if (color.id < vpColor::id_unknown) {
1107  cvRectangle( background,
1108  cvPoint( vpMath::round( topLeft.get_u() ),
1109  vpMath::round( topLeft.get_v() ) ),
1110  cvPoint( vpMath::round( topLeft.get_u()+width ),
1111  vpMath::round( topLeft.get_v()+height ) ),
1112  col[color.id], (int)thickness);
1113  }
1114  else {
1115  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1116  cvRectangle( background,
1117  cvPoint( vpMath::round( topLeft.get_u() ),
1118  vpMath::round( topLeft.get_v() ) ),
1119  cvPoint( vpMath::round( topLeft.get_u()+width ),
1120  vpMath::round( topLeft.get_v()+height ) ),
1121  cvcolor, (int)thickness);
1122  }
1123  }
1124  else {
1125  if (color.id < vpColor::id_unknown) {
1126  cvRectangle( background,
1127  cvPoint( vpMath::round( topLeft.get_u() ),
1128  vpMath::round( topLeft.get_v() ) ),
1129  cvPoint( vpMath::round( topLeft.get_u()+width ),
1130  vpMath::round( topLeft.get_v()+height ) ),
1131  col[color.id], CV_FILLED);
1132  }
1133  else {
1134  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1135  cvRectangle( background,
1136  cvPoint( vpMath::round( topLeft.get_u() ),
1137  vpMath::round( topLeft.get_v() ) ),
1138  cvPoint( vpMath::round( topLeft.get_u()+width ),
1139  vpMath::round( topLeft.get_v()+height ) ),
1140  cvcolor, CV_FILLED);
1141 
1142  }
1143  }
1144  }
1145  else
1146  {
1147  vpERROR_TRACE("OpenCV not initialized " ) ;
1149  "OpenCV not initialized")) ;
1150  }
1151 }
1164 void
1166  const vpImagePoint &bottomRight,
1167  const vpColor &color, bool fill,
1168  unsigned int thickness )
1169 {
1171  {
1172  if (fill == false) {
1173  if (color.id < vpColor::id_unknown) {
1174  cvRectangle( background,
1175  cvPoint( vpMath::round( topLeft.get_u() ),
1176  vpMath::round( topLeft.get_v() ) ),
1177  cvPoint( vpMath::round( bottomRight.get_u() ),
1178  vpMath::round( bottomRight.get_v() ) ),
1179  col[color.id], (int)thickness);
1180  }
1181  else {
1182  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1183  cvRectangle( background,
1184  cvPoint( vpMath::round( topLeft.get_u() ),
1185  vpMath::round( topLeft.get_v() ) ),
1186  cvPoint( vpMath::round( bottomRight.get_u() ),
1187  vpMath::round( bottomRight.get_v() ) ),
1188  cvcolor, (int)thickness);
1189  }
1190  }
1191  else {
1192  if (color.id < vpColor::id_unknown) {
1193  cvRectangle( background,
1194  cvPoint( vpMath::round( topLeft.get_u() ),
1195  vpMath::round( topLeft.get_v() ) ),
1196  cvPoint( vpMath::round( bottomRight.get_u() ),
1197  vpMath::round( bottomRight.get_v() ) ),
1198  col[color.id], CV_FILLED);
1199  }
1200  else {
1201  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1202  cvRectangle( background,
1203  cvPoint( vpMath::round( topLeft.get_u() ),
1204  vpMath::round( topLeft.get_v() ) ),
1205  cvPoint( vpMath::round( bottomRight.get_u() ),
1206  vpMath::round( bottomRight.get_v() ) ),
1207  cvcolor, CV_FILLED);
1208 
1209  }
1210  }
1211  }
1212  else
1213  {
1214  vpERROR_TRACE("OpenCV not initialized " ) ;
1216  "OpenCV not initialized")) ;
1217  }
1218 }
1219 
1232 void
1234  const vpColor &color, bool fill,
1235  unsigned int thickness)
1236 {
1238  {
1239  if (fill == false) {
1240  if (color.id < vpColor::id_unknown) {
1241  cvRectangle( background,
1242  cvPoint( vpMath::round( rectangle.getLeft() ),
1243  vpMath::round( rectangle.getBottom() ) ),
1244  cvPoint( vpMath::round( rectangle.getRight() ),
1245  vpMath::round( rectangle.getTop() ) ),
1246  col[color.id], (int)thickness);
1247  }
1248  else {
1249  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1250  cvRectangle( background,
1251  cvPoint( vpMath::round( rectangle.getLeft() ),
1252  vpMath::round( rectangle.getBottom() ) ),
1253  cvPoint( vpMath::round( rectangle.getRight() ),
1254  vpMath::round( rectangle.getTop() ) ),
1255  cvcolor, (int)thickness);
1256 
1257  }
1258  }
1259  else {
1260  if (color.id < vpColor::id_unknown) {
1261  cvRectangle( background,
1262  cvPoint( vpMath::round( rectangle.getLeft() ),
1263  vpMath::round( rectangle.getBottom() ) ),
1264  cvPoint( vpMath::round( rectangle.getRight() ),
1265  vpMath::round( rectangle.getTop() ) ),
1266  col[color.id], CV_FILLED);
1267  }
1268  else {
1269  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1270  cvRectangle( background,
1271  cvPoint( vpMath::round( rectangle.getLeft() ),
1272  vpMath::round( rectangle.getBottom() ) ),
1273  cvPoint( vpMath::round( rectangle.getRight() ),
1274  vpMath::round( rectangle.getTop() ) ),
1275  cvcolor, CV_FILLED);
1276  }
1277  }
1278  }
1279  else
1280  {
1281  vpERROR_TRACE("OpenCV not initialized " ) ;
1283  "OpenCV not initialized")) ;
1284  }
1285 }
1286 
1287 
1288 
1304 bool
1306 {
1307  bool ret = false;
1309  flushDisplay() ;
1310  if (blocking){
1311  lbuttondown = false;
1312  mbuttondown = false;
1313  rbuttondown = false;
1314  }
1315  do {
1316  if (lbuttondown){
1317  ret = true ;
1318  lbuttondown = false;
1319  }
1320  if (mbuttondown){
1321  ret = true ;
1322  mbuttondown = false;
1323  }
1324  if (rbuttondown){
1325  ret = true ;
1326  rbuttondown = false;
1327  }
1328  if (blocking) cvWaitKey(10);
1329  } while ( ret == false && blocking == true);
1330  }
1331  else {
1332  vpERROR_TRACE("OpenCV not initialized " ) ;
1334  "OpenCV not initialized")) ;
1335  }
1336  return ret;
1337 }
1338 
1356 bool
1358 {
1359  bool ret = false;
1360 
1362  flushDisplay() ;
1363 
1364  double u, v;
1365 
1366  if (blocking){
1367  lbuttondown = false;
1368  mbuttondown = false;
1369  rbuttondown = false;
1370  }
1371  do {
1372  if (lbuttondown){
1373  ret = true ;
1374  u = (unsigned int)x_lbuttondown;
1375  v = (unsigned int)y_lbuttondown;
1376  ip.set_u( u );
1377  ip.set_v( v );
1378  lbuttondown = false;
1379  }
1380  if (mbuttondown){
1381  ret = true ;
1382  u = (unsigned int)x_mbuttondown;
1383  v = (unsigned int)y_mbuttondown;
1384  ip.set_u( u );
1385  ip.set_v( v );
1386  mbuttondown = false;
1387  }
1388  if (rbuttondown){
1389  ret = true ;
1390  u = (unsigned int)x_rbuttondown;
1391  v = (unsigned int)y_rbuttondown;
1392  ip.set_u( u );
1393  ip.set_v( v );
1394  rbuttondown = false;
1395  }
1396  if (blocking) cvWaitKey(10);
1397  } while ( ret == false && blocking == true);
1398  }
1399  else {
1400  vpERROR_TRACE("OpenCV not initialized " ) ;
1402  "OpenCV not initialized")) ;
1403  }
1404  return ret ;
1405 }
1406 
1407 
1427 bool
1430  bool blocking)
1431 {
1432  bool ret = false;
1433 
1435  //flushDisplay() ;
1436  double u, v;
1437  if (blocking){
1438  lbuttondown = false;
1439  mbuttondown = false;
1440  rbuttondown = false;
1441  }
1442  do {
1443  if (lbuttondown){
1444  ret = true ;
1445  u = (unsigned int)x_lbuttondown;
1446  v = (unsigned int)y_lbuttondown;
1447  ip.set_u( u );
1448  ip.set_v( v );
1449  button = vpMouseButton::button1;
1450  lbuttondown = false;
1451  }
1452  if (mbuttondown){
1453  ret = true ;
1454  u = (unsigned int)x_mbuttondown;
1455  v = (unsigned int)y_mbuttondown;
1456  ip.set_u( u );
1457  ip.set_v( v );
1458  button = vpMouseButton::button2;
1459  mbuttondown = false;
1460  }
1461  if (rbuttondown){
1462  ret = true ;
1463  u = (unsigned int)x_rbuttondown;
1464  v = (unsigned int)y_rbuttondown;
1465  ip.set_u( u );
1466  ip.set_v( v );
1467  button = vpMouseButton::button3;
1468  rbuttondown = false;
1469  }
1470  if (blocking) cvWaitKey(10);
1471  } while ( ret == false && blocking == true);
1472  }
1473  else {
1474  vpERROR_TRACE("OpenCV not initialized " ) ;
1476  "OpenCV not initialized")) ;
1477  }
1478  return ret;
1479 }
1480 
1504 bool
1507  bool blocking)
1508 {
1509  bool ret = false;
1511  //flushDisplay() ;
1512  double u, v;
1513  if (blocking){
1514  lbuttonup = false;
1515  mbuttonup = false;
1516  rbuttonup = false;
1517  }
1518  do {
1519  if (lbuttonup){
1520  ret = true ;
1521  u = (unsigned int)x_lbuttonup;
1522  v = (unsigned int)y_lbuttonup;
1523  ip.set_u( u );
1524  ip.set_v( v );
1525  button = vpMouseButton::button1;
1526  lbuttonup = false;
1527  }
1528  if (mbuttonup){
1529  ret = true ;
1530  u = (unsigned int)x_mbuttonup;
1531  v = (unsigned int)y_mbuttonup;
1532  ip.set_u( u );
1533  ip.set_v( v );
1534  button = vpMouseButton::button2;
1535  mbuttonup = false;
1536  }
1537  if (rbuttonup){
1538  ret = true ;
1539  u = (unsigned int)x_rbuttonup;
1540  v = (unsigned int)y_rbuttonup;
1541  ip.set_u( u );
1542  ip.set_v( v );
1543  button = vpMouseButton::button3;
1544  rbuttonup = false;
1545  }
1546  if (blocking) cvWaitKey(10);
1547  } while ( ret == false && blocking == true);
1548  }
1549  else {
1550  vpERROR_TRACE ( "OpenCV not initialized " ) ;
1552  "OpenCV not initialized" ) ) ;
1553  }
1554  return ret;
1555 }
1556 
1557 /*
1558  \brief gets the displayed image (including the overlay plane)
1559  and returns an RGBa image
1560 */
1562 {
1563  vpImageConvert::convert(background,I);
1564  // shoudl certainly be optimized.
1565 }
1566 
1567 void vpDisplayOpenCV::on_mouse( int event, int x, int y, int /*flags*/, void* display )
1568 {
1569  vpDisplayOpenCV* disp = (vpDisplayOpenCV*)display;
1570  switch ( event )
1571  {
1572  case CV_EVENT_MOUSEMOVE:
1573  {
1574  disp->move = true;
1575  disp->x_move = x;
1576  disp->y_move = y;
1577  break;
1578  }
1579  case CV_EVENT_LBUTTONDOWN:
1580  {
1581  disp->lbuttondown = true;
1582  disp->x_lbuttondown = x;
1583  disp->y_lbuttondown = y;
1584  break;
1585  }
1586  case CV_EVENT_MBUTTONDOWN:
1587  {
1588  disp->mbuttondown = true;
1589  disp->x_mbuttondown = x;
1590  disp->y_mbuttondown = y;
1591  break;
1592  }
1593  case CV_EVENT_RBUTTONDOWN:
1594  {
1595  disp->rbuttondown = true;
1596  disp->x_rbuttondown = x;
1597  disp->y_rbuttondown = y;
1598  break;
1599  }
1600  case CV_EVENT_LBUTTONUP:
1601  {
1602  disp->lbuttonup = true;
1603  disp->x_lbuttonup = x;
1604  disp->y_lbuttonup = y;
1605  break;
1606  }
1607  case CV_EVENT_MBUTTONUP:
1608  {
1609  disp->mbuttonup = true;
1610  disp->x_mbuttonup = x;
1611  disp->y_mbuttonup = y;
1612  break;
1613  }
1614  case CV_EVENT_RBUTTONUP:
1615  {
1616  disp->rbuttonup = true;
1617  disp->x_rbuttonup = x;
1618  disp->y_rbuttonup = y;
1619  break;
1620  }
1621 
1622  default :
1623  break;
1624  }
1625 }
1626 
1643 bool
1645 {
1646  int key_pressed;
1647  int delay;
1649  flushDisplay() ;
1650  if (blocking)
1651  delay = 0;
1652  else
1653  delay = 10;
1654 
1655  key_pressed = cvWaitKey(delay);
1656  if (key_pressed == -1)
1657  return false;
1658  return true;
1659  }
1660  else {
1661  vpERROR_TRACE("OpenCV not initialized " ) ;
1663  "OpenCV not initialized")) ;
1664  }
1665  //return false; // Never reached after throw()
1666 }
1686 bool
1687 vpDisplayOpenCV::getKeyboardEvent(char *string, bool blocking)
1688 {
1689  int key_pressed;
1690  int delay;
1692  flushDisplay() ;
1693  if (blocking)
1694  delay = 0;
1695  else
1696  delay = 10;
1697 
1698  key_pressed = cvWaitKey(delay);
1699  if (key_pressed == -1)
1700  return false;
1701  else {
1702  //std::cout << "Key pressed: \"" << key_pressed << "\"" << std::endl;
1703  sprintf(string, "%c", key_pressed);
1704  }
1705  return true;
1706  }
1707  else {
1708  vpERROR_TRACE("OpenCV not initialized " ) ;
1710  "OpenCV not initialized")) ;
1711  }
1712  //return false; // Never reached after throw()
1713 }
1714 
1727 bool
1729 {
1730  bool ret = false;
1731 
1733  //flushDisplay() ;
1734  double u, v;
1735  if (move){
1736  ret = true ;
1737  u = (unsigned int)x_move;
1738  v = (unsigned int)y_move;
1739  ip.set_u( u );
1740  ip.set_v( v );
1741  move = false;
1742  }
1743  }
1744 
1745  else {
1746  vpERROR_TRACE("OpenCV not initialized " ) ;
1748  "OpenCV not initialized")) ;
1749  }
1750  return ret;
1751 }
1752 
1763 bool
1765 {
1767  //vpTRACE("Not implemented yet");
1768  bool moved = getPointerMotionEvent(ip);
1769  if (!moved)
1770  {
1771  double u, v;
1772  u = (unsigned int)x_move;
1773  v = (unsigned int)y_move;
1774  ip.set_u( u );
1775  ip.set_v( v );
1776  }
1777  return false;
1778  }
1779  else {
1780  vpERROR_TRACE("OpenCV not initialized " ) ;
1782  "OpenCV not initialized")) ;
1783  }
1784  //return false; // Never reached after throw()
1785 }
1786 
1787 #endif
1788 
1789 /*
1790  * Local variables:
1791  * c-basic-offset: 2
1792  * End:
1793  */
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:129
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