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