51 #include <visp/vpDebug.h>
52 #include <visp/vpImage.h>
53 #include <visp/vpImageIo.h>
54 #include <visp/vpHistogram.h>
55 #include <visp/vpParseArgv.h>
56 #include <visp/vpIoTools.h>
64 #define GETOPTARGS "i:o:h"
66 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user);
67 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath, std::string user);
87 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user)
90 Read an image on the disk, display it using X11, display some\n\
91 features (line, circle, caracters) in overlay and finaly write \n\
92 the image and the overlayed features in an image on the disk.\n\
95 %s [-i <input image path>] [-o <output histogram path>]\n\
101 -i <input image path> %s\n\
102 Set image input path.\n\
103 From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
105 Setting the VISP_INPUT_IMAGE_PATH environment\n\
106 variable produces the same behaviour than using\n\
109 -o <output histogram path> %s\n\
110 From this directory, creates the \"%s\"\n\
111 subdirectory depending on the username, where \n\
112 \"histogram.txt\" is saved.\n\
115 Print the help.\n\n",
116 ipath.c_str(), opath.c_str(), user.c_str());
119 fprintf(stderr,
"ERROR: \n" );
120 fprintf(stderr,
"\nBad parameter [%s]\n", badparam);
137 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath, std::string user)
144 case 'i': ipath = optarg_;
break;
145 case 'o': opath = optarg_;
break;
146 case 'h': usage(argv[0], NULL, ipath, opath, user);
return false;
break;
149 usage(argv[0], optarg_, ipath, opath, user);
return false;
break;
153 if ((c == 1) || (c == -1)) {
155 usage(argv[0], NULL, ipath, opath, user);
156 std::cerr <<
"ERROR: " << std::endl;
157 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
165 main(
int argc,
const char ** argv)
168 std::string env_ipath;
169 std::string opt_ipath;
170 std::string opt_opath;
173 std::string filename;
174 std::string username;
177 char *ptenv = getenv(
"VISP_INPUT_IMAGE_PATH");
183 if (! env_ipath.empty())
188 opt_opath =
"C:/temp";
197 if (getOptions(argc, argv, opt_ipath, opt_opath, username) ==
false) {
202 if (!opt_ipath.empty())
204 if (!opt_opath.empty())
217 usage(argv[0], NULL, ipath, opath, username);
218 std::cerr << std::endl
219 <<
"ERROR:" << std::endl;
220 std::cerr <<
" Cannot create " << dirname << std::endl;
221 std::cerr <<
" Check your -o " << opath <<
" option " << std::endl;
228 if (opt_ipath.empty()) {
229 if (ipath != env_ipath) {
230 std::cout << std::endl
231 <<
"WARNING: " << std::endl;
232 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
233 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
234 <<
" we skip the environment variable." << std::endl;
239 if (opt_ipath.empty() && env_ipath.empty()){
240 usage(argv[0], NULL, ipath, opath, username);
241 std::cerr << std::endl
242 <<
"ERROR:" << std::endl;
243 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
245 <<
" environment variable to specify the location of the " << std::endl
246 <<
" image path where test images are located." << std::endl << std::endl;
255 if (opt_ipath.empty())
258 std::cout <<
"Read: " << filename << std::endl;
261 unsigned char distance = 60;
269 std::cout <<
"Save the histogram in: " << filename << std::endl;
276 std::cout <<
"Save the smoothed histogram in: " << filename << std::endl;
279 std::list<vpHistogramPeak> peaks;
280 unsigned int nbpeaks = 0;
286 vpTRACE(
"Nb peaks: %d", nbpeaks);
288 for(std::list<vpHistogramPeak>::const_iterator it = peaks.begin(); it != peaks.end(); ++it)
297 nbpeaks = h.
sort(peaks);
299 vpTRACE(
"Sorted list of peaks");
300 vpTRACE(
"Nb peaks: %d", nbpeaks);
302 for(std::list<vpHistogramPeak>::const_iterator it = peaks.begin(); it != peaks.end(); ++it)
311 nbpeaks = h.
getPeaks(distance, peak1, peak2);
313 std::cout <<
"Not a bimodal histogram..." << std::endl;
316 vpTRACE(
"Bimodal histogram: main peak1: %d-%d second peak2: %d-%d",
323 if (h.
getValey(peak1, peak2, valey) ==
false) {
334 unsigned ret = h.
getValey(distance, peak1, valeyl, valeyr);
336 vpTRACE(
"No left and right valey for peak %d-%d...",
339 else if (ret == 0x10) {
340 vpTRACE(
"No right valey for peak %d-%d...",
344 else if (ret == 0x01) {
345 vpTRACE(
"No left valey for peak %d-%d...",
349 else if (ret == 0x11) {
356 unsigned ret = h.
getValey(distance, peak2, valeyl, valeyr);
358 vpTRACE(
"No left and right valey for peak %d-%d...",
361 else if (ret == 0x10) {
362 vpTRACE(
"No right valey for peak %d-%d...",
366 else if (ret == 0x01) {
367 vpTRACE(
"No left valey for peak %d-%d...",
371 else if (ret == 0x11) {
381 if (h.
getPeaks(distance, peakl, peakr, valey) ==
false) {
382 std::cout <<
"Not a bimodal histogram..." << std::endl;
385 vpTRACE(
"Bimodal histogram: valey %d-%d for peakl: %d-%d peakr: %d-%d",
393 std::cout <<
"Catch an exception: " << e << std::endl;
void calculate(const vpImage< unsigned char > &I)
unsigned getPeaks(std::list< vpHistogramPeak > &peaks)
unsigned getValue() const
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.
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
unsigned char getLevel() const
void smooth(const unsigned int fsize=3)
unsigned getValue() const
unsigned sort(std::list< vpHistogramPeak > &peaks)
unsigned char getLevel() const
bool write(const std::string &filename)
static void read(vpImage< unsigned char > &I, const char *filename)
Declaration of the valey (minimum value) in a gray level image histogram.
unsigned getValey(std::list< vpHistogramValey > &valey)