Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  *
33  * Authors:
34  * Souriya Trinh
35  *
36  *****************************************************************************/
44 #include <visp3/core/vpImageTools.h>
45 
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) << std::endl;
86  }
87  std::cout << std::endl;
88  }
89 
90  vpImageTools::binarise(I, (unsigned char) 5, (unsigned char) 12, (unsigned char) 0, (unsigned char) 127, (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 
120  //Check if results are the same between iterate and LUT methods
121  width = 32;
122  height = 8;
123  unsigned char *uchar_array1 = new unsigned char[width*height];
124  unsigned char *uchar_array2 = new unsigned char[width*height];
125  for(unsigned int i = 0; i < 256; i++) {
126  uchar_array1[i] = (unsigned char) i;
127  uchar_array2[i] = (unsigned char) i;
128  }
129 
130  vpImage<unsigned char> I_uchar1(uchar_array1, height, width);
131  vpImage<unsigned char> I_uchar2(uchar_array2, height, width);
132 
133  unsigned char threshold1 = 50, threshold2 = 200;
134  unsigned char value1 = 4, value2 = 127, value3 = 250;
135  vpImageTools::binarise(I_uchar1, threshold1, threshold2, value1, value2, value3, false);
136  vpImageTools::binarise(I_uchar2, threshold1, threshold2, value1, value2, value3, true);
137 
138  for(unsigned int i = 0; i < height; i++) {
139  for(unsigned int j = 0; j < width; j++) {
140  if(I_uchar1[i][j] != I_uchar2[i][j]) {
141  std::cerr << "Results are different between iterate and LUT methods !" << std::endl;
142  return -1;
143  }
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 
177  std::cout << "\ntestImageBinarise ok !" << std::endl;
178  return 0;
179 }
static void binarise(vpImage< Type > &I, Type threshold1, Type threshold2, Type value1, Type value2, Type value3, const bool useLUT=true)
Definition: vpImageTools.h:423
unsigned int getWidth() const
Definition: vpImage.h:226
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93
Definition: vpRGBa.h:66
unsigned int getHeight() const
Definition: vpImage.h:175