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