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