Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
testConversion.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 image conversions.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 #include <iomanip>
40 #include <stdlib.h>
41 
42 #include <visp3/core/vpConfig.h>
43 #include <visp3/core/vpDebug.h>
44 #include <visp3/core/vpImage.h>
45 #include <visp3/core/vpImageConvert.h>
46 #include <visp3/core/vpIoTools.h>
47 #include <visp3/core/vpTime.h>
48 #include <visp3/io/vpImageIo.h>
49 #include <visp3/io/vpParseArgv.h>
50 
57 // List of allowed command line options
58 #define GETOPTARGS "cdi:o:n:h"
59 
60 /*
61  Print the program options.
62 
63  \param name : Program name.
64  \param badparam : Bad parameter name.
65  \param ipath: Input image path.
66  \param opath : Output image path.
67  \param user : Username.
68  \param nbiter : Iteration number.
69 
70  */
71 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user, int nbiter)
72 {
73  fprintf(stdout, "\n\
74 Test image conversions.\n\
75 \n\
76 SYNOPSIS\n\
77  %s [-i <input image path>] [-o <output image path>] [-n <nb benchmark iterations>]\n\
78  [-h]\n \
79 ", name);
80 
81  fprintf(stdout, "\n\
82 OPTIONS: Default\n\
83  -i <input image path> %s\n\
84  Set image input path.\n\
85  From this path read \"Klimt/Klimt.pgm\"\n\
86  and \"Klimt/Klimt.ppm\" images.\n\
87  Setting the VISP_INPUT_IMAGE_PATH environment\n\
88  variable produces the same behaviour than using\n\
89  this option.\n\
90 \n\
91  -o <output image path> %s\n\
92  Set image output path.\n\
93  From this directory, creates the \"%s\"\n\
94  subdirectory depending on the username, where \n\
95  Klimt_grey.pgm and Klimt_color.ppm output images\n\
96  are written.\n\
97 \n\
98  -n <nb benchmark iterations> %d\n\
99  Set the number of benchmark iterations.\n\
100 \n\
101  -h\n\
102  Print the help.\n\n", ipath.c_str(), opath.c_str(), user.c_str(), nbiter);
103 
104  if (badparam)
105  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
106 }
107 
121 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, const std::string &user,
122  int &nbIterations)
123 {
124  const char *optarg_;
125  int c;
126  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
127 
128  switch (c) {
129  case 'i':
130  ipath = optarg_;
131  break;
132  case 'o':
133  opath = optarg_;
134  break;
135  case 'n':
136  nbIterations = atoi(optarg_);
137  break;
138  case 'h':
139  usage(argv[0], NULL, ipath, opath, user, nbIterations);
140  return false;
141 
142  case 'c':
143  case 'd':
144  break;
145 
146  default:
147  usage(argv[0], optarg_, ipath, opath, user, nbIterations);
148  return false;
149  }
150  }
151 
152  if ((c == 1) || (c == -1)) {
153  // standalone param or error
154  usage(argv[0], NULL, ipath, opath, user, nbIterations);
155  std::cerr << "ERROR: " << std::endl;
156  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
157  return false;
158  }
159 
160  return true;
161 }
162 
163 int main(int argc, const char **argv)
164 {
165  try {
166  std::string env_ipath;
167  std::string opt_ipath;
168  std::string opt_opath;
169  std::string ipath;
170  std::string opath;
171  std::string filename;
172  std::string username;
173  int nbIterations = 1;
174 
175  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
176  // environment variable value
177  env_ipath = vpIoTools::getViSPImagesDataPath();
178 
179  // Set the default input path
180  if (!env_ipath.empty())
181  ipath = env_ipath;
182 
183 // Set the default output path
184 #if defined(_WIN32)
185  opt_opath = "C:/temp";
186 #else
187  opt_opath = "/tmp";
188 #endif
189 
190  // Get the user login name
191  vpIoTools::getUserName(username);
192 
193  // Read the command line options
194  if (getOptions(argc, argv, opt_ipath, opt_opath, username, nbIterations) == false) {
195  exit(-1);
196  }
197 
198  // Get the option values
199  if (!opt_ipath.empty())
200  ipath = opt_ipath;
201  if (!opt_opath.empty())
202  opath = opt_opath;
203 
204  // Append to the output path string, the login name of the user
205  opath = vpIoTools::createFilePath(opath, username);
206 
207  // Test if the output path exist. If no try to create it
208  if (vpIoTools::checkDirectory(opath) == false) {
209  try {
210  // Create the dirname
212  } catch (...) {
213  usage(argv[0], NULL, ipath, opt_opath, username, nbIterations);
214  std::cerr << std::endl << "ERROR:" << std::endl;
215  std::cerr << " Cannot create " << opath << std::endl;
216  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
217  exit(-1);
218  }
219  }
220 
221  // Compare ipath and env_ipath. If they differ, we take into account
222  // the input path comming from the command line option
223  if (opt_ipath.empty()) {
224  if (ipath != env_ipath) {
225  std::cout << std::endl << "WARNING: " << std::endl;
226  std::cout << " Since -i <visp image path=" << ipath << "> "
227  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
228  << " we skip the environment variable." << std::endl;
229  }
230  }
231 
232  // Test if an input path is set
233  if (opt_ipath.empty() && env_ipath.empty()) {
234  usage(argv[0], NULL, ipath, opt_opath, username, nbIterations);
235  std::cerr << std::endl << "ERROR:" << std::endl;
236  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
237  << " environment variable to specify the location of the " << std::endl
238  << " image path where test images are located." << std::endl
239  << std::endl;
240  exit(-1);
241  }
242 
243  //
244  // Here starts really the test
245  //
246 
247  vpImage<unsigned char> Ig; // Grey image
248  vpImage<vpRGBa> Ic; // Color image
249 
250  //-------------------- .pgm -> .ppm
251  std::cout << "** Convert a grey image (.pgm) to a color image (.ppm)" << std::endl;
252  // Load a grey image from the disk
253  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
254  std::cout << " Load " << filename << std::endl;
255  vpImageIo::read(Ig, filename);
256  // Create a color image from the grey
257  vpImageConvert::convert(Ig, Ic);
258  filename = vpIoTools::createFilePath(opath, "Klimt_color.ppm");
259  std::cout << " Resulting image saved in: " << filename << std::endl;
260  vpImageIo::write(Ic, filename);
261 
262  //-------------------- .ppm -> .pgm
263  std::cout << "** Convert a color image (.ppm) to a grey image (.pgm)" << std::endl;
264  // Load a color image from the disk
265  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
266  std::cout << " Load " << filename << std::endl;
267  vpImageIo::read(Ic, filename);
268  // Create a grey image from the color
269  vpImageConvert::convert(Ic, Ig);
270  filename = vpIoTools::createFilePath(opath, "Klimt_grey.pgm");
271  std::cout << " Resulting image saved in: " << filename << std::endl;
272  vpImageIo::write(Ig, filename);
273 
274  //-------------------- YUV -> RGB
275  std::cout << "** Convert YUV pixel value to a RGB value" << std::endl;
276  unsigned char y = 187, u = 10, v = 30;
277  unsigned char r, g, b;
278 
279  // Convert a YUV pixel value to a RGB value
280  vpImageConvert::YUVToRGB(y, u, v, r, g, b);
281  std::cout << " y(" << (int)y << ") u(" << (int)u << ") v(" << (int)v << ") = r(" << (int)r << ") g(" << (int)g
282  << ") b(" << (int)b << ")" << std::endl;
283 
284  vpChrono chrono;
285 #ifdef VISP_HAVE_OPENCV
286 #if VISP_HAVE_OPENCV_VERSION < 0x020408
287  double t0 = vpTime::measureTimeMs();
289  // Convert a IplImage to a vpImage<vpRGBa>
291  std::cout << "** Convert an IplImage to a vpImage<vpRGBa>" << std::endl;
292  IplImage *image = NULL;
293  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
294 
295  /* Read the color image */
296 
297  std::cout << " Reading the color image with opencv: " << filename << std::endl;
298  if ((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR)) == NULL) {
299  std::cout << " Cannot read image: " << filename << std::endl;
300  return (-1);
301  }
302  vpImageConvert::convert(image, Ic);
303  filename = vpIoTools::createFilePath(opath, "Klimt_color_cv.ppm");
304  std::cout << " Resulting image saved in: " << filename << std::endl;
305  vpImageIo::write(Ic, filename);
306 
307  std::cout << " Convert result in " << filename << std::endl;
308 
309  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
310 
311  /* Read the pgm image */
312  std::cout << " Reading the greyscale image with opencv: " << filename << std::endl;
313  if (image != NULL)
314  cvReleaseImage(&image);
315  if ((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE)) == NULL) {
316  std::cout << " Cannot read image: " << filename << std::endl;
317  return (-1);
318  }
319  vpImageConvert::convert(image, Ic);
320  filename = vpIoTools::createFilePath(opath, "Klimt_grey_cv.ppm");
321  std::cout << " Resulting image saved in: " << filename << std::endl;
322  vpImageIo::write(Ic, filename);
323 
324  std::cout << " Convert result in " << filename << std::endl;
325 
327  // Convert a IplImage to a vpImage<unsigned char>
329  std::cout << "** Convert an IplImage to a vpImage<unsigned char>" << std::endl;
330  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
331 
332  /* Read the color image */
333 
334  std::cout << " Reading the color image with opencv: " << filename << std::endl;
335  if (image != NULL)
336  cvReleaseImage(&image);
337  if ((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR)) == NULL) {
338  std::cout << " Cannot read image: " << filename << std::endl;
339  return (-1);
340  }
341  vpImageConvert::convert(image, Ig);
342  filename = vpIoTools::createFilePath(opath, "Klimt_color_cv.pgm");
343  std::cout << " Resulting image saved in: " << filename << std::endl;
344  vpImageIo::write(Ig, filename);
345 
346  std::cout << " Convert result in " << filename << std::endl;
347 
348  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
349 
350  /* Read the pgm image */
351 
352  std::cout << " Reading the greyscale image with opencv: " << filename << std::endl;
353  if (image != NULL)
354  cvReleaseImage(&image);
355  if ((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE)) == NULL) {
356  std::cout << " Cannot read image: " << filename << std::endl;
357  return (-1);
358  }
359  vpImageConvert::convert(image, Ig);
360  filename = vpIoTools::createFilePath(opath, "Klimt_grey_cv.pgm");
361  std::cout << " Resulting image saved in: " << filename << std::endl;
362  vpImageIo::write(Ig, filename);
363 
364  std::cout << " Convert result in " << filename << std::endl;
365 
367  // Convert a vpImage<vpRGBa> to a IplImage
369  std::cout << "** Convert a vpImage<vpRGBa> to an IplImage" << std::endl;
370  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
371 
372  /* Read the color image */
373 
374  // Load a color image from the disk
375  std::cout << " Load " << filename << std::endl;
376  vpImageIo::read(Ic, filename);
377  vpImageConvert::convert(Ic, image);
378  filename = vpIoTools::createFilePath(opath, "Klimt_ipl_color_cv.ppm");
379  /* Save the the current image */
380  std::cout << " Write " << filename << std::endl;
381  if ((cvSaveImage(filename.c_str(), image)) == 0) {
382  std::cout << " Cannot write image: " << filename << std::endl;
383  if (image != NULL)
384  cvReleaseImage(&image);
385  return (-1);
386  }
387  std::cout << " Convert result in " << filename << std::endl;
388 
390  // Convert a vpImage<unsigned char> to an IplImage
392  std::cout << "** Convert a vpImage<unsigned char> to an IplImage" << std::endl;
393  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
394 
395  /* Read the grey image */
396 
397  // Load a color image from the disk
398  std::cout << " Load " << filename << std::endl;
399  vpImageIo::read(Ig, filename);
400  vpImageConvert::convert(Ig, image);
401  filename = vpIoTools::createFilePath(opath, "Klimt_ipl_grey_cv.pgm");
402  /* Save the the current image */
403 
404  std::cout << " Write " << filename << std::endl;
405  if ((cvSaveImage(filename.c_str(), image)) == 0) {
406  std::cout << " Cannot write image: " << std::endl << filename << std::endl;
407  if (image != NULL)
408  cvReleaseImage(&image);
409  return (-1);
410  }
411  std::cout << " Convert result in " << filename << std::endl;
412 
413  if (image != NULL)
414  cvReleaseImage(&image);
415  double t1 = vpTime::measureTimeMs();
416  std::cout << "== Conversion c interface : " << t1 - t0 << " ms" << std::endl;
417 #endif
418 
419 /* ------------------------------------------------------------------------ */
420 /* conversion for the new c++ interface */
421 /* ------------------------------------------------------------------------ */
422 
423 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
424  chrono.start();
426  // Convert a cv::Mat to a vpImage<vpRGBa>
428  std::cout << "** Convert a cv::Mat to a vpImage<vpRGBa>" << std::endl;
429  cv::Mat imageMat;
430  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
431  std::cout << " Reading the color image with c++ interface of opencv: " << filename << std::endl;
432  imageMat = cv::imread(filename, 1); // force to a three channel color image.
433  if (imageMat.data == NULL) {
434  std::cout << " Cannot read image: " << filename << std::endl;
435  return -1;
436  }
437  vpImageConvert::convert(imageMat, Ic);
438  filename = vpIoTools::createFilePath(opath, "Klimt_color_cvMat.ppm");
439  std::cout << " Resulting image saved in: " << filename << std::endl;
440  vpImageIo::write(Ic, filename);
441 
442  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
443  /* Read the pgm image */
444 
445  std::cout << " Reading the greyscale image with opencv: " << filename << std::endl;
446  imageMat = cv::imread(filename, 0); // forced to grayscale.
447  if (imageMat.data == NULL) {
448  std::cout << " Cannot read image: " << filename << std::endl;
449  return (-1);
450  }
451  vpImageConvert::convert(imageMat, Ic);
452  filename = vpIoTools::createFilePath(opath, "Klimt_grey_cvMat.ppm");
453  std::cout << " Resulting image saved in: " << filename << std::endl;
454  vpImageIo::write(Ic, filename);
455 
457  // Convert a cv::Mat to a vpImage<unsigned char>
459  std::cout << "** Convert a cv::Mat to a vpImage<nsigned char>" << std::endl;
460  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
461 
462  /* Read the color image */
463 
464  std::cout << " Reading the color image with opencv: " << filename << std::endl;
465  imageMat = cv::imread(filename, 1); // force to a three channel color image.
466  if (imageMat.data == NULL) {
467  std::cout << " Cannot read image: " << filename << std::endl;
468  return -1;
469  }
470  vpImageConvert::convert(imageMat, Ig);
471  filename = vpIoTools::createFilePath(opath, "Klimt_color_cvMat.pgm");
472  std::cout << " Resulting image saved in: " << filename << std::endl;
473  vpImageIo::write(Ig, filename);
474 
475  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
476 
477  /* Read the pgm image */
478 
479  std::cout << " Reading the greyscale image with opencv: " << filename << std::endl;
480  imageMat = cv::imread(filename, 0);
481  if (imageMat.data == NULL) {
482  std::cout << " Cannot read image: " << filename << std::endl;
483  return (-1);
484  }
485  vpImageConvert::convert(imageMat, Ig);
486  filename = vpIoTools::createFilePath(opath, "Klimt_grey_cvMat.pgm");
487  std::cout << " Resulting image saved in: " << filename << std::endl;
488  vpImageIo::write(Ig, filename);
489 
490  std::cout << " Convert result in " << filename << std::endl;
491 
493  // Convert a vpImage<vpRGBa> to a cv::Mat
495  std::cout << "** Convert a vpImage<vpRGBa> to a cv::Mat" << std::endl;
496  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
497 
498  /* Read the color image */
499 
500  // Load a color image from the disk
501  std::cout << " Load " << filename << std::endl;
502  vpImageIo::read(Ic, filename);
503  vpImageConvert::convert(Ic, imageMat);
504  filename = vpIoTools::createFilePath(opath, "Klimt_ipl_color_cvMat.ppm");
505  /* Save the the current image */
506  std::cout << " Resulting image saved in: " << filename << std::endl;
507  if (!cv::imwrite(filename, imageMat)) {
508  std::cout << " Cannot write image: " << filename << std::endl;
509  return (-1);
510  }
511  std::cout << " Convert result in " << filename << std::endl;
512 
514  // Convert a vpImage<unsigned char> to a cv::Mat
516  std::cout << "** Convert a vpImage<unsigned char> to a cv::Mat" << std::endl;
517  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
518 
519  /* Read the grey image */
520 
521  // Load a color image from the disk
522  std::cout << " Load " << filename << std::endl;
523  vpImageIo::read(Ig, filename);
524  vpImageConvert::convert(Ig, imageMat);
525  filename = vpIoTools::createFilePath(opath, "Klimt_ipl_grey_cvMat.pgm");
526  /* Save the the current image */
527 
528  std::cout << " Resulting image saved in: " << filename << std::endl;
529  if (!cv::imwrite(filename, imageMat)) {
530  std::cout << " Cannot write image: " << filename << std::endl;
531  return (-1);
532  }
533  std::cout << " Convert result in " << filename << std::endl;
534  chrono.stop();
535  std::cout << "== Conversion c++ interface : " << chrono.getDurationMs() << " ms" << std::endl;
536 #endif
537 #endif
538 
540  // Split a vpImage<vpRGBa> to vpImage<unsigned char>
542  std::cout << "** Split a vpImage<vpRGBa> to vpImage<unsigned char>" << std::endl;
543  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
544 
545  /* Read the color image */
546 
547  // Load a color image from the disk
548  std::cout << " Load " << filename << std::endl;
549  vpImageIo::read(Ic, filename);
550  vpImage<unsigned char> R, G, B, a;
551  vpImageConvert::split(Ic, &R, NULL, &B);
552  chrono.start();
553  for (int iteration = 0; iteration < nbIterations; iteration++) {
554  vpImageConvert::split(Ic, &R, NULL, &B);
555  }
556  chrono.stop();
557 
558  std::cout << " Time for " << nbIterations << " split (ms): " << chrono.getDurationMs() << std::endl;
559 
560  filename = vpIoTools::createFilePath(opath, "Klimt_RChannel.pgm");
561  /* Save the the current image */
562  std::cout << " Save Klimt R channel: " << filename << std::endl;
563  vpImageIo::write(R, filename);
564 
565  filename = vpIoTools::createFilePath(opath, "Klimt_BChannel.pgm");
566  /* Save the the current image */
567  std::cout << " Save Klimt B channel: " << filename << std::endl;
568  vpImageIo::write(B, filename);
569 
571  // Merge 4 vpImage<unsigned char> (RGBa) to vpImage<vpRGBa>
573  std::cout << "** Merge 4 vpImage<unsigned char> (RGBa) to vpImage<vpRGBa>" << std::endl;
574  vpImageConvert::split(Ic, &R, &G, &B, &a);
575  chrono.start();
576  vpImage<vpRGBa> I_merge;
577  for (int iteration = 0; iteration < nbIterations; iteration++) {
578  vpImageConvert::merge(&R, &G, &B, &a, I_merge);
579  }
580  chrono.stop();
581 
582  std::cout << " Time for 1000 merge (ms): " << chrono.getDurationMs() << std::endl;
583 
584  filename = vpIoTools::createFilePath(opath, "Klimt_merge.ppm");
585  std::cout << " Resulting image saved in: " << filename << std::endl;
586  vpImageIo::write(I_merge, filename);
587 
589  // Convert a vpImage<vpRGBa> in RGB color space to a vpImage<vpRGBa> in
590  // HSV color
592  std::cout << "** Convert a vpImage<vpRGBa> in RGB color space to a "
593  "vpImage<vpRGBa> in HSV color"
594  << std::endl;
595  unsigned int size = Ic.getSize();
596  unsigned int w = Ic.getWidth(), h = Ic.getHeight();
597  std::vector<unsigned char> hue(size);
598  std::vector<unsigned char> saturation(size);
599  std::vector<unsigned char> value(size);
600 
601  vpImageConvert::RGBaToHSV((unsigned char *)Ic.bitmap, &hue.front(), &saturation.front(), &value.front(), size);
602  vpImage<unsigned char> I_hue(&hue.front(), h, w);
603  vpImage<unsigned char> I_saturation(&saturation.front(), h, w);
604  vpImage<unsigned char> I_value(&value.front(), h, w);
605  vpImage<vpRGBa> I_HSV;
606  vpImageConvert::merge(&I_hue, &I_saturation, &I_value, NULL, I_HSV);
607 
608  filename = vpIoTools::createFilePath(opath, "Klimt_HSV.ppm");
609  std::cout << " Resulting image saved in: " << filename << std::endl;
610  vpImageIo::write(I_HSV, filename);
611 
612  // Check the conversion RGBa <==> HSV
613  std::vector<double> hue2(size);
614  std::vector<double> saturation2(size);
615  std::vector<double> value2(size);
616  vpImageConvert::RGBaToHSV((unsigned char *)Ic.bitmap, &hue2.front(), &saturation2.front(), &value2.front(), size);
617 
618  std::vector<unsigned char> rgba(size * 4);
619  vpImageConvert::HSVToRGBa(&hue2.front(), &saturation2.front(), &value2.front(), &rgba.front(), size);
620 
621  vpImage<vpRGBa> I_HSV2RGBa(reinterpret_cast<vpRGBa *>(&rgba.front()), h, w);
622  filename = vpIoTools::createFilePath(opath, "Klimt_HSV2RGBa.ppm");
623  std::cout << " Resulting image saved in: " << filename << std::endl;
624  vpImageIo::write(I_HSV2RGBa, filename);
625 
626  for (unsigned int i = 0; i < Ic.getHeight(); i++) {
627  for (unsigned int j = 0; j < Ic.getWidth(); j++) {
628  if (Ic[i][j].R != I_HSV2RGBa[i][j].R || Ic[i][j].G != I_HSV2RGBa[i][j].G || Ic[i][j].B != I_HSV2RGBa[i][j].B) {
629  std::cerr << "Ic[i][j].R=" << static_cast<unsigned>(Ic[i][j].R)
630  << " ; I_HSV2RGBa[i][j].R=" << static_cast<unsigned>(I_HSV2RGBa[i][j].R) << std::endl;
631  std::cerr << "Ic[i][j].G=" << static_cast<unsigned>(Ic[i][j].G)
632  << " ; I_HSV2RGBa[i][j].G=" << static_cast<unsigned>(I_HSV2RGBa[i][j].G) << std::endl;
633  std::cerr << "Ic[i][j].B=" << static_cast<unsigned>(Ic[i][j].B)
634  << " ; I_HSV2RGBa[i][j].B=" << static_cast<unsigned>(I_HSV2RGBa[i][j].B) << std::endl;
635  throw vpException(vpException::fatalError, "Problem with conversion between RGB <==> HSV");
636  }
637  }
638  }
639 
641  // Test construction of a vpImage from an array with copyData==true
643  std::cout << "** Construction of a vpImage from an array with copyData==true" << std::endl;
644  std::vector<unsigned char> rgba2(size*4);
645  std::fill(rgba2.begin(), rgba2.end(), 127);
646  vpImage<vpRGBa> I_copyData(reinterpret_cast<vpRGBa *>(&rgba2.front()), h, w, true);
647 
648  filename = vpIoTools::createFilePath(opath, "I_copyData.ppm");
649  std::cout << " Resulting image saved in: " << filename << std::endl;
650  vpImageIo::write(I_copyData, filename);
651 
652  if (I_copyData.getSize() > 0) {
653  I_copyData[0][0].R = 10;
654  }
655 
656  // Test color conversion
657  {
658  std::cout << "** Test color conversion" << std::endl;
659  // RGBa to Grayscale
660  vpImage<vpRGBa> I_color;
661  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
662  vpImageIo::read(I_color, filename);
663 
664  // RGB to Grayscale conversion
665  std::vector<unsigned char> rgb_array(I_color.getSize()*3);
666  vpImageConvert::RGBaToRGB((unsigned char *)I_color.bitmap, &rgb_array.front(), I_color.getSize());
667 
668 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
669  // BGR cv::Mat to Grayscale
670  std::cout << "\n BGR cv::Mat to Grayscale" << std::endl;
671  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
672  cv::Mat colorMat = cv::imread(filename);
673  std::cout << " colorMat=" << colorMat.cols << "x" << colorMat.rows << std::endl;
674 
675  // Test RGB to Grayscale + Flip
676  std::cout << "\n RGB to Grayscale + Flip" << std::endl;
677  std::vector<unsigned char> rgb2gray_flip_array_sse(I_color.getSize());
678  vpImageConvert::RGBToGrey(&rgb_array.front(), &rgb2gray_flip_array_sse.front(), I_color.getWidth(), I_color.getHeight(), true);
679  vpImage<unsigned char> I_rgb2gray_flip_sse(&rgb2gray_flip_array_sse.front(), I_color.getHeight(), I_color.getWidth());
680 
681  filename = vpIoTools::createFilePath(opath, "I_rgb2gray_flip_sse.pgm");
682  std::cout << " Resulting image saved in: " << filename << std::endl;
683  vpImageIo::write(I_rgb2gray_flip_sse, filename);
684 
685  // Test BGR to Grayscale + Flip
686  std::cout << "\n Conversion BGR to Grayscale + Flip" << std::endl;
687  std::vector<unsigned char> bgr2gray_flip_array_sse(I_color.getSize());
688  vpImage<unsigned char> I_bgr2gray_flip_sse(&bgr2gray_flip_array_sse.front(), I_color.getHeight(), I_color.getWidth());
689  vpImageConvert::convert(colorMat, I_bgr2gray_flip_sse, true);
690 
691  filename = vpIoTools::createFilePath(opath, "I_bgr2gray_flip_sse.pgm");
692  std::cout << " Resulting image saved in: " << filename << std::endl;
693  vpImageIo::write(I_bgr2gray_flip_sse, filename);
694 
695  // Test RGB to Grayscale + Flip + Crop
696  std::cout << "\n RGB to Grayscale + Flip + Crop" << std::endl;
697  cv::Rect rect_roi(11, 17, 347, 449);
698  cv::Mat colorMat_crop = colorMat(rect_roi);
699  cv::Mat colorMat_crop_continous = colorMat(rect_roi).clone();
700  std::cout << " colorMat_crop: " << colorMat_crop.cols << "x" << colorMat_crop.rows << " is continuous? "
701  << colorMat_crop.isContinuous() << std::endl;
702  std::cout << " colorMat_crop_continous: " << colorMat_crop_continous.cols << "x" << colorMat_crop_continous.rows
703  << " is continuous? " << colorMat_crop_continous.isContinuous() << std::endl;
704 
705  vpImage<vpRGBa> I_color_crop((unsigned int)(rect_roi.height - rect_roi.y),
706  (unsigned int)(rect_roi.width - rect_roi.x));
707  for (unsigned int i = (unsigned int)rect_roi.y; i < (unsigned int)rect_roi.height; i++) {
708  for (unsigned int j = (unsigned int)rect_roi.x; j < (unsigned int)rect_roi.width; j++) {
709  I_color_crop[(unsigned int)((int)i - rect_roi.y)][(unsigned int)((int)j - rect_roi.x)] = I_color[i][j];
710  }
711  }
712  filename = vpIoTools::createFilePath(opath, "I_color_crop.ppm");
713  std::cout << " Resulting image saved in: " << filename << std::endl;
714  vpImageIo::write(I_color_crop, filename);
715 
716  std::vector<unsigned char> rgb_array_crop(I_color_crop.getSize() * 3);
717  vpImageConvert::RGBaToRGB((unsigned char *)I_color_crop.bitmap, &rgb_array_crop.front(), I_color_crop.getSize());
718 
719  std::vector<unsigned char> rgb2gray_flip_crop_array_sse(I_color_crop.getSize());
720  vpImageConvert::RGBToGrey(&rgb_array_crop.front(), &rgb2gray_flip_crop_array_sse.front(), I_color_crop.getWidth(),
721  I_color_crop.getHeight(), true);
722  vpImage<unsigned char> I_rgb2gray_flip_crop_sse(&rgb2gray_flip_crop_array_sse.front(), I_color_crop.getHeight(),
723  I_color_crop.getWidth());
724 
725  filename = vpIoTools::createFilePath(opath, "I_rgb2gray_flip_crop_sse.pgm");
726  std::cout << " Resulting image saved in: " << filename << std::endl;
727  vpImageIo::write(I_rgb2gray_flip_crop_sse, filename);
728 
729  // Test BGR to Grayscale + Flip + Crop
730  std::cout << "\n BGR to Grayscale + Flip + Crop" << std::endl;
731  vpImage<unsigned char> I_bgr2gray_flip_crop_sse(I_color_crop.getHeight(), I_color_crop.getWidth());
732  vpImageConvert::convert(colorMat_crop_continous, I_bgr2gray_flip_crop_sse, true);
733 
734  filename = vpIoTools::createFilePath(opath, "I_bgr2gray_flip_crop_sse.pgm");
735  std::cout << " Resulting image saved in: " << filename << std::endl;
736  vpImageIo::write(I_bgr2gray_flip_crop_sse, filename);
737 
738  // Test BGR to Grayscale + Flip + Crop + No continuous Mat
739  std::cout << "\n BGR to Grayscale + Flip + Crop + No continuous Mat" << std::endl;
740  vpImage<unsigned char> I_bgr2gray_flip_crop_no_continuous_sse(I_color_crop.getHeight(), I_color_crop.getWidth());
741  vpImageConvert::convert(colorMat_crop, I_bgr2gray_flip_crop_no_continuous_sse, true);
742 
743  filename = vpIoTools::createFilePath(opath, "I_bgr2gray_flip_crop_no_continuous_sse.pgm");
744  std::cout << " Resulting image saved in: " << filename << std::endl;
745  vpImageIo::write(I_bgr2gray_flip_crop_no_continuous_sse, filename);
746 #endif
747  std::cout << "Test succeed" << std::endl;
748  }
749 
750  return EXIT_SUCCESS;
751  } catch (const vpException &e) {
752  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
753  return EXIT_FAILURE;
754  }
755 }
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:572
double getDurationMs()
Definition: vpTime.cpp:392
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1292
static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int size)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void RGBaToHSV(const unsigned char *rgba, double *hue, double *saturation, double *value, unsigned int size)
Type * bitmap
points toward the bitmap
Definition: vpImage.h:141
error that can be emited by ViSP classes.
Definition: vpException.h:71
static void split(const vpImage< vpRGBa > &src, vpImage< unsigned char > *pR, vpImage< unsigned char > *pG, vpImage< unsigned char > *pB, vpImage< unsigned char > *pa=NULL)
static void YUVToRGB(unsigned char y, unsigned char u, unsigned char v, unsigned char &r, unsigned char &g, unsigned char &b)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
void start(bool reset=true)
Definition: vpTime.cpp:409
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:422
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:445
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1537
void stop()
Definition: vpTime.cpp:424
static std::string getUserName()
Definition: vpIoTools.cpp:318
static void HSVToRGBa(const double *hue, const double *saturation, const double *value, unsigned char *rgba, unsigned int size)
static void merge(const vpImage< unsigned char > *R, const vpImage< unsigned char > *G, const vpImage< unsigned char > *B, const vpImage< unsigned char > *a, vpImage< vpRGBa > &RGBa)
const char * getMessage(void) const
Definition: vpException.cpp:90
unsigned int getHeight() const
Definition: vpImage.h:186
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:243
unsigned int getSize() const
Definition: vpImage.h:225
static void RGBaToRGB(unsigned char *rgba, unsigned char *rgb, unsigned int size)
unsigned int getWidth() const
Definition: vpImage.h:244