Visual Servoing Platform  version 3.6.1 under development (2024-03-29)
displayOpenCV.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Read an image on the disk and display it using OpenCV.
33  *
34 *****************************************************************************/
44 #include <iostream>
45 #include <stdlib.h>
46 #include <visp3/core/vpConfig.h>
47 #include <visp3/core/vpDebug.h>
48 
49 #if defined(HAVE_OPENCV_HIGHGUI)
50 
51 #include <visp3/core/vpImage.h>
52 #include <visp3/core/vpIoTools.h>
53 #include <visp3/gui/vpDisplayOpenCV.h>
54 #include <visp3/io/vpImageIo.h>
55 #include <visp3/io/vpParseArgv.h>
56 
57 #include <visp3/core/vpTime.h>
58 
68 // List of allowed command line options
69 #define GETOPTARGS "cdi:o:p:h"
70 
82 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
83 {
84  fprintf(stdout, "\n\
85 Read an image on the disk, display it using OpenCV, display some\n\
86 features (line, circle, characters) in overlay and finally write \n\
87 the image and the overlayed features in an image on the disk.\n\
88 \n\
89 SYNOPSIS\n\
90  %s [-i <input image path>] [-o <output image path>]\n\
91  [-c] [-d] [-h]\n \
92 ",
93 name);
94 
95  fprintf(stdout, "\n\
96 OPTIONS: Default\n\
97  -i <input image path> %s\n\
98  Set image input path.\n\
99  From this path read \"Klimt/Klimt.pgm\"\n\
100  image.\n\
101  Setting the VISP_INPUT_IMAGE_PATH environment\n\
102  variable produces the same behaviour than using\n\
103  this option.\n\
104 \n\
105  -o <output image path> %s\n\
106  Set image output path.\n\
107  From this directory, creates the \"%s\"\n\
108  subdirectory depending on the username, where \n\
109  Klimt_grey.overlay.ppm output image is written.\n\
110 \n\
111  -c\n\
112  Disable the mouse click. Useful to automate the \n\
113  execution of this program without human intervention.\n\
114 \n\
115  -d \n\
116  Disable the image display. This can be useful \n\
117  for automatic tests using crontab under Unix or \n\
118  using the task manager under Windows.\n\
119 \n\
120  -h\n\
121  Print the help.\n\n",
122  ipath.c_str(), opath.c_str(), user.c_str());
123 
124  if (badparam) {
125  fprintf(stderr, "ERROR: \n");
126  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
127  }
128 }
129 
148 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, bool &click_allowed,
149  const std::string &user, bool &display)
150 {
151  const char *optarg_;
152  int c;
153  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
154 
155  switch (c) {
156  case 'c':
157  click_allowed = false;
158  break;
159  case 'd':
160  display = false;
161  break;
162  case 'i':
163  ipath = optarg_;
164  break;
165  case 'o':
166  opath = optarg_;
167  break;
168  case 'h':
169  usage(argv[0], nullptr, ipath, opath, user);
170  return false;
171  break;
172 
173  default:
174  usage(argv[0], optarg_, ipath, opath, user);
175  return false;
176  break;
177  }
178  }
179 
180  if ((c == 1) || (c == -1)) {
181  // standalone param or error
182  usage(argv[0], nullptr, ipath, opath, user);
183  std::cerr << "ERROR: " << std::endl;
184  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
185  return false;
186  }
187 
188  return true;
189 }
190 
191 int main(int argc, const char **argv)
192 {
193  try {
194  std::string env_ipath;
195  std::string opt_ipath;
196  std::string opt_opath;
197  std::string ipath;
198  std::string opath;
199  std::string filename;
200  std::string username;
201  bool opt_click_allowed = true;
202  bool opt_display = true;
203 
204  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
205  // environment variable value
206  env_ipath = vpIoTools::getViSPImagesDataPath();
207 
208  // Set the default input path
209  if (!env_ipath.empty())
210  ipath = env_ipath;
211 
212  // Set the default output path
213 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
214  opt_opath = "/tmp";
215 #elif defined(_WIN32)
216  opt_opath = "C:\\temp";
217 #endif
218 
219  // Get the user login name
220  vpIoTools::getUserName(username);
221 
222  // Read the command line options
223  if (getOptions(argc, argv, opt_ipath, opt_opath, opt_click_allowed, username, opt_display) == false) {
224  return EXIT_FAILURE;
225  }
226 
227  // Get the option values
228  if (!opt_ipath.empty())
229  ipath = opt_ipath;
230  if (!opt_opath.empty())
231  opath = opt_opath;
232 
233  // Append to the output path string, the login name of the user
234  std::string odirname = vpIoTools::createFilePath(opath, username);
235 
236  // Test if the output path exist. If no try to create it
237  if (vpIoTools::checkDirectory(odirname) == false) {
238  try {
239  // Create the dirname
240  vpIoTools::makeDirectory(odirname);
241  }
242  catch (...) {
243  usage(argv[0], nullptr, ipath, opath, username);
244  std::cerr << std::endl << "ERROR:" << std::endl;
245  std::cerr << " Cannot create " << odirname << std::endl;
246  std::cerr << " Check your -o " << opath << " option " << std::endl;
247  return EXIT_FAILURE;
248  }
249  }
250 
251  // Compare ipath and env_ipath. If they differ, we take into account
252  // the input path coming from the command line option
253  if (!opt_ipath.empty() && !env_ipath.empty()) {
254  if (ipath != env_ipath) {
255  std::cout << std::endl << "WARNING: " << std::endl;
256  std::cout << " Since -i <visp image path=" << ipath << "> "
257  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
258  << " we skip the environment variable." << std::endl;
259  }
260  }
261 
262  // Test if an input path is set
263  if (opt_ipath.empty() && env_ipath.empty()) {
264  usage(argv[0], nullptr, ipath, opath, username);
265  std::cerr << std::endl << "ERROR:" << std::endl;
266  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
267  << " environment variable to specify the location of the " << std::endl
268  << " image path where test images are located." << std::endl
269  << std::endl;
270  return EXIT_FAILURE;
271  }
272 
273  // Create a grey level image
275  vpImagePoint ip, ip1, ip2;
276 
277  // Load a grey image from the disk
278  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
279  vpImageIo::read(I, filename);
280 
281  // Create a display using X11
283 
284  if (opt_display) {
285  // For this grey level image, open a X11 display at position 100,100
286  // in the screen, and with title "X11 display"
287  display.init(I, 100, 100, "X11 display");
288 
289  // Display the image
291 
292  // Display in overlay a red cross at position 10,10 in the
293  // image. The lines are 10 pixels long
294  ip.set_i(100);
295  ip.set_j(10);
296 
298 
299  // Display in overlay horizontal red lines
300  for (unsigned i = 0; i < I.getHeight(); i += 20) {
301  ip1.set_i(i);
302  ip1.set_j(0);
303  ip2.set_i(i);
304  ip2.set_j(I.getWidth());
305  vpDisplay::displayLine(I, ip1, ip2, vpColor::red);
306  }
307 
308  // Display a ligne in the diagonal
309  ip1.set_i(-10);
310  ip1.set_j(-10);
311  ip2.set_i(I.getHeight() + 10);
312  ip2.set_j(I.getWidth() + 10);
313 
314  vpDisplay::displayLine(I, ip1, ip2, vpColor::red);
315 
316  // Display in overlay vertical green dot lines
317  for (unsigned i = 0; i < I.getWidth(); i += 20) {
318  ip1.set_i(0);
319  ip1.set_j(i);
320  ip2.set_i(I.getWidth());
321  ip2.set_j(i);
323  }
324 
325  // Display a transparent rectangle
326  ip.set_i(I.getHeight() - 45);
327  ip.set_j(-10);
328  vpDisplay::displayRectangle(I, ip, 60, 80, vpColor(vpColor::orange, 127), true, 2U);
329 
330  // Display a transparent circle onto the image
331  vpColor transparent_red(vpColor::red, 127);
332  vpDisplay::displayCircle(I, vpImagePoint(I.getHeight() / 3, I.getWidth() / 2), I.getHeight() / 4, transparent_red,
333  true);
334 
335  // Display a second transparent circle
336  vpColor very_transparent_blue(0, 0, 255, 63);
337  vpDisplay::displayCircle(I, vpImagePoint(2 * I.getHeight() / 3, I.getWidth() / 2), I.getHeight() / 4,
338  very_transparent_blue, true);
339 
340  // Display in overlay a blue arrow
341  ip1.set_i(0);
342  ip1.set_j(0);
343  ip2.set_i(100);
344  ip2.set_j(100);
346 
347  // Display in overlay some circles. The position of the center is 200,
348  // 200 the radius is increased by 20 pixels for each circle
349 
350  for (unsigned int i = 0; i < 100; i += 20) {
351  ip.set_i(80);
352  ip.set_j(80);
354  }
355 
356  ip.set_i(-10);
357  ip.set_j(300);
359 
360  // Display in overlay a yellow string
361  ip.set_i(85);
362  ip.set_j(100);
363  vpDisplay::displayText(I, ip, "ViSP is a marvelous software", vpColor::yellow);
364  // Flush the display
365  vpDisplay::flush(I);
366 
367  // Create a color image
368  vpImage<vpRGBa> Ioverlay;
369  // Updates the color image with the original loaded image and the
370  // overlay
371  vpDisplay::getImage(I, Ioverlay);
372 
373  // Write the color image on the disk
374  filename = vpIoTools::createFilePath(odirname, "Klimt_grey.overlay.ppm");
375  vpImageIo::write(Ioverlay, filename);
376 
377  // If click is allowed, wait for a mouse click to close the display
378  if (opt_click_allowed) {
379  std::cout << "\nA click to close the windows..." << std::endl;
380  // Wait for a blocking mouse click
382  }
383 
384  // Close the display
385  vpDisplay::close(I);
386  }
387 
388  // Create a color image
389  vpImage<vpRGBa> Irgba;
390 
391  // Load a grey image from the disk and convert it to a color image
392  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
393  vpImageIo::read(Irgba, filename);
394 
395  // Create a new display
396  vpDisplayOpenCV displayRGBa;
397 
398  if (opt_display) {
399  // For this color image, open a X11 display at position 100,100
400  // in the screen, and with title "X11 color display"
401  displayRGBa.init(Irgba, 100, 100, "X11 color display");
402 
403  // Display the color image
404  vpDisplay::display(Irgba);
405  vpDisplay::flush(Irgba);
406 
407  // If click is allowed, wait for a blocking mouse click to display a
408  // cross at the clicked pixel position
409  if (opt_click_allowed) {
410  std::cout << "\nA click to display a cross..." << std::endl;
411  // Blocking wait for a click. Get the position of the selected pixel
412  // (i correspond to the row and j to the column coordinates in the
413  // image)
414  vpDisplay::getClick(Irgba, ip);
415  // Display a red cross on the click pixel position
416  std::cout << "Cross position: " << ip << std::endl;
417  vpDisplay::displayCross(Irgba, ip, 15, vpColor::red);
418  }
419  else {
420  ip.set_i(10);
421  ip.set_j(20);
422  // Display a red cross at position i, j (i correspond to the row
423  // and j to the column coordinates in the image)
424  std::cout << "Cross position: " << ip << std::endl;
425  vpDisplay::displayCross(Irgba, ip, 15, vpColor::red);
426  }
427  // Flush the display. Sometimes the display content is
428  // bufferized. Force to display the content that has been bufferized.
429  vpDisplay::flush(Irgba);
430 
431  // Create a color image
432  vpImage<vpRGBa> Ioverlay;
433  // Updates the color image with the original loaded image and the
434  // overlay
435  vpDisplay::getImage(Irgba, Ioverlay);
436 
437  // Write the color image on the disk
438  filename = vpIoTools::createFilePath(odirname, "Klimt_color.overlay.ppm");
439  vpImageIo::write(Ioverlay, filename);
440 
441  // If click is allowed, wait for a blocking mouse click to exit.
442  if (opt_click_allowed) {
443  std::cout << "\nA click to exit the program..." << std::endl;
444  vpDisplay::getClick(Irgba);
445  std::cout << "Bye" << std::endl;
446  }
447  }
448  return EXIT_SUCCESS;
449  }
450  catch (const vpException &e) {
451  std::cout << "Catch an exception: " << e << std::endl;
452  return EXIT_FAILURE;
453  }
454 }
455 #else
456 int main()
457 {
458  std::cout << "You do not have OpenCV functionalities to display images..." << std::endl;
459  std::cout << "Tip:" << std::endl;
460  std::cout << "- Install OpenCV, configure again ViSP using cmake and build again this example" << std::endl;
461  return EXIT_SUCCESS;
462 }
463 #endif
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:152
static const vpColor red
Definition: vpColor.h:211
static const vpColor orange
Definition: vpColor.h:221
static const vpColor blue
Definition: vpColor.h:217
static const vpColor yellow
Definition: vpColor.h:219
static const vpColor green
Definition: vpColor.h:214
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="") vp_override
static void close(vpImage< unsigned char > &I)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void displayCircle(const vpImage< unsigned char > &I, const vpImageCircle &circle, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void display(const vpImage< unsigned char > &I)
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
static void getImage(const vpImage< unsigned char > &Is, vpImage< vpRGBa > &Id)
Definition: vpDisplay.cpp:138
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
static void displayArrow(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color=vpColor::white, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)
static void displayDotLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
static void displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:59
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:143
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:287
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
void set_j(double jj)
Definition: vpImagePoint.h:304
void set_i(double ii)
Definition: vpImagePoint.h:293
unsigned int getWidth() const
Definition: vpImage.h:245
unsigned int getHeight() const
Definition: vpImage.h:184
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1781
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:818
static std::string getUserName()
Definition: vpIoTools.cpp:711
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:2142
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:967
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.