Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
displayXMulti.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 X11.
33 *
34*****************************************************************************/
44#include <visp3/core/vpConfig.h>
45#include <visp3/core/vpDebug.h>
46
47#ifdef VISP_HAVE_X11
48
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <visp3/core/vpImage.h>
53#include <visp3/gui/vpDisplayX.h>
54#include <visp3/io/vpImageIo.h>
55
56#include <visp3/core/vpIoTools.h>
57#include <visp3/io/vpParseArgv.h>
58
69// List of allowed command line options
70#define GETOPTARGS "cdi:o:h"
71
83void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
84{
85 fprintf(stdout, "\n\
86Read an image on the disk, display it using X11, display some\n\
87features (line, circle, characters) in overlay and finally write \n\
88the image and the overlayed features in an image on the disk.\n\
89\n\
90SYNOPSIS\n\
91 %s [-i <input image path>] [-o <output image path>]\n\
92 [-c] [-d] [-h]\n\
93",
94name);
95
96 fprintf(stdout, "\n\
97OPTIONS: Default\n\
98 -i <input image path> %s\n\
99 Set image input path.\n\
100 From this path read \"Klimt/Klimt.pgm\"\n\
101 and \"Klimt/Klimt.ppm\" images.\n\
102 Setting the VISP_INPUT_IMAGE_PATH environment\n\
103 variable produces the same behaviour than using\n\
104 this option.\n\
105\n\
106 -o <output image path> %s\n\
107 Set image output path.\n\
108 From this directory, creates the \"%s\"\n\
109 subdirectory depending on the username, where \n\
110 Klimt_grey.overlay.ppm output image is written.\n\
111\n\
112 -c\n\
113 Disable the mouse click. Useful to automate the \n\
114 execution of this program without human intervention.\n\
115\n\
116 -d \n\
117 Disable the image display. This can be useful \n\
118 for automatic tests using crontab under Unix or \n\
119 using the task manager under Windows.\n\
120\n\
121 -h\n\
122 Print the help.\n\n",
123 ipath.c_str(), opath.c_str(), user.c_str());
124
125 if (badparam) {
126 fprintf(stderr, "ERROR: \n");
127 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
128 }
129}
130
148bool 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], NULL, 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], NULL, 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
191int 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
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], NULL, 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 comming 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], NULL, 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 two color images
274 vpImage<vpRGBa> I1, I2;
275 vpImagePoint ip, ip1, ip2;
276
277 try {
278 // Load a grey image from the disk
279 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
280 vpImageIo::read(I1, filename);
281 }
282 catch (...) {
283 std::cerr << std::endl << "ERROR:" << std::endl;
284 std::cerr << " Cannot read " << filename << std::endl;
285 std::cerr << " Check your -i " << ipath << " option " << std::endl
286 << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
287 return EXIT_FAILURE;
288 }
289 try {
290 // Load a color image from the disk
291 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
292 vpImageIo::read(I2, filename);
293 }
294 catch (...) {
295 std::cerr << std::endl << "ERROR:" << std::endl;
296 std::cerr << " Cannot read " << filename << std::endl;
297 std::cerr << " Check your -i " << ipath << " option " << std::endl
298 << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
299 return EXIT_FAILURE;
300 }
301
302 // For each image, open a X11 display
303 vpDisplayX display1;
304 vpDisplayX display2;
305
306 if (opt_display) {
307 // Attach image 1 to display 1
308 display1.init(I1, 0, 0, "X11 Display 1...");
309 // Attach image 2 to display 2
310 display2.init(I2, 200, 200, "X11 Display 2...");
311 // Display the images
314
315 // In the first display, display in overlay horizontal red lines
316 for (unsigned int i = 0; i < I1.getHeight(); i += 20) {
317 ip1.set_i(i);
318 ip1.set_j(0);
319 ip2.set_i(i);
320 ip2.set_j(I1.getWidth());
322 }
323
324 // In the first display, display in overlay vertical green dot lines
325 for (unsigned int i = 0; i < I1.getWidth(); i += 20) {
326 ip1.set_i(0);
327 ip1.set_j(i);
328 ip2.set_i(I1.getWidth());
329 ip2.set_j(i);
331 }
332
333 // In the first display, display in overlay a blue arrow
334 ip1.set_i(0);
335 ip1.set_j(0);
336 ip2.set_i(100);
337 ip2.set_j(100);
339
340 // In the first display, display in overlay some circles. The
341 // position of the center is 200, 200 the radius is increased by 20
342 // pixels for each circle
343 for (unsigned int i = 0; i < 100; i += 20) {
344 ip.set_i(200);
345 ip.set_j(200);
347 }
348
349 // In the first display, display in overlay a yellow string
350 ip.set_i(100);
351 ip.set_j(100);
352 vpDisplay::displayText(I1, ip, "ViSP is a marvelous software", vpColor::blue);
353
354 // Flush displays. The displays must be flushed to show the overlay.
355 // without this line, nothing will be displayed.
358
359 // If click is allowed, wait for a blocking mouse click in the first
360 // display, to display a cross at the clicked pixel position
361 if (opt_click_allowed) {
362 std::cout << "\nA click in the first display to draw a cross..." << std::endl;
363 // Blocking wait for a click. Get the position of the selected pixel
364 // (i correspond to the row and j to the column coordinates in the
365 // image)
366 vpDisplay::getClick(I1, ip);
367 // Display a red cross on the click pixel position
368 std::cout << "Cross position: " << ip << std::endl;
371 }
372 else {
373 ip.set_i(50);
374 ip.set_j(50);
375 // Display a red cross at position ip in the first display
376 std::cout << "Cross position: " << ip << std::endl;
379 }
380
381 // Create a color image
382 vpImage<vpRGBa> Ioverlay;
383 // Updates the color image with the original loaded image 1 and the
384 // overlay
385 vpDisplay::getImage(I1, Ioverlay);
386
387 // Write the color image on the disk
388 filename = vpIoTools::createFilePath(odirname, "Klimt_grey.overlay.ppm");
389 vpImageIo::write(Ioverlay, filename);
390
391 // If click is allowed, wait for a mouse click to close the display
392 if (opt_click_allowed) {
393 std::cout << "\nA click in the second display to close the windows "
394 "and exit..."
395 << std::endl;
396 // Wait for a blocking mouse click
398 }
399 }
400 return EXIT_SUCCESS;
401 }
402 catch (const vpException &e) {
403 std::cout << "Catch an exception: " << e << std::endl;
404 return EXIT_FAILURE;
405 }
406}
407#else
408int main()
409{
410 std::cout << "You do not have X11 functionalities to display images..." << std::endl;
411 std::cout << "Tip if you are on a unix-like system:" << std::endl;
412 std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
413 return EXIT_SUCCESS;
414}
415#endif
static const vpColor red
Definition vpColor.h:211
static const vpColor blue
Definition vpColor.h:217
static const vpColor yellow
Definition vpColor.h:219
static const vpColor green
Definition vpColor.h:214
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition vpDisplayX.h:132
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="")
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)
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 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)
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
void set_j(double jj)
void set_i(double ii)
Definition of the vpImage class member functions.
Definition vpImage.h:135
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getHeight() const
Definition vpImage.h:184
static std::string getViSPImagesDataPath()
static bool checkDirectory(const std::string &dirname)
static std::string getUserName()
static std::string createFilePath(const std::string &parent, const std::string &child)
static void makeDirectory(const std::string &dirname)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)