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"
84 void usage(
const char *name,
const char *badparam, std::string ipath,
85 std::string opath, std::string user)
88 Read an image on the disk, display it using X11, display some\n\
89 features (line, circle, caracters) in overlay and finaly write \n\
90 the image and the overlayed features in an image on the disk.\n\
93 %s [-i <input image path>] [-o <output histogram path>]\n\
99 -i <input image path> %s\n\
100 Set image input path.\n\
101 From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
103 Setting the VISP_INPUT_IMAGE_PATH environment\n\
104 variable produces the same behaviour than using\n\
107 -o <output histogram path> %s\n\
108 From this directory, creates the \"%s\"\n\
109 subdirectory depending on the username, where \n\
110 \"histogram.txt\" is saved.\n\
113 Print the help.\n\n",
114 ipath.c_str(), opath.c_str(), user.c_str());
117 fprintf(stderr,
"ERROR: \n" );
118 fprintf(stderr,
"\nBad parameter [%s]\n", badparam);
135 bool getOptions(
int argc,
const char **argv,
136 std::string &ipath, std::string &opath,
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)
167 std::string env_ipath;
168 std::string opt_ipath;
169 std::string opt_opath;
172 std::string filename;
173 std::string username;
176 char *ptenv = getenv(
"VISP_INPUT_IMAGE_PATH");
182 if (! env_ipath.empty())
187 opt_opath =
"C:/temp";
196 if (getOptions(argc, argv, opt_ipath, opt_opath, username) ==
false) {
201 if (!opt_ipath.empty())
203 if (!opt_opath.empty())
216 usage(argv[0], NULL, ipath, opath, username);
217 std::cerr << std::endl
218 <<
"ERROR:" << std::endl;
219 std::cerr <<
" Cannot create " << dirname << std::endl;
220 std::cerr <<
" Check your -o " << opath <<
" option " << std::endl;
227 if (opt_ipath.empty()) {
228 if (ipath != env_ipath) {
229 std::cout << std::endl
230 <<
"WARNING: " << std::endl;
231 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
232 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
233 <<
" we skip the environment variable." << std::endl;
238 if (opt_ipath.empty() && env_ipath.empty()){
239 usage(argv[0], NULL, ipath, opath, username);
240 std::cerr << std::endl
241 <<
"ERROR:" << std::endl;
242 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
244 <<
" environment variable to specify the location of the " << std::endl
245 <<
" image path where test images are located." << std::endl << std::endl;
254 if (opt_ipath.empty())
257 std::cout <<
"Read: " << filename << std::endl;
260 unsigned char distance = 60;
268 std::cout <<
"Save the histogram in: " << filename << std::endl;
275 std::cout <<
"Save the smoothed histogram in: " << filename << std::endl;
278 std::list<vpHistogramPeak> peaks;
279 unsigned int nbpeaks = 0;
285 vpTRACE(
"Nb peaks: %d", nbpeaks);
287 for(std::list<vpHistogramPeak>::const_iterator it = peaks.begin(); it != peaks.end(); ++it)
296 nbpeaks = h.
sort(peaks);
298 vpTRACE(
"Sorted list of peaks");
299 vpTRACE(
"Nb peaks: %d", nbpeaks);
301 for(std::list<vpHistogramPeak>::const_iterator it = peaks.begin(); it != peaks.end(); ++it)
310 nbpeaks = h.
getPeaks(distance, peak1, peak2);
312 std::cout <<
"Not a bimodal histogram..." << std::endl;
315 vpTRACE(
"Bimodal histogram: main peak1: %d-%d second peak2: %d-%d",
322 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",
void calculate(const vpImage< unsigned char > &I)
unsigned getPeaks(std::list< vpHistogramPeak > &peaks)
unsigned getValue() const
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)