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>
55 #define GETOPTARGS "cdi:t:h"
65 void usage(
const char *name,
const char *badparam, std::string ipath)
71 %s [-i <input image path>] [-t <nb threads>]\n\
78 -i <input image path> %s\n\
79 Set image input path.\n\
80 From this path read \"Klimt/Klimt.ppm\"\n\
82 Setting the VISP_INPUT_IMAGE_PATH environment\n\
83 variable produces the same behaviour than using\n\
87 Set the number of threads to use for the computation.\n\
93 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
107 bool getOptions(
int argc,
const char **argv, std::string &ipath,
unsigned int &nbThreads)
118 nbThreads = (
unsigned int)atoi(optarg_);
121 usage(argv[0],
nullptr, ipath);
130 usage(argv[0], optarg_, ipath);
136 if ((c == 1) || (c == -1)) {
138 usage(argv[0],
nullptr, ipath);
139 std::cerr <<
"ERROR: " << std::endl;
140 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
156 unsigned int sum = 0;
159 histogram.
calculate(I, nbBins, nbThreads);
161 for (
unsigned int cpt = 0; cpt < histogram.
getSize(); cpt++) {
162 sum += histogram[cpt];
177 histogram_single_threaded.
calculate(I, nbBins, 1);
180 histogram_multi_threaded.
calculate(I, nbBins, 4);
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;
191 sum += histogram_single_threaded[cpt];
195 std::cerr <<
"Sum of histogram is different with the image size!" << std::endl;
202 int main(
int argc,
const char **argv)
205 std::string env_ipath;
206 std::string opt_ipath;
208 std::string filename;
209 unsigned int nbThreads = 4;
216 if (!env_ipath.empty())
220 if (getOptions(argc, argv, opt_ipath, nbThreads) ==
false) {
225 if (!opt_ipath.empty())
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;
240 if (opt_ipath.empty() && env_ipath.empty()) {
241 usage(argv[0],
nullptr, 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
259 std::cout <<
"Read image: " << filename << std::endl;
264 int nbIterations = 100;
265 unsigned int nbBins = 256;
266 unsigned int sum_single_thread = 0;
267 unsigned int sum_multi_thread = 0;
270 for (
int iteration = 0; iteration < nbIterations; iteration++) {
271 sum_single_thread = histogramSum(I, nbBins, 1);
276 for (
int iteration = 0; iteration < nbIterations; iteration++) {
277 sum_multi_thread = histogramSum(I, nbBins, nbThreads);
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_multi_thread (nbThreads=" << nbThreads <<
")=" << sum_multi_thread <<
" ; t_multi_thread=" << t_multi_thread
284 <<
" ms ; mean=" << t_multi_thread / (double)nbIterations <<
" ms" << std::endl;
285 std::cout <<
"Speed-up=" << t_single_thread / (double)t_multi_thread <<
"X" << std::endl;
287 if (sum_single_thread != I.
getSize() || sum_multi_thread != I.
getSize()) {
288 std::cerr <<
"Problem with histogram!" << std::endl;
293 if (!compareHistogram(I, nbBins)) {
294 std::cerr <<
"Histogram are different!" << std::endl;
299 std::cout <<
"Test histogram computation on empty image" << std::endl << std::flush;
303 if (histogram.
getSize() == 256) {
304 for (
unsigned int cpt = 0; cpt < 256; cpt++) {
305 if (histogram[cpt] != 0) {
306 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
307 <<
" but should be zero!" << std::endl;
312 std::cerr <<
"Bad histogram size!" << std::endl;
317 std::cout <<
"Test histogram computation on image size < nbThreads" << std::endl << std::flush;
321 if (histogram.
getSize() == 256) {
322 for (
unsigned int cpt = 0; cpt < 256; cpt++) {
324 if (histogram[cpt] != I_test.getSize()) {
325 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
326 <<
" but should be: " << I_test.
getSize() << std::endl;
331 if (histogram[cpt] != 0) {
332 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
333 <<
" but should be zero!" << std::endl;
339 std::cerr <<
"Bad histogram size!" << std::endl;
344 std::cout <<
"Test histogram computation on small image size" << std::endl << std::flush;
348 if (histogram.
getSize() == 256) {
349 for (
unsigned int cpt = 0; cpt < 256; cpt++) {
351 if (histogram[cpt] != I_test.getSize()) {
352 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
353 <<
" but should be: " << I_test.
getSize() << std::endl;
358 if (histogram[cpt] != 0) {
359 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
360 <<
" but should be zero!" << std::endl;
366 std::cerr <<
"Bad histogram size!" << std::endl;
370 std::cout <<
"testHistogram is OK!" << std::endl;
374 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
error that can be emitted by ViSP classes.
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)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
unsigned int getWidth() const
unsigned int getSize() const
unsigned int getHeight() const
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
VISP_EXPORT double measureTimeMs()