Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testXmlParserCamera.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 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 https://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 vpXmlParserCamera parse / save.
33 *
34*****************************************************************************/
35
42#include <visp3/core/vpIoTools.h>
43#include <visp3/core/vpXmlParserCamera.h>
44
45int main()
46{
47#if defined(_WIN32)
48 std::string tmp_dir = "C:/temp/";
49#else
50 std::string tmp_dir = "/tmp/";
51#endif
52
53 // Get the user login name
54 std::string username;
55 vpIoTools::getUserName(username);
56
57 tmp_dir += username + "/test_xml_parser_camera/";
58 vpIoTools::remove(tmp_dir);
59 std::cout << "Create: " << tmp_dir << std::endl;
61
62 {
63 std::cout << "-- Test to save/load one single camera without distortion in a single file" << std::endl;
65 cam.initPersProjWithoutDistortion(278.4691184118, 273.9196496040, 162.0747539621, 113.1741829586);
66 std::string filename = tmp_dir + "test_write_cam_without_distortion.xml";
67 {
69 std::cout << "Write to: " << filename << std::endl;
70 if (xml.save(cam, filename, "Camera", 320, 240) != vpXmlParserCamera::SEQUENCE_OK) {
71 std::cerr << "Cannot save XML file: " << filename << std::endl;
72 return EXIT_FAILURE;
73 }
74 }
75
76 {
77 vpCameraParameters cam_read;
79 xml.parse(cam_read, filename, "Camera", vpCameraParameters::perspectiveProjWithoutDistortion, 320, 240, false);
80 std::cout << "Cam write:\n" << cam << std::endl;
81 std::cout << "Cam read:\n" << cam_read << std::endl;
82 if (cam != cam_read) {
83 std::cerr << "Issue when parsing XML file: " << filename << std::endl;
84 return EXIT_FAILURE;
85 }
86 }
87
88 {
89 // Without specifying image size
90 vpCameraParameters cam_read;
92 xml.parse(cam_read, filename, "Camera", vpCameraParameters::perspectiveProjWithoutDistortion, 0, 0, false);
93 std::cout << "Cam write:\n" << cam << std::endl;
94 std::cout << "Cam read:\n" << cam_read << std::endl;
95 if (cam != cam_read) {
96 std::cerr << "Issue when parsing XML file: " << filename << std::endl;
97 return EXIT_FAILURE;
98 }
99 }
100 }
101
102 {
103 std::cout << "-- Test to save/load one single camera with distortion in a single file" << std::endl;
104 std::string filename = tmp_dir + "test_write_cam_with_distortion.xml";
106 cam.initPersProjWithDistortion(200, 200, 160, 120, 0.02, -0.02);
107 {
109 std::cout << "Write to: " << filename << std::endl;
110 if (xml.save(cam, filename, "Camera", 320, 240) != vpXmlParserCamera::SEQUENCE_OK) {
111 std::cerr << "Cannot save XML file: " << filename << std::endl;
112 return EXIT_FAILURE;
113 }
114 }
115
116 {
117 vpCameraParameters cam_read;
119 xml.parse(cam_read, filename, "Camera", vpCameraParameters::perspectiveProjWithDistortion, 320, 240, false);
120 std::cout << "Cam write:\n" << cam << std::endl;
121 std::cout << "Cam read:\n" << cam_read << std::endl;
122 if (cam != cam_read) {
123 std::cerr << "Issue when parsing XML file: " << filename << std::endl;
124 return EXIT_FAILURE;
125 }
126 }
127
128 {
129 // Without specifying image size
130 vpCameraParameters cam_read;
132 xml.parse(cam_read, filename, "Camera", vpCameraParameters::perspectiveProjWithDistortion, 0, 0, false);
133 std::cout << "Cam write:\n" << cam << std::endl;
134 std::cout << "Cam read:\n" << cam_read << std::endl;
135 if (cam != cam_read) {
136 std::cerr << "Issue when parsing XML file: " << filename << std::endl;
137 return EXIT_FAILURE;
138 }
139 }
140 }
141
142 {
143 std::cout << "-- Test to save/load multiple cameras with and without distortion in a single file" << std::endl;
144 std::string filename = tmp_dir + "test_write_cam_multiple.xml";
145 vpCameraParameters cam1_w_d;
146 cam1_w_d.initPersProjWithDistortion(200, 200, 160, 120, 0.02, -0.02);
147 {
149 std::cout << "Write to: " << filename << " camera:\n" << cam1_w_d << std::endl;
150 if (xml.save(cam1_w_d, filename, "Camera 1", 320, 240) != vpXmlParserCamera::SEQUENCE_OK) {
151 std::cerr << "Cannot save XML file: " << filename << std::endl;
152 return EXIT_FAILURE;
153 }
154 }
155
156 vpCameraParameters cam1_wo_d;
157 cam1_wo_d.initPersProjWithoutDistortion(200, 200, 160, 120);
158 {
160 std::cout << "Write to: " << filename << " camera:\n" << cam1_wo_d << std::endl;
161 if (xml.save(cam1_wo_d, filename, "Camera 1", 320, 240) != vpXmlParserCamera::SEQUENCE_OK) {
162 std::cerr << "Cannot save XML file: " << filename << std::endl;
163 return EXIT_FAILURE;
164 }
165 }
166 vpCameraParameters cam2_w_d;
167 cam2_w_d.initPersProjWithDistortion(400, 400, 320, 240, 0.02, -0.02);
168 {
170 std::cout << "Write to: " << filename << " camera:\n" << cam2_w_d << std::endl;
171 if (xml.save(cam2_w_d, filename, "Camera 2", 640, 480) != vpXmlParserCamera::SEQUENCE_OK) {
172 std::cerr << "Cannot save XML file: " << filename << std::endl;
173 return EXIT_FAILURE;
174 }
175 }
176
177 vpCameraParameters cam2_wo_d;
178 cam2_wo_d.initPersProjWithoutDistortion(400, 400, 320, 240);
179 {
181 std::cout << "Write to: " << filename << " camera:\n" << cam2_wo_d << std::endl;
182 if (xml.save(cam2_wo_d, filename, "Camera 2", 640, 480) != vpXmlParserCamera::SEQUENCE_OK) {
183 std::cerr << "Cannot save XML file: " << filename << std::endl;
184 return EXIT_FAILURE;
185 }
186 }
187
188 {
189 vpCameraParameters cam_read;
191 xml.parse(cam_read, filename, "Camera 1", vpCameraParameters::perspectiveProjWithDistortion, 320, 240, false);
192 std::cout << "Cam write:\n" << cam1_w_d << std::endl;
193 std::cout << "Cam read:\n" << cam_read << std::endl;
194 if (cam1_w_d != cam_read) {
195 std::cerr << "Issue when parsing XML file: " << filename << std::endl;
196 return EXIT_FAILURE;
197 }
198 }
199 {
200 vpCameraParameters cam_read;
202 xml.parse(cam_read, filename, "Camera 1", vpCameraParameters::perspectiveProjWithoutDistortion, 320, 240, false);
203 std::cout << "Cam write:\n" << cam1_wo_d << std::endl;
204 std::cout << "Cam read:\n" << cam_read << std::endl;
205 if (cam1_wo_d != cam_read) {
206 std::cerr << "Issue when parsing XML file: " << filename << std::endl;
207 return EXIT_FAILURE;
208 }
209 }
210 {
211 vpCameraParameters cam_read;
213 xml.parse(cam_read, filename, "Camera 2", vpCameraParameters::perspectiveProjWithDistortion, 640, 480, false);
214 std::cout << "Cam write:\n" << cam2_w_d << std::endl;
215 std::cout << "Cam read:\n" << cam_read << std::endl;
216 if (cam2_w_d != cam_read) {
217 std::cerr << "Issue when parsing XML file: " << filename << std::endl;
218 return EXIT_FAILURE;
219 }
220 }
221 {
222 vpCameraParameters cam_read;
224 xml.parse(cam_read, filename, "Camera 2", vpCameraParameters::perspectiveProjWithoutDistortion, 640, 480, false);
225 std::cout << "Cam write:\n" << cam2_wo_d << std::endl;
226 std::cout << "Cam read:\n" << cam_read << std::endl;
227 if (cam2_wo_d != cam_read) {
228 std::cerr << "Issue when parsing XML file: " << filename << std::endl;
229 return EXIT_FAILURE;
230 }
231 }
232 }
233
234 {
235 std::cout << "-- Test to save/load one single camera with Kannala Brandt distortion in a single file" << std::endl;
237 std::vector<double> distortion_coeffs;
238 distortion_coeffs.push_back(-0.00297341705299914);
239 distortion_coeffs.push_back(0.0352853797376156);
240 distortion_coeffs.push_back(-0.032205019146204);
241 distortion_coeffs.push_back(0.004446716979146);
242 distortion_coeffs.push_back(0);
243 cam.initProjWithKannalaBrandtDistortion(285.523895263672, 286.6708984375, 420.874114990234, 381.085388183594,
244 distortion_coeffs);
245 std::string filename = tmp_dir + "test_write_cam_with_KannalaBrandt_distortion.xml";
246 {
248 std::cout << "Write to: " << filename << std::endl;
249 if (xml.save(cam, filename, "Camera", 800, 848) != vpXmlParserCamera::SEQUENCE_OK) {
250 std::cerr << "Cannot save XML file: " << filename << std::endl;
251 return EXIT_FAILURE;
252 }
253 }
254
255 vpCameraParameters cam_read;
256 {
258 xml.parse(cam_read, filename, "Camera", vpCameraParameters::ProjWithKannalaBrandtDistortion, 800, 848, false);
259 std::cout << "Cam write:\n" << cam << std::endl;
260 std::cout << "Cam read:\n" << cam_read << std::endl;
261 if (cam != cam_read) {
262 std::cerr << "Issue when parsing XML file: " << filename << std::endl;
263 return EXIT_FAILURE;
264 }
265 }
266 }
267
268 vpIoTools::remove(tmp_dir);
269
270 return EXIT_SUCCESS;
271}
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()
static void makeDirectory(const std::string &dirname)
static bool remove(const std::string &filename)
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)