38 #include <visp3/core/vpImage.h>
39 #include <visp3/io/vpImageIo.h>
40 #include <visp3/io/vpParseArgv.h>
41 #include <visp3/core/vpIoTools.h>
42 #include <visp3/core/vpHistogram.h>
54 #define GETOPTARGS "cdi:t:h"
65 void usage(
const char *name,
const char *badparam, std::string ipath)
71 %s [-i <input image path>] [-t <nb threads>]\n\
77 -i <input image path> %s\n\
78 Set image input path.\n\
79 From this path read \"ViSP-images/Klimt/Klimt.ppm\"\n\
81 Setting the VISP_INPUT_IMAGE_PATH environment\n\
82 variable produces the same behaviour than using\n\
86 Set the number of threads to use for the computation.\n\
92 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
106 bool getOptions(
int argc,
const char **argv, std::string &ipath,
unsigned int &nbThreads)
113 case 'i': ipath = optarg_;
break;
114 case 't': nbThreads = (
unsigned int) atoi(optarg_);
break;
115 case 'h': usage(argv[0], NULL, ipath);
return false;
break;
122 usage(argv[0], optarg_, ipath);
return false;
break;
126 if ((c == 1) || (c == -1)) {
128 usage(argv[0], NULL, ipath);
129 std::cerr <<
"ERROR: " << std::endl;
130 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
144 unsigned int histogramSum(
const vpImage<unsigned char> &I,
const unsigned int nbBins,
const unsigned int nbThreads) {
145 unsigned int sum = 0;
148 histogram.
calculate(I, nbBins, nbThreads);
150 for(
unsigned int cpt = 0; cpt < histogram.
getSize(); cpt++) {
151 sum += histogram[cpt];
165 histogram_single_threaded.
calculate(I, nbBins, 1);
168 histogram_multi_threaded.
calculate(I, nbBins, 4);
170 unsigned int sum = 0;
171 for(
unsigned int cpt = 0; cpt < nbBins; cpt++) {
172 if(histogram_single_threaded[cpt] != histogram_multi_threaded[cpt]) {
173 std::cerr <<
"histogram_single_threaded[" << cpt <<
"]=" << histogram_single_threaded[cpt] <<
174 " ; histogram_multi_threaded[" << cpt <<
"]=" << histogram_multi_threaded[cpt] << std::endl;
179 sum += histogram_single_threaded[cpt];
183 std::cerr <<
"Sum of histogram is different with the image size!" << std::endl;
191 main(
int argc,
const char ** argv)
194 std::string env_ipath;
195 std::string opt_ipath;
197 std::string filename;
198 unsigned int nbThreads = 4;
204 if (! env_ipath.empty())
209 if (getOptions(argc, argv, opt_ipath, nbThreads) ==
false) {
214 if (!opt_ipath.empty())
219 if (!opt_ipath.empty() && !env_ipath.empty()) {
220 if (ipath != env_ipath) {
221 std::cout << std::endl
222 <<
"WARNING: " << std::endl;
223 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
224 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
225 <<
" we skip the environment variable." << std::endl;
230 if (opt_ipath.empty() && env_ipath.empty()){
231 usage(argv[0], NULL, ipath);
232 std::cerr << std::endl
233 <<
"ERROR:" << std::endl;
234 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
236 <<
" environment variable to specify the location of the " << std::endl
237 <<
" image path where test images are located." << std::endl << std::endl;
251 std::cout <<
"Read image: " << filename << std::endl;
256 int nbIterations = 100;
257 unsigned int nbBins = 256;
258 unsigned int sum_single_thread = 0;
259 unsigned int sum_single_multithread = 0;
262 for(
int iteration = 0; iteration < nbIterations; iteration++) {
263 sum_single_thread = histogramSum(I, nbBins, 1);
268 for(
int iteration = 0; iteration < nbIterations; iteration++) {
269 sum_single_multithread = histogramSum(I, nbBins, nbThreads);
273 std::cout <<
"sum_single_thread=" << sum_single_thread <<
" ; t_single_thread=" << t_single_thread
274 <<
" ms ; mean=" << t_single_thread / (double) nbIterations <<
" ms" << std::endl;
275 std::cout <<
"sum_single_multithread=" << sum_single_multithread <<
" ; t_multithread=" << t_multithread
276 <<
" ms ; mean=" << t_multithread / (double) nbIterations <<
" ms" << std::endl;
277 std::cout <<
"Speed-up=" << t_single_thread / (double) t_multithread <<
"X" << std::endl;
279 if(sum_single_thread != I.
getSize() || sum_single_multithread != I.
getSize()) {
280 std::cerr <<
"Problem with histogram!" << std::endl;
285 if(!compareHistogram(I, nbBins)) {
286 std::cerr <<
"Histogram are different!" << std::endl;
295 if(histogram.
getSize() == 256) {
296 for(
unsigned int cpt = 0; cpt < 256; cpt++) {
297 if(histogram[cpt] != 0) {
298 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
299 <<
" but should be zero!" << std::endl;
303 std::cerr <<
"Bad histogram size!" << std::endl;
312 if(histogram.
getSize() == 256) {
313 for(
unsigned int cpt = 0; cpt < 256; cpt++) {
315 if(histogram[cpt] != I_test.
getSize()) {
316 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
317 <<
" but should be: " << I_test.
getSize() << std::endl;
321 if(histogram[cpt] != 0) {
322 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
323 <<
" but should be zero!" << std::endl;
328 std::cerr <<
"Bad histogram size!" << std::endl;
337 if(histogram.
getSize() == 256) {
338 for(
unsigned int cpt = 0; cpt < 256; cpt++) {
340 if(histogram[cpt] != I_test.
getSize()) {
341 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
342 <<
" but should be: " << I_test.
getSize() << std::endl;
346 if(histogram[cpt] != 0) {
347 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
348 <<
" but should be zero!" << std::endl;
353 std::cerr <<
"Bad histogram size!" << std::endl;
358 std::cout <<
"testHistogram is OK!" << std::endl;
362 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
unsigned int getWidth() const
error that can be emited by ViSP classes.
Class to compute a gray level image histogram.
VISP_EXPORT double measureTimeMs()
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
const char * what() const
unsigned int getSize() const
void calculate(const vpImage< unsigned char > &I, const unsigned int nbins=256, const unsigned int nbThreads=1)
static void read(vpImage< unsigned char > &I, const std::string &filename)
unsigned int getHeight() const