OpenCV:如何将Mat::Rect保存到文件中?

3

我将摄像头图像捕获到图像中。我从图像中选择对象并跟踪对象。但是我想将选择保存到文件中,因为我不想每次都重新选择对象。 这是我的选择部分:

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);

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

2个回答

1
你不能直接使用FileStorage存储Rect,但是你可以存储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";

-1

正如@warci所述,在2.4.6.1版本中,您不能保存矩形本身。 - Fabien R

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接