Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testAutoThreshold.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2023 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
34#include <visp3/core/vpImageTools.h>
35#include <visp3/core/vpIoTools.h>
36#include <visp3/imgproc/vpImgproc.h>
37#include <visp3/io/vpImageIo.h>
38#include <visp3/io/vpParseArgv.h>
39
46// List of allowed command line options
47#define GETOPTARGS "cdi:o:h"
48
49void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user);
50bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user);
51
52/*
53 Print the program options.
54
55 \param name : Program name.
56 \param badparam : Bad parameter name.
57 \param ipath: Input image path.
58 \param opath : Output image path.
59 \param user : Username.
60 */
61void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
62{
63#if VISP_HAVE_DATASET_VERSION >= 0x030600
64 std::string ext("png");
65#else
66 std::string ext("pgm");
67#endif
68 fprintf(stdout, "\n\
69Test automatic thresholding.\n\
70\n\
71SYNOPSIS\n\
72 %s [-i <input image path>] [-o <output image path>]\n\
73 [-h]\n \
74",
75 name);
76
77 fprintf(stdout, "\n\
78OPTIONS: Default\n\
79 -i <input image path> %s\n\
80 Set image input path.\n\
81 From this path read \"calibration/grid36-03.%s\"\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 output result images are written.\n\
92\n\
93 -h\n\
94 Print the help.\n\n",
95 ipath.c_str(), ext.c_str(), opath.c_str(), user.c_str());
96
97 if (badparam)
98 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
99}
100
112bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user)
113{
114 const char *optarg_;
115 int c;
116 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
117
118 switch (c) {
119 case 'i':
120 ipath = optarg_;
121 break;
122 case 'o':
123 opath = optarg_;
124 break;
125 case 'h':
126 usage(argv[0], NULL, ipath, opath, user);
127 return false;
128 break;
129
130 case 'c':
131 case 'd':
132 break;
133
134 default:
135 usage(argv[0], optarg_, ipath, opath, user);
136 return false;
137 break;
138 }
139 }
140
141 if ((c == 1) || (c == -1)) {
142 // standalone param or error
143 usage(argv[0], NULL, ipath, opath, user);
144 std::cerr << "ERROR: " << std::endl;
145 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
146 return false;
147 }
148
149 return true;
150}
151
152int main(int argc, const char **argv)
153{
154 try {
155 std::string env_ipath;
156 std::string opt_ipath;
157 std::string opt_opath;
158 std::string ipath;
159 std::string opath;
160 std::string filename;
161 std::string username;
162
163#if VISP_HAVE_DATASET_VERSION >= 0x030600
164 std::string ext("png");
165#else
166 std::string ext("pgm");
167#endif
168
169 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
170 // environment variable value
172
173 // Set the default input path
174 if (!env_ipath.empty())
175 ipath = env_ipath;
176
177// Set the default output path
178#if defined(_WIN32)
179 opt_opath = "C:/temp";
180#else
181 opt_opath = "/tmp";
182#endif
183
184 // Get the user login name
185 vpIoTools::getUserName(username);
186
187 // Read the command line options
188 if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
189 exit(EXIT_FAILURE);
190 }
191
192 // Get the option values
193 if (!opt_ipath.empty())
194 ipath = opt_ipath;
195 if (!opt_opath.empty())
196 opath = opt_opath;
197
198 // Append to the output path string, the login name of the user
199 opath = vpIoTools::createFilePath(opath, username);
200
201 // Test if the output path exist. If no try to create it
202 if (vpIoTools::checkDirectory(opath) == false) {
203 try {
204 // Create the dirname
206 } catch (...) {
207 usage(argv[0], NULL, ipath, opt_opath, username);
208 std::cerr << std::endl << "ERROR:" << std::endl;
209 std::cerr << " Cannot create " << opath << std::endl;
210 std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
211 exit(EXIT_FAILURE);
212 }
213 }
214
215 // Compare ipath and env_ipath. If they differ, we take into account
216 // the input path comming from the command line option
217 if (!opt_ipath.empty() && !env_ipath.empty()) {
218 if (ipath != env_ipath) {
219 std::cout << std::endl << "WARNING: " << std::endl;
220 std::cout << " Since -i <visp image path=" << ipath << "> "
221 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
222 << " we skip the environment variable." << std::endl;
223 }
224 }
225
226 // Test if an input path is set
227 if (opt_ipath.empty() && env_ipath.empty()) {
228 usage(argv[0], NULL, ipath, opt_opath, username);
229 std::cerr << std::endl << "ERROR:" << std::endl;
230 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
231 << " environment variable to specify the location of the " << std::endl
232 << " image path where test images are located." << std::endl
233 << std::endl;
234 exit(EXIT_FAILURE);
235 }
236
237 //
238 // Here starts really the test
239 //
240
241 filename = vpIoTools::createFilePath(ipath, "calibration/grid36-03." + ext);
243 vpImageIo::read(I, filename);
244 std::cout << "Read: " << filename << " (" << I.getWidth() << "x" << I.getHeight() << ")" << std::endl;
245
246 vpImage<unsigned char> I_thresh = I;
247
248 // Huang
249 double t = vpTime::measureTimeMs();
250 double threshold = vp::autoThreshold(I_thresh, vp::AUTO_THRESHOLD_HUANG);
251 t = vpTime::measureTimeMs() - t;
252 std::cout << "\nAutomatic thresholding (Huang): " << threshold << " ; t=" << t << " ms" << std::endl;
253
254 filename = vpIoTools::createFilePath(opath, "grid36-03_auto_thresh_huang.png");
255 vpImageIo::write(I_thresh, filename);
256 std::cout << "Write: " << filename << std::endl;
257
258 // Intermodes
259 I_thresh = I;
262 t = vpTime::measureTimeMs() - t;
263 std::cout << "\nAutomatic thresholding (Intermodes): " << threshold << " ; t=" << t << " ms" << std::endl;
264
265 filename = vpIoTools::createFilePath(opath, "grid36-03_auto_thresh_intermodes.png");
266 vpImageIo::write(I_thresh, filename);
267 std::cout << "Write: " << filename << std::endl;
268
269 // IsoData
270 I_thresh = I;
272 threshold = vp::autoThreshold(I_thresh, vp::AUTO_THRESHOLD_ISODATA);
273 t = vpTime::measureTimeMs() - t;
274 std::cout << "\nAutomatic thresholding (IsoData): " << threshold << " ; t=" << t << " ms" << std::endl;
275
276 filename = vpIoTools::createFilePath(opath, "grid36-03_auto_thresh_isodata.png");
277 vpImageIo::write(I_thresh, filename);
278 std::cout << "Write: " << filename << std::endl;
279
280 // Mean
281 I_thresh = I;
283 threshold = vp::autoThreshold(I_thresh, vp::AUTO_THRESHOLD_MEAN);
284 t = vpTime::measureTimeMs() - t;
285 std::cout << "\nAutomatic thresholding (Mean): " << threshold << " ; t=" << t << " ms" << std::endl;
286
287 filename = vpIoTools::createFilePath(opath, "grid36-03_auto_thresh_mean.png");
288 vpImageIo::write(I_thresh, filename);
289 std::cout << "Write: " << filename << std::endl;
290
291 // Otsu
292 I_thresh = I;
294 threshold = vp::autoThreshold(I_thresh, vp::AUTO_THRESHOLD_OTSU);
295 t = vpTime::measureTimeMs() - t;
296 std::cout << "\nAutomatic thresholding (Otsu): " << threshold << " ; t=" << t << " ms" << std::endl;
297
298 filename = vpIoTools::createFilePath(opath, "grid36-03_auto_thresh_otsu.png");
299 vpImageIo::write(I_thresh, filename);
300 std::cout << "Write: " << filename << std::endl;
301
302 // Triangle
303 I_thresh = I;
305 threshold = vp::autoThreshold(I_thresh, vp::AUTO_THRESHOLD_TRIANGLE);
306 t = vpTime::measureTimeMs() - t;
307 std::cout << "\nAutomatic thresholding (Triangle): " << threshold << " ; t=" << t << " ms" << std::endl;
308
309 filename = vpIoTools::createFilePath(opath, "grid36-03_auto_thresh_Triangle.png");
310 vpImageIo::write(I_thresh, filename);
311 std::cout << "Write: " << filename << std::endl;
312
313 return EXIT_SUCCESS;
314 } catch (const vpException &e) {
315 std::cerr << "Catch an exception: " << e.what() << std::endl;
316 return EXIT_FAILURE;
317 }
318}
error that can be emitted by ViSP classes.
Definition vpException.h:59
const char * what() 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)
Definition of the vpImage class member functions.
Definition vpImage.h:135
unsigned int getWidth() const
Definition vpImage.h:242
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)
VISP_EXPORT unsigned char autoThreshold(vpImage< unsigned char > &I, const vp::vpAutoThresholdMethod &method, const unsigned char backgroundValue=0, const unsigned char foregroundValue=255)
VISP_EXPORT double measureTimeMs()
@ AUTO_THRESHOLD_ISODATA
Definition vpImgproc.h:75
@ AUTO_THRESHOLD_HUANG
Definition vpImgproc.h:67
@ AUTO_THRESHOLD_INTERMODES
Definition vpImgproc.h:71
@ AUTO_THRESHOLD_TRIANGLE
Definition vpImgproc.h:87
@ AUTO_THRESHOLD_MEAN
Definition vpImgproc.h:79
@ AUTO_THRESHOLD_OTSU
Definition vpImgproc.h:83