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