【发布时间】:2016-06-15 12:07:49
【问题描述】:
我想使用 FileStorage 类在 OpenCV 中输出 YAML 文件。目前 YAML 输出没有正确的缩进。 以下是 .cpp 代码。
FileStorage fs("detectionOutput.yml", FileStorage::WRITE)
Mat face_detect(Mat img, int count, cv::VideoWriter face_writer)
{
Rect r;
Mat detected_face;
CascadeClassifier face_cascade;
face_cascade.load("cascades_orig.xml");
std::vector<Rect> faces;
face_cascade.detectMultiScale( img, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
numOfValidRegion=0;
for( int i = 0; i < faces.size(); i++ )
{
r.x = faces[i].x;
r.y = faces[i].y;
r.width = faces[i].width;
r.height = faces[i].height;
rectangle(img, r, Scalar( 0, 255, 255 ), +2, 4, 0 );
Rect large_face;
large_face.x = faces[i].x ;
large_face.y = faces[i].y ;
large_face.width = faces[i].width;
large_face.height = faces[i].height;
detected_face = img(large_face);
string path1 = prepare_path("output_ppm/face", count, i);
string tmpStr2;
stringstream ssFrameID;
ssFrameID << curFrameNo;
stringstream ssRegionID;
ssRegionID << i;
tmpStr2 = "frame"+ ssFrameID.str()+"region"+ssRegionID.str();
fs << tmpStr2 << "";
fs << "numPts" << 2;
Mat tmpMat(2,2,DataType<int>::type);
tmpMat.at<int>(0,0) = large_face.x ; // TLx
tmpMat.at<int>(0,1) = large_face.y ; // TLy
tmpMat.at<int>(1,0) = large_face.x + large_face.width ; // BRx
tmpMat.at<int>(1,1) = large_face.y + large_face.height ; // BRy
fs << "matPts" << tmpMat;
fs << "attribute" << "head";
numOfValidRegion++;
}
string tmpStr;
stringstream ss;
ss << curFrameNo;
tmpStr = "frame"+ss.str()+"numValidRegions";
fs << tmpStr << numOfValidRegion;
return img;
}
以下是当前 YAML 输出
frame0Region0: ""
numPts: 2
matPts: !!opencv-matrix
rows: 2
cols: 2
dt: i
data: [ 1156, 336, 1206, 386 ]
attribute: head
frame0Region1: ""
numPts: 2
matPts: !!opencv-matrix
rows: 2
cols: 2
dt: i
data: [ 1112, 376, 1181, 445 ]
attribute: head
frame0Region2: ""
numPts: 2
matPts: !!opencv-matrix
rows: 2
cols: 2
dt: i
data: [ 1177, 385, 1252, 460 ]
attribute: head
frame0Region3: ""
numPts: 2
matPts: !!opencv-matrix
rows: 2
cols: 2
dt: i
data: [ 140, 310, 245, 415 ]
attribute: head
frame0numValidRegions: 4
我希望以下输出具有正确的缩进,我不想在 (frame0Region0: "") 前面加上双引号,但是当我删除双引号时它会显示错误。
frame0region0:
numPts: 2
matPts: !!opencv-matrix
rows: 2
cols: 2
dt: i
data: [ 1156, 336, 1206, 386 ]
attribute: head
frame0region1:
numPts: 2
matPts: !!opencv-matrix
rows: 2
cols: 2
dt: i
data: [ 1112, 376, 1181, 445 ]
attribute: head
frame0region2:
numPts: 2
matPts: !!opencv-matrix
rows: 2
cols: 2
dt: i
data: [ 1177, 385, 1252, 460 ]
attribute: head
frame0region3:
numPts: 2
matPts: !!opencv-matrix
rows: 2
cols: 2
dt: i
data: [ 140, 310, 245, 415 ]
attribute: head
frame0numValidRegions: 4
【问题讨论】:
标签: opencv yaml file-storage