Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testHistogram.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 https://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 histogram computation.
32 */
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <visp3/core/vpHistogram.h>
37#include <visp3/core/vpImage.h>
38#include <visp3/core/vpUniRand.h>
39#include <visp3/core/vpIoTools.h>
40#include <visp3/io/vpImageIo.h>
41#include <visp3/io/vpParseArgv.h>
42
49
50// List of allowed command line options
51#define GETOPTARGS "cdi:t:h"
52
53#ifdef ENABLE_VISP_NAMESPACE
54using namespace VISP_NAMESPACE_NAME;
55#endif
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 */
65void usage(const char *name, const char *badparam, std::string ipath)
66{
67 fprintf(stdout, "\n\
68Test histogram.\n\
69\n\
70SYNOPSIS\n\
71 %s [-i <input image path>] [-t <nb threads>]\n\
72 [-h]\n \
73",
74name);
75
76 fprintf(stdout, "\n\
77OPTIONS: 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
107bool 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 = static_cast<unsigned int>(atoi(optarg_));
119 break;
120 case 'h':
121 usage(argv[0], nullptr, ipath);
122 return false;
123
124 case 'c':
125 case 'd':
126 break;
127
128 default:
129 usage(argv[0], optarg_, ipath);
130 return false;
131 }
132 }
133
134 if ((c == 1) || (c == -1)) {
135 // standalone param or error
136 usage(argv[0], nullptr, 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
152unsigned int histogramSum(const vpImage<unsigned char> &I, unsigned int nbBins, 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
172bool compareHistogram(const vpImage<unsigned char> &I, 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
200int 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
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 return EXIT_FAILURE;
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 coming 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], nullptr, 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 return EXIT_FAILURE;
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_multi_thread = 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_multi_thread = vpTime::measureTimeMs();
274 for (int iteration = 0; iteration < nbIterations; iteration++) {
275 sum_multi_thread = histogramSum(I, nbBins, nbThreads);
276 }
277 t_multi_thread = vpTime::measureTimeMs() - t_multi_thread;
278
279 std::cout << "sum_single_thread=" << sum_single_thread << " ; t_single_thread=" << t_single_thread
280 << " ms ; mean=" << t_single_thread / static_cast<double>(nbIterations) << " ms" << std::endl;
281 std::cout << "sum_multi_thread (nbThreads=" << nbThreads << ")=" << sum_multi_thread << " ; t_multi_thread=" << t_multi_thread
282 << " ms ; mean=" << t_multi_thread / static_cast<double>(nbIterations) << " ms" << std::endl;
283 std::cout << "Speed-up=" << t_single_thread / static_cast<double>(t_multi_thread) << "X" << std::endl;
284
285 if (sum_single_thread != I.getSize() || sum_multi_thread != I.getSize()) {
286 std::cerr << "Problem with histogram!" << std::endl;
287 return EXIT_FAILURE;
288 }
289
290 nbBins = 101;
291 if (!compareHistogram(I, nbBins)) {
292 std::cerr << "Histogram are different!" << std::endl;
293 return EXIT_FAILURE;
294 }
295
296 // Test histogram computation on empty image
297 std::cout << "Test histogram computation on empty image" << std::endl << std::flush;
298 vpHistogram histogram;
299 vpImage<unsigned char> I_test(0, 0);
300 histogram.calculate(I_test, 256, 4);
301 if (histogram.getSize() == 256) {
302 for (unsigned int cpt = 0; cpt < 256; cpt++) {
303 if (histogram[cpt] != 0) {
304 std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
305 << " but should be zero!" << std::endl;
306 }
307 }
308 }
309 else {
310 std::cerr << "Bad histogram size!" << std::endl;
311 return EXIT_FAILURE;
312 }
313
314 // Test histogram computation on image size < nbThreads
315 std::cout << "Test histogram computation on image size < nbThreads" << std::endl << std::flush;
316 I_test.init(3, 1);
317 I_test = 100;
318 histogram.calculate(I_test, 256, 4);
319 if (histogram.getSize() == 256) {
320 for (unsigned int cpt = 0; cpt < 256; cpt++) {
321 if (cpt == 100) {
322 if (histogram[cpt] != I_test.getSize()) {
323 std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
324 << " but should be: " << I_test.getSize() << std::endl;
325 return EXIT_FAILURE;
326 }
327 }
328 else {
329 if (histogram[cpt] != 0) {
330 std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
331 << " but should be zero!" << std::endl;
332 }
333 }
334 }
335 }
336 else {
337 std::cerr << "Bad histogram size!" << std::endl;
338 return EXIT_FAILURE;
339 }
340
341 // Test histogram computation on small image size
342 std::cout << "Test histogram computation on small image size" << std::endl << std::flush;
343 I_test.init(7, 1);
344 I_test = 50;
345 histogram.calculate(I_test, 256, 4);
346 if (histogram.getSize() == 256) {
347 for (unsigned int cpt = 0; cpt < 256; cpt++) {
348 if (cpt == 50) {
349 if (histogram[cpt] != I_test.getSize()) {
350 std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
351 << " but should be: " << I_test.getSize() << std::endl;
352 return EXIT_FAILURE;
353 }
354 }
355 else {
356 if (histogram[cpt] != 0) {
357 std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
358 << " but should be zero!" << std::endl;
359 }
360 }
361 }
362 }
363 else {
364 std::cerr << "Bad histogram size!" << std::endl;
365 return EXIT_FAILURE;
366 }
367
368 // Test of smooth method
369 unsigned int nbRows = 4, nbCols = 15;
370 I.init(nbRows, nbCols);
371 for (unsigned int r = 0; r < nbRows; ++r) {
372 unsigned int c = 0;
373 for (unsigned int i = 1; i <= 5; ++i) {
374 for (unsigned int count = 0; count < i; ++count) {
375 I[r][c] = i + r;
376 ++c;
377 }
378 }
379 }
380
381 std::cout << "I:" << std::endl;
382 std::cout << I << std::endl;
383
384 nbBins = 256;
385 std::vector<unsigned int> expectedNumber(nbBins, 0);
386 expectedNumber[1] = 1;
387 expectedNumber[2] = 3;
388 expectedNumber[3] = 6;
389 expectedNumber[4] = 10;
390 expectedNumber[5] = 14;
391 expectedNumber[6] = 12;
392 expectedNumber[7] = 9;
393 expectedNumber[8] = 5;
394
395 vpHistogram histo;
396 histo.calculate(I, nbBins);
397 for (unsigned int bin = 0; bin < nbBins; ++bin) {
398 if (histo[bin] != expectedNumber[bin]) {
399 std::cerr << "Problem with histogram computation: histogram[" << bin << "]=" << histo[bin]
400 << " but should be: " << expectedNumber[bin] << std::endl;
401 return EXIT_FAILURE;
402 }
403 }
404
405 std::vector<unsigned int> expectedNumberSmooth(nbBins, 0);
406 expectedNumberSmooth[1] = 1;
407 expectedNumberSmooth[2] = 3;
408 expectedNumberSmooth[3] = 6;
409 expectedNumberSmooth[4] = 10;
410 expectedNumberSmooth[5] = 12;
411 expectedNumberSmooth[6] = 11;
412 expectedNumberSmooth[7] = 8;
413 expectedNumberSmooth[8] = 4;
414 expectedNumberSmooth[9] = 1;
415
416 histo.smooth();
417
418 for (unsigned int bin = 0; bin < nbBins; ++bin) {
419 if (histo[bin] != expectedNumberSmooth[bin]) {
420 std::cerr << "Problem with smooth computation: histogram[" << bin << "]=" << histo[bin]
421 << " but should be: " << expectedNumberSmooth[bin] << std::endl;
422 return EXIT_FAILURE;
423 }
424 }
425
426 // Test of mask
427 vpImage<bool> mask(nbRows, nbCols, false);
428 for (unsigned int r = 0; r < nbRows; ++r) {
429 for (unsigned int c = 0; c < nbCols; ++c) {
430 if ((r == 0) || (r == (nbRows - 1))) {
431 mask[r][c] = true;
432 }
433 if ((c == 0) || (c == (nbCols - 1))) {
434 mask[r][c] = true;
435 }
436 }
437 }
438
439 std::cout << "I to which is applied the mask:" << std::endl;
440 for (unsigned int r = 0; r < nbRows; ++r) {
441 for (unsigned int c = 0; c < nbCols; ++c) {
442 if (mask[r][c]) {
443 std::cout << static_cast<int>(I[r][c]);
444 }
445 else {
446 std::cout << "X";
447 }
448 std::cout << " ";
449 }
450 std::cout << std::endl;
451 }
452
453 vpHistogram histoWithMaskMono; // Histogram using mask monothreaded
454 histoWithMaskMono.setMask(&mask);
455 histoWithMaskMono.calculate(I, nbBins, 1);
456
457 vpHistogram histoWithMaskMulti; // Histogram using mask multithreaded
458 histoWithMaskMulti.setMask(&mask);
459 histoWithMaskMulti.calculate(I, nbBins, 2);
460
461 std::vector<unsigned int> expectedNumberMask(nbBins, 0);
462 expectedNumberMask[1] = 1;
463 expectedNumberMask[2] = 3;
464 expectedNumberMask[3] = 4;
465 expectedNumberMask[4] = 5;
466 expectedNumberMask[5] = 7;
467 expectedNumberMask[6] = 4;
468 expectedNumberMask[7] = 5;
469 expectedNumberMask[8] = 5;
470
471 for (unsigned int bin = 0; bin < nbBins; ++bin) {
472 if (histoWithMaskMono[bin] != expectedNumberMask[bin]) {
473 std::cerr << "Problem when using mask: histogram[" << bin << "]=" << histoWithMaskMono[bin]
474 << " but should be: " << expectedNumberMask[bin] << std::endl;
475 return EXIT_FAILURE;
476 }
477
478 if (histoWithMaskMulti[bin] != expectedNumberMask[bin]) {
479 std::cerr << "Problem when using mask: histogram[" << bin << "]=" << histoWithMaskMulti[bin]
480 << " but should be: " << expectedNumberMask[bin] << std::endl;
481 return EXIT_FAILURE;
482 }
483 }
484
485#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
486// Test of histogram on vpImage<double>
487 vpImage<double> Id(nbRows, nbCols);
488 nbBins = 8;
489 double min = 0., max = 1.;
490 double desiredStep = (max - min) / static_cast<double>(nbBins);
491 vpUniRand uniRand(4221);
492
493 for (unsigned int r = 0; r < nbRows; ++r) {
494 unsigned int c = 0;
495 for (unsigned int i = 1; i <= 5; ++i) {
496 for (unsigned int count = 0; count < i; ++count) {
497 double value = uniRand.uniform(0., desiredStep) + desiredStep * static_cast<double>(i + r - 1);
498 Id[r][c] = value;
499 ++c;
500 }
501 }
502 }
503
504 std::cout << "Id = " << std::endl;
505 for (unsigned int r = 0; r < nbRows; ++r) {
506 for (unsigned int c = 0; c < nbCols; ++c) {
507 std::cout << std::setprecision(3) << Id[r][c];
508 std::cout << " ";
509 }
510 std::cout << std::endl;
511 }
512
513 // test monothread
514 std::vector<unsigned char> expectedNumberDouble(nbBins, 0), expectedNumberDoubleMask(nbBins, 0);
515 for (unsigned int i = 0; i < nbBins; ++i) {
516 expectedNumberDouble[i] = expectedNumber[i + 1];
517 expectedNumberDoubleMask[i] = expectedNumberMask[i + 1];
518 }
519 double step = 0.;
520 vpHistogram histoDoubleMono;
521 histoDoubleMono.calculate(Id, min, max, step, nbBins, 1);
522
523 if (!vpMath::equal(step, desiredStep)) {
524 std::cerr << "Problem with histogram computation of floating point images" << std::endl;
525 std::cout << "Computed step: " << step << " but should be: " << desiredStep << std::endl;
526 return EXIT_FAILURE;
527 }
528 for (unsigned int bin = 0; bin < nbBins; ++bin) {
529 if (histoDoubleMono[bin] != expectedNumberDouble[bin]) {
530 std::cerr << "Problem with monothread histogram computation of floating point images: histogram[" << bin << "]=" << histoDoubleMono[bin]
531 << " but should be: " << expectedNumberDouble[bin] << std::endl;
532 return EXIT_FAILURE;
533 }
534 }
535
536 // test multithread
537 vpHistogram histoDoubleMulti;
538 histoDoubleMulti.calculate(Id, min, max, step, nbBins, 2);
539
540 if (!vpMath::equal(step, desiredStep)) {
541 std::cerr << "Problem with histogram computation of floating point images" << std::endl;
542 std::cout << "Computed step: " << step << " but should be: " << desiredStep << std::endl;
543 return EXIT_FAILURE;
544 }
545 for (unsigned int bin = 0; bin < nbBins; ++bin) {
546 if (histoDoubleMulti[bin] != expectedNumberDouble[bin]) {
547 std::cerr << "Problem with multithread histogram computation of floating point images: histogram[" << bin << "]=" << histoDoubleMulti[bin]
548 << " but should be: " << expectedNumberDouble[bin] << std::endl;
549 return EXIT_FAILURE;
550 }
551 }
552
553 std::cout << "Id with mask= " << std::endl;
554 for (unsigned int r = 0; r < nbRows; ++r) {
555 for (unsigned int c = 0; c < nbCols; ++c) {
556 if (mask[r][c]) {
557 std::cout << std::setprecision(3) << Id[r][c];
558 }
559 else {
560 std::cout << "XXXXX";
561 }
562 std::cout << " ";
563 }
564 std::cout << std::endl;
565 }
566
567 // Test mask
568 vpHistogram histoDoubleMonoMask;
569 histoDoubleMonoMask.setMask(&mask);
570 histoDoubleMonoMask.calculate(Id, min, max, step, nbBins, 2);
571
572 vpHistogram histoDoubleMultiMask;
573 histoDoubleMultiMask.setMask(&mask);
574 histoDoubleMultiMask.calculate(Id, min, max, step, nbBins, 2);
575
576 for (unsigned int bin = 0; bin < nbBins; ++bin) {
577 if (histoDoubleMonoMask[bin] != expectedNumberDoubleMask[bin]) {
578 std::cerr << "Problem with monothread histogram computation of floating point images using mask: histogram[" << bin << "]=" << histoDoubleMonoMask[bin]
579 << " but should be: " << expectedNumberDoubleMask[bin] << std::endl;
580 return EXIT_FAILURE;
581 }
582
583 if (histoDoubleMultiMask[bin] != expectedNumberDoubleMask[bin]) {
584 std::cerr << "Problem with multithread histogram computation of floating point images using mask: histogram[" << bin << "]=" << histoDoubleMultiMask[bin]
585 << " but should be: " << expectedNumberDoubleMask[bin] << std::endl;
586 return EXIT_FAILURE;
587 }
588 }
589#endif
590
591 std::cout << "testHistogram is OK!" << std::endl;
592 return EXIT_SUCCESS;
593 }
594 catch (const vpException &e) {
595 std::cerr << "Catch an exception: " << e.what() << std::endl;
596 return EXIT_FAILURE;
597 }
598}
error that can be emitted by ViSP classes.
Definition vpException.h:60
Class to compute a gray level image histogram.
void smooth(unsigned int fsize=3)
void calculate(const vpImage< unsigned char > &I, unsigned int nbins=256, unsigned int nbThreads=1)
unsigned getSize() const
void setMask(const vpImage< bool > *p_mask)
Set a mask to ignore pixels for which the mask is false.
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static std::string getViSPImagesDataPath()
static std::string createFilePath(const std::string &parent, const std::string &child)
static bool equal(double x, double y, double threshold=0.001)
Definition vpMath.h:470
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Class for generating random numbers with uniform probability density.
Definition vpUniRand.h:127
VISP_EXPORT double measureTimeMs()