Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
testCrop.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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  * Test for sub-image extraction.
32  */
33 
44 #include <visp3/core/vpDebug.h>
45 #include <visp3/core/vpImage.h>
46 #include <visp3/core/vpImageTools.h>
47 #include <visp3/core/vpIoTools.h>
48 #include <visp3/core/vpRect.h>
49 #include <visp3/io/vpImageIo.h>
50 #include <visp3/io/vpParseArgv.h>
51 
52 #include <stdlib.h>
53 
54 #ifdef ENABLE_VISP_NAMESPACE
55 using namespace VISP_NAMESPACE_NAME;
56 #endif
57 
58 // List of allowed command line options
59 #define GETOPTARGS "cdi:o:h"
60 
61 /*
62 
63  Print the program options.
64 
65  \param name : Program name.
66  \param badparam : Bad parameter name.
67  \param ipath: Input image path.
68  \param opath : Output image path.
69  \param user : Username.
70 
71  */
72 void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath,
73  const std::string &user)
74 {
75  fprintf(stdout, "\n\
76 Read an image from the disk (Klimt.pgm), crop a rectangular area\n\
77 and save the cropped image on the disk (Klimt_cropped.pgm).\n\
78 \n\
79 SYNOPSIS\n\
80  %s [-i <input image path>] [-o <output image path>]\n\
81  [-h]\n\
82 ",
83 name);
84 
85  fprintf(stdout, "\n\
86 OPTIONS: Default\n\
87  -i <input image path> %s\n\
88  Set image input path.\n\
89  From this path read \"Klimt/Klimt.pgm\"\n\
90  image.\n\
91  Setting the VISP_INPUT_IMAGE_PATH environment\n\
92  variable produces the same behaviour than using\n\
93  this option.\n\
94 \n\
95  -o <output image path> %s\n\
96  Set image output path.\n\
97  From this directory, creates the \"%s\"\n\
98  subdirectory depending on the username, where \n\
99  Klimt_cropped.pgm output image is written.\n\
100 \n\
101  -h\n\
102  Print the help.\n\n",
103  ipath.c_str(), opath.c_str(), user.c_str());
104 
105  if (badparam)
106  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
107 }
108 
121 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, const std::string &user)
122 {
123  const char *optarg_;
124  int c;
125  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
126 
127  switch (c) {
128  case 'i':
129  ipath = optarg_;
130  break;
131  case 'o':
132  opath = optarg_;
133  break;
134  case 'h':
135  usage(argv[0], nullptr, ipath, opath, user);
136  return false;
137  break;
138 
139  case 'c':
140  case 'd':
141  break;
142 
143  default:
144  usage(argv[0], optarg_, ipath, opath, user);
145  return false;
146  break;
147  }
148  }
149 
150  if ((c == 1) || (c == -1)) {
151  // standalone param or error
152  usage(argv[0], nullptr, ipath, opath, user);
153  std::cerr << "ERROR: " << std::endl;
154  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
155  return false;
156  }
157 
158  return true;
159 }
160 
161 int main(int argc, const char **argv)
162 {
163  try {
164  std::string env_ipath;
165  std::string opt_ipath;
166  std::string opt_opath;
167  std::string ipath;
168  std::string opath;
169  std::string filename;
170  std::string username;
171 
172  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
173  // environment variable value
174  env_ipath = vpIoTools::getViSPImagesDataPath();
175 
176  // Set the default input path
177  if (!env_ipath.empty())
178  ipath = env_ipath;
179 
180 // Set the default output path
181 #if defined(_WIN32)
182  opt_opath = "C:/temp";
183 #else
184  opt_opath = "/tmp";
185 #endif
186 
187  // Get the user login name
188  vpIoTools::getUserName(username);
189 
190  // Read the command line options
191  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
192  return EXIT_FAILURE;
193  }
194 
195  // Get the option values
196  if (!opt_ipath.empty())
197  ipath = opt_ipath;
198  if (!opt_opath.empty())
199  opath = opt_opath;
200 
201  // Append to the output path string, the login name of the user
202  opath = vpIoTools::createFilePath(opath, username);
203 
204  // Test if the output path exist. If no try to create it
205  if (vpIoTools::checkDirectory(opath) == false) {
206  try {
207  // Create the dirname
209  }
210  catch (...) {
211  usage(argv[0], nullptr, ipath, opt_opath, username);
212  std::cerr << std::endl << "ERROR:" << std::endl;
213  std::cerr << " Cannot create " << opath << std::endl;
214  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
215  return EXIT_FAILURE;
216  }
217  }
218 
219  // Compare ipath and env_ipath. If they differ, we take into account
220  // the input path coming from the command line option
221  if (opt_ipath.empty()) {
222  if (ipath != env_ipath) {
223  std::cout << std::endl << "WARNING: " << std::endl;
224  std::cout << " Since -i <visp image path=" << ipath << "> "
225  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
226  << " we skip the environment variable." << std::endl;
227  }
228  }
229 
230  // Test if an input path is set
231  if (opt_ipath.empty() && env_ipath.empty()) {
232  usage(argv[0], nullptr, ipath, opt_opath, username);
233  std::cerr << std::endl << "ERROR:" << std::endl;
234  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
235  << " environment variable to specify the location of the " << std::endl
236  << " image path where test images are located." << std::endl
237  << std::endl;
238  return EXIT_FAILURE;
239  }
240 
241  //
242  // Here starts really the test
243  //
244  vpImage<unsigned char> I; // Input image
245  vpImage<unsigned char> C; // Cropped output image
246 
247  // Read the input grey image from the disk
248  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
249  std::cout << "Read image: " << filename << std::endl;
250  vpImageIo::read(I, filename);
251 
252  // Specify the cropping area
253  vpRect roi;
254  roi.setLeft(-10.2);
255  roi.setTop(10.0);
256  roi.setWidth(I.getWidth());
257  roi.setHeight(20.0);
258 
259  // Create the cropped image
260  vpImageTools::crop(I, roi, C);
261 
262  // Write the cropped image on the disk
263  filename = vpIoTools::createFilePath(opath, "Klimt_crop.pgm");
264  std::cout << "Write cropped image: " << filename << std::endl;
265  vpImageIo::write(C, filename);
266  return EXIT_SUCCESS;
267  }
268  catch (const vpException &e) {
269  std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
270  return EXIT_FAILURE;
271  }
272 }
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const std::string & getStringMessage() const
Definition: vpException.cpp:67
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:291
static void crop(const vpImage< Type > &I, double roi_top, double roi_left, unsigned int roi_height, unsigned int roi_width, vpImage< Type > &crop, unsigned int v_scale=1, unsigned int h_scale=1)
Definition: vpImageTools.h:315
unsigned int getWidth() const
Definition: vpImage.h:242
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:396
static std::string getUserName()
Definition: vpIoTools.cpp:285
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:550
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
Defines a rectangle in the plane.
Definition: vpRect.h:79
void setHeight(double h)
Definition: vpRect.h:308
void setWidth(double w)
Definition: vpRect.h:378
void setTop(double pos)
Definition: vpRect.h:357
void setLeft(double pos)
Definition: vpRect.h:321