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