Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
displayXMulti.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Read an image on the disk and display it using X11.
32 */
42#include <visp3/core/vpConfig.h>
43#include <visp3/core/vpDebug.h>
44
45#ifdef VISP_HAVE_X11
46
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include <visp3/core/vpImage.h>
51#include <visp3/gui/vpDisplayX.h>
52#include <visp3/io/vpImageIo.h>
53
54#include <visp3/core/vpIoTools.h>
55#include <visp3/io/vpParseArgv.h>
56
66
67// List of allowed command line options
68#define GETOPTARGS "cdi:o:h"
69
70#ifdef ENABLE_VISP_NAMESPACE
71using namespace VISP_NAMESPACE_NAME;
72#endif
73
85void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath, const std::string &user)
86{
87 fprintf(stdout, "\n\
88Read an image on the disk, display it using X11, display some\n\
89features (line, circle, characters) in overlay and finally write \n\
90the image and the overlayed features in an image on the disk.\n\
91\n\
92SYNOPSIS\n\
93 %s [-i <input image path>] [-o <output image path>]\n\
94 [-c] [-d] [-h]\n\
95",
96name);
97
98 fprintf(stdout, "\n\
99OPTIONS: Default\n\
100 -i <input image path> %s\n\
101 Set image input path.\n\
102 From this path read \"Klimt/Klimt.pgm\"\n\
103 and \"Klimt/Klimt.ppm\" images.\n\
104 Setting the VISP_INPUT_IMAGE_PATH environment\n\
105 variable produces the same behaviour than using\n\
106 this option.\n\
107\n\
108 -o <output image path> %s\n\
109 Set image output path.\n\
110 From this directory, creates the \"%s\"\n\
111 subdirectory depending on the username, where \n\
112 Klimt_grey.overlay.ppm output image is written.\n\
113\n\
114 -c\n\
115 Disable the mouse click. Useful to automate the \n\
116 execution of this program without human intervention.\n\
117\n\
118 -d \n\
119 Disable the image display. This can be useful \n\
120 for automatic tests using crontab under Unix or \n\
121 using the task manager under Windows.\n\
122\n\
123 -h\n\
124 Print the help.\n\n",
125 ipath.c_str(), opath.c_str(), user.c_str());
126
127 if (badparam) {
128 fprintf(stderr, "ERROR: \n");
129 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
130 }
131}
132
150bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, bool &click_allowed,
151 const std::string &user, bool &display)
152{
153 const char *optarg_;
154 int c;
155 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
156
157 switch (c) {
158 case 'c':
159 click_allowed = false;
160 break;
161 case 'd':
162 display = false;
163 break;
164 case 'i':
165 ipath = optarg_;
166 break;
167 case 'o':
168 opath = optarg_;
169 break;
170 case 'h':
171 usage(argv[0], nullptr, ipath, opath, user);
172 return false;
173
174 default:
175 usage(argv[0], optarg_, ipath, opath, user);
176 return false;
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
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], 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 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:198
static const vpColor blue
Definition vpColor.h:204
static const vpColor yellow
Definition vpColor.h:206
static const vpColor green
Definition vpColor.h:201
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition vpDisplayX.h:135
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="") VP_OVERRIDE
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:60
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:131
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getHeight() const
Definition vpImage.h:181
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)