Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
testHistogram.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Test histogram computation.
32  */
33 
34 #include <stdio.h>
35 #include <stdlib.h>
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>
41 
49 // List of allowed command line options
50 #define GETOPTARGS "cdi:t:h"
51 
52 #ifdef ENABLE_VISP_NAMESPACE
53 using namespace VISP_NAMESPACE_NAME;
54 #endif
55 
56 /*
57  Print the program options.
58 
59  \param name : Program name.
60  \param badparam : Bad parameter name.
61  \param ipath: Input image path.
62 
63  */
64 void usage(const char *name, const char *badparam, std::string ipath)
65 {
66  fprintf(stdout, "\n\
67 Test histogram.\n\
68 \n\
69 SYNOPSIS\n\
70  %s [-i <input image path>] [-t <nb threads>]\n\
71  [-h]\n \
72 ",
73 name);
74 
75  fprintf(stdout, "\n\
76 OPTIONS: Default\n\
77  -i <input image path> %s\n\
78  Set image input path.\n\
79  From this path read \"Klimt/Klimt.ppm\"\n\
80  image.\n\
81  Setting the VISP_INPUT_IMAGE_PATH environment\n\
82  variable produces the same behaviour than using\n\
83  this option.\n\
84 \n\
85  -t <nb threads>\n\
86  Set the number of threads to use for the computation.\n\
87  -h\n\
88  Print the help.\n\n",
89  ipath.c_str());
90 
91  if (badparam)
92  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
93 }
94 
106 bool getOptions(int argc, const char **argv, std::string &ipath, unsigned int &nbThreads)
107 {
108  const char *optarg_;
109  int c;
110  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
111 
112  switch (c) {
113  case 'i':
114  ipath = optarg_;
115  break;
116  case 't':
117  nbThreads = (unsigned int)atoi(optarg_);
118  break;
119  case 'h':
120  usage(argv[0], nullptr, ipath);
121  return false;
122  break;
123 
124  case 'c':
125  case 'd':
126  break;
127 
128  default:
129  usage(argv[0], optarg_, ipath);
130  return false;
131  break;
132  }
133  }
134 
135  if ((c == 1) || (c == -1)) {
136  // standalone param or error
137  usage(argv[0], nullptr, ipath);
138  std::cerr << "ERROR: " << std::endl;
139  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
140  return false;
141  }
142 
143  return true;
144 }
145 
153 unsigned int histogramSum(const vpImage<unsigned char> &I, unsigned int nbBins, unsigned int nbThreads)
154 {
155  unsigned int sum = 0;
156 
157  vpHistogram histogram;
158  histogram.calculate(I, nbBins, nbThreads);
159 
160  for (unsigned int cpt = 0; cpt < histogram.getSize(); cpt++) {
161  sum += histogram[cpt];
162  }
163 
164  return sum;
165 }
166 
173 bool compareHistogram(const vpImage<unsigned char> &I, unsigned int nbBins)
174 {
175  vpHistogram histogram_single_threaded;
176  histogram_single_threaded.calculate(I, nbBins, 1);
177 
178  vpHistogram histogram_multi_threaded;
179  histogram_multi_threaded.calculate(I, nbBins, 4);
180 
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;
186 
187  return false;
188  }
189 
190  sum += histogram_single_threaded[cpt];
191  }
192 
193  if (sum != I.getSize()) {
194  std::cerr << "Sum of histogram is different with the image size!" << std::endl;
195  return false;
196  }
197 
198  return true;
199 }
200 
201 int main(int argc, const char **argv)
202 {
203  try {
204  std::string env_ipath;
205  std::string opt_ipath;
206  std::string ipath;
207  std::string filename;
208  unsigned int nbThreads = 4;
209 
210  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
211  // environment variable value
212  env_ipath = vpIoTools::getViSPImagesDataPath();
213 
214  // Set the default input path
215  if (!env_ipath.empty())
216  ipath = env_ipath;
217 
218  // Read the command line options
219  if (getOptions(argc, argv, opt_ipath, nbThreads) == false) {
220  return EXIT_FAILURE;
221  }
222 
223  // Get the option values
224  if (!opt_ipath.empty())
225  ipath = opt_ipath;
226 
227  // Compare ipath and env_ipath. If they differ, we take into account
228  // the input path coming from the command line option
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;
235  }
236  }
237 
238  // Test if an input path is set
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
245  << std::endl;
246  return EXIT_FAILURE;
247  }
248 
249  //
250  // Here starts really the test
251  //
252 
253  // Create a grey level image
255 
256  // Load a grey image from the disk
257  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
258  std::cout << "Read image: " << filename << std::endl;
259  vpImageIo::read(I, filename);
260 
261  std::cout << "I=" << I.getWidth() << "x" << I.getHeight() << std::endl;
262 
263  int nbIterations = 100;
264  unsigned int nbBins = 256;
265  unsigned int sum_single_thread = 0;
266  unsigned int sum_multi_thread = 0;
267 
268  double t_single_thread = vpTime::measureTimeMs();
269  for (int iteration = 0; iteration < nbIterations; iteration++) {
270  sum_single_thread = histogramSum(I, nbBins, 1);
271  }
272  t_single_thread = vpTime::measureTimeMs() - t_single_thread;
273 
274  double t_multi_thread = vpTime::measureTimeMs();
275  for (int iteration = 0; iteration < nbIterations; iteration++) {
276  sum_multi_thread = histogramSum(I, nbBins, nbThreads);
277  }
278  t_multi_thread = vpTime::measureTimeMs() - t_multi_thread;
279 
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;
285 
286  if (sum_single_thread != I.getSize() || sum_multi_thread != I.getSize()) {
287  std::cerr << "Problem with histogram!" << std::endl;
288  return EXIT_FAILURE;
289  }
290 
291  nbBins = 101;
292  if (!compareHistogram(I, nbBins)) {
293  std::cerr << "Histogram are different!" << std::endl;
294  return EXIT_FAILURE;
295  }
296 
297  // Test histogram computation on empty image
298  std::cout << "Test histogram computation on empty image" << std::endl << std::flush;
299  vpHistogram histogram;
300  vpImage<unsigned char> I_test(0, 0);
301  histogram.calculate(I_test, 256, 4);
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;
307  }
308  }
309  }
310  else {
311  std::cerr << "Bad histogram size!" << std::endl;
312  return EXIT_FAILURE;
313  }
314 
315  // Test histogram computation on image size < nbThreads
316  std::cout << "Test histogram computation on image size < nbThreads" << std::endl << std::flush;
317  I_test.init(3, 1);
318  I_test = 100;
319  histogram.calculate(I_test, 256, 4);
320  if (histogram.getSize() == 256) {
321  for (unsigned int cpt = 0; cpt < 256; cpt++) {
322  if (cpt == 100) {
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;
326  return EXIT_FAILURE;
327  }
328  }
329  else {
330  if (histogram[cpt] != 0) {
331  std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
332  << " but should be zero!" << std::endl;
333  }
334  }
335  }
336  }
337  else {
338  std::cerr << "Bad histogram size!" << std::endl;
339  return EXIT_FAILURE;
340  }
341 
342  // Test histogram computation on small image size
343  std::cout << "Test histogram computation on small image size" << std::endl << std::flush;
344  I_test.init(7, 1);
345  I_test = 50;
346  histogram.calculate(I_test, 256, 4);
347  if (histogram.getSize() == 256) {
348  for (unsigned int cpt = 0; cpt < 256; cpt++) {
349  if (cpt == 50) {
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;
353  return EXIT_FAILURE;
354  }
355  }
356  else {
357  if (histogram[cpt] != 0) {
358  std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
359  << " but should be zero!" << std::endl;
360  }
361  }
362  }
363  }
364  else {
365  std::cerr << "Bad histogram size!" << std::endl;
366  return EXIT_FAILURE;
367  }
368 
369  std::cout << "testHistogram is OK!" << std::endl;
370  return EXIT_SUCCESS;
371  }
372  catch (const vpException &e) {
373  std::cerr << "Catch an exception: " << e.what() << std::endl;
374  return EXIT_FAILURE;
375  }
376 }
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const char * what() const
Definition: vpException.cpp:71
Class to compute a gray level image histogram.
Definition: vpHistogram.h:106
void calculate(const vpImage< unsigned char > &I, unsigned int nbins=256, unsigned int nbThreads=1)
unsigned getSize() const
Definition: vpHistogram.h:278
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getSize() const
Definition: vpImage.h:221
unsigned int getHeight() const
Definition: vpImage.h:181
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
VISP_EXPORT double measureTimeMs()