Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testAutoThreshold.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 automatic thresholding.
32 */
33
39
40#include <visp3/core/vpImageTools.h>
41#include <visp3/core/vpIoTools.h>
42#include <visp3/imgproc/vpImgproc.h>
43#include <visp3/io/vpImageIo.h>
44#include <visp3/io/vpParseArgv.h>
45
46// List of allowed command line options
47#define GETOPTARGS "cdi:o:h"
48
49#ifdef ENABLE_VISP_NAMESPACE
50using namespace VISP_NAMESPACE_NAME;
51#endif
52
53void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath, const std::string &user);
54bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user);
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 */
65void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath, const std::string &user)
66{
67#if defined(VISP_HAVE_DATASET)
68#if VISP_HAVE_DATASET_VERSION >= 0x030600
69 std::string ext("png");
70#else
71 std::string ext("pgm");
72#endif
73#else
74 // We suppose that the user will download a recent dataset
75 std::string ext("png");
76#endif
77 fprintf(stdout, "\n\
78Test automatic thresholding.\n\
79\n\
80SYNOPSIS\n\
81 %s [-i <input image path>] [-o <output image path>]\n\
82 [-h]\n \
83",
84name);
85
86 fprintf(stdout, "\n\
87OPTIONS: Default\n\
88 -i <input image path> %s\n\
89 Set image input path.\n\
90 From this path read \"calibration/grid36-03.%s\"\n\
91 image.\n\
92 Setting the VISP_INPUT_IMAGE_PATH environment\n\
93 variable produces the same behaviour than using\n\
94 this option.\n\
95\n\
96 -o <output image path> %s\n\
97 Set image output path.\n\
98 From this directory, creates the \"%s\"\n\
99 subdirectory depending on the username, where \n\
100 output result images are written.\n\
101\n\
102 -h\n\
103 Print the help.\n\n",
104 ipath.c_str(), ext.c_str(), opath.c_str(), user.c_str());
105
106 if (badparam)
107 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
108}
109
121bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, 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#if defined(VISP_HAVE_DATASET)
171#if VISP_HAVE_DATASET_VERSION >= 0x030600
172 std::string ext("png");
173#else
174 std::string ext("pgm");
175#endif
176#else
177 // We suppose that the user will download a recent dataset
178 std::string ext("png");
179#endif
180
181 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
182 // environment variable value
184
185 // Set the default input path
186 if (!env_ipath.empty())
187 ipath = env_ipath;
188
189// Set the default output path
190#if defined(_WIN32)
191 opt_opath = "C:/temp";
192#else
193 opt_opath = "/tmp";
194#endif
195
196 // Get the user login name
197 vpIoTools::getUserName(username);
198
199 // Read the command line options
200 if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
201 exit(EXIT_FAILURE);
202 }
203
204 // Get the option values
205 if (!opt_ipath.empty())
206 ipath = opt_ipath;
207 if (!opt_opath.empty())
208 opath = opt_opath;
209
210 // Append to the output path string, the login name of the user
211 opath = vpIoTools::createFilePath(opath, username);
212
213 // Test if the output path exist. If no try to create it
214 if (vpIoTools::checkDirectory(opath) == false) {
215 try {
216 // Create the dirname
218 }
219 catch (...) {
220 usage(argv[0], nullptr, ipath, opt_opath, username);
221 std::cerr << std::endl << "ERROR:" << std::endl;
222 std::cerr << " Cannot create " << opath << std::endl;
223 std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
224 exit(EXIT_FAILURE);
225 }
226 }
227
228 // Compare ipath and env_ipath. If they differ, we take into account
229 // the input path coming from the command line option
230 if (!opt_ipath.empty() && !env_ipath.empty()) {
231 if (ipath != env_ipath) {
232 std::cout << std::endl << "WARNING: " << std::endl;
233 std::cout << " Since -i <visp image path=" << ipath << "> "
234 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
235 << " we skip the environment variable." << std::endl;
236 }
237 }
238
239 // Test if an input path is set
240 if (opt_ipath.empty() && env_ipath.empty()) {
241 usage(argv[0], nullptr, ipath, opt_opath, username);
242 std::cerr << std::endl << "ERROR:" << std::endl;
243 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
244 << " environment variable to specify the location of the " << std::endl
245 << " image path where test images are located." << std::endl
246 << std::endl;
247 exit(EXIT_FAILURE);
248 }
249
250 //
251 // Here starts really the test
252 //
253
254 filename = vpIoTools::createFilePath(ipath, "calibration/grid36-03." + ext);
256 vpImageIo::read(I, filename);
257 std::cout << "Read: " << filename << " (" << I.getWidth() << "x" << I.getHeight() << ")" << std::endl;
258
259 vpImage<unsigned char> I_thresh = I;
260
261 // Huang
262 double t = vpTime::measureTimeMs();
265 std::cout << "\nAutomatic thresholding (Huang): " << threshold << " ; t=" << t << " ms" << std::endl;
266
267 filename = vpIoTools::createFilePath(opath, "grid36-03_auto_thresh_huang.png");
268 vpImageIo::write(I_thresh, filename);
269 std::cout << "Write: " << filename << std::endl;
270
271 // Intermodes
272 I_thresh = I;
276 std::cout << "\nAutomatic thresholding (Intermodes): " << threshold << " ; t=" << t << " ms" << std::endl;
277
278 filename = vpIoTools::createFilePath(opath, "grid36-03_auto_thresh_intermodes.png");
279 vpImageIo::write(I_thresh, filename);
280 std::cout << "Write: " << filename << std::endl;
281
282 // IsoData
283 I_thresh = I;
287 std::cout << "\nAutomatic thresholding (IsoData): " << threshold << " ; t=" << t << " ms" << std::endl;
288
289 filename = vpIoTools::createFilePath(opath, "grid36-03_auto_thresh_isodata.png");
290 vpImageIo::write(I_thresh, filename);
291 std::cout << "Write: " << filename << std::endl;
292
293 // Mean
294 I_thresh = I;
298 std::cout << "\nAutomatic thresholding (Mean): " << threshold << " ; t=" << t << " ms" << std::endl;
299
300 filename = vpIoTools::createFilePath(opath, "grid36-03_auto_thresh_mean.png");
301 vpImageIo::write(I_thresh, filename);
302 std::cout << "Write: " << filename << std::endl;
303
304 // Otsu
305 I_thresh = I;
309 std::cout << "\nAutomatic thresholding (Otsu): " << threshold << " ; t=" << t << " ms" << std::endl;
310
311 filename = vpIoTools::createFilePath(opath, "grid36-03_auto_thresh_otsu.png");
312 vpImageIo::write(I_thresh, filename);
313 std::cout << "Write: " << filename << std::endl;
314
315 // Triangle
316 I_thresh = I;
320 std::cout << "\nAutomatic thresholding (Triangle): " << threshold << " ; t=" << t << " ms" << std::endl;
321
322 filename = vpIoTools::createFilePath(opath, "grid36-03_auto_thresh_Triangle.png");
323 vpImageIo::write(I_thresh, filename);
324 std::cout << "Write: " << filename << std::endl;
325
326 return EXIT_SUCCESS;
327 }
328 catch (const vpException &e) {
329 std::cerr << "Catch an exception: " << e.what() << std::endl;
330 return EXIT_FAILURE;
331 }
332}
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)
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)
VISP_EXPORT unsigned char autoThreshold(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, const vpAutoThresholdMethod &method, const unsigned char backgroundValue=0, const unsigned char foregroundValue=255)
VISP_EXPORT double measureTimeMs()