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