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);
216 int nbCamera = count(doc, node, camera_name,cam.
get_projModel(),
217 image_width, image_height);
220 <<
" set(s) of camera parameters is(are) already "<< std::endl
221 <<
"available in the file with your specifications : "<< std::endl
222 <<
"precise the grabber parameters or delete manually"<< std::endl
223 <<
"the previous one."<<std::endl;
229 nodeCamera = find_camera(doc, node, camera_name, image_width, image_height);
230 if(nodeCamera == NULL){
231 write(node, camera_name, image_width, image_height);
234 write_camera(nodeCamera);
236 xmlSaveFormatFile(filename,doc,1);
263 vpXmlParserCamera::read (xmlDocPtr doc, xmlNodePtr node,
264 const std::string& camera_name,
266 const unsigned int image_width,
267 const unsigned int image_height,
268 const unsigned int subsampling_width,
269 const unsigned int subsampling_height)
277 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
279 if (node->type != XML_ELEMENT_NODE)
continue;
280 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
300 if (
SEQUENCE_OK == this->read_camera (doc, node, camera_name, projModel,
301 image_width, image_height, subsampling_width, subsampling_height))
309 vpCERROR <<
"No camera parameters is available" << std::endl
310 <<
"with your specifications" << std::endl;
312 else if(nbCamera > 1){
314 vpCERROR << nbCamera <<
" sets of camera parameters are available" << std::endl
315 <<
"with your specifications : " << std::endl
316 <<
"precise your choice..." << std::endl;
341 vpXmlParserCamera::count (xmlDocPtr doc, xmlNodePtr node,
342 const std::string& camera_name,
344 const unsigned int image_width,
345 const unsigned int image_height,
346 const unsigned int subsampling_width,
347 const unsigned int subsampling_height)
353 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
355 if (node->type != XML_ELEMENT_NODE)
continue;
356 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
375 if (
SEQUENCE_OK == this->read_camera (doc, node, camera_name, projModel,
376 image_width, image_height,
377 subsampling_width, subsampling_height))
404 vpXmlParserCamera::find_camera (xmlDocPtr doc, xmlNodePtr node,
405 const std::string& camera_name,
406 const unsigned int image_width,
407 const unsigned int image_height,
408 const unsigned int subsampling_width,
409 const unsigned int subsampling_height)
414 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
416 if (node->type != XML_ELEMENT_NODE)
continue;
417 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
436 if (
SEQUENCE_OK == this->read_camera_header(doc, node, camera_name,
437 image_width, image_height,
438 subsampling_width, subsampling_height))
465 vpXmlParserCamera::read_camera (xmlDocPtr doc, xmlNodePtr node,
466 const std::string& camera_name,
468 const unsigned int image_width,
469 const unsigned int image_height,
470 const unsigned int subsampling_width,
471 const unsigned int subsampling_height)
475 std::string camera_name_tmp =
"";
476 unsigned int image_height_tmp = 0 ;
477 unsigned int image_width_tmp = 0 ;
478 unsigned int subsampling_width_tmp = 0;
479 unsigned int subsampling_height_tmp = 0;
484 bool projModelFound =
false;
487 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
490 if (node->type != XML_ELEMENT_NODE)
continue;
491 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
502 camera_name_tmp = val_char;
528 back = read_camera_model(doc, node, cam_tmp_model);
530 cam_tmp = cam_tmp_model;
531 projModelFound =
true;
553 if( !((projModelFound ==
true) && (camera_name == camera_name_tmp) &&
554 (abs((
int)image_width - (
int)image_width_tmp) < allowedPixelDiffOnImageSize || image_width == 0) &&
555 (abs((
int)image_height - (
int)image_height_tmp) < allowedPixelDiffOnImageSize || image_height == 0) &&
556 ( subsampling_width == 0 ||
557 abs((
int)subsampling_width - (
int)subsampling_width_tmp) < (allowedPixelDiffOnImageSize * (
int)(subsampling_width_tmp / subsampling_width)))&&
558 ( subsampling_height == 0 ||
559 abs((
int)subsampling_height - (
int)subsampling_height_tmp) < (allowedPixelDiffOnImageSize * (
int)(subsampling_width_tmp / subsampling_width))))){
563 this->camera = cam_tmp;
564 this->camera_name = camera_name_tmp;
565 this->image_width = image_width_tmp;
566 this->image_height = image_height_tmp;
567 this->subsampling_width = subsampling_width_tmp;
568 this->subsampling_height = subsampling_height_tmp;
569 this->full_width = subsampling_width_tmp * image_width_tmp;
570 this->full_height = subsampling_height_tmp * image_height_tmp;
595 read_camera_header (xmlDocPtr doc, xmlNodePtr node,
596 const std::string& camera_name,
597 const unsigned int image_width,
598 const unsigned int image_height,
599 const unsigned int subsampling_width,
600 const unsigned int subsampling_height)
604 std::string camera_name_tmp =
"";
605 unsigned int image_height_tmp = 0 ;
606 unsigned int image_width_tmp = 0 ;
607 unsigned int subsampling_width_tmp = 0;
608 unsigned int subsampling_height_tmp = 0;
613 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
616 if (node->type != XML_ELEMENT_NODE)
continue;
617 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
628 camera_name_tmp = val_char;
673 if( !((camera_name == camera_name_tmp) &&
674 (image_width == image_width_tmp || image_width == 0) &&
675 (image_height == image_height_tmp || image_height == 0) &&
676 (subsampling_width == subsampling_width_tmp ||
677 subsampling_width == 0)&&
678 (subsampling_height == subsampling_height_tmp ||
679 subsampling_height == 0))){
696 vpXmlParserCamera::read_camera_model (xmlDocPtr doc, xmlNodePtr node,
704 char* model_type = NULL;
705 double u0 = cam_tmp.
get_u0();
706 double v0 = cam_tmp.
get_v0();
707 double px = cam_tmp.
get_px();
708 double py = cam_tmp.
get_py();
709 double kud = cam_tmp.
get_kud();
710 double kdu = cam_tmp.
get_kdu();
714 for (node = node->xmlChildrenNode; node != NULL; node = node->next)
717 if (node->type != XML_ELEMENT_NODE)
continue;
718 if (
SEQUENCE_OK != str2xmlcode ((
char*)(node ->name), prop))
727 if(model_type != NULL){
732 validation = validation | 0x01;
737 validation = validation | 0x02;
742 validation = validation | 0x04;
747 validation = validation | 0x08;
752 validation = validation | 0x10;
757 validation = validation | 0x20;
762 validation = validation | 0x40;
781 if( !strcmp(model_type,LABEL_XML_MODEL_WITHOUT_DISTORTION)){
782 if (nb != 5 || validation != 0x1F)
784 vpCERROR <<
"ERROR in 'model' field:\n";
785 vpCERROR <<
"it must contain 5 parameters\n";
786 if(model_type != NULL){
793 else if( !strcmp(model_type,LABEL_XML_MODEL_WITH_DISTORTION)){
794 if (nb != 7 || validation != 0x7F)
796 vpCERROR <<
"ERROR in 'model' field:\n";
797 vpCERROR <<
"it must contain 7 parameters\n";
798 if(model_type != NULL){
806 vpERROR_TRACE(
"projection model type doesn't match with any known model !");
807 if(model_type != NULL){
812 if(model_type != NULL){
835 int vpXmlParserCamera::
836 write (xmlNodePtr node,
const std::string& camera_name,
837 const unsigned int image_width,
const unsigned int image_height,
838 const unsigned int subsampling_width,
839 const unsigned int subsampling_height)
844 xmlNodePtr node_camera;
847 node_camera = xmlNewNode(NULL,(xmlChar*)LABEL_XML_CAMERA);
848 xmlAddChild(node,node_camera);
852 if(!camera_name.empty()){
853 node_tmp = xmlNewComment((xmlChar*)
"Name of the camera");
854 xmlAddChild(node_camera,node_tmp);
855 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_CAMERA_NAME,
856 (xmlChar*)camera_name.c_str());
859 if(image_width != 0 || image_height != 0){
862 node_tmp = xmlNewComment((xmlChar*)
"Size of the image on which camera calibration was performed");
863 xmlAddChild(node_camera,node_tmp);
865 sprintf(str,
"%u",image_width);
866 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_WIDTH,(xmlChar*)str);
869 sprintf(str,
"%u",image_height);
870 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_HEIGHT,(xmlChar*)str);
871 if(subsampling_width != 0 || subsampling_height != 0){
872 node_tmp = xmlNewComment((xmlChar*)
"Subsampling used to obtain the current size of the image.");
873 xmlAddChild(node_camera,node_tmp);
876 sprintf(str,
"%u",subsampling_width);
877 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_SUBSAMPLING_WIDTH,
880 sprintf(str,
"%u",subsampling_height);
881 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_SUBSAMPLING_HEIGHT,
883 node_tmp = xmlNewComment((xmlChar*)
"The full size is the sensor size actually used to grab the image. full_width = subsampling_width * image_width");
884 xmlAddChild(node_camera,node_tmp);
887 sprintf(str,
"%u",image_width*subsampling_width);
888 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_FULL_WIDTH,
891 sprintf(str,
"%u",image_height*subsampling_height);
892 xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_FULL_HEIGHT,
897 node_tmp = xmlNewComment((xmlChar*)
"Intrinsic camera parameters computed for each projection model");
899 xmlAddChild(node_camera,node_tmp);
901 back = write_camera(node_camera);
912 int vpXmlParserCamera::
913 write_camera(xmlNodePtr node_camera){
914 xmlNodePtr node_model;
921 node_model = xmlNewNode(NULL,(xmlChar*)LABEL_XML_MODEL);
922 xmlAddChild(node_camera,node_model);
925 node_tmp = xmlNewComment((xmlChar*)
"Projection model type");
926 xmlAddChild(node_model,node_tmp);
929 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_MODEL_TYPE,
930 (xmlChar*)LABEL_XML_MODEL_WITHOUT_DISTORTION);
932 node_tmp = xmlNewComment((xmlChar*)
"Pixel ratio");
933 xmlAddChild(node_model,node_tmp);
935 sprintf(str,
"%.10f",camera.
get_px());
936 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PX,(xmlChar*)str);
938 sprintf(str,
"%.10f",camera.
get_py());
939 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PY,(xmlChar*)str);
941 node_tmp = xmlNewComment((xmlChar*)
"Principal point");
942 xmlAddChild(node_model,node_tmp);
945 sprintf(str,
"%.10f",camera.
get_u0());
946 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_U0,(xmlChar*)str);
948 sprintf(str,
"%.10f",camera.
get_v0());
949 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_V0,(xmlChar*)str);
954 node_model = xmlNewNode(NULL,(xmlChar*)LABEL_XML_MODEL);
955 xmlAddChild(node_camera,node_model);
958 node_tmp = xmlNewComment((xmlChar*)
"Projection model type");
959 xmlAddChild(node_model,node_tmp);
961 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_MODEL_TYPE,
962 (xmlChar*)LABEL_XML_MODEL_WITH_DISTORTION);
964 node_tmp = xmlNewComment((xmlChar*)
"Pixel ratio");
965 xmlAddChild(node_model,node_tmp);
967 sprintf(str,
"%.10f",camera.
get_px());
968 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PX,(xmlChar*)str);
970 sprintf(str,
"%.10f",camera.
get_py());
971 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PY,(xmlChar*)str);
973 node_tmp = xmlNewComment((xmlChar*)
"Principal point");
974 xmlAddChild(node_model,node_tmp);
976 sprintf(str,
"%.10f",camera.
get_u0());
977 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_U0,(xmlChar*)str);
979 sprintf(str,
"%.10f",camera.
get_v0());
980 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_V0,(xmlChar*)str);
983 node_tmp = xmlNewComment((xmlChar*)
"Undistorted to distorted distortion parameter");
984 xmlAddChild(node_model,node_tmp);
985 sprintf(str,
"%.10f",camera.
get_kud());
986 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_KUD,(xmlChar*)str);
989 node_tmp = xmlNewComment((xmlChar*)
"Distorted to undistorted distortion parameter");
990 xmlAddChild(node_model,node_tmp);
991 sprintf(str,
"%.10f",camera.
get_kdu());
992 xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_KDU,(xmlChar*)str);
1008 vpXmlParserCamera::str2xmlcode (
char * str, vpXmlCodeType & res)
1015 if (! strcmp (str, LABEL_XML_CAMERA))
1019 else if (! strcmp (str, LABEL_XML_CAMERA_NAME))
1023 else if (! strcmp (str, LABEL_XML_MODEL))
1027 else if (! strcmp (str, LABEL_XML_MODEL_TYPE))
1031 else if (! strcmp (str, LABEL_XML_WIDTH))
1035 else if (! strcmp (str, LABEL_XML_HEIGHT))
1039 else if (! strcmp (str, LABEL_XML_SUBSAMPLING_WIDTH))
1043 else if (! strcmp (str, LABEL_XML_SUBSAMPLING_HEIGHT))
1047 else if (! strcmp (str, LABEL_XML_FULL_WIDTH))
1051 else if (! strcmp (str, LABEL_XML_FULL_HEIGHT))
1055 else if (! strcmp (str, LABEL_XML_U0))
1059 else if (! strcmp (str, LABEL_XML_V0))
1063 else if (! strcmp (str, LABEL_XML_PX))
1067 else if (! strcmp (str, LABEL_XML_PY))
1071 else if (! strcmp (str, LABEL_XML_KUD))
1075 else if (! strcmp (str, LABEL_XML_KDU))
1087 #endif //VISP_HAVE_XML2