49 #include <visp/vpXmlParserCamera.h>
55 #include <visp/vpDebug.h>
60 #define LABEL_XML_ROOT "root"
61 #define LABEL_XML_CAMERA "camera"
62 #define LABEL_XML_CAMERA_NAME "name"
63 #define LABEL_XML_WIDTH "image_width"
64 #define LABEL_XML_HEIGHT "image_height"
65 #define LABEL_XML_SUBSAMPLING_WIDTH "subsampling_width"
66 #define LABEL_XML_SUBSAMPLING_HEIGHT "subsampling_height"
67 #define LABEL_XML_FULL_WIDTH "full_width"
68 #define LABEL_XML_FULL_HEIGHT "full_height"
69 #define LABEL_XML_MODEL "model"
70 #define LABEL_XML_MODEL_TYPE "type"
71 #define LABEL_XML_U0 "u0"
72 #define LABEL_XML_V0 "v0"
73 #define LABEL_XML_PX "px"
74 #define LABEL_XML_PY "py"
75 #define LABEL_XML_KUD "kud"
76 #define LABEL_XML_KDU "kdu"
78 #define LABEL_XML_MODEL_WITHOUT_DISTORTION "perspectiveProjWithoutDistortion"
79 #define LABEL_XML_MODEL_WITH_DISTORTION "perspectiveProjWithDistortion"
85 camera(), camera_name(), image_width(0), image_height(0),
86 subsampling_width(0), subsampling_height(0), full_width(0), full_height(0)
95 camera(), camera_name(), image_width(0), image_height(0),
96 subsampling_width(0), subsampling_height(0), full_width(0), full_height(0)
99 this->camera = twinParser.camera;
100 this->camera_name = twinParser.camera_name;
101 this->image_width = twinParser.image_width;
102 this->image_height = twinParser.image_height;
103 this->subsampling_width = twinParser.subsampling_width;
104 this->subsampling_height = twinParser.subsampling_height;
105 this->full_width = twinParser.full_width;
106 this->full_height = twinParser.full_height;
116 this->camera = twinParser.camera;
117 this->camera_name = twinParser.camera_name;
118 this->image_width = twinParser.image_width;
119 this->image_height = twinParser.image_height;
120 this->subsampling_width = twinParser.subsampling_width;
121 this->subsampling_height = twinParser.subsampling_height;
122 this->full_width = twinParser.full_width;
123 this->full_height = twinParser.full_height;
143 const std::string& cam_name,
145 const unsigned int im_width,
146 const unsigned int im_height)
151 doc = xmlParseFile(filename.c_str());
157 node = xmlDocGetRootElement(doc);
164 int ret =
this ->read (doc, node, cam_name, projModel, im_width, im_height);
188 const std::string& cam_name,
189 const unsigned int im_width,
190 const unsigned int im_height)
194 xmlNodePtr nodeCamera = NULL;
196 doc = xmlReadFile(filename.c_str(), NULL, XML_PARSE_NOWARNING + XML_PARSE_NOERROR
197 + XML_PARSE_NOBLANKS);
199 doc = xmlNewDoc ((xmlChar*)
"1.0");
200 node = xmlNewNode(NULL,(xmlChar*)LABEL_XML_ROOT);
201 xmlDocSetRootElement(doc,node);
202 xmlNodePtr node_tmp = xmlNewComment((xmlChar*)
203 "This file stores intrinsic camera parameters used\n"
204 " in the vpCameraParameters Class of ViSP available\n"
205 " at http://www.irisa.fr/lagadic/visp/visp.html .\n"
206 " It can be read with the parse method of\n"
207 " the vpXmlParserCamera class.");
208 xmlAddChild(node,node_tmp);
211 node = xmlDocGetRootElement(doc);
220 int nbCamera = count(doc, node, cam_name,cam.
get_projModel(),
221 im_width, im_height);
232 nodeCamera = find_camera(doc, node, cam_name, im_width, im_height);
233 if(nodeCamera == NULL){
234 write(node, cam_name, im_width, im_height);
237 write_camera(nodeCamera);
240 xmlSaveFormatFile(filename.c_str(), doc, 1);
267 vpXmlParserCamera::read (xmlDocPtr doc, xmlNodePtr node,
268 const std::string& cam_name,
270 const unsigned int im_width,
271 const unsigned int im_height,
272 const unsigned int subsampl_width,
273 const unsigned int subsampl_height)
279 unsigned int nbCamera = 0;
281 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
283 if (node->type != XML_ELEMENT_NODE)
continue;
284 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
304 if (
SEQUENCE_OK == this->read_camera (doc, node, cam_name, projModel,
305 im_width, im_height, subsampl_width, subsampl_height))
313 vpCERROR <<
"No camera parameters is available" << std::endl
314 <<
"with your specifications" << std::endl;
316 else if(nbCamera > 1){
318 vpCERROR << nbCamera <<
" sets of camera parameters are available" << std::endl
319 <<
"with your specifications : " << std::endl
320 <<
"precise your choice..." << std::endl;
345 vpXmlParserCamera::count (xmlDocPtr doc, xmlNodePtr node,
346 const std::string& cam_name,
348 const unsigned int im_width,
349 const unsigned int im_height,
350 const unsigned int subsampl_width,
351 const unsigned int subsampl_height)
357 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
359 if (node->type != XML_ELEMENT_NODE)
continue;
360 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
379 if (
SEQUENCE_OK == this->read_camera (doc, node, cam_name, projModel,
381 subsampl_width, subsampl_height))
408 vpXmlParserCamera::find_camera (xmlDocPtr doc, xmlNodePtr node,
409 const std::string& cam_name,
410 const unsigned int im_width,
411 const unsigned int im_height,
412 const unsigned int subsampl_width,
413 const unsigned int subsampl_height)
418 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
420 if (node->type != XML_ELEMENT_NODE)
continue;
421 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
440 if (
SEQUENCE_OK == this->read_camera_header(doc, node, cam_name,
442 subsampl_width, subsampl_height))
469 vpXmlParserCamera::read_camera (xmlDocPtr doc, xmlNodePtr node,
470 const std::string& cam_name,
472 const unsigned int im_width,
473 const unsigned int im_height,
474 const unsigned int subsampl_width,
475 const unsigned int subsampl_height)
479 std::string camera_name_tmp =
"";
480 unsigned int image_height_tmp = 0 ;
481 unsigned int image_width_tmp = 0 ;
482 unsigned int subsampling_width_tmp = 0;
483 unsigned int subsampling_height_tmp = 0;
488 bool projModelFound =
false;
491 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
494 if (node->type != XML_ELEMENT_NODE)
continue;
495 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
506 camera_name_tmp = val_char;
507 std::cout <<
"Found camera with name: \"" << camera_name_tmp <<
"\"" << std::endl;
533 back = read_camera_model(doc, node, cam_tmp_model);
535 cam_tmp = cam_tmp_model;
536 projModelFound =
true;
559 bool test_subsampling_width =
true;
560 bool test_subsampling_height =
true;
562 if (subsampling_width) {
563 test_subsampling_width = (abs((
int)subsampl_width - (
int)subsampling_width_tmp) < (allowedPixelDiffOnImageSize * (int)(subsampling_width_tmp / subsampling_width)));
565 if (subsampling_height) {
566 test_subsampling_height = (abs((
int)subsampl_height - (
int)subsampling_height_tmp) < (allowedPixelDiffOnImageSize * (int)(subsampling_height_tmp / subsampling_height)));
568 if( !((projModelFound ==
true) && (cam_name == camera_name_tmp) &&
569 (abs((
int)im_width - (int)image_width_tmp) < allowedPixelDiffOnImageSize || im_width == 0) &&
570 (abs((
int)im_height - (int)image_height_tmp) < allowedPixelDiffOnImageSize || im_height == 0) &&
571 (test_subsampling_width)&&
572 (test_subsampling_height))){
576 this->camera = cam_tmp;
577 this->camera_name = camera_name_tmp;
578 this->image_width = image_width_tmp;
579 this->image_height = image_height_tmp;
580 this->subsampling_width = subsampling_width_tmp;
581 this->subsampling_height = subsampling_height_tmp;
582 this->full_width = subsampling_width_tmp * image_width_tmp;
583 this->full_height = subsampling_height_tmp * image_height_tmp;
608 read_camera_header (xmlDocPtr doc, xmlNodePtr node,
609 const std::string& cam_name,
610 const unsigned int im_width,
611 const unsigned int im_height,
612 const unsigned int subsampl_width,
613 const unsigned int subsampl_height)
617 std::string camera_name_tmp =
"";
618 unsigned int image_height_tmp = 0 ;
619 unsigned int image_width_tmp = 0 ;
620 unsigned int subsampling_width_tmp = 0;
621 unsigned int subsampling_height_tmp = 0;
626 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
629 if (node->type != XML_ELEMENT_NODE)
continue;
630 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
641 camera_name_tmp = val_char;
686 if( !((cam_name == camera_name_tmp) &&
687 (im_width == image_width_tmp || im_width == 0) &&
688 (im_height == image_height_tmp || im_height == 0) &&
689 (subsampl_width == subsampling_width_tmp ||
690 subsampl_width == 0)&&
691 (subsampl_height == subsampling_height_tmp ||
692 subsampl_height == 0))){
709 vpXmlParserCamera::read_camera_model (xmlDocPtr doc, xmlNodePtr node,
717 char* model_type = NULL;
718 double u0 = cam_tmp.
get_u0();
719 double v0 = cam_tmp.
get_v0();
720 double px = cam_tmp.
get_px();
721 double py = cam_tmp.
get_py();
722 double kud = cam_tmp.
get_kud();
723 double kdu = cam_tmp.
get_kdu();
727 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
730 if (node->type != XML_ELEMENT_NODE)
continue;
731 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
740 if(model_type != NULL){
745 validation = validation | 0x01;
750 validation = validation | 0x02;
755 validation = validation | 0x04;
760 validation = validation | 0x08;
765 validation = validation | 0x10;
770 validation = validation | 0x20;
775 validation = validation | 0x40;
794 if(model_type == NULL) {
795 vpERROR_TRACE(
"projection model type doesn't match with any known model !");
799 if( !strcmp(model_type,LABEL_XML_MODEL_WITHOUT_DISTORTION)){
800 if (nb != 5 || validation != 0x1F)
802 vpCERROR <<
"ERROR in 'model' field:\n";
803 vpCERROR <<
"it must contain 5 parameters\n";
810 else if( !strcmp(model_type,LABEL_XML_MODEL_WITH_DISTORTION)){
811 if (nb != 7 || validation != 0x7F)
813 vpCERROR <<
"ERROR in 'model' field:\n";
814 vpCERROR <<
"it must contain 7 parameters\n";
822 vpERROR_TRACE(
"projection model type doesn't match with any known model !");
849 int vpXmlParserCamera::
850 write (xmlNodePtr node,
const std::string& cam_name,
851 const unsigned int im_width,
const unsigned int im_height,
852 const unsigned int subsampl_width,
853 const unsigned int subsampl_height)
858 xmlNodePtr node_camera;
861 node_camera = xmlNewNode(NULL,(xmlChar*)LABEL_XML_CAMERA);
862 xmlAddChild(node,node_camera);
866 if(!cam_name.empty()){
867 node_tmp = xmlNewComment((xmlChar*)
"Name of the camera");
868 xmlAddChild(node_camera,node_tmp);
869 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_CAMERA_NAME,
870 (xmlChar*)cam_name.c_str());
873 if(im_width != 0 || im_height != 0){
876 node_tmp = xmlNewComment((xmlChar*)
"Size of the image on which camera calibration was performed");
877 xmlAddChild(node_camera,node_tmp);
879 sprintf(str,
"%u",im_width);
880 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_WIDTH,(xmlChar*)str);
883 sprintf(str,
"%u",im_height);
884 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_HEIGHT,(xmlChar*)str);
885 if(subsampling_width != 0 || subsampling_height != 0){
886 node_tmp = xmlNewComment((xmlChar*)
"Subsampling used to obtain the current size of the image.");
887 xmlAddChild(node_camera,node_tmp);
890 sprintf(str,
"%u",subsampl_width);
891 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_SUBSAMPLING_WIDTH,
894 sprintf(str,
"%u",subsampl_height);
895 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_SUBSAMPLING_HEIGHT,
897 node_tmp = xmlNewComment((xmlChar*)
"The full size is the sensor size actually used to grab the image. full_width = subsampling_width * image_width");
898 xmlAddChild(node_camera,node_tmp);
901 sprintf(str,
"%u",im_width*subsampl_width);
902 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_FULL_WIDTH,
905 sprintf(str,
"%u",im_height*subsampl_height);
906 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_FULL_HEIGHT,
911 node_tmp = xmlNewComment((xmlChar*)
"Intrinsic camera parameters computed for each projection model");
913 xmlAddChild(node_camera,node_tmp);
915 back = write_camera(node_camera);
926 int vpXmlParserCamera::
927 write_camera(xmlNodePtr node_camera){
928 xmlNodePtr node_model;
935 node_model = xmlNewNode(NULL,(xmlChar*)LABEL_XML_MODEL);
936 xmlAddChild(node_camera,node_model);
939 node_tmp = xmlNewComment((xmlChar*)
"Projection model type");
940 xmlAddChild(node_model,node_tmp);
943 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_MODEL_TYPE,
944 (xmlChar*)LABEL_XML_MODEL_WITHOUT_DISTORTION);
946 node_tmp = xmlNewComment((xmlChar*)
"Pixel ratio");
947 xmlAddChild(node_model,node_tmp);
949 sprintf(str,
"%.10f",camera.
get_px());
950 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PX,(xmlChar*)str);
952 sprintf(str,
"%.10f",camera.
get_py());
953 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PY,(xmlChar*)str);
955 node_tmp = xmlNewComment((xmlChar*)
"Principal point");
956 xmlAddChild(node_model,node_tmp);
959 sprintf(str,
"%.10f",camera.
get_u0());
960 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_U0,(xmlChar*)str);
962 sprintf(str,
"%.10f",camera.
get_v0());
963 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_V0,(xmlChar*)str);
968 node_model = xmlNewNode(NULL,(xmlChar*)LABEL_XML_MODEL);
969 xmlAddChild(node_camera,node_model);
972 node_tmp = xmlNewComment((xmlChar*)
"Projection model type");
973 xmlAddChild(node_model,node_tmp);
975 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_MODEL_TYPE,
976 (xmlChar*)LABEL_XML_MODEL_WITH_DISTORTION);
978 node_tmp = xmlNewComment((xmlChar*)
"Pixel ratio");
979 xmlAddChild(node_model,node_tmp);
981 sprintf(str,
"%.10f",camera.
get_px());
982 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PX,(xmlChar*)str);
984 sprintf(str,
"%.10f",camera.
get_py());
985 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PY,(xmlChar*)str);
987 node_tmp = xmlNewComment((xmlChar*)
"Principal point");
988 xmlAddChild(node_model,node_tmp);
990 sprintf(str,
"%.10f",camera.
get_u0());
991 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_U0,(xmlChar*)str);
993 sprintf(str,
"%.10f",camera.
get_v0());
994 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_V0,(xmlChar*)str);
997 node_tmp = xmlNewComment((xmlChar*)
"Undistorted to distorted distortion parameter");
998 xmlAddChild(node_model,node_tmp);
999 sprintf(str,
"%.10f",camera.
get_kud());
1000 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_KUD,(xmlChar*)str);
1003 node_tmp = xmlNewComment((xmlChar*)
"Distorted to undistorted distortion parameter");
1004 xmlAddChild(node_model,node_tmp);
1005 sprintf(str,
"%.10f",camera.
get_kdu());
1006 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_KDU,(xmlChar*)str);
1022 vpXmlParserCamera::str2xmlcode (
char * str, vpXmlCodeType & res)
1029 if (! strcmp (str, LABEL_XML_CAMERA))
1033 else if (! strcmp (str, LABEL_XML_CAMERA_NAME))
1037 else if (! strcmp (str, LABEL_XML_MODEL))
1041 else if (! strcmp (str, LABEL_XML_MODEL_TYPE))
1045 else if (! strcmp (str, LABEL_XML_WIDTH))
1049 else if (! strcmp (str, LABEL_XML_HEIGHT))
1053 else if (! strcmp (str, LABEL_XML_SUBSAMPLING_WIDTH))
1057 else if (! strcmp (str, LABEL_XML_SUBSAMPLING_HEIGHT))
1061 else if (! strcmp (str, LABEL_XML_FULL_WIDTH))
1065 else if (! strcmp (str, LABEL_XML_FULL_HEIGHT))
1069 else if (! strcmp (str, LABEL_XML_U0))
1073 else if (! strcmp (str, LABEL_XML_V0))
1077 else if (! strcmp (str, LABEL_XML_PX))
1081 else if (! strcmp (str, LABEL_XML_PY))
1085 else if (! strcmp (str, LABEL_XML_KUD))
1089 else if (! strcmp (str, LABEL_XML_KDU))
1101 #endif //VISP_HAVE_XML2
Perspective projection without distortion model.
vpXmlParserCamera & operator=(const vpXmlParserCamera &twinparser)
double xmlReadDoubleChild(xmlDocPtr doc, xmlNodePtr node)
XML parser to load and save intrinsic camera parameters.
char * xmlReadCharChild(xmlDocPtr doc, xmlNodePtr node)
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
This class intends to simplify the creation of xml parser based on the libxml2 third party library...
int save(const vpCameraParameters &cam, const std::string &filename, const std::string &camera_name, const unsigned int image_width=0, const unsigned int image_height=0)
vpCameraParametersProjType
Generic class defining intrinsic camera parameters.
Perspective projection with distortion model.
unsigned int xmlReadUnsignedIntChild(xmlDocPtr doc, xmlNodePtr node)
vpCameraParametersProjType get_projModel() const
int parse(vpCameraParameters &cam, const std::string &filename, const std::string &camera_name, const vpCameraParameters::vpCameraParametersProjType &projModel, const unsigned int image_width=0, const unsigned int image_height=0)
void initPersProjWithDistortion(const double px, const double py, const double u0, const double v0, const double kud, const double kdu)