ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testConversion.cpp
1 /****************************************************************************
2  *
3  * $Id: testConversion.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 /*
65 
66  Print the program options.
67 
68  \param name : Program name.
69  \param badparam : Bad parameter name.
70  \param ipath: Input image path.
71  \param opath : Output image path.
72  \param user : Username.
73 
74  */
75 void usage(const char *name, const char *badparam, std::string ipath,
76  std::string opath, std::string user)
77 {
78  fprintf(stdout, "\n\
79 Test image conversions.\n\
80 \n\
81 SYNOPSIS\n\
82  %s [-i <input image path>] [-o <output image path>]\n\
83  [-h]\n \
84 ", name);
85 
86  fprintf(stdout, "\n\
87 OPTIONS: Default\n\
88  -i <input image path> %s\n\
89  Set image input path.\n\
90  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
91  and \"ViSP-images/Klimt/Klimt.ppm\" images.\n\
92  Setting the VISP_INPUT_IMAGE_PATH environment\n\
93  variable produces the same behaviour than using\n\
94  this option.\n\
95 \n\
96  -o <output image path> %s\n\
97  Set image output path.\n\
98  From this directory, creates the \"%s\"\n\
99  subdirectory depending on the username, where \n\
100  Klimt_grey.pgm and Klimt_color.ppm output images\n\
101  are written.\n\
102 \n\
103  -h\n\
104  Print the help.\n\n",
105  ipath.c_str(), opath.c_str(), user.c_str());
106 
107  if (badparam)
108  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
109 }
110 
123 bool getOptions(int argc, const char **argv,
124  std::string &ipath, std::string &opath, std::string user)
125 {
126  const char *optarg;
127  int c;
128  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
129 
130  switch (c) {
131  case 'i': ipath = optarg; break;
132  case 'o': opath = optarg; break;
133  case 'h': usage(argv[0], NULL, ipath, opath, user); return false; break;
134 
135  default:
136  usage(argv[0], optarg, ipath, opath, user); return false; break;
137  }
138  }
139 
140  if ((c == 1) || (c == -1)) {
141  // standalone param or error
142  usage(argv[0], NULL, ipath, opath, user);
143  std::cerr << "ERROR: " << std::endl;
144  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
145  return false;
146  }
147 
148  return true;
149 }
150 
151 int
152 main(int argc, const char ** argv)
153 {
154 
155  std::string env_ipath;
156  std::string opt_ipath;
157  std::string opt_opath;
158  std::string ipath;
159  std::string opath;
160  std::string filename;
161  std::string username;
162 
163  // Get the VISP_IMAGE_PATH environment variable value
164  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
165  if (ptenv != NULL)
166  env_ipath = ptenv;
167 
168  // Set the default input path
169  if (! env_ipath.empty())
170  ipath = env_ipath;
171 
172  // Set the default output path
173 #ifdef WIN32
174  opt_opath = "C:/temp";
175 #else
176  opt_opath = "/tmp";
177 #endif
178 
179  // Get the user login name
180  vpIoTools::getUserName(username);
181 
182  // Read the command line options
183  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
184  exit (-1);
185  }
186 
187  // Get the option values
188  if (!opt_ipath.empty())
189  ipath = opt_ipath;
190  if (!opt_opath.empty())
191  opath = opt_opath;
192 
193  // Append to the output path string, the login name of the user
194  opath += vpIoTools::path("/") + username;
195 
196  // Test if the output path exist. If no try to create it
197  if (vpIoTools::checkDirectory(opath) == false) {
198  try {
199  // Create the dirname
201  }
202  catch (...) {
203  usage(argv[0], NULL, ipath, opt_opath, username);
204  std::cerr << std::endl
205  << "ERROR:" << std::endl;
206  std::cerr << " Cannot create " << opath << std::endl;
207  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
208  exit(-1);
209  }
210  }
211 
212  // Compare ipath and env_ipath. If they differ, we take into account
213  // the input path comming from the command line option
214  if (opt_ipath.empty()) {
215  if (ipath != env_ipath) {
216  std::cout << std::endl
217  << "WARNING: " << std::endl;
218  std::cout << " Since -i <visp image path=" << ipath << "> "
219  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
220  << " we skip the environment variable." << std::endl;
221  }
222  }
223 
224  // Test if an input path is set
225  if (opt_ipath.empty() && env_ipath.empty()){
226  usage(argv[0], NULL, ipath, opt_opath, username);
227  std::cerr << std::endl
228  << "ERROR:" << std::endl;
229  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
230  << std::endl
231  << " environment variable to specify the location of the " << std::endl
232  << " image path where test images are located." << std::endl << std::endl;
233  exit(-1);
234  }
235 
236  //
237  // Here starts really the test
238  //
239 
240  vpImage<unsigned char> Ig ; // Grey image
241  vpImage<vpRGBa> Ic ; // Color image
242 
243  //-------------------- .pgm -> .ppm
244  vpTRACE("Convert a grey image (.pgm) to a color image (.ppm)");
245  // Load a grey image from the disk
246  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
247  vpCTRACE << "Load " << filename << std::endl;
248  vpImageIo::readPGM(Ig, filename) ;
249  // Create a color image from the grey
250  vpImageConvert::convert(Ig, Ic);
251  filename = opath + vpIoTools::path("/Klimt_color.ppm");
252  vpCTRACE << "Write " << filename << std::endl;
253  vpImageIo::writePPM(Ic, filename) ;
254 
255  //-------------------- .ppm -> .pgm
256  vpTRACE("Convert a color image (.ppm) to a grey image (.pgm)");
257  // Load a color image from the disk
258  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
259  vpCTRACE << "Load " << filename << std::endl;
260  vpImageIo::readPPM(Ic, filename) ;
261  // Create a grey image from the color
262  vpImageConvert::convert(Ic, Ig);
263  filename = opath + vpIoTools::path("/Klimt_grey.pgm");
264  vpCTRACE << "Write " << filename << std::endl;
265  vpImageIo::writePPM(Ig, filename) ;
266 
267  //-------------------- YUV -> RGB
268  unsigned char y=187, u=10, v=30;
269  unsigned char r, g, b;
270 
271  // Convert a YUV pixel value to a RGB value
272  vpImageConvert::YUVToRGB(y, u, v, r, g, b);
273  vpTRACE("y(%d) u(%d) v(%d) = r(%d) g(%d) b(%d)", y, u, v, r, g, b);
274 
275 #ifdef VISP_HAVE_OPENCV
276  double t0 = vpTime::measureTimeMs();
278  // Convert a IplImage to a vpImage<vpRGBa>
280  IplImage* image = NULL;
281  filename = ipath + vpIoTools::path("/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 = opath + vpIoTools::path("/Klimt_color_cv.ppm");
293  /* Save the the current image */
294  vpImageIo::writePPM(Ic, filename) ;
295 
296  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
297 
298  filename = ipath + vpIoTools::path("/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 = opath + vpIoTools::path("/Klimt_grey_cv.ppm");
311  /* Save the the current image */
312  vpImageIo::writePPM(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 = ipath + vpIoTools::path("/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 = opath + vpIoTools::path("/Klimt_color_cv.pgm");
332  /* Save the the current image */
333  vpImageIo::writePGM(Ig, filename) ;
334 
335  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
336 
337  filename = ipath + vpIoTools::path("/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 = opath + vpIoTools::path("/Klimt_grey_cv.pgm");
351  /* Save the the current image */
352  vpImageIo::writePGM(Ig, filename) ;
353 
354  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
355 
357  // Convert a vpImage<vpRGBa> to a IplImage
359  filename = ipath + vpIoTools::path("/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::readPPM(Ic, filename) ;
366  vpImageConvert::convert(Ic, image);
367  filename = opath + vpIoTools::path("/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 = ipath + vpIoTools::path("/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::readPGM(Ig, filename) ;
387  vpImageConvert::convert(Ig, image);
388  filename = opath + vpIoTools::path("/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 
403  /* ------------------------------------------------------------------------ */
404  /* conversion for the new c++ interface */
405  /* ------------------------------------------------------------------------ */
406 
407 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
408  double t2 = vpTime::measureTimeMs();
410  // Convert a cv::Mat to a vpImage<vpRGBa>
412  cv::Mat imageMat;
413  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
414  vpCTRACE << "Reading the color image with c++ interface of opencv: "<< std::endl
415  << filename << std::endl;
416  imageMat = cv::imread(filename, 1);// force to a three channel color image.
417  if(imageMat.data == NULL){
418  vpCTRACE<<"Cannot read image: "<< std::endl << filename << std::endl;
419  return -1;
420  }
421  vpImageConvert::convert(imageMat, Ic);
422  filename = opath + vpIoTools::path("/Klimt_color_cvMat.ppm");
423  /* Save the the current image */
424  vpImageIo::write(Ic, filename) ;
425  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
426 
427  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
428  /* Read the pgm image */
429 
430  vpCTRACE << "Reading the greyscale image with opencv: "<< std::endl
431  << filename << std::endl;
432  imageMat = cv::imread(filename, 0);// forced to grayscale.
433  if(imageMat.data == NULL) {
434  vpCTRACE<<"Cannot read image: "<< std::endl << filename << std::endl;
435  return (-1);
436  }
437  vpImageConvert::convert(imageMat, Ic);
438  filename = opath + vpIoTools::path("/Klimt_grey_cvMat.ppm");
439  /* Save the the current image */
440  vpImageIo::write(Ic, filename) ;
441  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
442 
444  // Convert a cv::Mat to a vpImage<unsigned char>
446  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
447 
448  /* Read the color image */
449 
450  vpCTRACE << "Reading the color image with opencv: "<< std::endl
451  << filename << std::endl;
452  imageMat = cv::imread(filename, 1);// force to a three channel color image.
453  if(imageMat.data == NULL){
454  vpCTRACE<<"Cannot read image: "<< std::endl << filename << std::endl;
455  return -1;
456  }
457  vpImageConvert::convert(imageMat, Ig);
458  filename = opath + vpIoTools::path("/Klimt_color_cvMat.pgm");
459  /* Save the the current image */
460  vpImageIo::write(Ig, filename) ;
461  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
462 
463  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
464 
465  /* Read the pgm image */
466 
467  vpCTRACE << "Reading the greyscale image with opencv: "<< std::endl
468  << filename << std::endl;
469  imageMat = cv::imread(filename, 0);
470  if(imageMat.data == NULL){
471  vpCTRACE<<"Cannot read image: "<< std::endl << filename << std::endl;
472  return (-1);
473  }
474  vpImageConvert::convert(imageMat, Ig);
475  filename = opath + vpIoTools::path("/Klimt_grey_cvMat.pgm");
476  /* Save the the current image */
477  vpImageIo::write(Ig, filename) ;
478 
479  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
480 
482  // Convert a vpImage<vpRGBa> to a cv::Mat
484  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
485 
486  /* Read the color image */
487 
488  // Load a color image from the disk
489  vpCTRACE << "Load " << filename << std::endl;
490  vpImageIo::read(Ic, filename) ;
491  vpImageConvert::convert(Ic, imageMat);
492  filename = opath + vpIoTools::path("/Klimt_ipl_color_cvMat.ppm");
493  /* Save the the current image */
494  vpCTRACE << "Write " << filename << std::endl;
495  if(!cv::imwrite(filename, imageMat)){
496  vpCTRACE<<"Cannot write image: "<< std::endl << filename << std::endl;
497  if(image!=NULL) cvReleaseImage( &image );
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 = ipath + vpIoTools::path("/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 = opath + vpIoTools::path("/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  if(image!=NULL) cvReleaseImage( &image );
520  return (-1);
521  }
522  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
523  double t3 = vpTime::measureTimeMs();
524  std::cout << "Conversion c++ interface : " << t3 - t2 << " ms" << std::endl;
525 #endif
526 #endif
527 
529  // Split a vpImage<vpRGBa> to vpImage<unsigned char>
531  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
532 
533  /* Read the color image */
534 
535  // Load a color image from the disk
536  vpCTRACE << "Load " << filename << std::endl;
537  vpImageIo::readPPM(Ic, filename) ;
538  vpImage<unsigned char> R,G,B,a;
539  vpImageConvert::split(Ic, &R,NULL,&B);
540  double begintime = vpTime::measureTimeMs();
541  for(int i=0; i<1000;i++){
542  vpImageConvert::split(Ic, &R,NULL,&B);
543  }
544  double endtime = vpTime::measureTimeMs();
545 
546  std::cout<<"Time for 1000 split (ms): "<< endtime - begintime <<std::endl;
547 
548  filename = opath + vpIoTools::path("/Klimt_RChannel.pgm");
549  /* Save the the current image */
550  vpCTRACE << "Write " << filename << std::endl;
551  vpImageIo::writePGM(R, filename) ;
552  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
553 
554  filename = opath + vpIoTools::path("/Klimt_BChannel.pgm");
555  /* Save the the current image */
556  vpCTRACE << "Write " << filename << std::endl;
557  vpImageIo::writePGM(B, filename) ;
558  vpCTRACE<< "Convert result in "<<std::endl<< filename << std::endl;
559 
560 }