Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testImageComparison.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Test the comparison of two vpImage objects of the same type.
32  *
33  * Authors:
34  * Souriya Trinh
35  *
36  *****************************************************************************/
43 #include <visp3/core/vpImage.h>
44 #include <visp3/core/vpIoTools.h>
45 #include <visp3/io/vpImageIo.h>
46 #include <visp3/io/vpParseArgv.h>
47 
48 // List of allowed command line options
49 #define GETOPTARGS "cdi:h"
50 
51 void usage(const char *name, const char *badparam, std::string ipath);
52 bool getOptions(int argc, const char **argv, std::string &ipath);
53 
54 
55 /*
56  Print the program options.
57 
58  \param name : Program name.
59  \param badparam : Bad parameter name.
60  \param ipath: Input image path.
61  */
62 void usage(const char *name, const char *badparam, std::string ipath)
63 {
64  fprintf(stdout, "\n\
65 Test the comparison of two vpImage objects of the same type.\n\
66 \n\
67 SYNOPSIS\n\
68  %s [-i <input image path>]\n\
69  [-h]\n \
70 ", name);
71 
72  fprintf(stdout, "\n\
73 OPTIONS: Default\n\
74  -i <input image path> %s\n\
75  Set image input path.\n\
76  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
77  and \"ViSP-images/Klimt/Klimt.ppm\" images.\n\
78  Setting the VISP_INPUT_IMAGE_PATH environment\n\
79  variable produces the same behaviour than using\n\
80  this option.\n\
81 \n\
82  -h\n\
83  Print the help.\n\n",
84  ipath.c_str());
85 
86  if (badparam)
87  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
88 }
89 
98 bool getOptions(int argc, const char **argv, std::string &ipath)
99 {
100  const char *optarg_;
101  int c;
102  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
103 
104  switch (c) {
105  case 'i': ipath = optarg_; break;
106  case 'h': usage(argv[0], NULL, ipath); return false; break;
107 
108  case 'c':
109  case 'd':
110  break;
111 
112  default:
113  usage(argv[0], optarg_, ipath); return false; break;
114  }
115  }
116 
117  if ((c == 1) || (c == -1)) {
118  // standalone param or error
119  usage(argv[0], NULL, ipath);
120  std::cerr << "ERROR: " << std::endl;
121  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
122  return false;
123  }
124 
125  return true;
126 }
127 
128 int main(int argc, const char ** argv) {
129  try {
130  std::string env_ipath;
131  std::string opt_ipath;
132  std::string ipath;
133  std::string filename;
134  std::string username;
135 
136  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
137  env_ipath = vpIoTools::getViSPImagesDataPath();
138 
139  // Set the default input path
140  if (! env_ipath.empty()) {
141  ipath = env_ipath;
142  }
143 
144  // Get the user login name
145  vpIoTools::getUserName(username);
146 
147  // Read the command line options
148  if (getOptions(argc, argv, opt_ipath) == false) {
149  exit (-1);
150  }
151 
152  // Get the option values
153  if (!opt_ipath.empty()) {
154  ipath = opt_ipath;
155  }
156 
157  // Compare ipath and env_ipath. If they differ, we take into account
158  // the input path comming from the command line option
159  if (!opt_ipath.empty() && !env_ipath.empty()) {
160  if (ipath != env_ipath) {
161  std::cout << std::endl
162  << "WARNING: " << std::endl;
163  std::cout << " Since -i <visp image path=" << ipath << "> "
164  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
165  << " we skip the environment variable." << std::endl;
166  }
167  }
168 
169  // Test if an input path is set
170  if (opt_ipath.empty() && env_ipath.empty()) {
171  usage(argv[0], NULL, ipath);
172  std::cerr << std::endl
173  << "ERROR:" << std::endl;
174  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
175  << std::endl
176  << " environment variable to specify the location of the " << std::endl
177  << " image path where test images are located." << std::endl << std::endl;
178  exit(EXIT_FAILURE);
179  }
180 
181  //
182  // Here starts really the test
183  //
184 
185  //Load grayscale Klimt
186  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.pgm");
187 
188  vpImage<unsigned char> I_Klimt1, I_Klimt2;
189  vpImageIo::read(I_Klimt1, filename);
190  vpImageIo::read(I_Klimt2, filename);
191 
192  std::cout << "\nI_Klimt1=" << I_Klimt1.getWidth() << "x" << I_Klimt1.getHeight() << std::endl;
193  std::cout << "I_Klimt2=" << I_Klimt2.getWidth() << "x" << I_Klimt2.getHeight() << std::endl;
194 
195  std::cout << "\nThe two grayscale images are equal." << std::endl;
196  std::cout << "(I_Klimt1 == I_Klimt2)=" << (I_Klimt1 == I_Klimt2) << std::endl;
197  std::cout << "(I_Klimt1 != I_Klimt2)=" << (I_Klimt1 != I_Klimt2) << std::endl;
198 
199  //The two images should be equal
200  if ( !(I_Klimt1 == I_Klimt2) || (I_Klimt1 != I_Klimt2) ) {
201  std::stringstream ss;
202  ss << "\nProblem when comparing two grayscale images!\n";
203  ss << "(I_Klimt1 == I_Klimt2)=" << (I_Klimt1 == I_Klimt2) << std::endl;
204  ss << "(I_Klimt1 != I_Klimt2)=" << (I_Klimt1 != I_Klimt2) << std::endl;
205 
206  throw vpException(vpException::fatalError, ss.str());
207  }
208 
209  //Modify I_Klimt1
210  if (I_Klimt1[I_Klimt1.getHeight()/2][I_Klimt1.getWidth()/2] < 255) {
211  I_Klimt1[I_Klimt1.getHeight()/2][I_Klimt1.getWidth()/2]++;
212  } else {
213  I_Klimt1[I_Klimt1.getHeight()/2][I_Klimt1.getWidth()/2]--;
214  }
215 
216  std::cout << "\nThe two grayscale images are different." << std::endl;
217  std::cout << "(I_Klimt1 == I_Klimt2)=" << (I_Klimt1 == I_Klimt2) << std::endl;
218  std::cout << "(I_Klimt1 != I_Klimt2)=" << (I_Klimt1 != I_Klimt2) << std::endl;
219 
220  //The two images should be different
221  if ( (I_Klimt1 == I_Klimt2) || !(I_Klimt1 != I_Klimt2) ) {
222  std::stringstream ss;
223  ss << "\nProblem when comparing two grayscale images!\n";
224  ss << "(I_Klimt1 == I_Klimt2)=" << (I_Klimt1 == I_Klimt2) << std::endl;
225  ss << "(I_Klimt1 != I_Klimt2)=" << (I_Klimt1 != I_Klimt2) << std::endl;
226 
227  throw vpException(vpException::fatalError, ss.str());
228  }
229 
230 
231  //Load color Klimt
232  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.pgm");
233 
234  vpImage<vpRGBa> I_color_Klimt1, I_color_Klimt2;
235  vpImageIo::read(I_color_Klimt1, filename);
236  I_color_Klimt2 = I_color_Klimt1;
237 
238  std::cout << "\nI_color_Klimt1=" << I_color_Klimt1.getWidth() << "x" << I_color_Klimt1.getHeight() << std::endl;
239  std::cout << "I_color_Klimt2=" << I_color_Klimt2.getWidth() << "x" << I_color_Klimt2.getHeight() << std::endl;
240 
241  std::cout << "\nThe two color images are equal." << std::endl;
242  std::cout << "(I_color_Klimt1 == I_color_Klimt2)=" << (I_color_Klimt1 == I_color_Klimt2) << std::endl;
243  std::cout << "(I_color_Klimt1 != I_color_Klimt2)=" << (I_color_Klimt1 != I_color_Klimt2) << std::endl;
244 
245  //The two images should be equal
246  if ( !(I_color_Klimt1 == I_color_Klimt2) || (I_color_Klimt1 != I_color_Klimt2) ) {
247  std::stringstream ss;
248  ss << "\nProblem when comparing two color images!\n";
249  ss << "(I_color_Klimt1 == I_color_Klimt2)=" << (I_color_Klimt1 == I_color_Klimt2) << std::endl;
250  ss << "(I_color_Klimt1 != I_color_Klimt2)=" << (I_color_Klimt1 != I_color_Klimt2) << std::endl;
251 
252  throw vpException(vpException::fatalError, ss.str());
253  }
254 
255  //Modify I_color_Klimt2
256  if (I_color_Klimt2[I_color_Klimt2.getHeight()/2][I_color_Klimt2.getWidth()/2].R < 255) {
257  I_color_Klimt2[I_color_Klimt2.getHeight()/2][I_color_Klimt2.getWidth()/2].R++;
258  } else {
259  I_color_Klimt2[I_color_Klimt2.getHeight()/2][I_color_Klimt2.getWidth()/2].R--;
260  }
261 
262  std::cout << "\nThe two color images are different." << std::endl;
263  std::cout << "(I_color_Klimt1 == I_color_Klimt2)=" << (I_color_Klimt1 == I_color_Klimt2) << std::endl;
264  std::cout << "(I_color_Klimt1 != I_color_Klimt2)=" << (I_color_Klimt1 != I_color_Klimt2) << std::endl;
265 
266  //The two images should be different
267  if ( (I_color_Klimt1 == I_color_Klimt2) || !(I_color_Klimt1 != I_color_Klimt2) ) {
268  std::stringstream ss;
269  ss << "\nProblem when comparing two color images!\n";
270  ss << "(I_color_Klimt1 == I_color_Klimt2)=" << (I_color_Klimt1 == I_color_Klimt2) << std::endl;
271  ss << "(I_color_Klimt1 != I_color_Klimt2)=" << (I_color_Klimt1 != I_color_Klimt2) << std::endl;
272 
273  throw vpException(vpException::fatalError, ss.str());
274  }
275 
276  } catch(vpException &e) {
277  std::cerr << "\nCatch an exception: " << e << std::endl;
278  return EXIT_FAILURE;
279  }
280 
281  std::cout << "\nThe comparison of two images of the same type is OK!" << std::endl;
282  return EXIT_SUCCESS;
283 }
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1157
unsigned int getWidth() const
Definition: vpImage.h:226
error that can be emited by ViSP classes.
Definition: vpException.h:73
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1366
static std::string getUserName()
Definition: vpIoTools.cpp:177
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:205
unsigned int getHeight() const
Definition: vpImage.h:175