Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testHistogram.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 histogram computation.
33 *
34 * Authors:
35 * Souriya Trinh
36 *
37*****************************************************************************/
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <visp3/core/vpHistogram.h>
42#include <visp3/core/vpImage.h>
43#include <visp3/core/vpIoTools.h>
44#include <visp3/io/vpImageIo.h>
45#include <visp3/io/vpParseArgv.h>
46
54// List of allowed command line options
55#define GETOPTARGS "cdi:t:h"
56
57/*
58 Print the program options.
59
60 \param name : Program name.
61 \param badparam : Bad parameter name.
62 \param ipath: Input image path.
63
64 */
65void usage(const char *name, const char *badparam, std::string ipath)
66{
67 fprintf(stdout, "\n\
68Test histogram.\n\
69\n\
70SYNOPSIS\n\
71 %s [-i <input image path>] [-t <nb threads>]\n\
72 [-h]\n \
73",
74 name);
75
76 fprintf(stdout, "\n\
77OPTIONS: Default\n\
78 -i <input image path> %s\n\
79 Set image input path.\n\
80 From this path read \"Klimt/Klimt.ppm\"\n\
81 image.\n\
82 Setting the VISP_INPUT_IMAGE_PATH environment\n\
83 variable produces the same behaviour than using\n\
84 this option.\n\
85\n\
86 -t <nb threads>\n\
87 Set the number of threads to use for the computation.\n\
88 -h\n\
89 Print the help.\n\n",
90 ipath.c_str());
91
92 if (badparam)
93 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
94}
95
107bool getOptions(int argc, const char **argv, std::string &ipath, unsigned int &nbThreads)
108{
109 const char *optarg_;
110 int c;
111 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
112
113 switch (c) {
114 case 'i':
115 ipath = optarg_;
116 break;
117 case 't':
118 nbThreads = (unsigned int)atoi(optarg_);
119 break;
120 case 'h':
121 usage(argv[0], NULL, ipath);
122 return false;
123 break;
124
125 case 'c':
126 case 'd':
127 break;
128
129 default:
130 usage(argv[0], optarg_, ipath);
131 return false;
132 break;
133 }
134 }
135
136 if ((c == 1) || (c == -1)) {
137 // standalone param or error
138 usage(argv[0], NULL, ipath);
139 std::cerr << "ERROR: " << std::endl;
140 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
141 return false;
142 }
143
144 return true;
145}
146
154unsigned int histogramSum(const vpImage<unsigned char> &I, unsigned int nbBins, unsigned int nbThreads)
155{
156 unsigned int sum = 0;
157
158 vpHistogram histogram;
159 histogram.calculate(I, nbBins, nbThreads);
160
161 for (unsigned int cpt = 0; cpt < histogram.getSize(); cpt++) {
162 sum += histogram[cpt];
163 }
164
165 return sum;
166}
167
174bool compareHistogram(const vpImage<unsigned char> &I, unsigned int nbBins)
175{
176 vpHistogram histogram_single_threaded;
177 histogram_single_threaded.calculate(I, nbBins, 1);
178
179 vpHistogram histogram_multi_threaded;
180 histogram_multi_threaded.calculate(I, nbBins, 4);
181
182 unsigned int sum = 0;
183 for (unsigned int cpt = 0; cpt < nbBins; cpt++) {
184 if (histogram_single_threaded[cpt] != histogram_multi_threaded[cpt]) {
185 std::cerr << "histogram_single_threaded[" << cpt << "]=" << histogram_single_threaded[cpt]
186 << " ; histogram_multi_threaded[" << cpt << "]=" << histogram_multi_threaded[cpt] << std::endl;
187
188 return false;
189 }
190
191 sum += histogram_single_threaded[cpt];
192 }
193
194 if (sum != I.getSize()) {
195 std::cerr << "Sum of histogram is different with the image size!" << std::endl;
196 return false;
197 }
198
199 return true;
200}
201
202int main(int argc, const char **argv)
203{
204 try {
205 std::string env_ipath;
206 std::string opt_ipath;
207 std::string ipath;
208 std::string filename;
209 unsigned int nbThreads = 4;
210
211 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
212 // environment variable value
214
215 // Set the default input path
216 if (!env_ipath.empty())
217 ipath = env_ipath;
218
219 // Read the command line options
220 if (getOptions(argc, argv, opt_ipath, nbThreads) == false) {
221 return EXIT_FAILURE;
222 }
223
224 // Get the option values
225 if (!opt_ipath.empty())
226 ipath = opt_ipath;
227
228 // Compare ipath and env_ipath. If they differ, we take into account
229 // the input path comming from the command line option
230 if (!opt_ipath.empty() && !env_ipath.empty()) {
231 if (ipath != env_ipath) {
232 std::cout << std::endl << "WARNING: " << std::endl;
233 std::cout << " Since -i <visp image path=" << ipath << "> "
234 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
235 << " we skip the environment variable." << std::endl;
236 }
237 }
238
239 // Test if an input path is set
240 if (opt_ipath.empty() && env_ipath.empty()) {
241 usage(argv[0], NULL, ipath);
242 std::cerr << std::endl << "ERROR:" << std::endl;
243 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
244 << " environment variable to specify the location of the " << std::endl
245 << " image path where test images are located." << std::endl
246 << std::endl;
247 return EXIT_FAILURE;
248 }
249
250 //
251 // Here starts really the test
252 //
253
254 // Create a grey level image
256
257 // Load a grey image from the disk
258 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
259 std::cout << "Read image: " << filename << std::endl;
260 vpImageIo::read(I, filename);
261
262 std::cout << "I=" << I.getWidth() << "x" << I.getHeight() << std::endl;
263
264 int nbIterations = 100;
265 unsigned int nbBins = 256;
266 unsigned int sum_single_thread = 0;
267 unsigned int sum_single_multithread = 0;
268
269 double t_single_thread = vpTime::measureTimeMs();
270 for (int iteration = 0; iteration < nbIterations; iteration++) {
271 sum_single_thread = histogramSum(I, nbBins, 1);
272 }
273 t_single_thread = vpTime::measureTimeMs() - t_single_thread;
274
275 double t_multithread = vpTime::measureTimeMs();
276 for (int iteration = 0; iteration < nbIterations; iteration++) {
277 sum_single_multithread = histogramSum(I, nbBins, nbThreads);
278 }
279 t_multithread = vpTime::measureTimeMs() - t_multithread;
280
281 std::cout << "sum_single_thread=" << sum_single_thread << " ; t_single_thread=" << t_single_thread
282 << " ms ; mean=" << t_single_thread / (double)nbIterations << " ms" << std::endl;
283 std::cout << "sum_single_multithread=" << sum_single_multithread << " ; t_multithread=" << t_multithread
284 << " ms ; mean=" << t_multithread / (double)nbIterations << " ms" << std::endl;
285 std::cout << "Speed-up=" << t_single_thread / (double)t_multithread << "X" << std::endl;
286
287 if (sum_single_thread != I.getSize() || sum_single_multithread != I.getSize()) {
288 std::cerr << "Problem with histogram!" << std::endl;
289 return EXIT_FAILURE;
290 }
291
292 nbBins = 101;
293 if (!compareHistogram(I, nbBins)) {
294 std::cerr << "Histogram are different!" << std::endl;
295 return EXIT_FAILURE;
296 }
297
298 // Test histogram computation on empty image
299 vpHistogram histogram;
300 vpImage<unsigned char> I_test(0, 0);
301 histogram.calculate(I_test, 256, 4);
302 if (histogram.getSize() == 256) {
303 for (unsigned int cpt = 0; cpt < 256; cpt++) {
304 if (histogram[cpt] != 0) {
305 std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
306 << " but should be zero!" << std::endl;
307 }
308 }
309 } else {
310 std::cerr << "Bad histogram size!" << std::endl;
311 return EXIT_FAILURE;
312 }
313
314 // Test histogram computation on image size < nbThreads
315 I_test.init(3, 1);
316 I_test = 100;
317 histogram.calculate(I_test, 256, 4);
318 if (histogram.getSize() == 256) {
319 for (unsigned int cpt = 0; cpt < 256; cpt++) {
320 if (cpt == 100) {
321 if (histogram[cpt] != I_test.getSize()) {
322 std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
323 << " but should be: " << I_test.getSize() << std::endl;
324 return EXIT_FAILURE;
325 }
326 } else {
327 if (histogram[cpt] != 0) {
328 std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
329 << " but should be zero!" << std::endl;
330 }
331 }
332 }
333 } else {
334 std::cerr << "Bad histogram size!" << std::endl;
335 return EXIT_FAILURE;
336 }
337
338 // Test histogram computation on small image size
339 I_test.init(7, 1);
340 I_test = 50;
341 histogram.calculate(I_test, 256, 4);
342 if (histogram.getSize() == 256) {
343 for (unsigned int cpt = 0; cpt < 256; cpt++) {
344 if (cpt == 50) {
345 if (histogram[cpt] != I_test.getSize()) {
346 std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
347 << " but should be: " << I_test.getSize() << std::endl;
348 return EXIT_FAILURE;
349 }
350 } else {
351 if (histogram[cpt] != 0) {
352 std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
353 << " but should be zero!" << std::endl;
354 }
355 }
356 }
357 } else {
358 std::cerr << "Bad histogram size!" << std::endl;
359 return EXIT_FAILURE;
360 }
361
362 std::cout << "testHistogram is OK!" << std::endl;
363 return EXIT_SUCCESS;
364 } catch (const vpException &e) {
365 std::cerr << "Catch an exception: " << e.what() << std::endl;
366 return EXIT_FAILURE;
367 }
368}
error that can be emitted by ViSP classes.
Definition vpException.h:59
const char * what() const
Class to compute a gray level image histogram.
void calculate(const vpImage< unsigned char > &I, unsigned int nbins=256, unsigned int nbThreads=1)
unsigned getSize() const
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition of the vpImage class member functions.
Definition vpImage.h:135
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getSize() const
Definition vpImage.h:223
unsigned int getHeight() const
Definition vpImage.h:184
static std::string getViSPImagesDataPath()
static std::string createFilePath(const std::string &parent, const std::string &child)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
VISP_EXPORT double measureTimeMs()