轉載自:利用TinyXML讀取VOC2012數據集的XML標注文件裁剪出所有人體目標保存為文件 - Why So Serious? - 博客頻道 - CSDN.NET ?http://blog.csdn.net/masibuaa/article/details/16104717
PASCAL VOC目標檢測數據集(The PASCAL Visual Object Classes)
http://pascallin.ecs.soton.ac.uk/challenges/VOC/
圖片中的目標用XML文件標注,格式為:
[html]?view plaincopy
<annotation>?? ????<folder>VOC2012</folder>?? ????<filename>2007_000346.jpg</filename>?? ????<source>?? ????????<database>The?VOC2007?Database</database>?? ????????<annotation>PASCAL?VOC2007</annotation>?? ????????<image>flickr</image>?? ????</source>?? ????<size>?? ????????<width>500</width>?? ????????<height>375</height>?? ????????<depth>3</depth>?? ????</size>?? ????<segmented>1</segmented>?? ????<object>?? ????????<name>bottle</name>?? ????????<pose>Unspecified</pose>?? ????????<truncated>0</truncated>?? ????????<difficult>0</difficult>?? ????????<bndbox>?? ????????????<xmin>124</xmin>?? ????????????<ymin>107</ymin>?? ????????????<xmax>230</xmax>?? ????????????<ymax>343</ymax>?? ????????</bndbox>?? ????</object>?? ????<object>?? ????????<name>person</name>?? ????????<pose>Unspecified</pose>?? ????????<truncated>0</truncated>?? ????????<difficult>0</difficult>?? ????????<bndbox>?? ????????????<xmin>137</xmin>?? ????????????<ymin>78</ymin>?? ????????????<xmax>497</xmax>?? ????????????<ymax>375</ymax>?? ????????</bndbox>?? ????</object>?? ????<object>?? ????????<name>person</name>?? ????????<pose>Unspecified</pose>?? ????????<truncated>1</truncated>?? ????????<difficult>0</difficult>?? ????????<bndbox>?? ????????????<xmin>89</xmin>?? ????????????<ymin>202</ymin>?? ????????????<xmax>129</xmax>?? ????????????<ymax>247</ymax>?? ????????</bndbox>?? ????</object>?? ????<object>?? ????????<name>person</name>?? ????????<pose>Frontal</pose>?? ????????<truncated>1</truncated>?? ????????<difficult>0</difficult>?? ????????<bndbox>?? ????????????<xmin>72</xmin>?? ????????????<ymin>209</ymin>?? ????????????<xmax>111</xmax>?? ????????????<ymax>259</ymax>?? ????????</bndbox>?? ????</object>?? </annotation>??
對應的圖片為:
所以如果想用這個數據集做某種目標識別的訓練集的話,需要先從中裁出需要的目標。
下面這個程序就是這個目的,其中用到了TinyXML這個簡單易用的XML解析器(XML入門)
[cpp]?view plaincopy
#include?<iostream>?? #include?<fstream>?? #include?<opencv2/core/core.hpp>?? #include?<opencv2/highgui/highgui.hpp>?? #include?<opencv2/imgproc/imgproc.hpp>?? #include?<opencv2/objdetect/objdetect.hpp>?? #include?<opencv2/ml/ml.hpp>?? ?? #include?<tinyxml.h>?? ?? using?namespace?std;?? using?namespace?cv;?? ?? int?CropImageCount=0;?? ?? ? ? ? ? ? ? ?? bool?GetAllNodePointerByName(TiXmlElement*?pRootEle,?string?strNodeName,?vector<TiXmlElement*>?&NodeVector)?? {?? ?????? ????if(strNodeName?==?pRootEle->Value())?? ????{?? ????????NodeVector.push_back(pRootEle);?? ?????????? ?????????? ????????for(TiXmlElement?*?pElement?=?pRootEle->NextSiblingElement();?pElement;?pElement?=?pElement->NextSiblingElement())?? ????????????if(strNodeName?==?pElement->Value())?? ????????????????NodeVector.push_back(pElement);?? ????????return?true;?? ????}?? ????TiXmlElement?*?pEle?=?pRootEle;?? ????for(pEle?=?pRootEle->FirstChildElement();?pEle;?pEle?=?pEle->NextSiblingElement())?? ????{?? ?????????? ????????if(GetAllNodePointerByName(pEle,strNodeName,NodeVector))?? ????????????return?true;?? ????}?? ????return?false;?? }?? ?? ? ? ? ? ? ?? bool?FiltObject(vector<TiXmlElement*>?&NodeVector,?string?objectName)?? {?? ????TiXmlElement?*?pEle?=?NULL;?? ????vector<TiXmlElement?*>::iterator?iter?=?NodeVector.begin();?? ????for(;?iter?!=?NodeVector.end();)?? ????{?? ????????pEle?=?*?iter;?? ?????????? ????????if(?objectName?!=?pEle->FirstChildElement()->GetText()?)?? ????????{?? ?????????????? ????????????iter?=?NodeVector.erase(iter);?? ????????}?? ????????else?? ????????????iter++;?? ????}?? ????if(?0?==?NodeVector.size())?? ????????return?false;?? ????else??? ????????return?true;?? }?? ?? ? ? ? ? ?? void?CropImage(Mat?img,?vector<TiXmlElement*>?NodeVector)?? {?? ????int?xmin,ymin,xmax,ymax;?? ????char?fileName[256];?? ?? ?????? ????vector<TiXmlElement?*>::iterator?iter?=?NodeVector.begin();?? ????for(;?iter?!=?NodeVector.end();?iter++)?? ????{?? ?????????? ????????TiXmlElement?*pEle?=?(*iter)->FirstChildElement();?? ????????for(;?pEle;?pEle?=?pEle->NextSiblingElement())?? ????????{?? ?????????????? ????????????if(string("bndbox")?==?pEle->Value())?? ????????????{?? ????????????????TiXmlElement?*?pCoord=?pEle->FirstChildElement();?? ?????????????????? ????????????????for(;?pCoord;?pCoord?=?pCoord->NextSiblingElement())?? ????????????????{?? ????????????????????if(string("xmin")?==?pCoord->Value())?? ????????????????????????xmin?=?atoi(pCoord->GetText());?? ????????????????????if(string("ymin")?==?pCoord->Value())?? ????????????????????????ymin?=?atoi(pCoord->GetText());?? ????????????????????if(string("xmax")?==?pCoord->Value())?? ????????????????????????xmax?=?atoi(pCoord->GetText());?? ????????????????????if(string("ymax")?==?pCoord->Value())?? ????????????????????????ymax?=?atoi(pCoord->GetText());?? ????????????????}?? ?????????????????? ?????????????????? ????????????????Mat?imgROI?=?img(Rect(xmin,ymin,xmax-xmin,ymax-ymin));?? ????????????????resize(imgROI,imgROI,Size(64,128));?? ????????????????sprintf(fileName,"person%06d.jpg",++CropImageCount);?? ????????????????imwrite(fileName,imgROI);?? ????????????????flip(imgROI,imgROI,1);?? ????????????????memset(fileName,0x00,sizeof(fileName));?? ????????????????sprintf(fileName,"person%06d.jpg",++CropImageCount);?? ????????????????imwrite(fileName,imgROI);?? ????????????}?? ????????}?? ????}?? }?? ?? ? ? ? ? ? ? ?? bool?CropImageAccordingToXML(string?XMLFile,?Mat?img,?string?objectName)?? {?? ????TiXmlDocument?*?pDoc?=?new?TiXmlDocument();?? ????pDoc->LoadFile(XMLFile.c_str());?? ????vector<TiXmlElement*>?nodeVector;?? ?? ?????? ????if(?false?==?GetAllNodePointerByName(pDoc->RootElement(),?"object",?nodeVector)?)?? ????????return?false;?? ?????? ?? ?????? ????if(?false?==?FiltObject(nodeVector,objectName)?)?? ????????return?false;?? ?????? ?? ?????? ????CropImage(img,nodeVector);?? }?? ?? ?? int?main()?? {?? ????int?fileCount=0;?? ????Mat?src;?? ????string?XMLName,ImgName;?? ?????? ????ifstream?fin("subset.txt");?? ?????? ?? ?????? ????while(getline(fin,XMLName))?? ????{?? ????????cout<<"處理:"<<XMLName<<endl;?? ????????ImgName?=?"D:\\DataSet\\VOCtrainval_11-May-2012\\VOCdevkit\\VOC2012\\JPEGImages\\"?+?XMLName?+?".jpg";?? ????????XMLName?=?"D:\\DataSet\\VOCtrainval_11-May-2012\\VOCdevkit\\VOC2012\\Annotations\\"?+?XMLName?+?".xml";?? ????????src?=?imread(ImgName);?? ????????CropImageAccordingToXML(XMLName,src,"person");?? ????}?? ?? ????system("pause");?? }??
源碼下載,環境為VS2010 + OpenCV2.4.4 + TinyXML2.6.2
http://download.csdn.net/detail/masikkk/6547823
編譯好的TinyXML2.6.2:
http://download.csdn.net/detail/masikkk/6547809
總結
以上是生活随笔為你收集整理的利用TinyXML读取VOC2012数据集的XML标注文件裁剪出所有人体目标保存为文件的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。