Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testImageBinarise.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 vpImageTools::binarise() function.
32  */
40 #include <visp3/core/vpImageTools.h>
41 
42 int main()
43 {
44 #ifdef ENABLE_VISP_NAMESPACE
45  using namespace VISP_NAMESPACE_NAME;
46 #endif
47  std::cout << "Test vpImageTools::binarise() with different data types." << std::endl;
48 
49  unsigned int width = 5, height = 4;
50  std::vector<unsigned char> uchar_array(width * height);
51  std::vector<double> double_array(width * height);
52  std::vector<vpRGBa> rgba_array(width * height);
53  for (unsigned char i = 0; i < width * height; i++) {
54  uchar_array[i] = i;
55  double_array[i] = i;
56  rgba_array[i] = vpRGBa(i, i, i, i);
57  }
58 
59  vpImage<unsigned char> I(&uchar_array.front(), height, width);
60  vpImage<double> I_double(&double_array.front(), height, width);
61  vpImage<vpRGBa> I_rgba(&rgba_array.front(), height, width);
62 
63  std::cout << "I:" << std::endl;
64  for (unsigned int i = 0; i < I.getHeight(); i++) {
65  for (unsigned int j = 0; j < I.getWidth(); j++) {
66  std::cout << static_cast<unsigned>(I[i][j]) << " ";
67  }
68  std::cout << std::endl;
69  }
70 
71  std::cout << "\nI_double:" << std::endl;
72  for (unsigned int i = 0; i < I_double.getHeight(); i++) {
73  for (unsigned int j = 0; j < I_double.getWidth(); j++) {
74  std::cout << I_double[i][j] << " ";
75  }
76  std::cout << std::endl;
77  }
78 
79  std::cout << "\nI_rgba:" << std::endl;
80  for (unsigned int i = 0; i < I_rgba.getHeight(); i++) {
81  for (unsigned int j = 0; j < I_rgba.getWidth(); j++) {
82  std::cout << static_cast<unsigned>(I_rgba[i][j].R) << " ; " << static_cast<unsigned>(I_rgba[i][j].G) << " ; "
83  << static_cast<unsigned>(I_rgba[i][j].B) << " ; " << static_cast<unsigned int>(I_rgba[i][j].A)
84  << std::endl;
85  }
86  std::cout << std::endl;
87  }
88 
89  vpImageTools::binarise(I, (unsigned char)5, (unsigned char)12, (unsigned char)0, (unsigned char)127,
90  (unsigned char)255);
91  vpImageTools::binarise(I_double, 5.0, 12.0, 0.0, 127.0, 255.0);
92  vpImageTools::binarise(I_rgba, vpRGBa(5), vpRGBa(12), vpRGBa(0), vpRGBa(127), vpRGBa(255));
93 
94  std::cout << "\nI binarise:" << std::endl;
95  for (unsigned int i = 0; i < I.getHeight(); i++) {
96  for (unsigned int j = 0; j < I.getWidth(); j++) {
97  std::cout << static_cast<unsigned>(I[i][j]) << " ";
98  }
99  std::cout << std::endl;
100  }
101 
102  std::cout << "\nI_double binarise:" << std::endl;
103  for (unsigned int i = 0; i < I_double.getHeight(); i++) {
104  for (unsigned int j = 0; j < I_double.getWidth(); j++) {
105  std::cout << I_double[i][j] << " ";
106  }
107  std::cout << std::endl;
108  }
109 
110  std::cout << "\nI_rgba binarise:" << std::endl;
111  for (unsigned int i = 0; i < I_rgba.getHeight(); i++) {
112  for (unsigned int j = 0; j < I_rgba.getWidth(); j++) {
113  std::cout << static_cast<unsigned>(I_rgba[i][j].R) << " ; " << static_cast<unsigned>(I_rgba[i][j].G) << " ; "
114  << static_cast<unsigned>(I_rgba[i][j].B) << " ; " << static_cast<unsigned>(I_rgba[i][j].A) << std::endl;
115  }
116  std::cout << std::endl;
117  }
118 
119  // Check if results are the same between iterate and LUT methods
120  width = 32;
121  height = 8;
122  std::vector<unsigned char> uchar_array1(width * height);
123  std::vector<unsigned char> uchar_array2(width * height);
124  for (unsigned int i = 0; i < 256; i++) {
125  uchar_array1[i] = (unsigned char)i;
126  uchar_array2[i] = (unsigned char)i;
127  }
128 
129  vpImage<unsigned char> I_uchar1(&uchar_array1.front(), height, width);
130  vpImage<unsigned char> I_uchar2(&uchar_array2.front(), height, width);
131 
132  unsigned char threshold1 = 50, threshold2 = 200;
133  unsigned char value1 = 4, value2 = 127, value3 = 250;
134  vpImageTools::binarise(I_uchar1, threshold1, threshold2, value1, value2, value3, false);
135  vpImageTools::binarise(I_uchar2, threshold1, threshold2, value1, value2, value3, true);
136 
137  for (unsigned int i = 0; i < height; i++) {
138  for (unsigned int j = 0; j < width; j++) {
139  if (I_uchar1[i][j] != I_uchar2[i][j]) {
140  std::cerr << "Results are different between iterate and LUT methods !" << std::endl;
141  return EXIT_FAILURE;
142  }
143  }
144  }
145 
146  // Test performance between iterate and LUT methods
147  width = 640;
148  height = 480;
149  std::vector<unsigned char> uchar_array_perf_lut(width * height);
150  std::vector<unsigned char> uchar_array_perf_iterate(width * height);
151  for (unsigned int i = 0; i < width * height; i++) {
152  uchar_array_perf_lut[i] = (unsigned char)i;
153  uchar_array_perf_iterate[i] = (unsigned char)i;
154  }
155 
156  vpImage<unsigned char> I_perf_lut(&uchar_array_perf_lut.front(), height, width);
157  vpImage<unsigned char> I_perf_iterate(&uchar_array_perf_iterate.front(), height, width);
158 
159  unsigned int nbIterations = 100;
160  vpChrono chrono;
161  chrono.start();
162  for (unsigned int cpt = 0; cpt < nbIterations; cpt++) {
163  vpImageTools::binarise(I_perf_iterate, threshold1, threshold2, value1, value2, value3, false);
164  }
165  chrono.stop();
166  std::cout << "Iterate: " << chrono.getDurationMs() << " ms for " << nbIterations << " iterations." << std::endl;
167 
168  chrono.start();
169  for (unsigned int cpt = 0; cpt < nbIterations; cpt++) {
170  vpImageTools::binarise(I_perf_lut, threshold1, threshold2, value1, value2, value3, true);
171  }
172  chrono.stop();
173  std::cout << "LUT: " << chrono.getDurationMs() << " ms for " << nbIterations << " iterations." << std::endl;
174 
175  std::cout << "\ntestImageBinarise ok !" << std::endl;
176  return EXIT_SUCCESS;
177 }
void start(bool reset=true)
Definition: vpTime.cpp:401
void stop()
Definition: vpTime.cpp:416
double getDurationMs()
Definition: vpTime.cpp:390
static void binarise(vpImage< Type > &I, Type threshold1, Type threshold2, Type value1, Type value2, Type value3, bool useLUT=true)
Definition: vpImageTools.h:473
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
Definition: vpRGBa.h:65