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;
180 if (! env_ipath.empty())
185 opt_opath =
"C:/temp";
194 if (getOptions(argc, argv, opt_ipath, opt_opath, username) ==
false) {
199 if (!opt_ipath.empty())
201 if (!opt_opath.empty())
214 usage(argv[0], NULL, ipath, opath, username);
215 std::cerr << std::endl
216 <<
"ERROR:" << std::endl;
217 std::cerr <<
" Cannot create " << dirname << std::endl;
218 std::cerr <<
" Check your -o " << opath <<
" option " << std::endl;
225 if (opt_ipath.empty()) {
226 if (ipath != env_ipath) {
227 std::cout << std::endl
228 <<
"WARNING: " << std::endl;
229 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
230 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
231 <<
" we skip the environment variable." << std::endl;
236 if (opt_ipath.empty() && env_ipath.empty()){
237 usage(argv[0], NULL, ipath, opath, username);
238 std::cerr << std::endl
239 <<
"ERROR:" << std::endl;
240 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
242 <<
" environment variable to specify the location of the " << std::endl
243 <<
" image path where test images are located." << std::endl << std::endl;
252 if (opt_ipath.empty())
255 std::cout <<
"Read: " << filename << std::endl;
258 unsigned char distance = 60;
266 std::cout <<
"Save the histogram in: " << filename << std::endl;
273 std::cout <<
"Save the smoothed histogram in: " << filename << std::endl;
276 std::list<vpHistogramPeak> peaks;
277 unsigned int nbpeaks = 0;
283 vpTRACE(
"Nb peaks: %d", nbpeaks);
285 for(std::list<vpHistogramPeak>::const_iterator it = peaks.begin(); it != peaks.end(); ++it)
294 nbpeaks = h.
sort(peaks);
296 vpTRACE(
"Sorted list of peaks");
297 vpTRACE(
"Nb peaks: %d", nbpeaks);
299 for(std::list<vpHistogramPeak>::const_iterator it = peaks.begin(); it != peaks.end(); ++it)
308 nbpeaks = h.
getPeaks(distance, peak1, peak2);
310 std::cout <<
"Not a bimodal histogram..." << std::endl;
313 vpTRACE(
"Bimodal histogram: main peak1: %d-%d second peak2: %d-%d",
320 if (h.
getValey(peak1, peak2, valey) ==
false) {
331 unsigned ret = h.
getValey(distance, peak1, valeyl, valeyr);
333 vpTRACE(
"No left and right valey for peak %d-%d...",
336 else if (ret == 0x10) {
337 vpTRACE(
"No right valey for peak %d-%d...",
341 else if (ret == 0x01) {
342 vpTRACE(
"No left valey for peak %d-%d...",
346 else if (ret == 0x11) {
353 unsigned ret = h.
getValey(distance, peak2, valeyl, valeyr);
355 vpTRACE(
"No left and right valey for peak %d-%d...",
358 else if (ret == 0x10) {
359 vpTRACE(
"No right valey for peak %d-%d...",
363 else if (ret == 0x01) {
364 vpTRACE(
"No left valey for peak %d-%d...",
368 else if (ret == 0x11) {
378 if (h.
getPeaks(distance, peakl, peakr, valey) ==
false) {
379 std::cout <<
"Not a bimodal histogram..." << std::endl;
382 vpTRACE(
"Bimodal histogram: valey %d-%d for peakl: %d-%d peakr: %d-%d",
390 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)