ViSP  2.9.0
testConversion.cpp
1 /****************************************************************************
2  *
3  * $Id: testConversion.cpp 4658 2014-02-09 09:50:14Z 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_IMAGE_PATH environment variable value
165  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
166  if (ptenv != NULL)
167  env_ipath = ptenv;
168 
169  // Set the default input path
170  if (! env_ipath.empty())
171  ipath = env_ipath;
172 
173  // Set the default output path
174 #if defined(_WIN32)
175  opt_opath = "C:/temp";
176 #else
177  opt_opath = "/tmp";
178 #endif
179 
180  // Get the user login name
181  vpIoTools::getUserName(username);
182 
183  // Read the command line options
184  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
185  exit (-1);
186  }
187 
188  // Get the option values
189  if (!opt_ipath.empty())
190  ipath = opt_ipath;
191  if (!opt_opath.empty())
192  opath = opt_opath;
193 
194  // Append to the output path string, the login name of the user
195  opath += vpIoTools::path("/") + username;
196 
197  // Test if the output path exist. If no try to create it
198  if (vpIoTools::checkDirectory(opath) == false) {
199  try {
200  // Create the dirname
202  }
203  catch (...) {
204  usage(argv[0], NULL, ipath, opt_opath, username);
205  std::cerr << std::endl
206  << "ERROR:" << std::endl;
207  std::cerr << " Cannot create " << opath << std::endl;
208  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
209  exit(-1);
210  }
211  }
212 
213  // Compare ipath and env_ipath. If they differ, we take into account
214  // the input path comming from the command line option
215  if (opt_ipath.empty()) {
216  if (ipath != env_ipath) {
217  std::cout << std::endl
218  << "WARNING: " << std::endl;
219  std::cout << " Since -i <visp image path=" << ipath << "> "
220  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
221  << " we skip the environment variable." << std::endl;
222  }
223  }
224 
225  // Test if an input path is set
226  if (opt_ipath.empty() && env_ipath.empty()){
227  usage(argv[0], NULL, ipath, opt_opath, username);
228  std::cerr << std::endl
229  << "ERROR:" << std::endl;
230  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
231  << std::endl
232  << " environment variable to specify the location of the " << std::endl
233  << " image path where test images are located." << std::endl << std::endl;
234  exit(-1);
235  }
236 
237  //
238  // Here starts really the test
239  //
240 
241  vpImage<unsigned char> Ig ; // Grey image
242  vpImage<vpRGBa> Ic ; // Color image
243 
244  //-------------------- .pgm -> .ppm
245  vpTRACE("Convert a grey image (.pgm) to a color image (.ppm)");
246  // Load a grey image from the disk
247  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
248  vpCTRACE << "Load " << filename << std::endl;
249  vpImageIo::read(Ig, filename) ;
250  // Create a color image from the grey
251  vpImageConvert::convert(Ig, Ic);
252  filename = opath + vpIoTools::path("/Klimt_color.ppm");
253  vpCTRACE << "Write " << filename << std::endl;
254  vpImageIo::write(Ic, filename) ;
255 
256  //-------------------- .ppm -> .pgm
257  vpTRACE("Convert a color image (.ppm) to a grey image (.pgm)");
258  // Load a color image from the disk
259  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
260  vpCTRACE << "Load " << filename << std::endl;
261  vpImageIo::read(Ic, filename) ;
262  // Create a grey image from the color
263  vpImageConvert::convert(Ic, Ig);
264  filename = opath + vpIoTools::path("/Klimt_grey.pgm");
265  vpCTRACE << "Write " << filename << std::endl;
266  vpImageIo::write(Ig, filename) ;
267 
268  //-------------------- YUV -> RGB
269  unsigned char y=187, u=10, v=30;
270  unsigned char r, g, b;
271 
272  // Convert a YUV pixel value to a RGB value
273  vpImageConvert::YUVToRGB(y, u, v, r, g, b);
274  vpTRACE("y(%d) u(%d) v(%d) = r(%d) g(%d) b(%d)", y, u, v, r, g, b);
275 
276 #ifdef VISP_HAVE_OPENCV
277  double t0 = vpTime::measureTimeMs();
279  // Convert a IplImage to a vpImage<vpRGBa>
281  IplImage* image = NULL;
282  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
283 
284  /* Read the color image */
285 
286  vpCTRACE << "Reading the color image with opencv: "<< std::endl
287  << filename << std::endl;
288  if((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR)) == NULL) {
289  vpCTRACE<<"Cannot read image: "<< std::endl << filename << std::endl;
290  return (-1);
291  }
292  vpImageConvert::convert(image, Ic);
293  filename = opath + vpIoTools::path("/Klimt_color_cv.ppm");
294  /* Save the the current image */
295  vpImageIo::write(Ic, filename) ;
296 
297  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
298 
299  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
300 
301  /* Read the pgm image */
302 
303  vpCTRACE << "Reading the greyscale image with opencv: "<< std::endl
304  << filename << std::endl;
305  if(image!=NULL) cvReleaseImage( &image );
306  if((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE)) == NULL) {
307  vpCTRACE<<"Cannot read image: "<< std::endl << filename << std::endl;
308  return (-1);
309  }
310  vpImageConvert::convert(image, Ic);
311  filename = opath + vpIoTools::path("/Klimt_grey_cv.ppm");
312  /* Save the the current image */
313  vpImageIo::write(Ic, filename) ;
314 
315  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
316 
318  // Convert a IplImage to a vpImage<unsigned char>
320  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
321 
322  /* Read the color image */
323 
324  vpCTRACE << "Reading the color image with opencv: "<< std::endl
325  << filename << std::endl;
326  if(image!=NULL) cvReleaseImage( &image );
327  if((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR)) == NULL) {
328  vpCTRACE<<"Cannot read image: "<< std::endl << filename << std::endl;
329  return (-1);
330  }
331  vpImageConvert::convert(image, Ig);
332  filename = opath + vpIoTools::path("/Klimt_color_cv.pgm");
333  /* Save the the current image */
334  vpImageIo::write(Ig, filename) ;
335 
336  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
337 
338  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
339 
340  /* Read the pgm image */
341 
342  vpCTRACE << "Reading the greyscale image with opencv: "<< std::endl
343  << filename << std::endl;
344  if(image!=NULL) cvReleaseImage( &image );
345  if((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE)) == NULL) {
346  vpCTRACE<<"Cannot read image: "<< std::endl << filename << std::endl;
347 
348  return (-1);
349  }
350  vpImageConvert::convert(image, Ig);
351  filename = opath + vpIoTools::path("/Klimt_grey_cv.pgm");
352  /* Save the the current image */
353  vpImageIo::write(Ig, filename) ;
354 
355  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
356 
358  // Convert a vpImage<vpRGBa> to a IplImage
360  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
361 
362  /* Read the color image */
363 
364  // Load a color image from the disk
365  vpCTRACE << "Load " << filename << std::endl;
366  vpImageIo::read(Ic, filename) ;
367  vpImageConvert::convert(Ic, image);
368  filename = opath + vpIoTools::path("/Klimt_ipl_color_cv.ppm");
369  /* Save the the current image */
370  vpCTRACE << "Write " << filename << std::endl;
371  if((cvSaveImage(filename.c_str(), image)) == 0) {
372  vpCTRACE<<"Cannot write image: "<< std::endl << filename << std::endl;
373  if(image!=NULL) cvReleaseImage( &image );
374  return (-1);
375  }
376  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
377 
379  // Convert a IplImage to a vpImage<unsigned char>
381  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
382 
383  /* Read the grey image */
384 
385  // Load a color image from the disk
386  vpCTRACE << "Load " << filename << std::endl;
387  vpImageIo::read(Ig, filename) ;
388  vpImageConvert::convert(Ig, image);
389  filename = opath + vpIoTools::path("/Klimt_ipl_grey_cv.pgm");
390  /* Save the the current image */
391 
392  vpCTRACE << "Write " << filename << std::endl;
393  if((cvSaveImage(filename.c_str(), image)) == 0) {
394  vpCTRACE<<"Cannot write image: "<< std::endl << filename << std::endl;
395  if(image!=NULL) cvReleaseImage( &image );
396  return (-1);
397  }
398  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
399 
400  if(image!=NULL) cvReleaseImage( &image );
401  double t1 = vpTime::measureTimeMs();
402  std::cout << "Conversion c interface : " << t1 - t0 << " ms" << std::endl;
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 = ipath + vpIoTools::path("/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 = opath + vpIoTools::path("/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 = ipath + vpIoTools::path("/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 = opath + vpIoTools::path("/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 = ipath + vpIoTools::path("/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 = opath + vpIoTools::path("/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 = ipath + vpIoTools::path("/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 = opath + vpIoTools::path("/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 = ipath + vpIoTools::path("/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 = opath + vpIoTools::path("/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  if(image!=NULL) cvReleaseImage( &image );
499  return (-1);
500  }
501  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
502 
504  // Convert a IplImage to a vpImage<unsigned char>
506  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
507 
508  /* Read the grey image */
509 
510  // Load a color image from the disk
511  vpCTRACE << "Load " << filename << std::endl;
512  vpImageIo::read(Ig, filename);
513  vpImageConvert::convert(Ig, imageMat);
514  filename = opath + vpIoTools::path("/Klimt_ipl_grey_cvMat.pgm");
515  /* Save the the current image */
516 
517  vpCTRACE << "Write " << filename << std::endl;
518  if(!cv::imwrite(filename, imageMat)){
519  vpCTRACE<<"Cannot write image: "<< std::endl << filename << std::endl;
520  if(image!=NULL) cvReleaseImage( &image );
521  return (-1);
522  }
523  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
524  double t3 = vpTime::measureTimeMs();
525  std::cout << "Conversion c++ interface : " << t3 - t2 << " ms" << std::endl;
526 #endif
527 #endif
528 
530  // Split a vpImage<vpRGBa> to vpImage<unsigned char>
532  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
533 
534  /* Read the color image */
535 
536  // Load a color image from the disk
537  vpCTRACE << "Load " << filename << std::endl;
538  vpImageIo::read(Ic, filename) ;
539  vpImage<unsigned char> R,G,B,a;
540  vpImageConvert::split(Ic, &R,NULL,&B);
541  double begintime = vpTime::measureTimeMs();
542  for(int i=0; i<1000;i++){
543  vpImageConvert::split(Ic, &R,NULL,&B);
544  }
545  double endtime = vpTime::measureTimeMs();
546 
547  std::cout<<"Time for 1000 split (ms): "<< endtime - begintime <<std::endl;
548 
549  filename = opath + vpIoTools::path("/Klimt_RChannel.pgm");
550  /* Save the the current image */
551  vpCTRACE << "Write " << filename << std::endl;
552  vpImageIo::write(R, filename) ;
553  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
554 
555  filename = opath + vpIoTools::path("/Klimt_BChannel.pgm");
556  /* Save the the current image */
557  vpCTRACE << "Write " << filename << std::endl;
558  vpImageIo::write(B, filename) ;
559  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
560  return 0;
561  }
562  catch(vpException e) {
563  std::cout << "Catch an exception: " << e << std::endl;
564  return 1;
565  }
566 }
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:452
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
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 std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
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:79
#define vpCTRACE
Definition: vpDebug.h:341
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
static std::string getUserName()
Definition: vpIoTools.cpp:140
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278