Visual Servoing Platform  version 3.4.0
testImageComparison.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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 the comparison of two vpImage objects of the same type.
33  *
34  * Authors:
35  * Souriya Trinh
36  *
37  *****************************************************************************/
44 #include <visp3/core/vpImage.h>
45 #include <visp3/core/vpIoTools.h>
46 #include <visp3/io/vpImageIo.h>
47 #include <visp3/io/vpParseArgv.h>
48 
49 // List of allowed command line options
50 #define GETOPTARGS "cdi:h"
51 
52 void usage(const char *name, const char *badparam, std::string ipath);
53 bool getOptions(int argc, const char **argv, std::string &ipath);
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 \"Klimt/Klimt.pgm\"\n\
77  and \"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", ipath.c_str());
84 
85  if (badparam)
86  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
87 }
88 
97 bool getOptions(int argc, const char **argv, std::string &ipath)
98 {
99  const char *optarg_;
100  int c;
101  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
102 
103  switch (c) {
104  case 'i':
105  ipath = optarg_;
106  break;
107  case 'h':
108  usage(argv[0], NULL, ipath);
109  return false;
110  break;
111 
112  case 'c':
113  case 'd':
114  break;
115 
116  default:
117  usage(argv[0], optarg_, ipath);
118  return false;
119  break;
120  }
121  }
122 
123  if ((c == 1) || (c == -1)) {
124  // standalone param or error
125  usage(argv[0], NULL, ipath);
126  std::cerr << "ERROR: " << std::endl;
127  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
128  return false;
129  }
130 
131  return true;
132 }
133 
134 int main(int argc, const char **argv)
135 {
136  try {
137  std::string env_ipath;
138  std::string opt_ipath;
139  std::string ipath;
140  std::string filename;
141  std::string username;
142 
143  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
144  // environment variable value
145  env_ipath = vpIoTools::getViSPImagesDataPath();
146 
147  // Set the default input path
148  if (!env_ipath.empty()) {
149  ipath = env_ipath;
150  }
151 
152  // Get the user login name
153  vpIoTools::getUserName(username);
154 
155  // Read the command line options
156  if (getOptions(argc, argv, opt_ipath) == false) {
157  exit(-1);
158  }
159 
160  // Get the option values
161  if (!opt_ipath.empty()) {
162  ipath = opt_ipath;
163  }
164 
165  // Compare ipath and env_ipath. If they differ, we take into account
166  // the input path comming from the command line option
167  if (!opt_ipath.empty() && !env_ipath.empty()) {
168  if (ipath != env_ipath) {
169  std::cout << std::endl << "WARNING: " << std::endl;
170  std::cout << " Since -i <visp image path=" << ipath << "> "
171  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
172  << " we skip the environment variable." << std::endl;
173  }
174  }
175 
176  // Test if an input path is set
177  if (opt_ipath.empty() && env_ipath.empty()) {
178  usage(argv[0], NULL, ipath);
179  std::cerr << std::endl << "ERROR:" << std::endl;
180  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
181  << " environment variable to specify the location of the " << std::endl
182  << " image path where test images are located." << std::endl
183  << std::endl;
184  exit(EXIT_FAILURE);
185  }
186 
187  //
188  // Here starts really the test
189  //
190 
191  // Load grayscale Klimt
192  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
193 
194  vpImage<unsigned char> I_Klimt1, I_Klimt2;
195  vpImageIo::read(I_Klimt1, filename);
196  vpImageIo::read(I_Klimt2, filename);
197 
198  std::cout << "\nI_Klimt1=" << I_Klimt1.getWidth() << "x" << I_Klimt1.getHeight() << std::endl;
199  std::cout << "I_Klimt2=" << I_Klimt2.getWidth() << "x" << I_Klimt2.getHeight() << std::endl;
200 
201  std::cout << "\nThe two grayscale images are equal." << std::endl;
202  std::cout << "(I_Klimt1 == I_Klimt2)=" << (I_Klimt1 == I_Klimt2) << std::endl;
203  std::cout << "(I_Klimt1 != I_Klimt2)=" << (I_Klimt1 != I_Klimt2) << std::endl;
204 
205  // The two images should be equal
206  if (!(I_Klimt1 == I_Klimt2) || (I_Klimt1 != I_Klimt2)) {
207  std::stringstream ss;
208  ss << "\nProblem when comparing two grayscale images!\n";
209  ss << "(I_Klimt1 == I_Klimt2)=" << (I_Klimt1 == I_Klimt2) << std::endl;
210  ss << "(I_Klimt1 != I_Klimt2)=" << (I_Klimt1 != I_Klimt2) << std::endl;
211 
212  throw vpException(vpException::fatalError, ss.str());
213  }
214 
215  // Modify I_Klimt1
216  if (I_Klimt1[I_Klimt1.getHeight() / 2][I_Klimt1.getWidth() / 2] < 255) {
217  I_Klimt1[I_Klimt1.getHeight() / 2][I_Klimt1.getWidth() / 2]++;
218  } else {
219  I_Klimt1[I_Klimt1.getHeight() / 2][I_Klimt1.getWidth() / 2]--;
220  }
221 
222  std::cout << "\nThe two grayscale images are different." << std::endl;
223  std::cout << "(I_Klimt1 == I_Klimt2)=" << (I_Klimt1 == I_Klimt2) << std::endl;
224  std::cout << "(I_Klimt1 != I_Klimt2)=" << (I_Klimt1 != I_Klimt2) << std::endl;
225 
226  // The two images should be different
227  if ((I_Klimt1 == I_Klimt2) || !(I_Klimt1 != I_Klimt2)) {
228  std::stringstream ss;
229  ss << "\nProblem when comparing two grayscale images!\n";
230  ss << "(I_Klimt1 == I_Klimt2)=" << (I_Klimt1 == I_Klimt2) << std::endl;
231  ss << "(I_Klimt1 != I_Klimt2)=" << (I_Klimt1 != I_Klimt2) << std::endl;
232 
233  throw vpException(vpException::fatalError, ss.str());
234  }
235 
236  // Load color Klimt
237  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
238 
239  vpImage<vpRGBa> I_color_Klimt1, I_color_Klimt2;
240  vpImageIo::read(I_color_Klimt1, filename);
241  I_color_Klimt2 = I_color_Klimt1;
242 
243  std::cout << "\nI_color_Klimt1=" << I_color_Klimt1.getWidth() << "x" << I_color_Klimt1.getHeight() << std::endl;
244  std::cout << "I_color_Klimt2=" << I_color_Klimt2.getWidth() << "x" << I_color_Klimt2.getHeight() << std::endl;
245 
246  std::cout << "\nThe two color images are equal." << std::endl;
247  std::cout << "(I_color_Klimt1 == I_color_Klimt2)=" << (I_color_Klimt1 == I_color_Klimt2) << std::endl;
248  std::cout << "(I_color_Klimt1 != I_color_Klimt2)=" << (I_color_Klimt1 != I_color_Klimt2) << std::endl;
249 
250  // The two images should be equal
251  if (!(I_color_Klimt1 == I_color_Klimt2) || (I_color_Klimt1 != I_color_Klimt2)) {
252  std::stringstream ss;
253  ss << "\nProblem when comparing two color images!\n";
254  ss << "(I_color_Klimt1 == I_color_Klimt2)=" << (I_color_Klimt1 == I_color_Klimt2) << std::endl;
255  ss << "(I_color_Klimt1 != I_color_Klimt2)=" << (I_color_Klimt1 != I_color_Klimt2) << std::endl;
256 
257  throw vpException(vpException::fatalError, ss.str());
258  }
259 
260  // Modify I_color_Klimt2
261  if (I_color_Klimt2[I_color_Klimt2.getHeight() / 2][I_color_Klimt2.getWidth() / 2].R < 255) {
262  I_color_Klimt2[I_color_Klimt2.getHeight() / 2][I_color_Klimt2.getWidth() / 2].R++;
263  } else {
264  I_color_Klimt2[I_color_Klimt2.getHeight() / 2][I_color_Klimt2.getWidth() / 2].R--;
265  }
266 
267  std::cout << "\nThe two color images are different." << std::endl;
268  std::cout << "(I_color_Klimt1 == I_color_Klimt2)=" << (I_color_Klimt1 == I_color_Klimt2) << std::endl;
269  std::cout << "(I_color_Klimt1 != I_color_Klimt2)=" << (I_color_Klimt1 != I_color_Klimt2) << std::endl;
270 
271  // The two images should be different
272  if ((I_color_Klimt1 == I_color_Klimt2) || !(I_color_Klimt1 != I_color_Klimt2)) {
273  std::stringstream ss;
274  ss << "\nProblem when comparing two color images!\n";
275  ss << "(I_color_Klimt1 == I_color_Klimt2)=" << (I_color_Klimt1 == I_color_Klimt2) << std::endl;
276  ss << "(I_color_Klimt1 != I_color_Klimt2)=" << (I_color_Klimt1 != I_color_Klimt2) << std::endl;
277 
278  throw vpException(vpException::fatalError, ss.str());
279  }
280 
281  } catch (const vpException &e) {
282  std::cerr << "\nCatch an exception: " << e << std::endl;
283  return EXIT_FAILURE;
284  }
285 
286  std::cout << "\nThe comparison of two images of the same type is OK!" << std::endl;
287  return EXIT_SUCCESS;
288 }
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1202
unsigned int getWidth() const
Definition: vpImage.h:246
error that can be emited by ViSP classes.
Definition: vpException.h:71
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1446
static std::string getUserName()
Definition: vpIoTools.cpp:228
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:244
unsigned int getHeight() const
Definition: vpImage.h:188