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"
87 subsampling_width = 0;
88 subsampling_height = 0;
97 this->camera = twinParser.camera;
98 this->camera_name = twinParser.camera_name;
99 this->image_width = twinParser.image_width;
100 this->image_height = twinParser.image_height;
101 this->subsampling_width = twinParser.subsampling_width;
102 this->subsampling_height = twinParser.subsampling_height;
103 this->full_width = twinParser.full_width;
104 this->full_height = twinParser.full_height;
114 this->camera = twinParser.camera;
115 this->camera_name = twinParser.camera_name;
116 this->image_width = twinParser.image_width;
117 this->image_height = twinParser.image_height;
118 this->subsampling_width = twinParser.subsampling_width;
119 this->subsampling_height = twinParser.subsampling_height;
120 this->full_width = twinParser.full_width;
121 this->full_height = twinParser.full_height;
141 const std::string& camera_name,
143 const unsigned int image_width,
144 const unsigned int image_height)
149 doc = xmlParseFile(filename);
155 node = xmlDocGetRootElement(doc);
162 int ret =
this ->read (doc, node, camera_name, projModel, image_width, image_height);
186 const std::string& camera_name,
187 const unsigned int image_width,
188 const unsigned int image_height)
192 xmlNodePtr nodeCamera = NULL;
194 doc = xmlReadFile(filename,NULL,XML_PARSE_NOWARNING + XML_PARSE_NOERROR
195 + XML_PARSE_NOBLANKS);
197 doc = xmlNewDoc ((xmlChar*)
"1.0");
198 node = xmlNewNode(NULL,(xmlChar*)LABEL_XML_ROOT);
199 xmlDocSetRootElement(doc,node);
200 xmlNodePtr node_tmp = xmlNewComment((xmlChar*)
201 "This file stores intrinsic camera parameters used\n"
202 " in the vpCameraParameters Class of ViSP available\n"
203 " at http://www.irisa.fr/lagadic/visp/visp.html .\n"
204 " It can be read with the parse method of\n"
205 " the vpXmlParserCamera class.");
206 xmlAddChild(node,node_tmp);
209 node = xmlDocGetRootElement(doc);
218 int nbCamera = count(doc, node, camera_name,cam.
get_projModel(),
219 image_width, image_height);
230 nodeCamera = find_camera(doc, node, camera_name, image_width, image_height);
231 if(nodeCamera == NULL){
232 write(node, camera_name, image_width, image_height);
235 write_camera(nodeCamera);
238 xmlSaveFormatFile(filename,doc,1);
265 vpXmlParserCamera::read (xmlDocPtr doc, xmlNodePtr node,
266 const std::string& camera_name,
268 const unsigned int image_width,
269 const unsigned int image_height,
270 const unsigned int subsampling_width,
271 const unsigned int subsampling_height)
279 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
281 if (node->type != XML_ELEMENT_NODE)
continue;
282 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
302 if (
SEQUENCE_OK == this->read_camera (doc, node, camera_name, projModel,
303 image_width, image_height, subsampling_width, subsampling_height))
311 vpCERROR <<
"No camera parameters is available" << std::endl
312 <<
"with your specifications" << std::endl;
314 else if(nbCamera > 1){
316 vpCERROR << nbCamera <<
" sets of camera parameters are available" << std::endl
317 <<
"with your specifications : " << std::endl
318 <<
"precise your choice..." << std::endl;
343 vpXmlParserCamera::count (xmlDocPtr doc, xmlNodePtr node,
344 const std::string& camera_name,
346 const unsigned int image_width,
347 const unsigned int image_height,
348 const unsigned int subsampling_width,
349 const unsigned int subsampling_height)
355 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
357 if (node->type != XML_ELEMENT_NODE)
continue;
358 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
377 if (
SEQUENCE_OK == this->read_camera (doc, node, camera_name, projModel,
378 image_width, image_height,
379 subsampling_width, subsampling_height))
406 vpXmlParserCamera::find_camera (xmlDocPtr doc, xmlNodePtr node,
407 const std::string& camera_name,
408 const unsigned int image_width,
409 const unsigned int image_height,
410 const unsigned int subsampling_width,
411 const unsigned int subsampling_height)
416 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
418 if (node->type != XML_ELEMENT_NODE)
continue;
419 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
438 if (
SEQUENCE_OK == this->read_camera_header(doc, node, camera_name,
439 image_width, image_height,
440 subsampling_width, subsampling_height))
467 vpXmlParserCamera::read_camera (xmlDocPtr doc, xmlNodePtr node,
468 const std::string& camera_name,
470 const unsigned int image_width,
471 const unsigned int image_height,
472 const unsigned int subsampling_width,
473 const unsigned int subsampling_height)
477 std::string camera_name_tmp =
"";
478 unsigned int image_height_tmp = 0 ;
479 unsigned int image_width_tmp = 0 ;
480 unsigned int subsampling_width_tmp = 0;
481 unsigned int subsampling_height_tmp = 0;
486 bool projModelFound =
false;
489 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
492 if (node->type != XML_ELEMENT_NODE)
continue;
493 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
504 camera_name_tmp = val_char;
530 back = read_camera_model(doc, node, cam_tmp_model);
532 cam_tmp = cam_tmp_model;
533 projModelFound =
true;
555 if( !((projModelFound ==
true) && (camera_name == camera_name_tmp) &&
556 (abs((
int)image_width - (
int)image_width_tmp) < allowedPixelDiffOnImageSize || image_width == 0) &&
557 (abs((
int)image_height - (
int)image_height_tmp) < allowedPixelDiffOnImageSize || image_height == 0) &&
558 ( subsampling_width == 0 ||
559 abs((
int)subsampling_width - (
int)subsampling_width_tmp) < (allowedPixelDiffOnImageSize * (
int)(subsampling_width_tmp / subsampling_width)))&&
560 ( subsampling_height == 0 ||
561 abs((
int)subsampling_height - (
int)subsampling_height_tmp) < (allowedPixelDiffOnImageSize * (
int)(subsampling_width_tmp / subsampling_width))))){
565 this->camera = cam_tmp;
566 this->camera_name = camera_name_tmp;
567 this->image_width = image_width_tmp;
568 this->image_height = image_height_tmp;
569 this->subsampling_width = subsampling_width_tmp;
570 this->subsampling_height = subsampling_height_tmp;
571 this->full_width = subsampling_width_tmp * image_width_tmp;
572 this->full_height = subsampling_height_tmp * image_height_tmp;
597 read_camera_header (xmlDocPtr doc, xmlNodePtr node,
598 const std::string& camera_name,
599 const unsigned int image_width,
600 const unsigned int image_height,
601 const unsigned int subsampling_width,
602 const unsigned int subsampling_height)
606 std::string camera_name_tmp =
"";
607 unsigned int image_height_tmp = 0 ;
608 unsigned int image_width_tmp = 0 ;
609 unsigned int subsampling_width_tmp = 0;
610 unsigned int subsampling_height_tmp = 0;
615 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
618 if (node->type != XML_ELEMENT_NODE)
continue;
619 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
630 camera_name_tmp = val_char;
675 if( !((camera_name == camera_name_tmp) &&
676 (image_width == image_width_tmp || image_width == 0) &&
677 (image_height == image_height_tmp || image_height == 0) &&
678 (subsampling_width == subsampling_width_tmp ||
679 subsampling_width == 0)&&
680 (subsampling_height == subsampling_height_tmp ||
681 subsampling_height == 0))){
698 vpXmlParserCamera::read_camera_model (xmlDocPtr doc, xmlNodePtr node,
706 char* model_type = NULL;
707 double u0 = cam_tmp.
get_u0();
708 double v0 = cam_tmp.
get_v0();
709 double px = cam_tmp.
get_px();
710 double py = cam_tmp.
get_py();
711 double kud = cam_tmp.
get_kud();
712 double kdu = cam_tmp.
get_kdu();
716 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
719 if (node->type != XML_ELEMENT_NODE)
continue;
720 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
729 if(model_type != NULL){
734 validation = validation | 0x01;
739 validation = validation | 0x02;
744 validation = validation | 0x04;
749 validation = validation | 0x08;
754 validation = validation | 0x10;
759 validation = validation | 0x20;
764 validation = validation | 0x40;
783 if( !strcmp(model_type,LABEL_XML_MODEL_WITHOUT_DISTORTION)){
784 if (nb != 5 || validation != 0x1F)
786 vpCERROR <<
"ERROR in 'model' field:\n";
787 vpCERROR <<
"it must contain 5 parameters\n";
788 if(model_type != NULL){
795 else if( !strcmp(model_type,LABEL_XML_MODEL_WITH_DISTORTION)){
796 if (nb != 7 || validation != 0x7F)
798 vpCERROR <<
"ERROR in 'model' field:\n";
799 vpCERROR <<
"it must contain 7 parameters\n";
800 if(model_type != NULL){
808 vpERROR_TRACE(
"projection model type doesn't match with any known model !");
809 if(model_type != NULL){
814 if(model_type != NULL){
837 int vpXmlParserCamera::
838 write (xmlNodePtr node,
const std::string& camera_name,
839 const unsigned int image_width,
const unsigned int image_height,
840 const unsigned int subsampling_width,
841 const unsigned int subsampling_height)
846 xmlNodePtr node_camera;
849 node_camera = xmlNewNode(NULL,(xmlChar*)LABEL_XML_CAMERA);
850 xmlAddChild(node,node_camera);
854 if(!camera_name.empty()){
855 node_tmp = xmlNewComment((xmlChar*)
"Name of the camera");
856 xmlAddChild(node_camera,node_tmp);
857 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_CAMERA_NAME,
858 (xmlChar*)camera_name.c_str());
861 if(image_width != 0 || image_height != 0){
864 node_tmp = xmlNewComment((xmlChar*)
"Size of the image on which camera calibration was performed");
865 xmlAddChild(node_camera,node_tmp);
867 sprintf(str,
"%u",image_width);
868 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_WIDTH,(xmlChar*)str);
871 sprintf(str,
"%u",image_height);
872 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_HEIGHT,(xmlChar*)str);
873 if(subsampling_width != 0 || subsampling_height != 0){
874 node_tmp = xmlNewComment((xmlChar*)
"Subsampling used to obtain the current size of the image.");
875 xmlAddChild(node_camera,node_tmp);
878 sprintf(str,
"%u",subsampling_width);
879 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_SUBSAMPLING_WIDTH,
882 sprintf(str,
"%u",subsampling_height);
883 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_SUBSAMPLING_HEIGHT,
885 node_tmp = xmlNewComment((xmlChar*)
"The full size is the sensor size actually used to grab the image. full_width = subsampling_width * image_width");
886 xmlAddChild(node_camera,node_tmp);
889 sprintf(str,
"%u",image_width*subsampling_width);
890 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_FULL_WIDTH,
893 sprintf(str,
"%u",image_height*subsampling_height);
894 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_FULL_HEIGHT,
899 node_tmp = xmlNewComment((xmlChar*)
"Intrinsic camera parameters computed for each projection model");
901 xmlAddChild(node_camera,node_tmp);
903 back = write_camera(node_camera);
914 int vpXmlParserCamera::
915 write_camera(xmlNodePtr node_camera){
916 xmlNodePtr node_model;
923 node_model = xmlNewNode(NULL,(xmlChar*)LABEL_XML_MODEL);
924 xmlAddChild(node_camera,node_model);
927 node_tmp = xmlNewComment((xmlChar*)
"Projection model type");
928 xmlAddChild(node_model,node_tmp);
931 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_MODEL_TYPE,
932 (xmlChar*)LABEL_XML_MODEL_WITHOUT_DISTORTION);
934 node_tmp = xmlNewComment((xmlChar*)
"Pixel ratio");
935 xmlAddChild(node_model,node_tmp);
937 sprintf(str,
"%.10f",camera.
get_px());
938 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PX,(xmlChar*)str);
940 sprintf(str,
"%.10f",camera.
get_py());
941 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PY,(xmlChar*)str);
943 node_tmp = xmlNewComment((xmlChar*)
"Principal point");
944 xmlAddChild(node_model,node_tmp);
947 sprintf(str,
"%.10f",camera.
get_u0());
948 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_U0,(xmlChar*)str);
950 sprintf(str,
"%.10f",camera.
get_v0());
951 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_V0,(xmlChar*)str);
956 node_model = xmlNewNode(NULL,(xmlChar*)LABEL_XML_MODEL);
957 xmlAddChild(node_camera,node_model);
960 node_tmp = xmlNewComment((xmlChar*)
"Projection model type");
961 xmlAddChild(node_model,node_tmp);
963 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_MODEL_TYPE,
964 (xmlChar*)LABEL_XML_MODEL_WITH_DISTORTION);
966 node_tmp = xmlNewComment((xmlChar*)
"Pixel ratio");
967 xmlAddChild(node_model,node_tmp);
969 sprintf(str,
"%.10f",camera.
get_px());
970 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PX,(xmlChar*)str);
972 sprintf(str,
"%.10f",camera.
get_py());
973 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PY,(xmlChar*)str);
975 node_tmp = xmlNewComment((xmlChar*)
"Principal point");
976 xmlAddChild(node_model,node_tmp);
978 sprintf(str,
"%.10f",camera.
get_u0());
979 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_U0,(xmlChar*)str);
981 sprintf(str,
"%.10f",camera.
get_v0());
982 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_V0,(xmlChar*)str);
985 node_tmp = xmlNewComment((xmlChar*)
"Undistorted to distorted distortion parameter");
986 xmlAddChild(node_model,node_tmp);
987 sprintf(str,
"%.10f",camera.
get_kud());
988 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_KUD,(xmlChar*)str);
991 node_tmp = xmlNewComment((xmlChar*)
"Distorted to undistorted distortion parameter");
992 xmlAddChild(node_model,node_tmp);
993 sprintf(str,
"%.10f",camera.
get_kdu());
994 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_KDU,(xmlChar*)str);
1010 vpXmlParserCamera::str2xmlcode (
char * str, vpXmlCodeType & res)
1017 if (! strcmp (str, LABEL_XML_CAMERA))
1021 else if (! strcmp (str, LABEL_XML_CAMERA_NAME))
1025 else if (! strcmp (str, LABEL_XML_MODEL))
1029 else if (! strcmp (str, LABEL_XML_MODEL_TYPE))
1033 else if (! strcmp (str, LABEL_XML_WIDTH))
1037 else if (! strcmp (str, LABEL_XML_HEIGHT))
1041 else if (! strcmp (str, LABEL_XML_SUBSAMPLING_WIDTH))
1045 else if (! strcmp (str, LABEL_XML_SUBSAMPLING_HEIGHT))
1049 else if (! strcmp (str, LABEL_XML_FULL_WIDTH))
1053 else if (! strcmp (str, LABEL_XML_FULL_HEIGHT))
1057 else if (! strcmp (str, LABEL_XML_U0))
1061 else if (! strcmp (str, LABEL_XML_V0))
1065 else if (! strcmp (str, LABEL_XML_PX))
1069 else if (! strcmp (str, LABEL_XML_PY))
1073 else if (! strcmp (str, LABEL_XML_KUD))
1077 else if (! strcmp (str, LABEL_XML_KDU))
1089 #endif //VISP_HAVE_XML2
Perspective projection without distortion model.
vpXmlParserCamera & operator=(const vpXmlParserCamera &twinparser)
double xmlReadDoubleChild(xmlDocPtr doc, xmlNodePtr node)
int save(const vpCameraParameters &cam, const char *filename, const std::string &camera_name, const unsigned int image_width=0, const unsigned int image_height=0)
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...
vpCameraParametersProjType
Generic class defining intrinsic camera parameters.
Perspective projection with distortion model.
int parse(vpCameraParameters &cam, const char *filename, const std::string &camera_name, const vpCameraParameters::vpCameraParametersProjType &projModel, const unsigned int image_width=0, const unsigned int image_height=0)
unsigned int xmlReadUnsignedIntChild(xmlDocPtr doc, xmlNodePtr node)
vpCameraParametersProjType get_projModel() const
void initPersProjWithDistortion(const double px, const double py, const double u0, const double v0, const double kud, const double kdu)