Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testCrop.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
44#include <visp3/core/vpDebug.h>
45#include <visp3/core/vpImage.h>
46#include <visp3/core/vpImageTools.h>
47#include <visp3/core/vpIoTools.h>
48#include <visp3/core/vpRect.h>
49#include <visp3/io/vpImageIo.h>
50#include <visp3/io/vpParseArgv.h>
51
52#include <stdlib.h>
53
54#ifdef ENABLE_VISP_NAMESPACE
55using namespace VISP_NAMESPACE_NAME;
56#endif
57
58// List of allowed command line options
59#define GETOPTARGS "cdi:o:h"
60
61/*
62
63 Print the program options.
64
65 \param name : Program name.
66 \param badparam : Bad parameter name.
67 \param ipath : Input image path.
68 \param opath : Output image path.
69 \param user : Username.
70
71 */
72void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath,
73 const std::string &user)
74{
75 fprintf(stdout, "\n\
76Read an image from the disk (Klimt.pgm), crop a rectangular area\n\
77and save the cropped image on the disk (Klimt_cropped.pgm).\n\
78\n\
79SYNOPSIS\n\
80 %s [-i <input image path>] [-o <output image path>]\n\
81 [-h]\n\
82",
83name);
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",
103 ipath.c_str(), opath.c_str(), user.c_str());
104
105 if (badparam)
106 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
107}
108
121bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, const std::string &user)
122{
123 const char *optarg_;
124 int c;
125 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
126
127 switch (c) {
128 case 'i':
129 ipath = optarg_;
130 break;
131 case 'o':
132 opath = optarg_;
133 break;
134 case 'h':
135 usage(argv[0], nullptr, ipath, opath, user);
136 return false;
137
138 case 'c':
139 case 'd':
140 break;
141
142 default:
143 usage(argv[0], optarg_, ipath, opath, user);
144 return false;
145 }
146 }
147
148 if ((c == 1) || (c == -1)) {
149 // standalone param or error
150 usage(argv[0], nullptr, ipath, opath, user);
151 std::cerr << "ERROR: " << std::endl;
152 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
153 return false;
154 }
155
156 return true;
157}
158
159int main(int argc, const char **argv)
160{
161 try {
162 std::string env_ipath;
163 std::string opt_ipath;
164 std::string opt_opath;
165 std::string ipath;
166 std::string opath;
167 std::string filename;
168 std::string username;
169
170 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
171 // environment variable value
173
174 // Set the default input path
175 if (!env_ipath.empty())
176 ipath = env_ipath;
177
178// Set the default output path
179#if defined(_WIN32)
180 opt_opath = "C:/temp";
181#else
182 opt_opath = "/tmp";
183#endif
184
185 // Get the user login name
186 vpIoTools::getUserName(username);
187
188 // Read the command line options
189 if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
190 return EXIT_FAILURE;
191 }
192
193 // Get the option values
194 if (!opt_ipath.empty())
195 ipath = opt_ipath;
196 if (!opt_opath.empty())
197 opath = opt_opath;
198
199 // Append to the output path string, the login name of the user
200 opath = vpIoTools::createFilePath(opath, username);
201
202 // Test if the output path exist. If no try to create it
203 if (vpIoTools::checkDirectory(opath) == false) {
204 try {
205 // Create the dirname
207 }
208 catch (...) {
209 usage(argv[0], nullptr, 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 return EXIT_FAILURE;
214 }
215 }
216
217 // Compare ipath and env_ipath. If they differ, we take into account
218 // the input path coming 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], nullptr, 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 return EXIT_FAILURE;
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 EXIT_SUCCESS;
265 }
266 catch (const vpException &e) {
267 std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
268 return EXIT_FAILURE;
269 }
270}
error that can be emitted by ViSP classes.
Definition vpException.h:60
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:131
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:79
void setHeight(double h)
Definition vpRect.h:308
void setWidth(double w)
Definition vpRect.h:378
void setTop(double pos)
Definition vpRect.h:357
void setLeft(double pos)
Definition vpRect.h:321