ViSP  2.10.0
testConversion.cpp
1 /****************************************************************************
2  *
3  * $Id: testConversion.cpp 5023 2014-12-03 16:07:48Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Test for image conversions.
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
42 #include <stdlib.h>
43 
44 #include <visp/vpConfig.h>
45 #include <visp/vpImage.h>
46 #include <visp/vpImageIo.h>
47 #include <visp/vpImageConvert.h>
48 #include <visp/vpParseArgv.h>
49 #include <visp/vpIoTools.h>
50 #include <visp/vpDebug.h>
51 #include <visp/vpTime.h>
52 
53 
61 // List of allowed command line options
62 #define GETOPTARGS "i:o:h"
63 
64 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user);
65 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user);
66 
67 /*
68 
69  Print the program options.
70 
71  \param name : Program name.
72  \param badparam : Bad parameter name.
73  \param ipath: Input image path.
74  \param opath : Output image path.
75  \param user : Username.
76 
77  */
78 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
79 {
80  fprintf(stdout, "\n\
81 Test image conversions.\n\
82 \n\
83 SYNOPSIS\n\
84  %s [-i <input image path>] [-o <output image path>]\n\
85  [-h]\n \
86 ", name);
87 
88  fprintf(stdout, "\n\
89 OPTIONS: Default\n\
90  -i <input image path> %s\n\
91  Set image input path.\n\
92  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
93  and \"ViSP-images/Klimt/Klimt.ppm\" images.\n\
94  Setting the VISP_INPUT_IMAGE_PATH environment\n\
95  variable produces the same behaviour than using\n\
96  this option.\n\
97 \n\
98  -o <output image path> %s\n\
99  Set image output path.\n\
100  From this directory, creates the \"%s\"\n\
101  subdirectory depending on the username, where \n\
102  Klimt_grey.pgm and Klimt_color.ppm output images\n\
103  are written.\n\
104 \n\
105  -h\n\
106  Print the help.\n\n",
107  ipath.c_str(), opath.c_str(), user.c_str());
108 
109  if (badparam)
110  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
111 }
112 
125 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user)
126 {
127  const char *optarg_;
128  int c;
129  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
130 
131  switch (c) {
132  case 'i': ipath = optarg_; break;
133  case 'o': opath = optarg_; break;
134  case 'h': usage(argv[0], NULL, ipath, opath, user); return false; break;
135 
136  default:
137  usage(argv[0], optarg_, ipath, opath, user); return false; break;
138  }
139  }
140 
141  if ((c == 1) || (c == -1)) {
142  // standalone param or error
143  usage(argv[0], NULL, ipath, opath, user);
144  std::cerr << "ERROR: " << std::endl;
145  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
146  return false;
147  }
148 
149  return true;
150 }
151 
152 int
153 main(int argc, const char ** argv)
154 {
155  try {
156  std::string env_ipath;
157  std::string opt_ipath;
158  std::string opt_opath;
159  std::string ipath;
160  std::string opath;
161  std::string filename;
162  std::string username;
163 
164  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
165  env_ipath = vpIoTools::getViSPImagesDataPath();
166 
167  // Set the default input path
168  if (! env_ipath.empty())
169  ipath = env_ipath;
170 
171  // Set the default output path
172 #if defined(_WIN32)
173  opt_opath = "C:/temp";
174 #else
175  opt_opath = "/tmp";
176 #endif
177 
178  // Get the user login name
179  vpIoTools::getUserName(username);
180 
181  // Read the command line options
182  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
183  exit (-1);
184  }
185 
186  // Get the option values
187  if (!opt_ipath.empty())
188  ipath = opt_ipath;
189  if (!opt_opath.empty())
190  opath = opt_opath;
191 
192  // Append to the output path string, the login name of the user
193  opath = vpIoTools::createFilePath(opath, username);
194 
195  // Test if the output path exist. If no try to create it
196  if (vpIoTools::checkDirectory(opath) == false) {
197  try {
198  // Create the dirname
200  }
201  catch (...) {
202  usage(argv[0], NULL, ipath, opt_opath, username);
203  std::cerr << std::endl
204  << "ERROR:" << std::endl;
205  std::cerr << " Cannot create " << opath << std::endl;
206  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
207  exit(-1);
208  }
209  }
210 
211  // Compare ipath and env_ipath. If they differ, we take into account
212  // the input path comming from the command line option
213  if (opt_ipath.empty()) {
214  if (ipath != env_ipath) {
215  std::cout << std::endl
216  << "WARNING: " << std::endl;
217  std::cout << " Since -i <visp image path=" << ipath << "> "
218  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
219  << " we skip the environment variable." << std::endl;
220  }
221  }
222 
223  // Test if an input path is set
224  if (opt_ipath.empty() && env_ipath.empty()){
225  usage(argv[0], NULL, ipath, opt_opath, username);
226  std::cerr << std::endl
227  << "ERROR:" << std::endl;
228  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
229  << std::endl
230  << " environment variable to specify the location of the " << std::endl
231  << " image path where test images are located." << std::endl << std::endl;
232  exit(-1);
233  }
234 
235  //
236  // Here starts really the test
237  //
238 
239  vpImage<unsigned char> Ig ; // Grey image
240  vpImage<vpRGBa> Ic ; // Color image
241 
242  //-------------------- .pgm -> .ppm
243  vpTRACE("Convert a grey image (.pgm) to a color image (.ppm)");
244  // Load a grey image from the disk
245  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.pgm");
246  vpCTRACE << "Load " << filename << std::endl;
247  vpImageIo::read(Ig, filename) ;
248  // Create a color image from the grey
249  vpImageConvert::convert(Ig, Ic);
250  filename = vpIoTools::createFilePath(opath, "Klimt_color.ppm");
251  vpCTRACE << "Write " << filename << std::endl;
252  vpImageIo::write(Ic, filename) ;
253 
254  //-------------------- .ppm -> .pgm
255  vpTRACE("Convert a color image (.ppm) to a grey image (.pgm)");
256  // Load a color image from the disk
257  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
258  vpCTRACE << "Load " << filename << std::endl;
259  vpImageIo::read(Ic, filename) ;
260  // Create a grey image from the color
261  vpImageConvert::convert(Ic, Ig);
262  filename = vpIoTools::createFilePath(opath, "Klimt_grey.pgm");
263  vpCTRACE << "Write " << filename << std::endl;
264  vpImageIo::write(Ig, filename) ;
265 
266  //-------------------- YUV -> RGB
267  unsigned char y=187, u=10, v=30;
268  unsigned char r, g, b;
269 
270  // Convert a YUV pixel value to a RGB value
271  vpImageConvert::YUVToRGB(y, u, v, r, g, b);
272  vpTRACE("y(%d) u(%d) v(%d) = r(%d) g(%d) b(%d)", y, u, v, r, g, b);
273 
274 #ifdef VISP_HAVE_OPENCV
275 #if VISP_HAVE_OPENCV_VERSION < 0x020408
276  double t0 = vpTime::measureTimeMs();
278  // Convert a IplImage to a vpImage<vpRGBa>
280  IplImage* image = NULL;
281  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
282 
283  /* Read the color image */
284 
285  vpCTRACE << "Reading the color image with opencv: "<< std::endl
286  << filename << std::endl;
287  if((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR)) == NULL) {
288  vpCTRACE<<"Cannot read image: "<< std::endl << filename << std::endl;
289  return (-1);
290  }
291  vpImageConvert::convert(image, Ic);
292  filename = vpIoTools::createFilePath(opath, "Klimt_color_cv.ppm");
293  /* Save the the current image */
294  vpImageIo::write(Ic, filename) ;
295 
296  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
297 
298  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.pgm");
299 
300  /* Read the pgm image */
301 
302  vpCTRACE << "Reading the greyscale image with opencv: "<< std::endl
303  << filename << std::endl;
304  if(image!=NULL) cvReleaseImage( &image );
305  if((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE)) == NULL) {
306  vpCTRACE<<"Cannot read image: "<< std::endl << filename << std::endl;
307  return (-1);
308  }
309  vpImageConvert::convert(image, Ic);
310  filename = vpIoTools::createFilePath(opath, "Klimt_grey_cv.ppm");
311  /* Save the the current image */
312  vpImageIo::write(Ic, filename) ;
313 
314  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
315 
317  // Convert a IplImage to a vpImage<unsigned char>
319  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
320 
321  /* Read the color image */
322 
323  vpCTRACE << "Reading the color image with opencv: "<< std::endl
324  << filename << std::endl;
325  if(image!=NULL) cvReleaseImage( &image );
326  if((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR)) == NULL) {
327  vpCTRACE<<"Cannot read image: "<< std::endl << filename << std::endl;
328  return (-1);
329  }
330  vpImageConvert::convert(image, Ig);
331  filename = vpIoTools::createFilePath(opath, "Klimt_color_cv.pgm");
332  /* Save the the current image */
333  vpImageIo::write(Ig, filename) ;
334 
335  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
336 
337  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.pgm");
338 
339  /* Read the pgm image */
340 
341  vpCTRACE << "Reading the greyscale image with opencv: "<< std::endl
342  << filename << std::endl;
343  if(image!=NULL) cvReleaseImage( &image );
344  if((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE)) == NULL) {
345  vpCTRACE<<"Cannot read image: "<< std::endl << filename << std::endl;
346 
347  return (-1);
348  }
349  vpImageConvert::convert(image, Ig);
350  filename = vpIoTools::createFilePath(opath, "Klimt_grey_cv.pgm");
351  /* Save the the current image */
352  vpImageIo::write(Ig, filename) ;
353 
354  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
355 
357  // Convert a vpImage<vpRGBa> to a IplImage
359  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
360 
361  /* Read the color image */
362 
363  // Load a color image from the disk
364  vpCTRACE << "Load " << filename << std::endl;
365  vpImageIo::read(Ic, filename) ;
366  vpImageConvert::convert(Ic, image);
367  filename = vpIoTools::createFilePath(opath, "Klimt_ipl_color_cv.ppm");
368  /* Save the the current image */
369  vpCTRACE << "Write " << filename << std::endl;
370  if((cvSaveImage(filename.c_str(), image)) == 0) {
371  vpCTRACE<<"Cannot write image: "<< std::endl << filename << std::endl;
372  if(image!=NULL) cvReleaseImage( &image );
373  return (-1);
374  }
375  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
376 
378  // Convert a IplImage to a vpImage<unsigned char>
380  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.pgm");
381 
382  /* Read the grey image */
383 
384  // Load a color image from the disk
385  vpCTRACE << "Load " << filename << std::endl;
386  vpImageIo::read(Ig, filename) ;
387  vpImageConvert::convert(Ig, image);
388  filename = vpIoTools::createFilePath(opath, "Klimt_ipl_grey_cv.pgm");
389  /* Save the the current image */
390 
391  vpCTRACE << "Write " << filename << std::endl;
392  if((cvSaveImage(filename.c_str(), image)) == 0) {
393  vpCTRACE<<"Cannot write image: "<< std::endl << filename << std::endl;
394  if(image!=NULL) cvReleaseImage( &image );
395  return (-1);
396  }
397  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
398 
399  if(image!=NULL) cvReleaseImage( &image );
400  double t1 = vpTime::measureTimeMs();
401  std::cout << "Conversion c interface : " << t1 - t0 << " ms" << std::endl;
402 #endif
403 
404  /* ------------------------------------------------------------------------ */
405  /* conversion for the new c++ interface */
406  /* ------------------------------------------------------------------------ */
407 
408 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
409  double t2 = vpTime::measureTimeMs();
411  // Convert a cv::Mat to a vpImage<vpRGBa>
413  cv::Mat imageMat;
414  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
415  vpCTRACE << "Reading the color image with c++ interface of opencv: "<< std::endl
416  << filename << std::endl;
417  imageMat = cv::imread(filename, 1);// force to a three channel color image.
418  if(imageMat.data == NULL){
419  vpCTRACE<<"Cannot read image: "<< std::endl << filename << std::endl;
420  return -1;
421  }
422  vpImageConvert::convert(imageMat, Ic);
423  filename = vpIoTools::createFilePath(opath, "Klimt_color_cvMat.ppm");
424  /* Save the the current image */
425  vpImageIo::write(Ic, filename) ;
426  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
427 
428  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.pgm");
429  /* Read the pgm image */
430 
431  vpCTRACE << "Reading the greyscale image with opencv: "<< std::endl
432  << filename << std::endl;
433  imageMat = cv::imread(filename, 0);// forced to grayscale.
434  if(imageMat.data == NULL) {
435  vpCTRACE<<"Cannot read image: "<< std::endl << filename << std::endl;
436  return (-1);
437  }
438  vpImageConvert::convert(imageMat, Ic);
439  filename = vpIoTools::createFilePath(opath, "Klimt_grey_cvMat.ppm");
440  /* Save the the current image */
441  vpImageIo::write(Ic, filename) ;
442  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
443 
445  // Convert a cv::Mat to a vpImage<unsigned char>
447  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
448 
449  /* Read the color image */
450 
451  vpCTRACE << "Reading the color image with opencv: "<< std::endl
452  << filename << std::endl;
453  imageMat = cv::imread(filename, 1);// force to a three channel color image.
454  if(imageMat.data == NULL){
455  vpCTRACE<<"Cannot read image: "<< std::endl << filename << std::endl;
456  return -1;
457  }
458  vpImageConvert::convert(imageMat, Ig);
459  filename = vpIoTools::createFilePath(opath, "Klimt_color_cvMat.pgm");
460  /* Save the the current image */
461  vpImageIo::write(Ig, filename) ;
462  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
463 
464  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.pgm");
465 
466  /* Read the pgm image */
467 
468  vpCTRACE << "Reading the greyscale image with opencv: "<< std::endl
469  << filename << std::endl;
470  imageMat = cv::imread(filename, 0);
471  if(imageMat.data == NULL){
472  vpCTRACE<<"Cannot read image: "<< std::endl << filename << std::endl;
473  return (-1);
474  }
475  vpImageConvert::convert(imageMat, Ig);
476  filename = vpIoTools::createFilePath(opath, "Klimt_grey_cvMat.pgm");
477  /* Save the the current image */
478  vpImageIo::write(Ig, filename) ;
479 
480  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
481 
483  // Convert a vpImage<vpRGBa> to a cv::Mat
485  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
486 
487  /* Read the color image */
488 
489  // Load a color image from the disk
490  vpCTRACE << "Load " << filename << std::endl;
491  vpImageIo::read(Ic, filename) ;
492  vpImageConvert::convert(Ic, imageMat);
493  filename = vpIoTools::createFilePath(opath, "Klimt_ipl_color_cvMat.ppm");
494  /* Save the the current image */
495  vpCTRACE << "Write " << filename << std::endl;
496  if(!cv::imwrite(filename, imageMat)){
497  vpCTRACE<<"Cannot write image: "<< std::endl << filename << std::endl;
498  return (-1);
499  }
500  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
501 
503  // Convert a IplImage to a vpImage<unsigned char>
505  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.pgm");
506 
507  /* Read the grey image */
508 
509  // Load a color image from the disk
510  vpCTRACE << "Load " << filename << std::endl;
511  vpImageIo::read(Ig, filename);
512  vpImageConvert::convert(Ig, imageMat);
513  filename = vpIoTools::createFilePath(opath, "Klimt_ipl_grey_cvMat.pgm");
514  /* Save the the current image */
515 
516  vpCTRACE << "Write " << filename << std::endl;
517  if(!cv::imwrite(filename, imageMat)){
518  vpCTRACE<<"Cannot write image: "<< std::endl << filename << std::endl;
519  return (-1);
520  }
521  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
522  double t3 = vpTime::measureTimeMs();
523  std::cout << "Conversion c++ interface : " << t3 - t2 << " ms" << std::endl;
524 #endif
525 #endif
526 
528  // Split a vpImage<vpRGBa> to vpImage<unsigned char>
530  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
531 
532  /* Read the color image */
533 
534  // Load a color image from the disk
535  vpCTRACE << "Load " << filename << std::endl;
536  vpImageIo::read(Ic, filename) ;
537  vpImage<unsigned char> R,G,B,a;
538  vpImageConvert::split(Ic, &R,NULL,&B);
539  double begintime = vpTime::measureTimeMs();
540  for(int i=0; i<1000;i++){
541  vpImageConvert::split(Ic, &R,NULL,&B);
542  }
543  double endtime = vpTime::measureTimeMs();
544 
545  std::cout<<"Time for 1000 split (ms): "<< endtime - begintime <<std::endl;
546 
547  filename = vpIoTools::createFilePath(opath, "Klimt_RChannel.pgm");
548  /* Save the the current image */
549  vpCTRACE << "Write " << filename << std::endl;
550  vpImageIo::write(R, filename) ;
551  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
552 
553  filename = vpIoTools::createFilePath(opath, "Klimt_BChannel.pgm");
554  /* Save the the current image */
555  vpCTRACE << "Write " << filename << std::endl;
556  vpImageIo::write(B, filename) ;
557  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
558  return 0;
559  }
560  catch(vpException e) {
561  std::cout << "Catch an exception: " << e << std::endl;
562  return 1;
563  }
564 }
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:476
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:315
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1071
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
#define vpTRACE
Definition: vpDebug.h:418
error that can be emited by ViSP classes.
Definition: vpException.h:76
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)
static double measureTimeMs()
Definition: vpTime.cpp:86
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
#define vpCTRACE
Definition: vpDebug.h:341
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:384
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1245
static std::string getUserName()
Definition: vpIoTools.cpp:141
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278