gpt4 book ai didi

c++ - OpenCv:如何将 Mat::Rect 保存到文件中?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:57:41 25 4
gpt4 key购买 nike

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

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

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

最佳答案

您不能直接使用FileStorage 存储Rect,但可以存储xy宽度高度

写入文件:

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

关于c++ - OpenCv:如何将 Mat::Rect 保存到文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15764956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com