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