我如何从c ++中读取xml?

时间:2015-03-18 16:26:49

标签: c++ opencv

我从c ++开始,我尝试在我的代码中使用xml,这是我的源代码:

cv::FileStorage fs("facedata.xml", cv::FileStorage::WRITE);
cv::Mat mask = cv::Mat::zeros(8, 8, CV_32FC1); // all 0
mask(Rect(2,2,4,4)) = 1;
fs << "histo" <<mask;
CvMat * Mat=cvCreateMat(8,8,CV_32FC1);
cvZero(Mat);
fs.release();
cv::FileStorage fs2("facedata.xml", cv::FileStorage::READ);
CvScalar pix;
int K,L;
fs2 [ "histo" ]>> Mat;
for ( K=0;K<8;K++)
   for ( L=0;L<8;L++){
        pix=cvGet2D( Mat,K,L);
        cout<<(int) pix.val[0]<<endl;}

Mat未返回histo的内容,我需要您的帮助。

1 个答案:

答案 0 :(得分:3)

David,请避免您的opencv代码中的任何c-api构造。

任何想要指针或以cv *开头的东西都应该被遗漏!!

cv::FileStorage fs("facedata.xml", cv::FileStorage::WRITE);
cv::Mat mask = cv::Mat::zeros(8, 8, CV_32FC1); // all 0
mask(Rect(2,2,4,4)) = 1;
fs << "histo" <<mask;
fs.release();

//CvMat * Mat=cvCreateMat(8,8,CV_32FC1); // OUCH, NOO!
//cvZero(Mat);
cv::Mat m; // please do not call a variable 'Mat', since there's a class with same name already.

cv::FileStorage fs2("facedata.xml", cv::FileStorage::READ);
fs2 [ "histo" ]>> m;
cout<< m <<endl; // you can just dump a cv::Mat to stdout !