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