47 #include <visp3/core/vpDebug.h> 48 #include <visp3/core/vpImage.h> 49 #include <visp3/io/vpImageIo.h> 50 #include <visp3/core/vpHistogram.h> 51 #include <visp3/io/vpParseArgv.h> 52 #include <visp3/core/vpIoTools.h> 60 #define GETOPTARGS "i:o:h" 62 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user);
63 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath, std::string user);
83 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user)
86 Read an image on the disk, display it using X11, display some\n\ 87 features (line, circle, caracters) in overlay and finaly write \n\ 88 the image and the overlayed features in an image on the disk.\n\ 91 %s [-i <input image path>] [-o <output histogram path>]\n\ 97 -i <input image path> %s\n\ 98 Set image input path.\n\ 99 From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\ 101 Setting the VISP_INPUT_IMAGE_PATH environment\n\ 102 variable produces the same behaviour than using\n\ 105 -o <output histogram path> %s\n\ 106 From this directory, creates the \"%s\"\n\ 107 subdirectory depending on the username, where \n\ 108 \"histogram.txt\" is saved.\n\ 111 Print the help.\n\n",
112 ipath.c_str(), opath.c_str(), user.c_str());
115 fprintf(stderr,
"ERROR: \n" );
116 fprintf(stderr,
"\nBad parameter [%s]\n", badparam);
133 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath, std::string user)
140 case 'i': ipath = optarg_;
break;
141 case 'o': opath = optarg_;
break;
142 case 'h': usage(argv[0], NULL, ipath, opath, user);
return false;
break;
145 usage(argv[0], optarg_, ipath, opath, user);
return false;
break;
149 if ((c == 1) || (c == -1)) {
151 usage(argv[0], NULL, ipath, opath, user);
152 std::cerr <<
"ERROR: " << std::endl;
153 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
161 main(
int argc,
const char ** argv)
164 std::string env_ipath;
165 std::string opt_ipath;
166 std::string opt_opath;
169 std::string filename;
170 std::string username;
176 if (! env_ipath.empty())
181 opt_opath =
"C:/temp";
190 if (getOptions(argc, argv, opt_ipath, opt_opath, username) ==
false) {
195 if (!opt_ipath.empty())
197 if (!opt_opath.empty())
210 usage(argv[0], NULL, ipath, opath, username);
211 std::cerr << std::endl
212 <<
"ERROR:" << std::endl;
213 std::cerr <<
" Cannot create " << dirname << std::endl;
214 std::cerr <<
" Check your -o " << opath <<
" option " << std::endl;
221 if (opt_ipath.empty()) {
222 if (ipath != env_ipath) {
223 std::cout << std::endl
224 <<
"WARNING: " << std::endl;
225 std::cout <<
" Since -i <visp image path=" << ipath <<
"> " 226 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
227 <<
" we skip the environment variable." << std::endl;
232 if (opt_ipath.empty() && env_ipath.empty()){
233 usage(argv[0], NULL, ipath, opath, username);
234 std::cerr << std::endl
235 <<
"ERROR:" << std::endl;
236 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " 238 <<
" environment variable to specify the location of the " << std::endl
239 <<
" image path where test images are located." << std::endl << 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;
279 vpTRACE(
"Nb peaks: %d", nbpeaks);
281 for(std::list<vpHistogramPeak>::const_iterator it = peaks.begin(); it != peaks.end(); ++it)
290 nbpeaks = h.
sort(peaks);
292 vpTRACE(
"Sorted list of peaks");
293 vpTRACE(
"Nb peaks: %d", nbpeaks);
295 for(std::list<vpHistogramPeak>::const_iterator it = peaks.begin(); it != peaks.end(); ++it)
304 nbpeaks = h.
getPeaks(distance, peak1, peak2);
306 std::cout <<
"Not a bimodal histogram..." << std::endl;
309 vpTRACE(
"Bimodal histogram: main peak1: %d-%d second peak2: %d-%d",
310 peak1.getLevel(), peak1.getValue(),
311 peak2.getLevel(), peak2.getValue());
316 if (h.
getValey(peak1, peak2, valey) ==
false) {
320 vpTRACE(
"Valey: %d-%d", valey.getLevel(), valey.getValue());
327 unsigned ret = h.
getValey(distance, peak1, valeyl, valeyr);
329 vpTRACE(
"No left and right valey for peak %d-%d...",
330 peak1.getLevel(), peak1.getValue());
332 else if (ret == 0x10) {
333 vpTRACE(
"No right valey for peak %d-%d...",
334 peak1.getLevel(), peak1.getValue());
335 vpTRACE(
"Left valey: %d-%d", valeyl.getLevel(), valeyl.getValue());
337 else if (ret == 0x01) {
338 vpTRACE(
"No left valey for peak %d-%d...",
339 peak1.getLevel(), peak1.getValue());
340 vpTRACE(
"Right valey: %d-%d", valeyr.getLevel(), valeyr.getValue());
342 else if (ret == 0x11) {
343 vpTRACE(
"Left valey: %d-%d", valeyl.getLevel(), valeyl.getValue());
344 vpTRACE(
"Right valey: %d-%d", valeyr.getLevel(), valeyr.getValue());
349 unsigned ret = h.
getValey(distance, peak2, valeyl, valeyr);
351 vpTRACE(
"No left and right valey for peak %d-%d...",
352 peak2.getLevel(), peak2.getValue());
354 else if (ret == 0x10) {
355 vpTRACE(
"No right valey for peak %d-%d...",
356 peak2.getLevel(), peak2.getValue());
357 vpTRACE(
"Left valey: %d-%d", valeyl.getLevel(), valeyl.getValue());
359 else if (ret == 0x01) {
360 vpTRACE(
"No left valey for peak %d-%d...",
361 peak2.getLevel(), peak2.getValue());
362 vpTRACE(
"Right valey: %d-%d", valeyr.getLevel(), valeyr.getValue());
364 else if (ret == 0x11) {
365 vpTRACE(
"Left valey: %d-%d", valeyl.getLevel(), valeyl.getValue());
366 vpTRACE(
"Right valey: %d-%d", valeyr.getLevel(), valeyr.getValue());
374 if (h.
getPeaks(distance, peakl, peakr, valey) ==
false) {
375 std::cout <<
"Not a bimodal histogram..." << std::endl;
378 vpTRACE(
"Bimodal histogram: valey %d-%d for peakl: %d-%d peakr: %d-%d",
379 valey.getLevel(), valey.getValue(),
380 peakl.getLevel(), peakl.getValue(),
381 peakr.getLevel(), peakr.getValue());
386 std::cout <<
"Catch an exception: " << e << std::endl;
unsigned getPeaks(std::list< vpHistogramPeak > &peaks)
error that can be emited by ViSP classes.
Class to compute a gray level image histogram.
Declaration of the peak (maximum value) in a gray level image histogram.
unsigned char getLevel() const
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
void smooth(const unsigned int fsize=3)
void calculate(const vpImage< unsigned char > &I, const unsigned int nbins=256, const unsigned int nbThreads=1)
unsigned sort(std::list< vpHistogramPeak > &peaks)
static void read(vpImage< unsigned char > &I, const std::string &filename)
unsigned getValue() const
bool write(const std::string &filename)
Declaration of the valey (minimum value) in a gray level image histogram.
unsigned getValey(std::list< vpHistogramValey > &valey)