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