36 #include <visp3/core/vpHistogram.h>
37 #include <visp3/core/vpImage.h>
38 #include <visp3/core/vpIoTools.h>
39 #include <visp3/io/vpImageIo.h>
40 #include <visp3/io/vpParseArgv.h>
50 #define GETOPTARGS "cdi:t:h"
52 #ifdef ENABLE_VISP_NAMESPACE
64 void usage(
const char *name,
const char *badparam, std::string ipath)
70 %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 \"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)
117 nbThreads = (
unsigned int)atoi(optarg_);
120 usage(argv[0],
nullptr, ipath);
129 usage(argv[0], optarg_, ipath);
135 if ((c == 1) || (c == -1)) {
137 usage(argv[0],
nullptr, ipath);
138 std::cerr <<
"ERROR: " << std::endl;
139 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
155 unsigned int sum = 0;
158 histogram.
calculate(I, nbBins, nbThreads);
160 for (
unsigned int cpt = 0; cpt < histogram.
getSize(); cpt++) {
161 sum += histogram[cpt];
176 histogram_single_threaded.
calculate(I, nbBins, 1);
179 histogram_multi_threaded.
calculate(I, nbBins, 4);
181 unsigned int sum = 0;
182 for (
unsigned int cpt = 0; cpt < nbBins; cpt++) {
183 if (histogram_single_threaded[cpt] != histogram_multi_threaded[cpt]) {
184 std::cerr <<
"histogram_single_threaded[" << cpt <<
"]=" << histogram_single_threaded[cpt]
185 <<
" ; histogram_multi_threaded[" << cpt <<
"]=" << histogram_multi_threaded[cpt] << std::endl;
190 sum += histogram_single_threaded[cpt];
194 std::cerr <<
"Sum of histogram is different with the image size!" << std::endl;
201 int main(
int argc,
const char **argv)
204 std::string env_ipath;
205 std::string opt_ipath;
207 std::string filename;
208 unsigned int nbThreads = 4;
215 if (!env_ipath.empty())
219 if (getOptions(argc, argv, opt_ipath, nbThreads) ==
false) {
224 if (!opt_ipath.empty())
229 if (!opt_ipath.empty() && !env_ipath.empty()) {
230 if (ipath != env_ipath) {
231 std::cout << std::endl <<
"WARNING: " << std::endl;
232 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
233 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
234 <<
" we skip the environment variable." << std::endl;
239 if (opt_ipath.empty() && env_ipath.empty()) {
240 usage(argv[0],
nullptr, ipath);
241 std::cerr << std::endl <<
"ERROR:" << std::endl;
242 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
243 <<
" environment variable to specify the location of the " << std::endl
244 <<
" image path where test images are located." << std::endl
258 std::cout <<
"Read image: " << filename << std::endl;
263 int nbIterations = 100;
264 unsigned int nbBins = 256;
265 unsigned int sum_single_thread = 0;
266 unsigned int sum_multi_thread = 0;
269 for (
int iteration = 0; iteration < nbIterations; iteration++) {
270 sum_single_thread = histogramSum(I, nbBins, 1);
275 for (
int iteration = 0; iteration < nbIterations; iteration++) {
276 sum_multi_thread = histogramSum(I, nbBins, nbThreads);
280 std::cout <<
"sum_single_thread=" << sum_single_thread <<
" ; t_single_thread=" << t_single_thread
281 <<
" ms ; mean=" << t_single_thread / (double)nbIterations <<
" ms" << std::endl;
282 std::cout <<
"sum_multi_thread (nbThreads=" << nbThreads <<
")=" << sum_multi_thread <<
" ; t_multi_thread=" << t_multi_thread
283 <<
" ms ; mean=" << t_multi_thread / (double)nbIterations <<
" ms" << std::endl;
284 std::cout <<
"Speed-up=" << t_single_thread / (double)t_multi_thread <<
"X" << std::endl;
286 if (sum_single_thread != I.
getSize() || sum_multi_thread != I.
getSize()) {
287 std::cerr <<
"Problem with histogram!" << std::endl;
292 if (!compareHistogram(I, nbBins)) {
293 std::cerr <<
"Histogram are different!" << std::endl;
298 std::cout <<
"Test histogram computation on empty image" << std::endl << std::flush;
302 if (histogram.
getSize() == 256) {
303 for (
unsigned int cpt = 0; cpt < 256; cpt++) {
304 if (histogram[cpt] != 0) {
305 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
306 <<
" but should be zero!" << std::endl;
311 std::cerr <<
"Bad histogram size!" << std::endl;
316 std::cout <<
"Test histogram computation on image size < nbThreads" << std::endl << std::flush;
320 if (histogram.
getSize() == 256) {
321 for (
unsigned int cpt = 0; cpt < 256; cpt++) {
323 if (histogram[cpt] != I_test.getSize()) {
324 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
325 <<
" but should be: " << I_test.
getSize() << std::endl;
330 if (histogram[cpt] != 0) {
331 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
332 <<
" but should be zero!" << std::endl;
338 std::cerr <<
"Bad histogram size!" << std::endl;
343 std::cout <<
"Test histogram computation on small image size" << std::endl << std::flush;
347 if (histogram.
getSize() == 256) {
348 for (
unsigned int cpt = 0; cpt < 256; cpt++) {
350 if (histogram[cpt] != I_test.getSize()) {
351 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
352 <<
" but should be: " << I_test.
getSize() << std::endl;
357 if (histogram[cpt] != 0) {
358 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
359 <<
" but should be zero!" << std::endl;
365 std::cerr <<
"Bad histogram size!" << std::endl;
369 std::cout <<
"testHistogram is OK!" << std::endl;
373 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
error that can be emitted by ViSP classes.
const char * what() const
Class to compute a gray level image histogram.
void calculate(const vpImage< unsigned char > &I, unsigned int nbins=256, unsigned int nbThreads=1)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
unsigned int getWidth() const
unsigned int getSize() const
unsigned int getHeight() const
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
VISP_EXPORT double measureTimeMs()