45 #include <visp3/core/vpDebug.h> 46 #include <visp3/core/vpHistogram.h> 47 #include <visp3/core/vpImage.h> 48 #include <visp3/core/vpIoTools.h> 49 #include <visp3/io/vpImageIo.h> 50 #include <visp3/io/vpParseArgv.h> 58 #define GETOPTARGS "i:o:h" 78 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user)
81 Read an image on the disk, display it using X11, display some\n\ 82 features (line, circle, caracters) in overlay and finaly write \n\ 83 the image and the overlayed features in an image on the disk.\n\ 86 %s [-i <input image path>] [-o <output histogram path>]\n\ 92 -i <input image path> %s\n\ 93 Set image input path.\n\ 94 From this path read \"Klimt/Klimt.pgm\"\n\ 96 Setting the VISP_INPUT_IMAGE_PATH environment\n\ 97 variable produces the same behaviour than using\n\ 100 -o <output histogram path> %s\n\ 101 From this directory, creates the \"%s\"\n\ 102 subdirectory depending on the username, where \n\ 103 \"histogram.txt\" is saved.\n\ 106 Print the help.\n\n", ipath.c_str(), opath.c_str(), user.c_str());
109 fprintf(stderr,
"ERROR: \n");
110 fprintf(stderr,
"\nBad parameter [%s]\n", badparam);
126 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath,
const std::string &user)
140 usage(argv[0], NULL, ipath, opath, user);
145 usage(argv[0], optarg_, ipath, opath, user);
151 if ((c == 1) || (c == -1)) {
153 usage(argv[0], NULL, ipath, opath, user);
154 std::cerr <<
"ERROR: " << std::endl;
155 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
162 int main(
int argc,
const char **argv)
165 std::string env_ipath;
166 std::string opt_ipath;
167 std::string opt_opath;
170 std::string filename;
171 std::string username;
178 if (!env_ipath.empty())
183 opt_opath =
"C:/temp";
192 if (getOptions(argc, argv, opt_ipath, opt_opath, username) ==
false) {
197 if (!opt_ipath.empty())
199 if (!opt_opath.empty())
211 usage(argv[0], NULL, ipath, opath, username);
212 std::cerr << std::endl <<
"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 <<
"WARNING: " << std::endl;
224 std::cout <<
" Since -i <visp image path=" << ipath <<
"> " 225 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
226 <<
" we skip the environment variable." << std::endl;
231 if (opt_ipath.empty() && env_ipath.empty()) {
232 usage(argv[0], NULL, ipath, opath, username);
233 std::cerr << std::endl <<
"ERROR:" << std::endl;
234 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
235 <<
" environment variable to specify the location of the " << std::endl
236 <<
" image path where test images are located." << std::endl
246 if (opt_ipath.empty())
249 std::cout <<
"Read: " << filename << std::endl;
252 unsigned char distance = 60;
260 std::cout <<
"Save the histogram in: " << filename << std::endl;
267 std::cout <<
"Save the smoothed histogram in: " << filename << std::endl;
270 std::list<vpHistogramPeak> peaks;
271 unsigned int nbpeaks = 0;
277 vpTRACE(
"Nb peaks: %d", nbpeaks);
279 for (std::list<vpHistogramPeak>::const_iterator it = peaks.begin(); it != peaks.end(); ++it) {
287 nbpeaks = h.
sort(peaks);
289 vpTRACE(
"Sorted list of peaks");
290 vpTRACE(
"Nb peaks: %d", nbpeaks);
292 for (std::list<vpHistogramPeak>::const_iterator it = peaks.begin(); it != peaks.end(); ++it) {
300 nbpeaks = h.
getPeaks(distance, peak1, peak2);
302 std::cout <<
"Not a bimodal histogram..." << std::endl;
304 vpTRACE(
"Bimodal histogram: main peak1: %d-%d second peak2: %d-%d", peak1.getLevel(), peak1.getValue(),
305 peak2.getLevel(), peak2.getValue());
310 if (h.
getValey(peak1, peak2, valey) ==
false) {
313 vpTRACE(
"Valey: %d-%d", valey.getLevel(), valey.getValue());
320 unsigned ret = h.
getValey(distance, peak1, valeyl, valeyr);
322 vpTRACE(
"No left and right valey for peak %d-%d...", peak1.getLevel(), peak1.getValue());
323 }
else if (ret == 0x10) {
324 vpTRACE(
"No right valey for peak %d-%d...", peak1.getLevel(), peak1.getValue());
325 vpTRACE(
"Left valey: %d-%d", valeyl.getLevel(), valeyl.getValue());
326 }
else if (ret == 0x01) {
327 vpTRACE(
"No left valey for peak %d-%d...", peak1.getLevel(), peak1.getValue());
328 vpTRACE(
"Right valey: %d-%d", valeyr.getLevel(), valeyr.getValue());
329 }
else if (ret == 0x11) {
330 vpTRACE(
"Left valey: %d-%d", valeyl.getLevel(), valeyl.getValue());
331 vpTRACE(
"Right valey: %d-%d", valeyr.getLevel(), valeyr.getValue());
336 unsigned ret = h.
getValey(distance, peak2, valeyl, valeyr);
338 vpTRACE(
"No left and right valey for peak %d-%d...", peak2.getLevel(), peak2.getValue());
339 }
else if (ret == 0x10) {
340 vpTRACE(
"No right valey for peak %d-%d...", peak2.getLevel(), peak2.getValue());
341 vpTRACE(
"Left valey: %d-%d", valeyl.getLevel(), valeyl.getValue());
342 }
else if (ret == 0x01) {
343 vpTRACE(
"No left valey for peak %d-%d...", peak2.getLevel(), peak2.getValue());
344 vpTRACE(
"Right valey: %d-%d", valeyr.getLevel(), valeyr.getValue());
345 }
else if (ret == 0x11) {
346 vpTRACE(
"Left valey: %d-%d", valeyl.getLevel(), valeyl.getValue());
347 vpTRACE(
"Right valey: %d-%d", valeyr.getLevel(), valeyr.getValue());
355 if (h.
getPeaks(distance, peakl, peakr, valey) ==
false) {
356 std::cout <<
"Not a bimodal histogram..." << std::endl;
358 vpTRACE(
"Bimodal histogram: valey %d-%d for peakl: %d-%d peakr: %d-%d", valey.getLevel(), valey.getValue(),
359 peakl.getLevel(), peakl.getValue(), peakr.getLevel(), peakr.getValue());
363 std::cout <<
"Catch an exception: " << e << std::endl;
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)
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)
unsigned char getLevel() const
static void read(vpImage< unsigned char > &I, const std::string &filename)
bool write(const std::string &filename)
Declaration of the valey (minimum value) in a gray level image histogram.
unsigned getValey(std::list< vpHistogramValey > &valey)