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