41#include <visp3/core/vpConfig.h>
42#include <visp3/core/vpDebug.h>
43#include <visp3/core/vpHistogram.h>
44#include <visp3/core/vpImage.h>
45#include <visp3/core/vpIoTools.h>
46#include <visp3/io/vpImageIo.h>
47#include <visp3/io/vpParseArgv.h>
55#define GETOPTARGS "i:o:h"
57#ifdef ENABLE_VISP_NAMESPACE
79void usage(
const char *name,
const char *badparam,
const std::string &ipath,
const std::string &opath,
const std::string &user)
82Read an image on the disk, display it using X11, display some\n\
83features (line, circle, characters) in overlay and finally write \n\
84the image and the overlayed features in an image on the disk.\n\
87 %s [-i <input image path>] [-o <output histogram path>]\n\
94 -i <input image path> %s\n\
95 Set image input path.\n\
96 From this path read \"Klimt/Klimt.pgm\"\n\
98 Setting the VISP_INPUT_IMAGE_PATH environment\n\
99 variable produces the same behaviour than using\n\
102 -o <output histogram path> %s\n\
103 From this directory, creates the \"%s\"\n\
104 subdirectory depending on the username, where \n\
105 \"histogram.txt\" is saved.\n\
108 Print the help.\n\n",
109 ipath.c_str(), opath.c_str(), user.c_str());
112 fprintf(stderr,
"ERROR: \n");
113 fprintf(stderr,
"\nBad parameter [%s]\n", badparam);
129bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath,
const std::string &user)
143 usage(argv[0],
nullptr, ipath, opath, user);
147 usage(argv[0], optarg_, ipath, opath, user);
152 if ((c == 1) || (c == -1)) {
154 usage(argv[0],
nullptr, ipath, opath, user);
155 std::cerr <<
"ERROR: " << std::endl;
156 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
163int main(
int argc,
const char **argv)
166 std::string env_ipath;
167 std::string opt_ipath;
168 std::string opt_opath;
172 std::string username;
179 if (!env_ipath.empty())
184 opt_opath =
"C:/temp";
193 if (getOptions(argc, argv, opt_ipath, opt_opath, username) ==
false) {
198 if (!opt_ipath.empty())
200 if (!opt_opath.empty())
213 usage(argv[0],
nullptr, ipath, opath, username);
214 std::cerr << std::endl <<
"ERROR:" << std::endl;
215 std::cerr <<
" Cannot create " << dirname << std::endl;
216 std::cerr <<
" Check your -o " << opath <<
" option " << std::endl;
223 if (opt_ipath.empty()) {
224 if (ipath != env_ipath) {
225 std::cout << std::endl <<
"WARNING: " << std::endl;
226 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
227 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
228 <<
" we skip the environment variable." << std::endl;
233 if (opt_ipath.empty() && env_ipath.empty()) {
234 usage(argv[0],
nullptr, ipath, opath, username);
235 std::cerr << std::endl <<
"ERROR:" << std::endl;
236 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
237 <<
" environment variable to specify the location of the " << std::endl
238 <<
" image path where test images are located." << std::endl
248 if (opt_ipath.empty())
251 std::cout <<
"Read: " <<
filename << std::endl;
254 unsigned char distance = 60;
262 std::cout <<
"Save the histogram in: " <<
filename << std::endl;
269 std::cout <<
"Save the smoothed histogram in: " <<
filename << std::endl;
272 std::list<vpHistogramPeak> peaks;
273 unsigned int nbpeaks = 0;
276 nbpeaks =
h.getPeaks(peaks);
279 vpTRACE(
"Nb peaks: %d", nbpeaks);
281 for (std::list<vpHistogramPeak>::const_iterator it = peaks.begin(); it != peaks.end(); ++it) {
283 vpTRACE(
"Peak: gray level: %d value: %d",
p.getLevel(),
p.getValue());
289 nbpeaks =
h.sort(peaks);
291 vpTRACE(
"Sorted list of peaks");
292 vpTRACE(
"Nb peaks: %d", nbpeaks);
294 for (std::list<vpHistogramPeak>::const_iterator it = peaks.begin(); it != peaks.end(); ++it) {
296 vpTRACE(
"Peak: gray level: %d value: %d",
p.getLevel(),
p.getValue());
302 nbpeaks =
h.getPeaks(distance, peak1, peak2);
304 std::cout <<
"Not a bimodal histogram..." << std::endl;
313 if (
h.getValey(peak1, peak2, valey) ==
false) {
324 unsigned ret =
h.getValey(distance, peak1, valeyl, valeyr);
328 else if (ret == 0x10) {
332 else if (ret == 0x01) {
336 else if (ret == 0x11) {
343 unsigned ret =
h.getValey(distance, peak2, valeyl, valeyr);
347 else if (ret == 0x10) {
351 else if (ret == 0x01) {
355 else if (ret == 0x11) {
365 if (
h.getPeaks(distance, peakl, peakr, valey) ==
false) {
366 std::cout <<
"Not a bimodal histogram..." << std::endl;
375 std::cout <<
"Catch an exception: " <<
e << std::endl;
error that can be emitted by ViSP classes.
Declaration of the peak (maximum value) in a gray level image histogram.
unsigned getValue() const
unsigned char getLevel() const
Declaration of the valey (minimum value) in a gray level image histogram.
unsigned char getLevel() const
unsigned getValue() const
Class to compute a gray level image histogram.
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition of the vpImage class member functions.
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)