2013-04-02 99 views
2

我将相机图像捕捉到图像中。我从图像中选择对象并跟踪对象。但我想将选择保存到文件中,因为我不想每次选择对象。 这是我的选择部分;OpenCv:如何将Mat :: Rect保存到文件中?

Mat image; 
Rect selection; 

selection.x = MIN(x, origin.x); 
selection.y = MIN(y, origin.y); 
selection.width = abs(x - origin.x); 
selection.height = abs(y - origin.y); 
selection &= Rect(0, 0, image.cols, image.rows); 

如何保存选择或如何第一次选择对象? 谢谢

回答

1

不能存储Rect直接使用FileStorage,但你可以存储xywidthheight整数值。

写入文件:

Rect rect; 
// ... init your Rect 

FileStorage fs("rect.yml", FileStorage::WRITE); 
if(fs.isOpened()){ 
    fs << "x" << rect.x << "y" << rect.y; 
    fs << "width" << rect.width << "height" << rect.height; 
    fs.release(); 
} 
else cout << "Error: can not save the rect\n"; 

从文件中读取

Rect rect; 

FileStorage fs("rect.yml", FileStorage::READ); 
if(fs.isOpened()){ 
    fs["x"] >> rect.x; 
    fs["y"] >> rect.y; 
    fs["width"] >> rect.width; 
    fs["height"] >> rect.height; 
} 
else cout << "Error: can not load the rect\n";