2013-04-02 31 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";