43 #include <visp3/core/vpDebug.h>
44 #include <visp3/core/vpHistogram.h>
45 #include <visp3/core/vpImage.h>
46 #include <visp3/core/vpIoTools.h>
47 #include <visp3/io/vpImageIo.h>
48 #include <visp3/io/vpParseArgv.h>
56 #define GETOPTARGS "i:o:h"
76 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user)
79 Read an image on the disk, display it using X11, display some\n\
80 features (line, circle, characters) in overlay and finally write \n\
81 the image and the overlayed features in an image on the disk.\n\
84 %s [-i <input image path>] [-o <output histogram path>]\n\
91 -i <input image path> %s\n\
92 Set image input path.\n\
93 From this path read \"Klimt/Klimt.pgm\"\n\
95 Setting the VISP_INPUT_IMAGE_PATH environment\n\
96 variable produces the same behaviour than using\n\
99 -o <output histogram path> %s\n\
100 From this directory, creates the \"%s\"\n\
101 subdirectory depending on the username, where \n\
102 \"histogram.txt\" is saved.\n\
105 Print the help.\n\n",
106 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],
nullptr, ipath, opath, user);
145 usage(argv[0], optarg_, ipath, opath, user);
151 if ((c == 1) || (c == -1)) {
153 usage(argv[0],
nullptr, 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],
nullptr, 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],
nullptr, 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;
310 if (h.
getValey(peak1, peak2, valey) ==
false) {
320 unsigned ret = h.
getValey(distance, peak1, valeyl, valeyr);
323 }
else if (ret == 0x10) {
326 }
else if (ret == 0x01) {
329 }
else if (ret == 0x11) {
336 unsigned ret = h.
getValey(distance, peak2, valeyl, valeyr);
339 }
else if (ret == 0x10) {
342 }
else if (ret == 0x01) {
345 }
else if (ret == 0x11) {
355 if (h.
getPeaks(distance, peakl, peakr, valey) ==
false) {
356 std::cout <<
"Not a bimodal histogram..." << std::endl;
363 std::cout <<
"Catch an exception: " << e << std::endl;
error that can be emitted by ViSP classes.
Declaration of the peak (maximum value) in a gray level image histogram.
unsigned getValue() const
unsigned char getLevel() const
Declaration of the valey (minimum value) in a gray level image histogram.
unsigned char getLevel() const
unsigned getValue() const
Class to compute a gray level image histogram.
unsigned getPeaks(std::list< vpHistogramPeak > &peaks)
void smooth(unsigned int fsize=3)
void calculate(const vpImage< unsigned char > &I, unsigned int nbins=256, unsigned int nbThreads=1)
unsigned sort(std::list< vpHistogramPeak > &peaks)
unsigned getValey(std::list< vpHistogramValey > &valey)
bool write(const std::string &filename)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)