Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testImageMeanAndStdev.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 for vpImagePoint::getValue().
32  */
39 #include <iostream>
40 #include <visp3/core/vpImage.h>
41 
42 void printHelp(const std::string &progName)
43 {
44  std::cout << "SYNOPSIS: " << std::endl;
45  std::cout << " " << progName << " [-v, --verbose] [-h, --help]" << std::endl;
46  std::cout << "DETAILS:" << std::endl;
47  std::cout << " -v, --verbose" << std::endl;
48  std::cout << " Activate verbose mode to have some logs in the console." << std::endl;
49  std::cout << std::endl;
50  std::cout << " -h, --help" << std::endl;
51  std::cout << " Display the help about the program." << std::endl;
52  std::cout << std::endl;
53 }
54 
55 int main(const int argc, const char *argv[])
56 {
57 #ifdef ENABLE_VISP_NAMESPACE
58  using namespace VISP_NAMESPACE_NAME;
59 #endif
60  bool opt_verbose = false;
61  for (int i = 1; i < argc; ++i) {
62  std::string argName(argv[i]);
63  if ((argName == "-v") || (argName == "--verbose")) {
64  opt_verbose = true;
65  }
66  else if ((argName == "-h") || (argName == "--help")) {
67  printHelp(std::string(argv[0]));
68  return EXIT_FAILURE;
69  }
70  }
71 
72  const unsigned int nbRows = 4, nbCols = 4;
73  vpImage<unsigned char> I_uchar_ref(nbRows, nbCols);
74  vpImage<vpRGBa> I_rgba_ref(nbRows, nbCols);
75  vpImage<vpRGBf> I_rgbf_ref(nbRows, nbCols);
76  double sum_uchar_ref = 0.;
77  double sum_rgba_ref = 0.;
78  double sum_rgbf_ref = 0.;
79  unsigned int count_ref = 0;
80  // Initialization of the images
81  for (unsigned int r = 0; r < nbRows; ++r) {
82  for (unsigned int c = 0; c < nbCols; ++c) {
83  unsigned int val = r * nbCols + c;
84  I_uchar_ref[r][c] = val;
85  I_rgba_ref[r][c].R = val;
86  I_rgba_ref[r][c].G = 2*val;
87  I_rgba_ref[r][c].B = 3*val;
88  I_rgbf_ref[r][c].R = I_rgba_ref[r][c].R;
89  I_rgbf_ref[r][c].G = I_rgba_ref[r][c].G;
90  I_rgbf_ref[r][c].B = I_rgba_ref[r][c].B;
91  sum_uchar_ref += static_cast<double>(val);
92  double val_rgb = static_cast<double>(I_rgba_ref[r][c].R) + static_cast<double>(I_rgba_ref[r][c].G) + static_cast<double>(I_rgba_ref[r][c].B);
93  sum_rgba_ref += val_rgb;
94  sum_rgbf_ref += val_rgb;
95  ++count_ref;
96  }
97  }
98 
99  // Computation of the means
100  double mean_uchar_ref = sum_uchar_ref / static_cast<double>(count_ref);
101  double mean_rgba_ref = sum_rgba_ref / static_cast<double>(count_ref);
102  double mean_rgbf_ref = sum_rgbf_ref / static_cast<double>(count_ref);
103 
104  // Computation of the standard deviations
105  double stdev_uchar_ref = 0.;
106  double stdev_rgba_ref = 0.;
107  double stdev_rgbf_ref = 0.;
108  for (unsigned int r = 0; r < nbRows; ++r) {
109  for (unsigned int c = 0; c < nbCols; ++c) {
110  stdev_uchar_ref += std::pow(static_cast<double>(I_uchar_ref[r][c]) - mean_uchar_ref, 2);
111  stdev_rgba_ref += std::pow(static_cast<double>(I_rgba_ref[r][c].R) + static_cast<double>(I_rgba_ref[r][c].G) + static_cast<double>(I_rgba_ref[r][c].B) - mean_rgba_ref, 2);
112  stdev_rgbf_ref += std::pow(static_cast<double>(I_rgbf_ref[r][c].R) + static_cast<double>(I_rgbf_ref[r][c].G) + static_cast<double>(I_rgbf_ref[r][c].B) - mean_rgbf_ref, 2);
113  }
114  }
115  stdev_uchar_ref = std::sqrt((1./static_cast<double>(nbRows * nbCols))* stdev_uchar_ref);
116  stdev_rgba_ref = std::sqrt((1./static_cast<double>(nbRows * nbCols))* stdev_rgba_ref);
117  stdev_rgbf_ref = std::sqrt((1./static_cast<double>(nbRows * nbCols))* stdev_rgbf_ref);
118  if (opt_verbose) {
119  std::cout << "----- Input data-----" << std::endl;
120  std::cout << "I_uchar_ref = \n" << I_uchar_ref << std::endl;
121  std::cout << "sum_uchar_ref(I_uchar_ref) = " << sum_uchar_ref << std::endl;
122  std::cout << "mean_uchar_ref(I_uchar_ref) = " << mean_uchar_ref << std::endl;
123  std::cout << "stdev_uchar_ref(I_uchar_ref) = " << stdev_uchar_ref << std::endl;
124  std::cout << std::endl;
125  std::cout << "I_rgba_ref = \n" << I_rgba_ref << std::endl;
126  std::cout << "sum_rgba_ref(I_uchar_ref) = " << sum_rgba_ref << std::endl;
127  std::cout << "mean_rgba_ref(I_rgba_ref) = " << mean_rgba_ref << std::endl;
128  std::cout << "stdev_rgba_ref(I_rgba_ref) = " << stdev_rgba_ref << std::endl;
129  std::cout << std::endl;
130  std::cout << "I_rgbf_ref = \n" << I_rgbf_ref << std::endl;
131  std::cout << "sum_rgbf_ref(I_rgbf_ref) = " << sum_rgbf_ref << std::endl;
132  std::cout << "mean_rgbf_ref(I_rgbf_ref) = " << mean_rgbf_ref << std::endl;
133  std::cout << "stdev_rgbf_ref(I_rgbf_ref) = " << stdev_rgbf_ref << std::endl;
134  std::cout << std::endl;
135  }
136 
137  vpImage<bool> I_mask(nbRows, nbCols);
138  unsigned int count_true = 0;
139  double sum_uchar_true = 0.;
140  double sum_rgba_true = 0.;
141  double sum_rgbf_true = 0.;
142  for (unsigned int r = 0; r < nbRows; ++r) {
143  for (unsigned int c = 0; c < nbCols; ++c) {
144  bool isTrue = ((r + c) % 2) == 0;
145  I_mask[r][c] = isTrue;
146  if (isTrue) {
147  ++count_true;
148  sum_uchar_true += static_cast<double>(I_uchar_ref[r][c]);
149  double val_rgba = static_cast<double>(I_rgba_ref[r][c].R) + static_cast<double>(I_rgba_ref[r][c].G) + static_cast<double>(I_rgba_ref[r][c].B);
150  sum_rgba_true += val_rgba;
151  double val_rgbf = static_cast<double>(I_rgbf_ref[r][c].R) + static_cast<double>(I_rgbf_ref[r][c].G) + static_cast<double>(I_rgbf_ref[r][c].B);
152  sum_rgbf_true += val_rgbf;
153  }
154  }
155  }
156  // Computation of the means when a boolean mask is used
157  double mean_uchar_true = sum_uchar_true / static_cast<double>(count_true);
158  double mean_rgba_true = sum_rgba_true / static_cast<double>(count_true);
159  double mean_rgbf_true = sum_rgbf_true / static_cast<double>(count_true);
160  // Computation of the standard deviations when a boolean mask is used
161  double stdev_uchar_true = 0.;
162  double stdev_rgba_true = 0.;
163  double stdev_rgbf_true = 0.;
164  for (unsigned int r = 0; r < nbRows; ++r) {
165  for (unsigned int c = 0; c < nbCols; ++c) {
166  if (I_mask[r][c]) {
167  stdev_uchar_true += (static_cast<double>(I_uchar_ref[r][c]) - mean_uchar_true) * (static_cast<double>(I_uchar_ref[r][c]) - mean_uchar_true);
168  double val_rgba = static_cast<double>(I_rgba_ref[r][c].R) + static_cast<double>(I_rgba_ref[r][c].G) + static_cast<double>(I_rgba_ref[r][c].B);
169  stdev_rgba_true += (val_rgba - mean_rgba_true) * (val_rgba - mean_rgba_true);
170  double val_rgbf = static_cast<double>(I_rgbf_ref[r][c].R) + static_cast<double>(I_rgbf_ref[r][c].G) + static_cast<double>(I_rgbf_ref[r][c].B);
171  stdev_rgbf_true += (val_rgbf - mean_rgbf_true) * (val_rgbf - mean_rgbf_true);
172  }
173  }
174  }
175  stdev_uchar_true = std::sqrt((1./static_cast<double>(count_true)) * stdev_uchar_true);
176  stdev_rgba_true = std::sqrt((1./static_cast<double>(count_true)) * stdev_rgba_true);
177  stdev_rgbf_true = std::sqrt((1./static_cast<double>(count_true)) * stdev_rgbf_true);
178  if (opt_verbose) {
179  std::cout << "I_mask = \n";
180  for (unsigned int r = 0; r < nbRows; ++r) {
181  for (unsigned int c = 0; c < nbCols; ++c) {
182  std::cout << (I_mask[r][c] ? "true" : "false") << " ";
183  }
184  std::cout << "\n";
185  }
186  std::cout << std::endl;
187  std::cout << "nb_true(I_uchar_ref, I_mask) = " << count_true << std::endl;
188  std::cout << "sum_uchar_true(I_uchar_ref, I_mask) = " << sum_uchar_true << std::endl;
189  std::cout << "mean_uchar_true(I_uchar_ref, I_mask) = " << mean_uchar_true << std::endl;
190  std::cout << "stdev_uchar_true(I_uchar_ref, I_mask) = " << stdev_uchar_true << std::endl;
191  std::cout << "sum_rgba_true(I_rgba_ref, I_mask) = " << sum_rgba_true << std::endl;
192  std::cout << "mean_rgba_true(I_rgba_ref, I_mask) = " << mean_rgba_true << std::endl;
193  std::cout << "stdev_rgba_true(I_rgba_ref, I_mask) = " << stdev_rgba_true << std::endl;
194  std::cout << "sum_rgbf_true(I_rgbf_ref, I_mask) = " << sum_rgbf_true << std::endl;
195  std::cout << "mean_rgbf_true(I_rgbf_ref, I_mask) = " << mean_rgbf_true << std::endl;
196  std::cout << "stdev_rgbf_true(I_rgbf_ref, I_mask) = " << stdev_rgbf_true << std::endl;
197  std::cout << std::endl;
198  }
199 
200  bool areTestOK = true;
201  unsigned int nbFailedTests = 0;
202  std::vector<std::string> failedTestsNames;
203  if (opt_verbose) {
204  std::cout << "----- BEGIN tests-----" << std::endl;
205  }
206 
207  // Tests on the sum
208  {
209  if (opt_verbose) {
210  std::cout << "Tests on the sum" << std::endl;
211  }
212  std::string nameTest("vpImage<uchar>::getSum()");
213  double sum = I_uchar_ref.getSum();
214  bool success = vpMath::equal(sum, sum_uchar_ref);
215  if (!success) {
216  ++nbFailedTests;
217  failedTestsNames.push_back(nameTest);
218  areTestOK = false;
219  }
220  if (opt_verbose) {
221  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
222  if (!success) {
223  std::cout << "Theoretical sum = " << sum_uchar_ref << " | returned value = " << sum << std::endl;
224  }
225  }
226 
227  unsigned int nbValidPoints = 0;
228  nameTest = ("vpImage<uchar>::getSum( const vpImage<bool> *, unsigned int * )");
229  sum = I_uchar_ref.getSum(&I_mask, &nbValidPoints);
230  success = vpMath::equal(sum, sum_uchar_true) && (nbValidPoints == count_true);
231  if (!success) {
232  ++nbFailedTests;
233  failedTestsNames.push_back(nameTest);
234  areTestOK = false;
235  }
236  if (opt_verbose) {
237  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
238  if (!success) {
239  std::cout << "Theoretical count = " << count_true << " | returned value = " << nbValidPoints << std::endl;
240  std::cout << "Theoretical sum = " << sum_uchar_true << " | returned value = " << sum << std::endl;
241  }
242  }
243 
244  nameTest = ("vpImage<uchar>::getSum( vpImage<bool> * = nullptr, unsigned int * )");
245  sum = I_uchar_ref.getSum(nullptr, &nbValidPoints);
246  success = vpMath::equal(sum, sum_uchar_ref) && (nbValidPoints == (nbCols * nbRows));
247  if (!success) {
248  ++nbFailedTests;
249  failedTestsNames.push_back(nameTest);
250  areTestOK = false;
251  }
252  if (opt_verbose) {
253  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
254  if (!success) {
255  std::cout << "Theoretical count = " << nbCols * nbRows << " | returned value = " << nbValidPoints << std::endl;
256  std::cout << "Theoretical sum = " << sum_uchar_ref << " | returned value = " << sum << std::endl;
257  }
258  }
259 
260  nameTest = ("vpImage<vpRGBa>::getSum()");
261  sum = I_rgba_ref.getSum();
262  success = vpMath::equal(sum, sum_rgba_ref);
263  if (!success) {
264  ++nbFailedTests;
265  failedTestsNames.push_back(nameTest);
266  areTestOK = false;
267  }
268  if (opt_verbose) {
269  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
270  if (!success) {
271  std::cout << "Theoretical sum = " << sum_rgba_ref << " | returned value = " << sum << std::endl;
272  }
273  }
274 
275  nameTest = ("vpImage<vpRGBa>::getSum( vpImage<bool> *, unsigned int * )");
276  sum = I_rgba_ref.getSum(&I_mask, &nbValidPoints);
277  success = vpMath::equal(sum, sum_rgba_true) && (nbValidPoints == count_true);
278  if (!success) {
279  ++nbFailedTests;
280  failedTestsNames.push_back(nameTest);
281  areTestOK = false;
282  }
283  if (opt_verbose) {
284  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
285  if (!success) {
286  std::cout << "Theoretical count = " << count_true << " | returned value = " << nbValidPoints << std::endl;
287  std::cout << "Theoretical sum = " << sum_rgba_true << " | returned value = " << sum << std::endl;
288  }
289  }
290 
291  nameTest = ("vpImage<vpRGBa>::getSum( vpImage<bool> * = nullptr, unsigned int * )");
292  sum = I_rgba_ref.getSum(nullptr, &nbValidPoints);
293  success = vpMath::equal(sum, sum_rgba_ref) && (nbValidPoints == (nbCols * nbRows));
294  if (!success) {
295  ++nbFailedTests;
296  failedTestsNames.push_back(nameTest);
297  areTestOK = false;
298  }
299  if (opt_verbose) {
300  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
301  if (!success) {
302  std::cout << "Theoretical count = " << nbCols * nbRows << " | returned value = " << nbValidPoints << std::endl;
303  std::cout << "Theoretical sum = " << sum_rgba_ref << " | returned value = " << sum << std::endl;
304  }
305  }
306 
307  nameTest = ("vpImage<vpRGBf>::getSum()");
308  sum = I_rgbf_ref.getSum();
309  success = vpMath::equal(sum, sum_rgbf_ref);
310  if (!success) {
311  ++nbFailedTests;
312  failedTestsNames.push_back(nameTest);
313  areTestOK = false;
314  }
315  if (opt_verbose) {
316  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
317  if (!success) {
318  std::cout << "Theoretical sum = " << sum_rgbf_ref << " | returned value = " << sum << std::endl;
319  }
320  }
321 
322  nameTest = ("vpImage<vpRGBf>::getSum( vpImage<bool> *, unsigned int * )");
323  sum = I_rgbf_ref.getSum(&I_mask, &nbValidPoints);
324  success = vpMath::equal(sum, sum_rgbf_true) && (nbValidPoints == count_true);
325  if (!success) {
326  ++nbFailedTests;
327  failedTestsNames.push_back(nameTest);
328  areTestOK = false;
329  }
330  if (opt_verbose) {
331  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
332  if (!success) {
333  std::cout << "Theoretical count = " << count_true << " | returned value = " << nbValidPoints << std::endl;
334  std::cout << "Theoretical sum = " << sum_rgbf_true << " | returned value = " << sum << std::endl;
335  }
336  }
337 
338  nameTest = ("vpImage<vpRGBf>::getSum( vpImage<bool> * = nullptr, unsigned int * )");
339  sum = I_rgbf_ref.getSum(nullptr, &nbValidPoints);
340  success = vpMath::equal(sum, sum_rgbf_ref) && (nbValidPoints == (nbCols * nbRows));
341  if (!success) {
342  ++nbFailedTests;
343  failedTestsNames.push_back(nameTest);
344  areTestOK = false;
345  }
346  if (opt_verbose) {
347  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
348  if (!success) {
349  std::cout << "Theoretical count = " << nbCols * nbRows << " | returned value = " << nbValidPoints << std::endl;
350  std::cout << "Theoretical sum = " << sum_rgbf_ref << " | returned value = " << sum << std::endl;
351  }
352  }
353  }
354 
355  // Tests on the mean
356  {
357  if (opt_verbose) {
358  std::cout << "Tests on the mean" << std::endl;
359  }
360  std::string nameTest("vpImage<uchar>::getMeanValue()");
361  double mean = I_uchar_ref.getMeanValue();
362  bool success = vpMath::equal(mean, mean_uchar_ref);
363  if (!success) {
364  ++nbFailedTests;
365  failedTestsNames.push_back(nameTest);
366  areTestOK = false;
367  }
368  if (opt_verbose) {
369  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
370  if (!success) {
371  std::cout << "Theoretical mean = " << mean_uchar_ref << " | returned value = " << mean << std::endl;
372  }
373  }
374 
375  nameTest = "vpImage<uchar>::getMeanValue(vpImage<bool> *)";
376  mean = I_uchar_ref.getMeanValue(&I_mask);
377  success = vpMath::equal(mean, mean_uchar_true);
378  if (!success) {
379  ++nbFailedTests;
380  failedTestsNames.push_back(nameTest);
381  areTestOK = false;
382  }
383  if (opt_verbose) {
384  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
385  if (!success) {
386  std::cout << "Theoretical mean = " << mean_uchar_true << " | returned value = " << mean << std::endl;
387  }
388  }
389 
390  nameTest = "vpImage<uchar>::getMeanValue(vpImage<bool> * = nullptr)";
391  mean = I_uchar_ref.getMeanValue(nullptr);
392  success = vpMath::equal(mean, mean_uchar_ref);
393  if (!success) {
394  ++nbFailedTests;
395  failedTestsNames.push_back(nameTest);
396  areTestOK = false;
397  }
398  if (opt_verbose) {
399  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
400  if (!success) {
401  std::cout << "Theoretical mean = " << mean_uchar_ref << " | returned value = " << mean << std::endl;
402  }
403  }
404 
405  unsigned int nbValidPoints = 0;
406  nameTest = "vpImage<uchar>::getMeanValue(vpImage<bool> *, unsigned int &)";
407  mean = I_uchar_ref.getMeanValue(&I_mask, &nbValidPoints);
408  success = vpMath::equal(mean, mean_uchar_true) && (nbValidPoints == count_true);
409  if (!success) {
410  ++nbFailedTests;
411  failedTestsNames.push_back(nameTest);
412  areTestOK = false;
413  }
414  if (opt_verbose) {
415  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
416  if (!success) {
417  std::cout << "Theoretical count = " << count_true << " | returned value = " << nbValidPoints << std::endl;
418  std::cout << "Theoretical mean = " << mean_uchar_true << " | returned value = " << mean << std::endl;
419  }
420  }
421 
422  nbValidPoints = 0;
423  nameTest = "vpImage<uchar>::getMeanValue(vpImage<bool> * = nullptr, unsigned int &)";
424  mean = I_uchar_ref.getMeanValue(nullptr, &nbValidPoints);
425  success = vpMath::equal(mean, mean_uchar_ref) && (nbValidPoints == (nbCols * nbRows));
426  if (!success) {
427  ++nbFailedTests;
428  failedTestsNames.push_back(nameTest);
429  areTestOK = false;
430  }
431  if (opt_verbose) {
432  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
433  if (!success) {
434  std::cout << "Theoretical count = " << nbCols * nbRows << " | returned value = " << nbValidPoints << std::endl;
435  std::cout << "Theoretical mean = " << mean_uchar_ref << " | returned value = " << mean << std::endl;
436  }
437  }
438 
439  nameTest = "vpImage<vpRGBa>::getMeanValue()";
440  mean = I_rgba_ref.getMeanValue();
441  success = vpMath::equal(mean, mean_rgba_ref);
442  if (!success) {
443  ++nbFailedTests;
444  failedTestsNames.push_back(nameTest);
445  areTestOK = false;
446  }
447  if (opt_verbose) {
448  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
449  if (!success) {
450  std::cout << "Theoretical mean = " << mean_rgba_ref << " | returned value = " << mean << std::endl;
451  }
452  }
453 
454  nameTest = "vpImage<vpRGBa>::getMeanValue(vpImage<bool> *)";
455  mean = I_rgba_ref.getMeanValue(&I_mask);
456  success = vpMath::equal(mean, mean_rgba_true);
457  if (!success) {
458  ++nbFailedTests;
459  failedTestsNames.push_back(nameTest);
460  areTestOK = false;
461  }
462  if (opt_verbose) {
463  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
464  if (!success) {
465  std::cout << "Theoretical mean = " << mean_rgba_true << " | returned value = " << mean << std::endl;
466  }
467  }
468 
469  nameTest = "vpImage<vpRGBa>::getMeanValue(vpImage<bool> * = nullptr)";
470  mean = I_rgba_ref.getMeanValue(nullptr);
471  success = vpMath::equal(mean, mean_rgba_ref);
472  if (!success) {
473  ++nbFailedTests;
474  failedTestsNames.push_back(nameTest);
475  areTestOK = false;
476  }
477  if (opt_verbose) {
478  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
479  if (!success) {
480  std::cout << "Theoretical mean = " << mean_rgba_ref << " | returned value = " << mean << std::endl;
481  }
482  }
483 
484  nbValidPoints = 0;
485  nameTest = "vpImage<vpRGBa>::getMeanValue(vpImage<bool> *, unsigned int &)";
486  mean = I_rgba_ref.getMeanValue(&I_mask, &nbValidPoints);
487  success = vpMath::equal(mean, mean_rgba_true) && (nbValidPoints == count_true);
488  if (!success) {
489  ++nbFailedTests;
490  failedTestsNames.push_back(nameTest);
491  areTestOK = false;
492  }
493  if (opt_verbose) {
494  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
495  if (!success) {
496  std::cout << "Theoretical count = " << count_true << " | returned value = " << nbValidPoints << std::endl;
497  std::cout << "Theoretical mean = " << mean_rgba_true << " | returned value = " << mean << std::endl;
498  }
499  }
500 
501  nbValidPoints = 0;
502  nameTest = "vpImage<vpRGBa>::getMeanValue(vpImage<bool> * = nullptr, unsigned int &)";
503  mean = I_rgba_ref.getMeanValue(nullptr, &nbValidPoints);
504  success = vpMath::equal(mean, mean_rgba_ref) && (nbValidPoints == (nbRows * nbCols));
505  if (!success) {
506  ++nbFailedTests;
507  failedTestsNames.push_back(nameTest);
508  areTestOK = false;
509  }
510  if (opt_verbose) {
511  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
512  if (!success) {
513  std::cout << "Theoretical count = " << nbRows * nbCols << " | returned value = " << nbValidPoints << std::endl;
514  std::cout << "Theoretical mean = " << mean_rgba_ref << " | returned value = " << mean << std::endl;
515  }
516  }
517 
518  nameTest = "vpImage<vpRGBf>::getMeanValue()";
519  mean = I_rgbf_ref.getMeanValue();
520  success = vpMath::equal(mean, mean_rgbf_ref);
521  if (!success) {
522  ++nbFailedTests;
523  failedTestsNames.push_back(nameTest);
524  areTestOK = false;
525  }
526  if (opt_verbose) {
527  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
528  if (!success) {
529  std::cout << "Theoretical mean = " << mean_rgbf_ref << " | returned value = " << mean << std::endl;
530  }
531  }
532 
533  nameTest = "vpImage<vpRGBf>::getMeanValue(vpImage<bool> *)";
534  mean = I_rgbf_ref.getMeanValue(&I_mask);
535  success = vpMath::equal(mean, mean_rgbf_true);
536  if (!success) {
537  ++nbFailedTests;
538  failedTestsNames.push_back(nameTest);
539  areTestOK = false;
540  }
541  if (opt_verbose) {
542  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
543  if (!success) {
544  std::cout << "Theoretical mean = " << mean_rgbf_true << " | returned value = " << mean << std::endl;
545  }
546  }
547 
548  nameTest = "vpImage<vpRGBf>::getMeanValue(vpImage<bool> * = nullptr)";
549  mean = I_rgbf_ref.getMeanValue(nullptr);
550  success = vpMath::equal(mean, mean_rgbf_ref);
551  if (!success) {
552  ++nbFailedTests;
553  failedTestsNames.push_back(nameTest);
554  areTestOK = false;
555  }
556  if (opt_verbose) {
557  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
558  if (!success) {
559  std::cout << "Theoretical mean = " << mean_rgbf_ref << " | returned value = " << mean << std::endl;
560  }
561  }
562 
563  nbValidPoints = 0;
564  nameTest = "vpImage<vpRGBf>::getMeanValue(vpImage<bool> *, unsigned int &)";
565  mean = I_rgbf_ref.getMeanValue(&I_mask, &nbValidPoints);
566  success = vpMath::equal(mean, mean_rgbf_true) && (nbValidPoints == count_true);
567  if (!success) {
568  ++nbFailedTests;
569  failedTestsNames.push_back(nameTest);
570  areTestOK = false;
571  }
572  if (opt_verbose) {
573  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
574  if (!success) {
575  std::cout << "Theoretical count = " << count_true << " | returned value = " << nbValidPoints << std::endl;
576  std::cout << "Theoretical mean = " << mean_rgbf_true << " | returned value = " << mean << std::endl;
577  }
578  }
579 
580  nbValidPoints = 0;
581  nameTest = "vpImage<vpRGBf>::getMeanValue(vpImage<bool> * = nullptr, unsigned int &)";
582  mean = I_rgbf_ref.getMeanValue(nullptr, &nbValidPoints);
583  success = vpMath::equal(mean, mean_rgbf_ref) && (nbValidPoints == (nbRows * nbCols));
584  if (!success) {
585  ++nbFailedTests;
586  failedTestsNames.push_back(nameTest);
587  areTestOK = false;
588  }
589  if (opt_verbose) {
590  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
591  if (!success) {
592  std::cout << "Theoretical count = " << nbRows * nbCols << " | returned value = " << nbValidPoints << std::endl;
593  std::cout << "Theoretical mean = " << mean_rgbf_ref << " | returned value = " << mean << std::endl;
594  }
595  }
596  }
597 
598  // Tests on the stdev
599  {
600  if (opt_verbose) {
601  std::cout << "Tests on the stdev" << std::endl;
602  }
603  std::string nameTest("vpImage<uchar>::getStdev()");
604  double stdev = I_uchar_ref.getStdev();
605  bool success = vpMath::equal(stdev, stdev_uchar_ref);
606  if (!success) {
607  ++nbFailedTests;
608  failedTestsNames.push_back(nameTest);
609  areTestOK = false;
610  }
611  if (opt_verbose) {
612  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
613  if (!success) {
614  std::cout << "Theoretical stdev = " << stdev_uchar_ref << " | returned value = " << stdev << std::endl;
615  }
616  }
617 
618  nameTest = ("vpImage<uchar>::getStdev(const vpImage<bool> *)");
619  stdev = I_uchar_ref.getStdev(&I_mask);
620  success = vpMath::equal(stdev, stdev_uchar_true);
621  if (!success) {
622  ++nbFailedTests;
623  failedTestsNames.push_back(nameTest);
624  areTestOK = false;
625  }
626  if (opt_verbose) {
627  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
628  if (!success) {
629  std::cout << "Theoretical stdev = " << stdev_uchar_true << " | returned value = " << stdev << std::endl;
630  }
631  }
632 
633  nameTest = ("vpImage<uchar>::getStdev(const vpImage<bool> * = nullptr)");
634  stdev = I_uchar_ref.getStdev(nullptr);
635  success = vpMath::equal(stdev, stdev_uchar_ref);
636  if (!success) {
637  ++nbFailedTests;
638  failedTestsNames.push_back(nameTest);
639  areTestOK = false;
640  }
641  if (opt_verbose) {
642  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
643  if (!success) {
644  std::cout << "Theoretical stdev = " << stdev_uchar_ref << " | returned value = " << stdev << std::endl;
645  }
646  }
647 
648  nameTest = ("vpImage<uchar>::getStdev(const double &)");
649  double mean = I_uchar_ref.getMeanValue();
650  stdev = I_uchar_ref.getStdev(mean);
651  success = vpMath::equal(stdev, stdev_uchar_ref);
652  if (!success) {
653  ++nbFailedTests;
654  failedTestsNames.push_back(nameTest);
655  areTestOK = false;
656  }
657  if (opt_verbose) {
658  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
659  if (!success) {
660  std::cout << "Theoretical stdev = " << stdev_uchar_ref << " | returned value = " << stdev << std::endl;
661  }
662  }
663 
664  nameTest = ("vpImage<uchar>::getStdev(const double &, vpImage<bool> *, unsigned int *)");
665  unsigned int nbValidPoints = 0;
666  mean = I_uchar_ref.getMeanValue(&I_mask, &nbValidPoints);
667  stdev = I_uchar_ref.getStdev(mean, &I_mask, &nbValidPoints);
668  success = vpMath::equal(stdev, stdev_uchar_true);
669  if (!success) {
670  ++nbFailedTests;
671  failedTestsNames.push_back(nameTest);
672  areTestOK = false;
673  }
674  if (opt_verbose) {
675  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
676  if (!success) {
677  std::cout << "Theoretical stdev = " << stdev_uchar_true << " | returned value = " << stdev << std::endl;
678  }
679  }
680 
681  nameTest = ("vpImage<uchar>::getStdev(const double &, vpImage<bool> *, unsigned int * = nullptr)");
682  nbValidPoints = 0;
683  mean = I_uchar_ref.getMeanValue(nullptr, &nbValidPoints);
684  stdev = I_uchar_ref.getStdev(mean, nullptr, &nbValidPoints);
685  success = vpMath::equal(stdev, stdev_uchar_ref) && (nbValidPoints == (nbRows * nbCols));
686  if (!success) {
687  ++nbFailedTests;
688  failedTestsNames.push_back(nameTest);
689  areTestOK = false;
690  }
691  if (opt_verbose) {
692  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
693  if (!success) {
694  std::cout << "Theoretical count = " << nbRows * nbCols << " | returned value = " << nbValidPoints << std::endl;
695  std::cout << "Theoretical stdev = " << stdev_uchar_ref << " | returned value = " << stdev << std::endl;
696  }
697  }
698 
699  nameTest = "vpImage<vpRGBa>::getStdev()";
700  stdev = I_rgba_ref.getStdev();
701  success = vpMath::equal(stdev, stdev_rgba_ref);
702  if (!success) {
703  ++nbFailedTests;
704  failedTestsNames.push_back(nameTest);
705  areTestOK = false;
706  }
707  if (opt_verbose) {
708  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
709  if (!success) {
710  std::cout << "Theoretical stdev = " << stdev_rgba_ref << " | returned value = " << stdev << std::endl;
711  }
712  }
713 
714  nameTest = ("vpImage<vpRGBa>::getStdev(const vpImage<bool> *)");
715  stdev = I_rgba_ref.getStdev(&I_mask);
716  success = vpMath::equal(stdev, stdev_rgba_true);
717  if (!success) {
718  ++nbFailedTests;
719  failedTestsNames.push_back(nameTest);
720  areTestOK = false;
721  }
722  if (opt_verbose) {
723  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
724  if (!success) {
725  std::cout << "Theoretical stdev = " << stdev_rgba_true << " | returned value = " << stdev << std::endl;
726  }
727  }
728 
729  nameTest = ("vpImage<vpRGBa>::getStdev(const vpImage<bool> * = nullptr)");
730  stdev = I_rgba_ref.getStdev(nullptr);
731  success = vpMath::equal(stdev, stdev_rgba_ref);
732  if (!success) {
733  ++nbFailedTests;
734  failedTestsNames.push_back(nameTest);
735  areTestOK = false;
736  }
737  if (opt_verbose) {
738  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
739  if (!success) {
740  std::cout << "Theoretical stdev = " << stdev_rgba_ref << " | returned value = " << stdev << std::endl;
741  }
742  }
743 
744  nameTest = ("vpImage<vpRGBa>::getStdev(const double &)");
745  mean = I_rgba_ref.getMeanValue();
746  stdev = I_rgba_ref.getStdev(mean);
747  success = vpMath::equal(stdev, stdev_rgba_ref);
748  if (!success) {
749  ++nbFailedTests;
750  failedTestsNames.push_back(nameTest);
751  areTestOK = false;
752  }
753  if (opt_verbose) {
754  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
755  if (!success) {
756  std::cout << "Theoretical stdev = " << stdev_rgba_ref << " | returned value = " << stdev << std::endl;
757  }
758  }
759 
760  nameTest = ("vpImage<vpRGBa>::getStdev(const double &, vpImage<bool> *, unsigned int *)");
761  nbValidPoints = 0;
762  mean = I_rgba_ref.getMeanValue(&I_mask, &nbValidPoints);
763  stdev = I_rgba_ref.getStdev(mean, &I_mask, &nbValidPoints);
764  success = vpMath::equal(stdev, stdev_rgba_true);
765  if (!success) {
766  ++nbFailedTests;
767  failedTestsNames.push_back(nameTest);
768  areTestOK = false;
769  }
770  if (opt_verbose) {
771  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
772  if (!success) {
773  std::cout << "Theoretical stdev = " << stdev_rgba_true << " | returned value = " << stdev << std::endl;
774  }
775  }
776 
777  nameTest = ("vpImage<vpRGBa>::getStdev(const double &, vpImage<bool> *, unsigned int * = nullptr)");
778  nbValidPoints = 0;
779  mean = I_rgba_ref.getMeanValue(nullptr, &nbValidPoints);
780  stdev = I_rgba_ref.getStdev(mean, nullptr, &nbValidPoints);
781  success = vpMath::equal(stdev, stdev_rgba_ref) && (nbValidPoints == (nbRows * nbCols));
782  if (!success) {
783  ++nbFailedTests;
784  failedTestsNames.push_back(nameTest);
785  areTestOK = false;
786  }
787  if (opt_verbose) {
788  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
789  if (!success) {
790  std::cout << "Theoretical count = " << nbRows * nbCols << " | returned value = " << nbValidPoints << std::endl;
791  std::cout << "Theoretical stdev = " << stdev_rgba_ref << " | returned value = " << stdev << std::endl;
792  }
793  }
794 
795  nameTest = "vpImage<vpRGBf>::getStdev()";
796  stdev = I_rgbf_ref.getStdev();
797  success = vpMath::equal(stdev, stdev_rgbf_ref);
798  if (!success) {
799  ++nbFailedTests;
800  failedTestsNames.push_back(nameTest);
801  areTestOK = false;
802  }
803  if (opt_verbose) {
804  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
805  if (!success) {
806  std::cout << "Theoretical stdev = " << stdev_rgbf_ref << " | returned value = " << stdev << std::endl;
807  }
808  }
809 
810  nameTest = ("vpImage<vpRGBf>::getStdev(const vpImage<bool> *)");
811  stdev = I_rgbf_ref.getStdev(&I_mask);
812  success = vpMath::equal(stdev, stdev_rgbf_true);
813  if (!success) {
814  ++nbFailedTests;
815  failedTestsNames.push_back(nameTest);
816  areTestOK = false;
817  }
818  if (opt_verbose) {
819  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
820  if (!success) {
821  std::cout << "Theoretical stdev = " << stdev_rgbf_true << " | returned value = " << stdev << std::endl;
822  }
823  }
824 
825  nameTest = ("vpImage<vpRGBf>::getStdev(const vpImage<bool> * = nullptr)");
826  stdev = I_rgbf_ref.getStdev(nullptr);
827  success = vpMath::equal(stdev, stdev_rgbf_ref);
828  if (!success) {
829  ++nbFailedTests;
830  failedTestsNames.push_back(nameTest);
831  areTestOK = false;
832  }
833  if (opt_verbose) {
834  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
835  if (!success) {
836  std::cout << "Theoretical stdev = " << stdev_rgbf_ref << " | returned value = " << stdev << std::endl;
837  }
838  }
839 
840  nameTest = ("vpImage<vpRGBf>::getStdev(const double &)");
841  mean = I_rgbf_ref.getMeanValue();
842  stdev = I_rgbf_ref.getStdev(mean);
843  success = vpMath::equal(stdev, stdev_rgbf_ref);
844  if (!success) {
845  ++nbFailedTests;
846  failedTestsNames.push_back(nameTest);
847  areTestOK = false;
848  }
849  if (opt_verbose) {
850  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
851  if (!success) {
852  std::cout << "Theoretical stdev = " << stdev_rgbf_ref << " | returned value = " << stdev << std::endl;
853  }
854  }
855 
856  nameTest = ("vpImage<vpRGBf>::getStdev(const double &, vpImage<bool> *, unsigned int *)");
857  nbValidPoints = 0;
858  mean = I_rgbf_ref.getMeanValue(&I_mask, &nbValidPoints);
859  stdev = I_rgbf_ref.getStdev(mean, &I_mask, &nbValidPoints);
860  success = vpMath::equal(stdev, stdev_rgbf_true);
861  if (!success) {
862  ++nbFailedTests;
863  failedTestsNames.push_back(nameTest);
864  areTestOK = false;
865  }
866  if (opt_verbose) {
867  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
868  if (!success) {
869  std::cout << "Theoretical stdev = " << stdev_rgbf_true << " | returned value = " << stdev << std::endl;
870  }
871  }
872 
873  nameTest = ("vpImage<vpRGBf>::getStdev(const double &, vpImage<bool> *, unsigned int * = nullptr)");
874  nbValidPoints = 0;
875  mean = I_rgbf_ref.getMeanValue(nullptr, &nbValidPoints);
876  stdev = I_rgbf_ref.getStdev(mean, nullptr, &nbValidPoints);
877  success = vpMath::equal(stdev, stdev_rgbf_ref) && (nbValidPoints == (nbRows * nbCols));
878  if (!success) {
879  ++nbFailedTests;
880  failedTestsNames.push_back(nameTest);
881  areTestOK = false;
882  }
883  if (opt_verbose) {
884  std::cout << "\tTest " << nameTest << ": " << (success ? "OK" : "failure") << std::endl;
885  if (!success) {
886  std::cout << "Theoretical count = " << nbRows * nbCols << " | returned value = " << nbValidPoints << std::endl;
887  std::cout << "Theoretical stdev = " << stdev_rgbf_ref << " | returned value = " << stdev << std::endl;
888  }
889  }
890  }
891 
892  if (areTestOK) {
893  std::cout << "All tests succeeded" << std::endl;
894  return EXIT_SUCCESS;
895  }
896  else {
897  std::cerr << nbFailedTests << " tests failed: " << std::endl;
898  for (unsigned int i = 0; i < nbFailedTests; ++i) {
899  std::cerr << " - " << failedTestsNames[i] << std::endl;
900  }
901  return EXIT_FAILURE;
902  }
903 }
static bool equal(double x, double y, double threshold=0.001)
Definition: vpMath.h:459