Visual Servoing Platform version 3.5.0
testImageBinarise.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 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
47int main()
48{
49 std::cout << "Test vpImageTools::binarise() with different data types." << std::endl;
50
51 unsigned int width = 5, height = 4;
52 std::vector<unsigned char> uchar_array(width * height);
53 std::vector<double> double_array(width * height);
54 std::vector<vpRGBa> rgba_array(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.front(), height, width);
62 vpImage<double> I_double(&double_array.front(), height, width);
63 vpImage<vpRGBa> I_rgba(&rgba_array.front(), 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 std::vector<unsigned char> uchar_array1(width * height);
125 std::vector<unsigned char> uchar_array2(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.front(), height, width);
132 vpImage<unsigned char> I_uchar2(&uchar_array2.front(), 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 std::vector<unsigned char> uchar_array_perf_lut(width * height);
152 std::vector<unsigned char> uchar_array_perf_iterate(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.front(), height, width);
159 vpImage<unsigned char> I_perf_iterate(&uchar_array_perf_iterate.front(), height, width);
160
161 unsigned int nbIterations = 100;
162 vpChrono chrono;
163 chrono.start();
164 for (unsigned int cpt = 0; cpt < nbIterations; cpt++) {
165 vpImageTools::binarise(I_perf_iterate, threshold1, threshold2, value1, value2, value3, false);
166 }
167 chrono.stop();
168 std::cout << "Iterate: " << chrono.getDurationMs() << " ms for " << nbIterations << " iterations." << std::endl;
169
170 chrono.start();
171 for (unsigned int cpt = 0; cpt < nbIterations; cpt++) {
172 vpImageTools::binarise(I_perf_lut, threshold1, threshold2, value1, value2, value3, true);
173 }
174 chrono.stop();
175 std::cout << "LUT: " << chrono.getDurationMs() << " ms for " << nbIterations << " iterations." << std::endl;
176
177 std::cout << "\ntestImageBinarise ok !" << std::endl;
178 return 0;
179}
void start(bool reset=true)
Definition: vpTime.cpp:409
void stop()
Definition: vpTime.cpp:424
double getDurationMs()
Definition: vpTime.cpp:392
static void binarise(vpImage< Type > &I, Type threshold1, Type threshold2, Type value1, Type value2, Type value3, bool useLUT=true)
Definition: vpImageTools.h:459
unsigned int getWidth() const
Definition: vpImage.h:246
unsigned int getHeight() const
Definition: vpImage.h:188
Definition: vpRGBa.h:67