Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testPerformanceLUT.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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 performance between iteration and LUT.
32  *
33  * Authors:
34  * Souriya Trinh
35  *
36  *****************************************************************************/
37 
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/vpMath.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 
53 // List of allowed command line options
54 #define GETOPTARGS "cdi:o:t:h"
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  \param opath : Output image path.
63  \param user : Username.
64 
65  */
66 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
67 {
68  fprintf(stdout, "\n\
69 Test performance between methods to iterate over pixel image.\n\
70 \n\
71 SYNOPSIS\n\
72  %s [-i <input image path>] [-o <output image path>] [-t <nb threads>]\n\
73  [-h]\n \
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 \"ViSP-images/Klimt/Klimt.pgm\"\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  -o <output image path> %s\n\
87  Set image output path.\n\
88  From this directory, creates the \"%s\"\n\
89  subdirectory depending on the username, where \n\
90  Klimt_grey.pgm output image is written.\n\
91 \n\
92  -t <nb threads> \n\
93  Set the number of threads to use for the computation.\n\
94 \n\
95  -h\n\
96  Print the help.\n\n",
97  ipath.c_str(), opath.c_str(), user.c_str());
98 
99  if (badparam)
100  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
101 }
102 
116 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath,
117  std::string user, unsigned int &nbThreads)
118 {
119  const char *optarg_;
120  int c;
121  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
122 
123  switch (c) {
124  case 'i': ipath = optarg_; break;
125  case 'o': opath = optarg_; break;
126  case 't': nbThreads = (unsigned int) atoi(optarg_); break;
127  case 'h': usage(argv[0], NULL, ipath, opath, user); return false; break;
128 
129  case 'c':
130  case 'd':
131  break;
132 
133  default:
134  usage(argv[0], optarg_, ipath, opath, user); return false; break;
135  }
136  }
137 
138  if ((c == 1) || (c == -1)) {
139  // standalone param or error
140  usage(argv[0], NULL, ipath, opath, user);
141  std::cerr << "ERROR: " << std::endl;
142  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
143  return false;
144  }
145 
146  return true;
147 }
148 
157 void iterate_method1(vpImage<vpRGBa> &I, const double alpha, const double beta) {
158  unsigned int size = I.getWidth() * I.getHeight();
159  unsigned char *ptrStart = (unsigned char*) I.bitmap;
160  unsigned char *ptrEnd = ptrStart + size*4;
161  unsigned char *ptrCurrent = ptrStart;
162 
163  while(ptrCurrent != ptrEnd) {
164  *ptrCurrent = vpMath::saturate<unsigned char>((*ptrCurrent) * alpha + beta);
165  ++ptrCurrent;
166  }
167 }
168 
177 void iterate_method1(vpImage<unsigned char> &I, const double alpha, const double beta) {
178  unsigned int size = I.getWidth() * I.getHeight();
179  unsigned char *ptrStart = (unsigned char*) I.bitmap;
180  unsigned char *ptrEnd = ptrStart + size;
181  unsigned char *ptrCurrent = ptrStart;
182 
183  while(ptrCurrent != ptrEnd) {
184  *ptrCurrent = vpMath::saturate<unsigned char>((*ptrCurrent) * alpha + beta);
185  ++ptrCurrent;
186  }
187 }
188 
197 void iterate_method2(vpImage<vpRGBa> &I, const double alpha, const double beta) {
198  for(unsigned int i = 0; i < I.getHeight(); i++) {
199  for(unsigned int j = 0; j < I.getWidth(); j++) {
200  I[i][j].R = vpMath::saturate<unsigned char>(I[i][j].R * alpha + beta);
201  I[i][j].G = vpMath::saturate<unsigned char>(I[i][j].G * alpha + beta);
202  I[i][j].B = vpMath::saturate<unsigned char>(I[i][j].B * alpha + beta);
203  I[i][j].A = vpMath::saturate<unsigned char>(I[i][j].A * alpha + beta);
204  }
205  }
206 }
207 
208 int
209 main(int argc, const char ** argv)
210 {
211  try {
212  std::string env_ipath;
213  std::string opt_ipath;
214  std::string opt_opath;
215  std::string ipath;
216  std::string opath;
217  std::string filename;
218  std::string username;
219  unsigned int nbThreads = 4;
220 
221  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
222  env_ipath = vpIoTools::getViSPImagesDataPath();
223 
224  // Set the default input path
225  if (! env_ipath.empty())
226  ipath = env_ipath;
227 
228  // Set the default output path
229 #if defined(_WIN32)
230  opt_opath = "C:/temp";
231 #else
232  opt_opath = "/tmp";
233 #endif
234 
235  // Get the user login name
236  vpIoTools::getUserName(username);
237 
238  // Read the command line options
239  if (getOptions(argc, argv, opt_ipath, opt_opath, username, nbThreads) == false) {
240  exit (-1);
241  }
242 
243  // Get the option values
244  if (!opt_ipath.empty())
245  ipath = opt_ipath;
246  if (!opt_opath.empty())
247  opath = opt_opath;
248 
249  // Append to the output path string, the login name of the user
250  opath = vpIoTools::createFilePath(opath, username);
251 
252  // Test if the output path exist. If no try to create it
253  if (vpIoTools::checkDirectory(opath) == false) {
254  try {
255  // Create the dirname
257  }
258  catch (...) {
259  usage(argv[0], NULL, ipath, opt_opath, username);
260  std::cerr << std::endl
261  << "ERROR:" << std::endl;
262  std::cerr << " Cannot create " << opath << std::endl;
263  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
264  exit(-1);
265  }
266  }
267 
268  // Compare ipath and env_ipath. If they differ, we take into account
269  // the input path comming from the command line option
270  if (!opt_ipath.empty() && !env_ipath.empty()) {
271  if (ipath != env_ipath) {
272  std::cout << std::endl
273  << "WARNING: " << std::endl;
274  std::cout << " Since -i <visp image path=" << ipath << "> "
275  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
276  << " we skip the environment variable." << std::endl;
277  }
278  }
279 
280  // Test if an input path is set
281  if (opt_ipath.empty() && env_ipath.empty()){
282  usage(argv[0], NULL, ipath, opt_opath, username);
283  std::cerr << std::endl
284  << "ERROR:" << std::endl;
285  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
286  << std::endl
287  << " environment variable to specify the location of the " << std::endl
288  << " image path where test images are located." << std::endl << std::endl;
289  exit(-1);
290  }
291 
292 
293  //
294  // Here starts really the test
295  //
296 
297  // Create a grey level image
298  vpImage<vpRGBa> I_iterate1, I_iterate2, I_lut;
299 
300  // Load a grey image from the disk
301  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
302  std::cout << "\nRead image: " << filename << std::endl;
303  vpImageIo::read(I_iterate1, filename);
304  vpImageIo::read(I_iterate2, filename);
305  vpImageIo::read(I_lut, filename);
306 
307  std::cout << "I=" << I_iterate1.getWidth() << "x" << I_iterate1.getHeight() << std::endl;
308 
309  double alpha = 1.5, beta = -30.0;
310  unsigned int nbIterations = 10;
311 
312  //Iterate method 1
313  double t_iterate1 = vpTime::measureTimeMs();
314  for(unsigned int cpt = 0; cpt < nbIterations; cpt++) {
315  iterate_method1(I_iterate1, alpha, beta);
316  }
317  t_iterate1 = vpTime::measureTimeMs() - t_iterate1;
318  std::cout << "t_iterate1=" << t_iterate1 << " ms ; t_iterate1/" << nbIterations << "="
319  << (t_iterate1/nbIterations) << " ms" << std::endl;
320 
321  filename = vpIoTools::createFilePath(opath, "Klimt_performance_iterate1.ppm");
322  vpImageIo::write(I_iterate1, filename);
323 
324 
325  //Iterate method 2
326  double t_iterate2 = vpTime::measureTimeMs();
327  for(unsigned int cpt = 0; cpt < nbIterations; cpt++) {
328  iterate_method2(I_iterate2, alpha, beta);
329  }
330  t_iterate2 = vpTime::measureTimeMs() - t_iterate2;
331  std::cout << "t_iterate2=" << t_iterate2 << " ms ; t_iterate2/" << nbIterations << "="
332  << (t_iterate2/nbIterations) << " ms" << std::endl;
333 
334  filename = vpIoTools::createFilePath(opath, "Klimt_performance_iterate2.ppm");
335  vpImageIo::write(I_iterate2, filename);
336 
337 
338  //LUT method
339  double t_lut = vpTime::measureTimeMs();
340  for(unsigned int cpt = 0; cpt < nbIterations; cpt++) {
341  //Construct the LUT
342  vpRGBa lut[256];
343  for(unsigned int i = 0; i < 256; i++) {
344  lut[i].R = vpMath::saturate<unsigned char>(alpha * i + beta);
345  lut[i].G = vpMath::saturate<unsigned char>(alpha * i + beta);
346  lut[i].B = vpMath::saturate<unsigned char>(alpha * i + beta);
347  lut[i].A = vpMath::saturate<unsigned char>(alpha * i + beta);
348  }
349 
350  I_lut.performLut(lut, nbThreads);
351  }
352  t_lut = vpTime::measureTimeMs() - t_lut;
353  std::cout << "t_lut=" << t_lut << " ms ; t_lut/" << nbIterations << "="
354  << (t_lut/nbIterations) << " ms" << std::endl;
355 
356  filename = vpIoTools::createFilePath(opath, "Klimt_performance_lut.ppm");
357  vpImageIo::write(I_lut, filename);
358 
359 
360  //Check results
361  bool same = true;
362  for(unsigned int i = 0; i < I_iterate1.getHeight() && same; i++) {
363  for(unsigned int j = 0; j < I_iterate1.getWidth() && same; j++) {
364  if(I_iterate1[i][j] != I_iterate2[i][j] || I_iterate1[i][j] != I_lut[i][j]) {
365  same = false;
366  }
367  }
368  }
369 
370  if(!same) {
371  std::cerr << "Color images are different!" << std::endl;
372  return -1;
373  }
374 
375 
376  //Test LUT on grayscale image
377  vpImage<unsigned char> I_iterate_grayscale1, I_lut_grayscale;
378 
379  // Load a grayscale image from the disk
380  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.pgm");
381  std::cout << "\nRead image: " << filename << std::endl;
382  vpImageIo::read(I_iterate_grayscale1, filename);
383  vpImageIo::read(I_lut_grayscale, filename);
384 
385  std::cout << "I_grayscale=" << I_lut_grayscale.getWidth() << "x" << I_lut_grayscale.getHeight() << std::endl;
386 
387 
388  //Iterate method 1 on grayscale
389  double t_iterate_grayscale1 = vpTime::measureTimeMs();
390  for(unsigned int cpt = 0; cpt < nbIterations; cpt++) {
391  iterate_method1(I_iterate_grayscale1, alpha, beta);
392  }
393  t_iterate_grayscale1 = vpTime::measureTimeMs() - t_iterate_grayscale1;
394  std::cout << "t_iterate_grayscale1=" << t_iterate_grayscale1 << " ms ; t_iterate1/" << nbIterations << "="
395  << (t_iterate_grayscale1/nbIterations) << " ms" << std::endl;
396 
397  filename = vpIoTools::createFilePath(opath, "Klimt_performance_iterate1_grayscale.pgm");
398  vpImageIo::write(I_iterate_grayscale1, filename);
399 
400 
401  //LUT method on grayscale
402  double t_lut_grayscale = vpTime::measureTimeMs();
403  for(unsigned int cpt = 0; cpt < nbIterations; cpt++) {
404  //Construct the LUT
405  unsigned char lut[256];
406  for(unsigned int i = 0; i < 256; i++) {
407  lut[i] = vpMath::saturate<unsigned char>(alpha * i + beta);
408  }
409 
410  I_lut_grayscale.performLut(lut, nbThreads);
411  }
412  t_lut_grayscale = vpTime::measureTimeMs() - t_lut_grayscale;
413  std::cout << "t_lut_grayscale=" << t_lut_grayscale << " ms ; t_lut_grayscale/" << nbIterations << "="
414  << (t_lut_grayscale/nbIterations) << " ms" << std::endl;
415 
416  filename = vpIoTools::createFilePath(opath, "Klimt_performance_lut_grayscale.pgm");
417  vpImageIo::write(I_lut_grayscale, filename);
418 
419 
420  //Check grayscale image
421  same = true;
422  for(unsigned int i = 0; i < I_lut_grayscale.getHeight() && same; i++) {
423  for(unsigned int j = 0; j < I_lut_grayscale.getWidth() && same; j++) {
424  if(I_lut_grayscale[i][j] != I_iterate_grayscale1[i][j]) {
425  same = false;
426  }
427  }
428  }
429 
430  if(!same) {
431  std::cerr << "Grayscale images are different!" << std::endl;
432  return -1;
433  }
434 
435 
436  //Computation time on color image
437  vpImageIo::read(I_lut, filename);
438 
439  double t_lut_multithread = vpTime::measureTimeMs();
440  for(unsigned int cpt = 0; cpt < nbIterations*10; cpt++) {
441  //Construct the LUT
442  vpRGBa lut[256];
443  for(unsigned int i = 0; i < 256; i++) {
444  lut[i].R = vpMath::saturate<unsigned char>(alpha * i + beta);
445  lut[i].G = vpMath::saturate<unsigned char>(alpha * i + beta);
446  lut[i].B = vpMath::saturate<unsigned char>(alpha * i + beta);
447  lut[i].A = vpMath::saturate<unsigned char>(alpha * i + beta);
448  }
449 
450  I_lut.performLut(lut, 4);
451  }
452  t_lut_multithread = vpTime::measureTimeMs() - t_lut_multithread;
453 
454 
455  vpImageIo::read(I_lut, filename);
456 
457  double t_lut_singlethread = vpTime::measureTimeMs();
458  for(unsigned int cpt = 0; cpt < nbIterations*10; cpt++) {
459  //Construct the LUT
460  vpRGBa lut[256];
461  for(unsigned int i = 0; i < 256; i++) {
462  lut[i].R = vpMath::saturate<unsigned char>(alpha * i + beta);
463  lut[i].G = vpMath::saturate<unsigned char>(alpha * i + beta);
464  lut[i].B = vpMath::saturate<unsigned char>(alpha * i + beta);
465  lut[i].A = vpMath::saturate<unsigned char>(alpha * i + beta);
466  }
467 
468  I_lut.performLut(lut, 1);
469  }
470  t_lut_singlethread = vpTime::measureTimeMs() - t_lut_singlethread;
471 
472  std::cout << "\nt_lut_singlethread/t_lut_multithread (color)="
473  << t_lut_singlethread/t_lut_multithread << "X" << std::endl;
474 
475 
476  //Computation time on grayscale image
477  vpImageIo::read(I_lut_grayscale, filename);
478 
479  t_lut_multithread = vpTime::measureTimeMs();
480  for(unsigned int cpt = 0; cpt < nbIterations*10; cpt++) {
481  //Construct the LUT
482  unsigned char lut[256];
483  for(unsigned int i = 0; i < 256; i++) {
484  lut[i] = vpMath::saturate<unsigned char>(alpha * i + beta);
485  }
486 
487  I_lut_grayscale.performLut(lut, 4);
488  }
489  t_lut_multithread = vpTime::measureTimeMs() - t_lut_multithread;
490 
491 
492  vpImageIo::read(I_lut_grayscale, filename);
493 
494  t_lut_singlethread = vpTime::measureTimeMs();
495  for(unsigned int cpt = 0; cpt < nbIterations*10; cpt++) {
496  //Construct the LUT
497  unsigned char lut[256];
498  for(unsigned int i = 0; i < 256; i++) {
499  lut[i] = vpMath::saturate<unsigned char>(alpha * i + beta);
500  }
501 
502  I_lut_grayscale.performLut(lut, 1);
503  }
504  t_lut_singlethread = vpTime::measureTimeMs() - t_lut_singlethread;
505 
506  std::cout << "\nt_lut_singlethread/t_lut_multithread (grayscale)="
507  << t_lut_singlethread/t_lut_multithread << "X" << std::endl;
508 
509 
510 
511  //Check performLut with multithreading and image size not divisible by 8
512  vpImage<unsigned char> I_test_grayscale(49, 7);
513  //Construct the LUT
514  unsigned char lut_grayscale[256];
515  for(unsigned int i = 0; i < 256; i++) {
516  lut_grayscale[i] = vpMath::saturate<unsigned char>(alpha * i + beta);
517  }
518  I_test_grayscale.performLut(lut_grayscale, nbThreads);
519 
520  vpImage<vpRGBa> I_test_color(49, 7);
521  //Construct the LUT
522  vpRGBa lut_color[256];
523  for(unsigned int i = 0; i < 256; i++) {
524  lut_color[i].R = vpMath::saturate<unsigned char>(alpha * i + beta);
525  lut_color[i].G = vpMath::saturate<unsigned char>(alpha * i + beta);
526  lut_color[i].B = vpMath::saturate<unsigned char>(alpha * i + beta);
527  lut_color[i].A = vpMath::saturate<unsigned char>(alpha * i + beta);
528  }
529  I_test_color.performLut(lut_color, nbThreads);
530 
531 
532  return 0;
533  }
534  catch(vpException &e) {
535  std::cerr << "Catch an exception: " << e.what() << std::endl;
536  return 1;
537  }
538 }
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1157
unsigned int getWidth() const
Definition: vpImage.h:226
unsigned char B
Blue component.
Definition: vpRGBa.h:155
Type * bitmap
points toward the bitmap
Definition: vpImage.h:134
error that can be emited by ViSP classes.
Definition: vpException.h:73
unsigned char G
Green component.
Definition: vpRGBa.h:154
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
Definition: vpRGBa.h:66
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:368
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:427
const char * what() const
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1366
unsigned char A
Additionnal component.
Definition: vpRGBa.h:156
static std::string getUserName()
Definition: vpIoTools.cpp:177
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:205
unsigned char R
Red component.
Definition: vpRGBa.h:153
unsigned int getHeight() const
Definition: vpImage.h:175
void performLut(const Type(&lut)[256], const unsigned int nbThreads=1)
Definition: vpImage.h:1846