Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
testXmlParserCamera.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 vpXmlParserCamera parse / save.
32  */
33 
40 #include <visp3/core/vpIoTools.h>
41 #include <visp3/core/vpXmlParserCamera.h>
42 
43 int main()
44 {
45 #ifdef ENABLE_VISP_NAMESPACE
46  using namespace VISP_NAMESPACE_NAME;
47 #endif
48 #if defined(VISP_HAVE_PUGIXML)
49 
50 #if defined(_WIN32)
51  std::string tmp_dir = "C:/temp/";
52 #else
53  std::string tmp_dir = "/tmp/";
54 #endif
55 
56  // Get the user login name
57  std::string username;
58  vpIoTools::getUserName(username);
59 
60  tmp_dir += username + "/test_xml_parser_camera/";
61  vpIoTools::remove(tmp_dir);
62  std::cout << "Create: " << tmp_dir << std::endl;
63  vpIoTools::makeDirectory(tmp_dir);
64 
65  {
66  std::cout << "-- Test to save/load one single camera without distortion in a single file" << std::endl;
68  cam.initPersProjWithoutDistortion(278.4691184118, 273.9196496040, 162.0747539621, 113.1741829586);
69  std::string filename = tmp_dir + "test_write_cam_without_distortion.xml";
70  {
72  std::cout << "Write to: " << filename << std::endl;
73  if (xml.save(cam, filename, "Camera", 320, 240) != vpXmlParserCamera::SEQUENCE_OK) {
74  std::cerr << "Cannot save XML file: " << filename << std::endl;
75  return EXIT_FAILURE;
76  }
77  }
78 
79  {
80  vpCameraParameters cam_read;
82  xml.parse(cam_read, filename, "Camera", vpCameraParameters::perspectiveProjWithoutDistortion, 320, 240, false);
83  std::cout << "Cam write:\n" << cam << std::endl;
84  std::cout << "Cam read:\n" << cam_read << std::endl;
85  if (cam != cam_read) {
86  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
87  return EXIT_FAILURE;
88  }
89  }
90 
91  {
92  // Without specifying image size
93  vpCameraParameters cam_read;
95  xml.parse(cam_read, filename, "Camera", vpCameraParameters::perspectiveProjWithoutDistortion, 0, 0, false);
96  std::cout << "Cam write:\n" << cam << std::endl;
97  std::cout << "Cam read:\n" << cam_read << std::endl;
98  if (cam != cam_read) {
99  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
100  return EXIT_FAILURE;
101  }
102  }
103  }
104 
105  {
106  std::cout << "-- Test to save/load one single camera with distortion in a single file" << std::endl;
107  std::string filename = tmp_dir + "test_write_cam_with_distortion.xml";
108  vpCameraParameters cam;
109  cam.initPersProjWithDistortion(200, 200, 160, 120, 0.02, -0.02);
110  {
111  vpXmlParserCamera xml;
112  std::cout << "Write to: " << filename << std::endl;
113  if (xml.save(cam, filename, "Camera", 320, 240) != vpXmlParserCamera::SEQUENCE_OK) {
114  std::cerr << "Cannot save XML file: " << filename << std::endl;
115  return EXIT_FAILURE;
116  }
117  }
118 
119  {
120  vpCameraParameters cam_read;
121  vpXmlParserCamera xml;
122  xml.parse(cam_read, filename, "Camera", vpCameraParameters::perspectiveProjWithDistortion, 320, 240, false);
123  std::cout << "Cam write:\n" << cam << std::endl;
124  std::cout << "Cam read:\n" << cam_read << std::endl;
125  if (cam != cam_read) {
126  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
127  return EXIT_FAILURE;
128  }
129  }
130 
131  {
132  // Without specifying image size
133  vpCameraParameters cam_read;
134  vpXmlParserCamera xml;
135  xml.parse(cam_read, filename, "Camera", vpCameraParameters::perspectiveProjWithDistortion, 0, 0, false);
136  std::cout << "Cam write:\n" << cam << std::endl;
137  std::cout << "Cam read:\n" << cam_read << std::endl;
138  if (cam != cam_read) {
139  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
140  return EXIT_FAILURE;
141  }
142  }
143  }
144 
145  {
146  std::cout << "-- Test to save/load multiple cameras with and without distortion in a single file" << std::endl;
147  std::string filename = tmp_dir + "test_write_cam_multiple.xml";
148  vpCameraParameters cam1_w_d;
149  cam1_w_d.initPersProjWithDistortion(200, 200, 160, 120, 0.02, -0.02);
150  {
151  vpXmlParserCamera xml;
152  std::cout << "Write to: " << filename << " camera:\n" << cam1_w_d << std::endl;
153  if (xml.save(cam1_w_d, filename, "Camera 1", 320, 240) != vpXmlParserCamera::SEQUENCE_OK) {
154  std::cerr << "Cannot save XML file: " << filename << std::endl;
155  return EXIT_FAILURE;
156  }
157  }
158 
159  vpCameraParameters cam1_wo_d;
160  cam1_wo_d.initPersProjWithoutDistortion(200, 200, 160, 120);
161  {
162  vpXmlParserCamera xml;
163  std::cout << "Write to: " << filename << " camera:\n" << cam1_wo_d << std::endl;
164  if (xml.save(cam1_wo_d, filename, "Camera 1", 320, 240) != vpXmlParserCamera::SEQUENCE_OK) {
165  std::cerr << "Cannot save XML file: " << filename << std::endl;
166  return EXIT_FAILURE;
167  }
168  }
169  vpCameraParameters cam2_w_d;
170  cam2_w_d.initPersProjWithDistortion(400, 400, 320, 240, 0.02, -0.02);
171  {
172  vpXmlParserCamera xml;
173  std::cout << "Write to: " << filename << " camera:\n" << cam2_w_d << std::endl;
174  if (xml.save(cam2_w_d, filename, "Camera 2", 640, 480) != vpXmlParserCamera::SEQUENCE_OK) {
175  std::cerr << "Cannot save XML file: " << filename << std::endl;
176  return EXIT_FAILURE;
177  }
178  }
179 
180  vpCameraParameters cam2_wo_d;
181  cam2_wo_d.initPersProjWithoutDistortion(400, 400, 320, 240);
182  {
183  vpXmlParserCamera xml;
184  std::cout << "Write to: " << filename << " camera:\n" << cam2_wo_d << std::endl;
185  if (xml.save(cam2_wo_d, filename, "Camera 2", 640, 480) != vpXmlParserCamera::SEQUENCE_OK) {
186  std::cerr << "Cannot save XML file: " << filename << std::endl;
187  return EXIT_FAILURE;
188  }
189  }
190 
191  {
192  vpCameraParameters cam_read;
193  vpXmlParserCamera xml;
194  xml.parse(cam_read, filename, "Camera 1", vpCameraParameters::perspectiveProjWithDistortion, 320, 240, false);
195  std::cout << "Cam write:\n" << cam1_w_d << std::endl;
196  std::cout << "Cam read:\n" << cam_read << std::endl;
197  if (cam1_w_d != cam_read) {
198  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
199  return EXIT_FAILURE;
200  }
201  }
202  {
203  vpCameraParameters cam_read;
204  vpXmlParserCamera xml;
205  xml.parse(cam_read, filename, "Camera 1", vpCameraParameters::perspectiveProjWithoutDistortion, 320, 240, false);
206  std::cout << "Cam write:\n" << cam1_wo_d << std::endl;
207  std::cout << "Cam read:\n" << cam_read << std::endl;
208  if (cam1_wo_d != cam_read) {
209  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
210  return EXIT_FAILURE;
211  }
212  }
213  {
214  vpCameraParameters cam_read;
215  vpXmlParserCamera xml;
216  xml.parse(cam_read, filename, "Camera 2", vpCameraParameters::perspectiveProjWithDistortion, 640, 480, false);
217  std::cout << "Cam write:\n" << cam2_w_d << std::endl;
218  std::cout << "Cam read:\n" << cam_read << std::endl;
219  if (cam2_w_d != cam_read) {
220  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
221  return EXIT_FAILURE;
222  }
223  }
224  {
225  vpCameraParameters cam_read;
226  vpXmlParserCamera xml;
227  xml.parse(cam_read, filename, "Camera 2", vpCameraParameters::perspectiveProjWithoutDistortion, 640, 480, false);
228  std::cout << "Cam write:\n" << cam2_wo_d << std::endl;
229  std::cout << "Cam read:\n" << cam_read << std::endl;
230  if (cam2_wo_d != cam_read) {
231  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
232  return EXIT_FAILURE;
233  }
234  }
235  }
236 
237  {
238  std::cout << "-- Test to save/load one single camera with Kannala Brandt distortion in a single file" << std::endl;
239  vpCameraParameters cam;
240  std::vector<double> distortion_coeffs;
241  distortion_coeffs.push_back(-0.00297341705299914);
242  distortion_coeffs.push_back(0.0352853797376156);
243  distortion_coeffs.push_back(-0.032205019146204);
244  distortion_coeffs.push_back(0.004446716979146);
245  distortion_coeffs.push_back(0);
246  cam.initProjWithKannalaBrandtDistortion(285.523895263672, 286.6708984375, 420.874114990234, 381.085388183594,
247  distortion_coeffs);
248  std::string filename = tmp_dir + "test_write_cam_with_KannalaBrandt_distortion.xml";
249  {
250  vpXmlParserCamera xml;
251  std::cout << "Write to: " << filename << std::endl;
252  if (xml.save(cam, filename, "Camera", 800, 848) != vpXmlParserCamera::SEQUENCE_OK) {
253  std::cerr << "Cannot save XML file: " << filename << std::endl;
254  return EXIT_FAILURE;
255  }
256  }
257 
258  vpCameraParameters cam_read;
259  {
260  vpXmlParserCamera xml;
261  xml.parse(cam_read, filename, "Camera", vpCameraParameters::ProjWithKannalaBrandtDistortion, 800, 848, false);
262  std::cout << "Cam write:\n" << cam << std::endl;
263  std::cout << "Cam read:\n" << cam_read << std::endl;
264  if (cam != cam_read) {
265  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
266  return EXIT_FAILURE;
267  }
268  }
269  }
270 
271  {
272  std::cout << "-- Test to save/load one single camera with Kannala Brandt distortion in a single file wo name" << std::endl;
273  vpCameraParameters cam;
274  std::vector<double> distortion_coeffs;
275  distortion_coeffs.push_back(-0.00297341705299914);
276  distortion_coeffs.push_back(0.0352853797376156);
277  distortion_coeffs.push_back(-0.032205019146204);
278  distortion_coeffs.push_back(0.004446716979146);
279  distortion_coeffs.push_back(0);
280  cam.initProjWithKannalaBrandtDistortion(285.523895263672, 286.6708984375, 420.874114990234, 381.085388183594,
281  distortion_coeffs);
282  std::string filename = tmp_dir + "test_write_cam_with_KannalaBrandt_distortion_wo_name.xml";
283  {
284  vpXmlParserCamera xml;
285  std::cout << "Write to: " << filename << std::endl;
286  if (xml.save(cam, filename, "Camera", 800, 848) != vpXmlParserCamera::SEQUENCE_OK) {
287  std::cerr << "Cannot save XML file: " << filename << std::endl;
288  return EXIT_FAILURE;
289  }
290  }
291 
292  vpCameraParameters cam_read;
293  {
294  vpXmlParserCamera xml;
295  xml.parse(cam_read, filename, "", vpCameraParameters::ProjWithKannalaBrandtDistortion, 800, 848, false);
296  std::cout << "Cam write:\n" << cam << std::endl;
297  std::cout << "Cam read:\n" << cam_read << std::endl;
298  if (cam != cam_read) {
299  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
300  return EXIT_FAILURE;
301  }
302  }
303  }
304 
305  {
306  std::cout << "-- Test to save 2 cameras and parse them wo name thanks they differ in distortion" << std::endl;
307  vpCameraParameters cam1;
308 
309  std::string filename = tmp_dir + "test_write_2_cam_differ_in_distortion.xml";
310 
311  {
312  vpXmlParserCamera xml;
313  std::cout << "Write to: " << filename << std::endl;
314  std::cout << "Cam write:\n" << cam1 << std::endl;
315  if (xml.save(cam1, filename, "Camera 1", 320, 240, "", false) != vpXmlParserCamera::SEQUENCE_OK) {
316  std::cerr << "Cannot save XML file: " << filename << std::endl;
317  return EXIT_FAILURE;
318  }
319  }
320 
321  vpCameraParameters cam2;
322  std::vector<double> distortion_coeffs;
323  distortion_coeffs.push_back(-0.00297341705299914);
324  distortion_coeffs.push_back(0.0352853797376156);
325  distortion_coeffs.push_back(-0.032205019146204);
326  distortion_coeffs.push_back(0.004446716979146);
327  distortion_coeffs.push_back(0);
328  cam2.initProjWithKannalaBrandtDistortion(285.523895263672, 286.6708984375, 420.874114990234, 381.085388183594,
329  distortion_coeffs);
330  {
331  vpXmlParserCamera xml;
332  std::cout << "Write to: " << filename << std::endl;
333  std::cout << "Cam write:\n" << cam2 << std::endl;
334  if (xml.save(cam2, filename, "Camera 2", 800, 848, "", false) != vpXmlParserCamera::SEQUENCE_OK) {
335  std::cerr << "Cannot save XML file: " << filename << std::endl;
336  return EXIT_FAILURE;
337  }
338  }
339 
340  {
341  std::cout << "Attempt to read camera with perspective projection without distortion and without name" << std::endl;
342  vpCameraParameters cam_read;
343  vpXmlParserCamera xml;
344  xml.parse(cam_read, filename, "", vpCameraParameters::perspectiveProjWithoutDistortion, 320, 240, false);
345 
346  std::cout << "Cam read:\n" << cam_read << std::endl;
347  if (cam1 != cam_read) {
348  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
349  return EXIT_FAILURE;
350  }
351  }
352 
353  {
354  std::cout << "Attempt to read camera with Kannala Brandt distortion and without name" << std::endl;
355  vpCameraParameters cam_read;
356  vpXmlParserCamera xml;
357  xml.parse(cam_read, filename, "", vpCameraParameters::ProjWithKannalaBrandtDistortion, 800, 848, false);
358 
359  std::cout << "Cam read:\n" << cam_read << std::endl;
360  if (cam2 != cam_read) {
361  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
362  return EXIT_FAILURE;
363  }
364  }
365  }
366 
367  vpIoTools::remove(tmp_dir);
368 
369  std::cout << "Test succeed" << std::endl;
370 #endif
371 
372  return EXIT_SUCCESS;
373 }
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
@ ProjWithKannalaBrandtDistortion
Projection with Kannala-Brandt distortion model.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
void initPersProjWithDistortion(double px, double py, double u0, double v0, double kud, double kdu)
void initProjWithKannalaBrandtDistortion(double px, double py, double u0, double v0, const std::vector< double > &distortion_coefficients)
static std::string getUserName()
Definition: vpIoTools.cpp:285
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:550
static bool remove(const std::string &filename)
Definition: vpIoTools.cpp:921
XML parser to load and save intrinsic camera parameters.
int save(const vpCameraParameters &cam, const std::string &filename, const std::string &camera_name, unsigned int image_width=0, unsigned int image_height=0, const std::string &additionalInfo="", bool verbose=true)
int parse(vpCameraParameters &cam, const std::string &filename, const std::string &camera_name, const vpCameraParameters::vpCameraParametersProjType &projModel, unsigned int image_width=0, unsigned int image_height=0, bool verbose=true)