我想使用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
答案 0 :(得分:0)
YAML从左向右阅读,因此""
实际上位于frame0Region0
键后面。
如果删除""
,然后缩进以下值,则实际上完全改变了含义:
abc: ""
xyz: 1
定义了两个键:abc
,其值为空字符串标量,xyz
,其值为整数。鉴于:
abc:
xyz: 1
由于缩进,将键abc
的值定义为映射,其第一个键是xyz
。这有一个完全不同的结构,不会加载。
即使你不会缩进,但只是删除""
,你会将空标量字符串更改为空标量(在C ++中作为NULL指针加载),这不太可能被处理所接受没有明确测试的软件。