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>
55#define GETOPTARGS "cdi:o:t:h"
67void usage(
const char *name,
const char *badparam,
const std::string &ipath,
const std::string &opath,
68 const std::string &user)
71Test performance between methods to iterate over pixel image.\n\
74 %s [-i <input image path>] [-o <output image path>] [-t <nb threads>]\n\
81 -i <input image path> %s\n\
82 Set image input path.\n\
83 From this path read \"Klimt/Klimt.pgm\"\n\
85 Setting the VISP_INPUT_IMAGE_PATH environment\n\
86 variable produces the same behaviour than using\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\
96 Set the number of threads to use for the computation.\n\
100 ipath.c_str(), opath.c_str(), user.c_str());
103 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
119bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath,
const std::string &user,
120 unsigned int &nbThreads)
134 nbThreads = (
unsigned int)atoi(optarg_);
137 usage(argv[0], NULL, ipath, opath, user);
146 usage(argv[0], optarg_, ipath, opath, user);
152 if ((c == 1) || (c == -1)) {
154 usage(argv[0], NULL, ipath, opath, user);
155 std::cerr <<
"ERROR: " << std::endl;
156 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
174 unsigned char *ptrStart = (
unsigned char *)I.
bitmap;
175 unsigned char *ptrEnd = ptrStart + size * 4;
176 unsigned char *ptrCurrent = ptrStart;
178 while (ptrCurrent != ptrEnd) {
195 unsigned char *ptrStart = (
unsigned char *)I.
bitmap;
196 unsigned char *ptrEnd = ptrStart + size;
197 unsigned char *ptrCurrent = ptrStart;
199 while (ptrCurrent != ptrEnd) {
215 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
216 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
225int main(
int argc,
const char **argv)
228 std::string env_ipath;
229 std::string opt_ipath;
230 std::string opt_opath;
233 std::string filename;
234 std::string username;
235 unsigned int nbThreads = 4;
242 if (!env_ipath.empty())
247 opt_opath =
"C:/temp";
256 if (getOptions(argc, argv, opt_ipath, opt_opath, username, nbThreads) ==
false) {
261 if (!opt_ipath.empty())
263 if (!opt_opath.empty())
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;
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;
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
314 std::cout <<
"\nRead image: " << filename << std::endl;
319 std::cout <<
"I=" << I_iterate1.
getWidth() <<
"x" << I_iterate1.
getHeight() << std::endl;
321 double alpha = 1.5, beta = -30.0;
322 unsigned int nbIterations = 10;
326 for (
unsigned int cpt = 0; cpt < nbIterations; cpt++) {
327 iterate_method1(I_iterate1, alpha, beta);
330 std::cout <<
"t_iterate1=" << t_iterate1 <<
" ms ; t_iterate1/" << nbIterations <<
"="
331 << (t_iterate1 / nbIterations) <<
" ms" << std::endl;
338 for (
unsigned int cpt = 0; cpt < nbIterations; cpt++) {
339 iterate_method2(I_iterate2, alpha, beta);
342 std::cout <<
"t_iterate2=" << t_iterate2 <<
" ms ; t_iterate2/" << nbIterations <<
"="
343 << (t_iterate2 / nbIterations) <<
" ms" << std::endl;
350 for (
unsigned int cpt = 0; cpt < nbIterations; cpt++) {
353 for (
unsigned int i = 0; i < 256; i++) {
363 std::cout <<
"t_lut=" << t_lut <<
" ms ; t_lut/" << nbIterations <<
"=" << (t_lut / nbIterations) <<
" ms"
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]) {
380 std::cerr <<
"Color images are different!" << std::endl;
389 std::cout <<
"\nRead image: " << filename << std::endl;
393 std::cout <<
"I_grayscale=" << I_lut_grayscale.
getWidth() <<
"x" << I_lut_grayscale.
getHeight() << std::endl;
397 for (
unsigned int cpt = 0; cpt < nbIterations; cpt++) {
398 iterate_method1(I_iterate_grayscale1, alpha, beta);
401 std::cout <<
"t_iterate_grayscale1=" << t_iterate_grayscale1 <<
" ms ; t_iterate1/" << nbIterations <<
"="
402 << (t_iterate_grayscale1 / nbIterations) <<
" ms" << std::endl;
409 for (
unsigned int cpt = 0; cpt < nbIterations; cpt++) {
411 unsigned char lut[256];
412 for (
unsigned int i = 0; i < 256; i++) {
419 std::cout <<
"t_lut_grayscale=" << t_lut_grayscale <<
" ms ; t_lut_grayscale/" << nbIterations <<
"="
420 << (t_lut_grayscale / nbIterations) <<
" ms" << std::endl;
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]) {
436 std::cerr <<
"Grayscale images are different!" << std::endl;
444 for (
unsigned int cpt = 0; cpt < nbIterations * 10; cpt++) {
447 for (
unsigned int i = 0; i < 256; i++) {
461 for (
unsigned int cpt = 0; cpt < nbIterations * 10; cpt++) {
464 for (
unsigned int i = 0; i < 256; i++) {
475 std::cout <<
"\nt_lut_singlethread/t_lut_multithread (color)=" << t_lut_singlethread / t_lut_multithread <<
"X"
482 for (
unsigned int cpt = 0; cpt < nbIterations * 10; cpt++) {
484 unsigned char lut[256];
485 for (
unsigned int i = 0; i < 256; i++) {
496 for (
unsigned int cpt = 0; cpt < nbIterations * 10; cpt++) {
498 unsigned char lut[256];
499 for (
unsigned int i = 0; i < 256; i++) {
507 std::cout <<
"\nt_lut_singlethread/t_lut_multithread (grayscale)=" << t_lut_singlethread / t_lut_multithread <<
"X"
513 unsigned char lut_grayscale[256];
514 for (
unsigned int i = 0; i < 256; i++) {
517 I_test_grayscale.performLut(lut_grayscale, nbThreads);
522 for (
unsigned int i = 0; i < 256; i++) {
528 I_test_color.performLut(lut_color, nbThreads);
532 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
error that can be emitted by ViSP classes.
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.
unsigned int getWidth() const
void performLut(const Type(&lut)[256], unsigned int nbThreads=1)
Type * bitmap
points toward the bitmap
unsigned int getHeight() const
static _Tp saturate(unsigned char v)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
unsigned char B
Blue component.
unsigned char R
Red component.
unsigned char G
Green component.
unsigned char A
Additionnal component.
VISP_EXPORT double measureTimeMs()