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