簡體   English   中英

OpenCv:如何將Mat :: Rect保存到文件中?

[英]OpenCv: How to save Mat::Rect into file?

我將相機圖像捕獲到圖像中。 我從圖像中選擇對象,然后跟蹤對象。 但是我想將選擇保存到文件中,因為我不想每次都選擇對象。 這是我的選擇部分;

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 ,但可以存儲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";

嘗試使用OpenCV文件存儲來保存矩形。 或者,如果要保存圖片的選定部分,請使用cv :: Mat roi構造函數 ,然后使用cv :: imwrite保存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM