Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
testCropAdvanced.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 #include <visp3/core/vpImageTools.h>
40 #include <visp3/core/vpIoTools.h>
41 #include <visp3/io/vpImageIo.h>
42 #include <visp3/io/vpParseArgv.h>
43 
53 // List of allowed command line options
54 #define GETOPTARGS "cdi:o:h"
55 
56 /*
57 
58  Print the program options.
59 
60  \param name : Program name.
61  \param badparam : Bad parameter name.
62  \param ipath: Input image path.
63  \param opath : Output image path.
64  \param user : Username.
65 
66  */
67 void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath,
68  const std::string &user)
69 {
70  fprintf(stdout, "\n\
71 Read an image from the disk (Klimt.pgm and klimp.ppm), crop a rectangular area\n\
72 and check the resultings images.\n\
73 \n\
74 SYNOPSIS\n\
75  %s [-i <input image path>] [-o <output image path>] [-h]\n", name);
76 
77  fprintf(stdout, "\n\
78 OPTIONS: Default\n\
79  -i <input image path> %s\n\
80  Set image input path.\n\
81  From this path read \"Klimt/Klimt.pgm\"\n\
82  image.\n\
83  Setting the VISP_INPUT_IMAGE_PATH environment\n\
84  variable produces the same behaviour than using\n\
85  this option.\n\
86 \n\
87  -o <output image path> %s\n\
88  Set image output path.\n\
89  From this directory, creates the \"%s\"\n\
90  subdirectory depending on the username, where \n\
91  resulting images are written.\n\
92  \n\
93  -h\n\
94  Print the help.\n\n", ipath.c_str(), opath.c_str(), user.c_str());
95 
96  if (badparam)
97  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
98 }
99 
112 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, const std::string &user)
113 {
114  const char *optarg_;
115  int c;
116  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
117 
118  switch (c) {
119  case 'i':
120  ipath = optarg_;
121  break;
122  case 'o':
123  opath = optarg_;
124  break;
125  case 'h':
126  usage(argv[0], NULL, ipath, opath, user);
127  return false;
128  break;
129 
130  case 'c':
131  case 'd':
132  break;
133 
134  default:
135  usage(argv[0], optarg_, ipath, opath, user);
136  return false;
137  break;
138  }
139  }
140 
141  if ((c == 1) || (c == -1)) {
142  // standalone param or error
143  usage(argv[0], NULL, ipath, opath, user);
144  std::cerr << "ERROR: " << std::endl;
145  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
146  return false;
147  }
148 
149  return true;
150 }
151 
152 int main(int argc, const char **argv)
153 {
154  try {
155  std::string env_ipath;
156  std::string opt_ipath;
157  std::string opt_opath;
158  std::string ipath;
159  std::string opath;
160  std::string username;
161 
162  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
163  // environment variable value
164  env_ipath = vpIoTools::getViSPImagesDataPath();
165 
166  // Set the default input path
167  if (!env_ipath.empty())
168  ipath = env_ipath;
169 
170 // Set the default output path
171 #if defined(_WIN32)
172  opt_opath = "C:/temp";
173 #else
174  opt_opath = "/tmp";
175 #endif
176 
177  // Get the user login name
178  vpIoTools::getUserName(username);
179 
180  // Read the command line options
181  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
182  exit(-1);
183  }
184 
185  // Get the option values
186  if (!opt_ipath.empty())
187  ipath = opt_ipath;
188  if (!opt_opath.empty())
189  opath = opt_opath;
190 
191  // Append to the output path string, the login name of the user
192  opath = vpIoTools::createFilePath(opath, username);
193 
194  // Test if the output path exist. If no try to create it
195  if (vpIoTools::checkDirectory(opath) == false) {
196  try {
197  // Create the dirname
199  } catch (...) {
200  usage(argv[0], NULL, ipath, opt_opath, username);
201  std::cerr << std::endl << "ERROR:" << std::endl;
202  std::cerr << " Cannot create " << opath << std::endl;
203  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
204  exit(-1);
205  }
206  }
207 
208  // Compare ipath and env_ipath. If they differ, we take into account
209  // the input path comming from the command line option
210  if (opt_ipath.empty()) {
211  if (ipath != env_ipath) {
212  std::cout << std::endl << "WARNING: " << std::endl;
213  std::cout << " Since -i <visp image path=" << ipath << "> "
214  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
215  << " we skip the environment variable." << std::endl;
216  }
217  }
218 
219  // Test if an input path is set
220  if (opt_ipath.empty() && env_ipath.empty()) {
221  usage(argv[0], NULL, ipath, opt_opath, username);
222  std::cerr << std::endl << "ERROR:" << std::endl;
223  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
224  << " environment variable to specify the location of the " << std::endl
225  << " image path where test images are located." << std::endl
226  << std::endl;
227  exit(-1);
228  }
229 
230  if (1) {
232 
233  // Read the input grey image from the disk
234  std::string filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
235  std::cout << "Read image: " << filename << std::endl;
236  vpImageIo::read(I, filename);
237  std::cout << "Image size: " << I.getWidth() << " " << I.getHeight() << std::endl;
238 
239  std::vector<vpImage<unsigned char> > reference;
240 
242  vpImageTools::crop(I, 100, 100, 640, 640, crop);
243  reference.push_back(crop);
244  filename = vpIoTools::createFilePath(opath, "Klimt_uchar_ref_crop-1x1.pgm");
245  std::cout << " Cropped image reference saved in: " << filename << std::endl;
246  vpImageIo::write(crop, filename);
247 
248  vpImageTools::crop(I, 100, 100, 640, 640, crop, 2, 1);
249  reference.push_back(crop);
250  filename = vpIoTools::createFilePath(opath, "Klimt_uchar_ref_crop-2x1.pgm");
251  std::cout << " Cropped image reference saved in: " << filename << std::endl;
252  vpImageIo::write(crop, filename);
253 
254  vpImageTools::crop(I, 100, 100, 640, 640, crop, 2, 2);
255  reference.push_back(crop);
256  filename = vpIoTools::createFilePath(opath, "Klimt_uchar_ref_crop-2x2.pgm");
257  std::cout << " Cropped image reference saved in: " << filename << std::endl;
258  vpImageIo::write(crop, filename);
259 
260  vpRect roi(100, 100, 640, 640);
261  vpImageTools::crop(I, roi, crop);
262  filename = vpIoTools::createFilePath(opath, "Klimt_uchar_roi_crop-1x1.pgm");
263  std::cout << " Cropped image saved in: " << filename << std::endl;
264  vpImageIo::write(crop, filename);
265  if (crop != reference[0]) {
266  std::cout << "Test 1 failed on uchar" << std::endl;
267  return -1;
268  }
269 
270  vpImageTools::crop(I, roi, crop, 2, 1);
271  filename = vpIoTools::createFilePath(opath, "Klimt_uchar_roi_crop-2x1.pgm");
272  std::cout << " Cropped image saved in: " << filename << std::endl;
273  vpImageIo::write(crop, filename);
274  if (crop != reference[1]) {
275  std::cout << "Test 2 failed on uchar" << std::endl;
276  return -1;
277  }
278 
279  vpImageTools::crop(I, roi, crop, 2, 2);
280  filename = vpIoTools::createFilePath(opath, "Klimt_uchar_roi_crop-2x2.pgm");
281  std::cout << " Cropped image saved in: " << filename << std::endl;
282  vpImageIo::write(crop, filename);
283  if (crop != reference[2]) {
284  std::cout << "Test 3 failed on uchar" << std::endl;
285  return -1;
286  }
287 
288  vpImageTools::crop(I.bitmap, I.getWidth(), I.getHeight(), roi, crop);
289  filename = vpIoTools::createFilePath(opath, "Klimt_uchar_bitmap_crop-1x1.pgm");
290  std::cout << " Cropped image saved in: " << filename << std::endl;
291  vpImageIo::write(crop, filename);
292  if (crop != reference[0]) {
293  std::cout << "Test 4 failed on uchar" << std::endl;
294  return -1;
295  }
296 
297  vpImageTools::crop(I.bitmap, I.getWidth(), I.getHeight(), roi, crop, 2, 1);
298  filename = vpIoTools::createFilePath(opath, "Klimt_uchar_bitmap_crop-2x1.pgm");
299  std::cout << " Cropped image saved in: " << filename << std::endl;
300  vpImageIo::write(crop, filename);
301  if (crop != reference[1]) {
302  std::cout << "Test 5 failed on uchar" << std::endl;
303  return -1;
304  }
305 
306  vpImageTools::crop(I.bitmap, I.getWidth(), I.getHeight(), roi, crop, 2, 2);
307  filename = vpIoTools::createFilePath(opath, "Klimt_uchar_bitmap_crop-2x2.pgm");
308  std::cout << " Cropped image saved in: " << filename << std::endl;
309  vpImageIo::write(crop, filename);
310  if (crop != reference[2]) {
311  std::cout << "Test 6 failed on uchar" << std::endl;
312  return -1;
313  }
314  }
315 
316  if (1) {
317  vpImage<vpRGBa> I;
318 
319  // Read the input color image from the disk
320  std::string filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
321  std::cout << "Read image: " << filename << std::endl;
322  vpImageIo::read(I, filename);
323  std::cout << "Image size: " << I.getWidth() << " " << I.getHeight() << std::endl;
324 
325  vpImage<vpRGBa> crop;
326 
327  std::vector<vpImage<vpRGBa> > reference;
328 
329  vpImageTools::crop(I, 100, 100, 640, 640, crop);
330  reference.push_back(crop);
331  filename = vpIoTools::createFilePath(opath, "Klimt_rgba_ref_crop-1x1.pgm");
332  std::cout << " Cropped image reference saved in: " << filename << std::endl;
333  vpImageIo::write(crop, filename);
334 
335  vpImageTools::crop(I, 100, 100, 640, 640, crop, 2, 1);
336  reference.push_back(crop);
337  filename = vpIoTools::createFilePath(opath, "Klimt_rgba_ref_crop-2x1.pgm");
338  std::cout << " Cropped image reference saved in: " << filename << std::endl;
339  vpImageIo::write(crop, filename);
340 
341  vpImageTools::crop(I, 100, 100, 640, 640, crop, 2, 2);
342  reference.push_back(crop);
343  filename = vpIoTools::createFilePath(opath, "Klimt_rgba_ref_crop-2x2.pgm");
344  std::cout << " Cropped image reference saved in: " << filename << std::endl;
345  vpImageIo::write(crop, filename);
346 
347  vpRect roi(100, 100, 640, 640);
348  vpImageTools::crop(I, roi, crop);
349  filename = vpIoTools::createFilePath(opath, "Klimt_rgba_roi_crop-1x1.pgm");
350  std::cout << " Cropped image saved in: " << filename << std::endl;
351  vpImageIo::write(crop, filename);
352  if (crop != reference[0]) {
353  std::cout << "Test 1 failed on uchar" << std::endl;
354  return -1;
355  }
356 
357  vpImageTools::crop(I, roi, crop, 2, 1);
358  filename = vpIoTools::createFilePath(opath, "Klimt_rgba_roi_crop-2x1.pgm");
359  std::cout << " Cropped image saved in: " << filename << std::endl;
360  vpImageIo::write(crop, filename);
361  if (crop != reference[1]) {
362  std::cout << "Test 2 failed on uchar" << std::endl;
363  return -1;
364  }
365 
366  vpImageTools::crop(I, roi, crop, 2, 2);
367  filename = vpIoTools::createFilePath(opath, "Klimt_rgba_roi_crop-2x2.pgm");
368  std::cout << " Cropped image saved in: " << filename << std::endl;
369  vpImageIo::write(crop, filename);
370  if (crop != reference[2]) {
371  std::cout << "Test 3 failed on uchar" << std::endl;
372  return -1;
373  }
374 
375  vpImageTools::crop((unsigned char *)I.bitmap, I.getWidth(), I.getHeight(), roi, crop);
376  filename = vpIoTools::createFilePath(opath, "Klimt_rgba_bitmap_crop-1x1.pgm");
377  std::cout << " Cropped image saved in: " << filename << std::endl;
378  vpImageIo::write(crop, filename);
379  if (crop != reference[0]) {
380  std::cout << "Test 4 failed on uchar" << std::endl;
381  return -1;
382  }
383 
384  vpImageTools::crop((unsigned char *)I.bitmap, I.getWidth(), I.getHeight(), roi, crop, 2, 1);
385  filename = vpIoTools::createFilePath(opath, "Klimt_rgba_bitmap_crop-2x1.pgm");
386  std::cout << " Cropped image saved in: " << filename << std::endl;
387  vpImageIo::write(crop, filename);
388  if (crop != reference[1]) {
389  std::cout << "Test 5 failed on uchar" << std::endl;
390  return -1;
391  }
392 
393  vpImageTools::crop((unsigned char *)I.bitmap, I.getWidth(), I.getHeight(), roi, crop, 2, 2);
394  filename = vpIoTools::createFilePath(opath, "Klimt_rgba_bitmap_crop-2x2.pgm");
395  std::cout << " Cropped image saved in: " << filename << std::endl;
396  vpImageIo::write(crop, filename);
397  if (crop != reference[2]) {
398  std::cout << "Test 6 failed on uchar" << std::endl;
399  return -1;
400  }
401  }
402 
403  std::cout << "Test succeed" << std::endl;
404  return 0;
405  } catch (const vpException &e) {
406  std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
407  return -1;
408  }
409 }
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:467
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1316
unsigned int getWidth() const
Definition: vpImage.h:239
Type * bitmap
points toward the bitmap
Definition: vpImage.h:133
error that can be emited by ViSP classes.
Definition: vpException.h:71
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:375
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:597
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1541
static std::string getUserName()
Definition: vpIoTools.cpp:298
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:207
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:272
unsigned int getHeight() const
Definition: vpImage.h:178
Defines a rectangle in the plane.
Definition: vpRect.h:78
const std::string & getStringMessage(void) const
Send a reference (constant) related the error message (can be empty).
Definition: vpException.cpp:92