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