Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testImageComparison.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 https://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
39
40#include <visp3/core/vpImage.h>
41#include <visp3/core/vpIoTools.h>
42#include <visp3/io/vpImageIo.h>
43#include <visp3/io/vpParseArgv.h>
44
45#ifdef ENABLE_VISP_NAMESPACE
46using namespace VISP_NAMESPACE_NAME;
47#endif
48
49// List of allowed command line options
50#define GETOPTARGS "cdi:h"
51
52void usage(const char *name, const char *badparam, std::string ipath);
53bool 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 */
62void usage(const char *name, const char *badparam, std::string ipath)
63{
64 fprintf(stdout, "\n\
65Test the comparison of two vpImage objects of the same type.\n\
66\n\
67SYNOPSIS\n\
68 %s [-i <input image path>]\n\
69 [-h]\n \
70",
71name);
72
73 fprintf(stdout, "\n\
74OPTIONS: Default\n\
75 -i <input image path> %s\n\
76 Set image input path.\n\
77 From this path read \"Klimt/Klimt.pgm\"\n\
78 and \"Klimt/Klimt.ppm\" images.\n\
79 Setting the VISP_INPUT_IMAGE_PATH environment\n\
80 variable produces the same behaviour than using\n\
81 this option.\n\
82\n\
83 -h\n\
84 Print the help.\n\n",
85 ipath.c_str());
86
87 if (badparam)
88 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
89}
90
99bool getOptions(int argc, const char **argv, std::string &ipath)
100{
101 const char *optarg_;
102 int c;
103 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
104
105 switch (c) {
106 case 'i':
107 ipath = optarg_;
108 break;
109 case 'h':
110 usage(argv[0], nullptr, ipath);
111 return false;
112
113 case 'c':
114 case 'd':
115 break;
116
117 default:
118 usage(argv[0], optarg_, ipath);
119 return false;
120 }
121 }
122
123 if ((c == 1) || (c == -1)) {
124 // standalone param or error
125 usage(argv[0], nullptr, 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
134int 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
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 return EXIT_FAILURE;
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 coming 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], nullptr, 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 }
219 else {
220 I_Klimt1[I_Klimt1.getHeight() / 2][I_Klimt1.getWidth() / 2]--;
221 }
222
223 std::cout << "\nThe two grayscale images are different." << std::endl;
224 std::cout << "(I_Klimt1 == I_Klimt2)=" << (I_Klimt1 == I_Klimt2) << std::endl;
225 std::cout << "(I_Klimt1 != I_Klimt2)=" << (I_Klimt1 != I_Klimt2) << std::endl;
226
227 // The two images should be different
228 if ((I_Klimt1 == I_Klimt2) || !(I_Klimt1 != I_Klimt2)) {
229 std::stringstream ss;
230 ss << "\nProblem when comparing two grayscale images!\n";
231 ss << "(I_Klimt1 == I_Klimt2)=" << (I_Klimt1 == I_Klimt2) << std::endl;
232 ss << "(I_Klimt1 != I_Klimt2)=" << (I_Klimt1 != I_Klimt2) << std::endl;
233
234 throw vpException(vpException::fatalError, ss.str());
235 }
236
237 // Load color Klimt
238 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
239
240 vpImage<vpRGBa> I_color_Klimt1, I_color_Klimt2;
241 vpImageIo::read(I_color_Klimt1, filename);
242 I_color_Klimt2 = I_color_Klimt1;
243
244 std::cout << "\nI_color_Klimt1=" << I_color_Klimt1.getWidth() << "x" << I_color_Klimt1.getHeight() << std::endl;
245 std::cout << "I_color_Klimt2=" << I_color_Klimt2.getWidth() << "x" << I_color_Klimt2.getHeight() << std::endl;
246
247 std::cout << "\nThe two color images are equal." << std::endl;
248 std::cout << "(I_color_Klimt1 == I_color_Klimt2)=" << (I_color_Klimt1 == I_color_Klimt2) << std::endl;
249 std::cout << "(I_color_Klimt1 != I_color_Klimt2)=" << (I_color_Klimt1 != I_color_Klimt2) << std::endl;
250
251 // The two images should be equal
252 if (!(I_color_Klimt1 == I_color_Klimt2) || (I_color_Klimt1 != I_color_Klimt2)) {
253 std::stringstream ss;
254 ss << "\nProblem when comparing two color images!\n";
255 ss << "(I_color_Klimt1 == I_color_Klimt2)=" << (I_color_Klimt1 == I_color_Klimt2) << std::endl;
256 ss << "(I_color_Klimt1 != I_color_Klimt2)=" << (I_color_Klimt1 != I_color_Klimt2) << std::endl;
257
258 throw vpException(vpException::fatalError, ss.str());
259 }
260
261 // Modify I_color_Klimt2
262 if (I_color_Klimt2[I_color_Klimt2.getHeight() / 2][I_color_Klimt2.getWidth() / 2].R < 255) {
263 I_color_Klimt2[I_color_Klimt2.getHeight() / 2][I_color_Klimt2.getWidth() / 2].R++;
264 }
265 else {
266 I_color_Klimt2[I_color_Klimt2.getHeight() / 2][I_color_Klimt2.getWidth() / 2].R--;
267 }
268
269 std::cout << "\nThe two color images are different." << std::endl;
270 std::cout << "(I_color_Klimt1 == I_color_Klimt2)=" << (I_color_Klimt1 == I_color_Klimt2) << std::endl;
271 std::cout << "(I_color_Klimt1 != I_color_Klimt2)=" << (I_color_Klimt1 != I_color_Klimt2) << std::endl;
272
273 // The two images should be different
274 if ((I_color_Klimt1 == I_color_Klimt2) || !(I_color_Klimt1 != I_color_Klimt2)) {
275 std::stringstream ss;
276 ss << "\nProblem when comparing two color images!\n";
277 ss << "(I_color_Klimt1 == I_color_Klimt2)=" << (I_color_Klimt1 == I_color_Klimt2) << std::endl;
278 ss << "(I_color_Klimt1 != I_color_Klimt2)=" << (I_color_Klimt1 != I_color_Klimt2) << std::endl;
279
280 throw vpException(vpException::fatalError, ss.str());
281 }
282
283 }
284 catch (const vpException &e) {
285 std::cerr << "\nCatch an exception: " << e << std::endl;
286 return EXIT_FAILURE;
287 }
288
289 std::cout << "\nThe comparison of two images of the same type is OK!" << std::endl;
290 return EXIT_SUCCESS;
291}
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ fatalError
Fatal error.
Definition vpException.h:72
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition of the vpImage class member functions.
Definition vpImage.h:131
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getHeight() const
Definition vpImage.h:181
static std::string getViSPImagesDataPath()
static std::string getUserName()
static std::string createFilePath(const std::string &parent, const std::string &child)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)