Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
testHistogram.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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 ", 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", ipath.c_str());
89 
90  if (badparam)
91  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
92 }
93 
105 bool getOptions(int argc, const char **argv, std::string &ipath, unsigned int &nbThreads)
106 {
107  const char *optarg_;
108  int c;
109  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
110 
111  switch (c) {
112  case 'i':
113  ipath = optarg_;
114  break;
115  case 't':
116  nbThreads = (unsigned int)atoi(optarg_);
117  break;
118  case 'h':
119  usage(argv[0], NULL, ipath);
120  return false;
121  break;
122 
123  case 'c':
124  case 'd':
125  break;
126 
127  default:
128  usage(argv[0], optarg_, ipath);
129  return false;
130  break;
131  }
132  }
133 
134  if ((c == 1) || (c == -1)) {
135  // standalone param or error
136  usage(argv[0], NULL, ipath);
137  std::cerr << "ERROR: " << std::endl;
138  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
139  return false;
140  }
141 
142  return true;
143 }
144 
152 unsigned int histogramSum(const vpImage<unsigned char> &I, const unsigned int nbBins, const unsigned int nbThreads)
153 {
154  unsigned int sum = 0;
155 
156  vpHistogram histogram;
157  histogram.calculate(I, nbBins, nbThreads);
158 
159  for (unsigned int cpt = 0; cpt < histogram.getSize(); cpt++) {
160  sum += histogram[cpt];
161  }
162 
163  return sum;
164 }
165 
172 bool compareHistogram(const vpImage<unsigned char> &I, const unsigned int nbBins)
173 {
174  vpHistogram histogram_single_threaded;
175  histogram_single_threaded.calculate(I, nbBins, 1);
176 
177  vpHistogram histogram_multi_threaded;
178  histogram_multi_threaded.calculate(I, nbBins, 4);
179 
180  unsigned int sum = 0;
181  for (unsigned int cpt = 0; cpt < nbBins; cpt++) {
182  if (histogram_single_threaded[cpt] != histogram_multi_threaded[cpt]) {
183  std::cerr << "histogram_single_threaded[" << cpt << "]=" << histogram_single_threaded[cpt]
184  << " ; histogram_multi_threaded[" << cpt << "]=" << histogram_multi_threaded[cpt] << std::endl;
185 
186  return false;
187  }
188 
189  sum += histogram_single_threaded[cpt];
190  }
191 
192  if (sum != I.getSize()) {
193  std::cerr << "Sum of histogram is different with the image size!" << std::endl;
194  return false;
195  }
196 
197  return true;
198 }
199 
200 int main(int argc, const char **argv)
201 {
202  try {
203  std::string env_ipath;
204  std::string opt_ipath;
205  std::string ipath;
206  std::string filename;
207  unsigned int nbThreads = 4;
208 
209  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
210  // environment variable value
211  env_ipath = vpIoTools::getViSPImagesDataPath();
212 
213  // Set the default input path
214  if (!env_ipath.empty())
215  ipath = env_ipath;
216 
217  // Read the command line options
218  if (getOptions(argc, argv, opt_ipath, nbThreads) == false) {
219  exit(-1);
220  }
221 
222  // Get the option values
223  if (!opt_ipath.empty())
224  ipath = opt_ipath;
225 
226  // Compare ipath and env_ipath. If they differ, we take into account
227  // the input path comming from the command line option
228  if (!opt_ipath.empty() && !env_ipath.empty()) {
229  if (ipath != env_ipath) {
230  std::cout << std::endl << "WARNING: " << std::endl;
231  std::cout << " Since -i <visp image path=" << ipath << "> "
232  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
233  << " we skip the environment variable." << std::endl;
234  }
235  }
236 
237  // Test if an input path is set
238  if (opt_ipath.empty() && env_ipath.empty()) {
239  usage(argv[0], NULL, ipath);
240  std::cerr << std::endl << "ERROR:" << std::endl;
241  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
242  << " environment variable to specify the location of the " << std::endl
243  << " image path where test images are located." << std::endl
244  << std::endl;
245  exit(-1);
246  }
247 
248  //
249  // Here starts really the test
250  //
251 
252  // Create a grey level image
254 
255  // Load a grey image from the disk
256  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
257  std::cout << "Read image: " << filename << std::endl;
258  vpImageIo::read(I, filename);
259 
260  std::cout << "I=" << I.getWidth() << "x" << I.getHeight() << std::endl;
261 
262  int nbIterations = 100;
263  unsigned int nbBins = 256;
264  unsigned int sum_single_thread = 0;
265  unsigned int sum_single_multithread = 0;
266 
267  double t_single_thread = vpTime::measureTimeMs();
268  for (int iteration = 0; iteration < nbIterations; iteration++) {
269  sum_single_thread = histogramSum(I, nbBins, 1);
270  }
271  t_single_thread = vpTime::measureTimeMs() - t_single_thread;
272 
273  double t_multithread = vpTime::measureTimeMs();
274  for (int iteration = 0; iteration < nbIterations; iteration++) {
275  sum_single_multithread = histogramSum(I, nbBins, nbThreads);
276  }
277  t_multithread = vpTime::measureTimeMs() - t_multithread;
278 
279  std::cout << "sum_single_thread=" << sum_single_thread << " ; t_single_thread=" << t_single_thread
280  << " ms ; mean=" << t_single_thread / (double)nbIterations << " ms" << std::endl;
281  std::cout << "sum_single_multithread=" << sum_single_multithread << " ; t_multithread=" << t_multithread
282  << " ms ; mean=" << t_multithread / (double)nbIterations << " ms" << std::endl;
283  std::cout << "Speed-up=" << t_single_thread / (double)t_multithread << "X" << std::endl;
284 
285  if (sum_single_thread != I.getSize() || sum_single_multithread != I.getSize()) {
286  std::cerr << "Problem with histogram!" << std::endl;
287  return -1;
288  }
289 
290  nbBins = 101;
291  if (!compareHistogram(I, nbBins)) {
292  std::cerr << "Histogram are different!" << std::endl;
293  return -1;
294  }
295 
296  // Test histogram computation on empty image
297  vpHistogram histogram;
298  vpImage<unsigned char> I_test(0, 0);
299  histogram.calculate(I_test, 256, 4);
300  if (histogram.getSize() == 256) {
301  for (unsigned int cpt = 0; cpt < 256; cpt++) {
302  if (histogram[cpt] != 0) {
303  std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
304  << " but should be zero!" << std::endl;
305  }
306  }
307  } else {
308  std::cerr << "Bad histogram size!" << std::endl;
309  return -1;
310  }
311 
312  // Test histogram computation on image size < nbThreads
313  I_test.init(3, 1);
314  I_test = 100;
315  histogram.calculate(I_test, 256, 4);
316  if (histogram.getSize() == 256) {
317  for (unsigned int cpt = 0; cpt < 256; cpt++) {
318  if (cpt == 100) {
319  if (histogram[cpt] != I_test.getSize()) {
320  std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
321  << " but should be: " << I_test.getSize() << std::endl;
322  return -1;
323  }
324  } else {
325  if (histogram[cpt] != 0) {
326  std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
327  << " but should be zero!" << std::endl;
328  }
329  }
330  }
331  } else {
332  std::cerr << "Bad histogram size!" << std::endl;
333  return -1;
334  }
335 
336  // Test histogram computation on small image size
337  I_test.init(7, 1);
338  I_test = 50;
339  histogram.calculate(I_test, 256, 4);
340  if (histogram.getSize() == 256) {
341  for (unsigned int cpt = 0; cpt < 256; cpt++) {
342  if (cpt == 50) {
343  if (histogram[cpt] != I_test.getSize()) {
344  std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
345  << " but should be: " << I_test.getSize() << std::endl;
346  return -1;
347  }
348  } else {
349  if (histogram[cpt] != 0) {
350  std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
351  << " but should be zero!" << std::endl;
352  }
353  }
354  }
355  } else {
356  std::cerr << "Bad histogram size!" << std::endl;
357  return -1;
358  }
359 
360  std::cout << "testHistogram is OK!" << std::endl;
361  return 0;
362  } catch (const vpException &e) {
363  std::cerr << "Catch an exception: " << e.what() << std::endl;
364  return 1;
365  }
366 }
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1316
unsigned int getWidth() const
Definition: vpImage.h:239
unsigned getSize() const
Definition: vpHistogram.h:267
error that can be emited by ViSP classes.
Definition: vpException.h:71
Class to compute a gray level image histogram.
Definition: vpHistogram.h:112
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:88
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
const char * what() const
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1541
unsigned int getSize() const
Definition: vpImage.h:219
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)
Definition: vpImageIo.cpp:207
unsigned int getHeight() const
Definition: vpImage.h:178