如何在OpenCV中保存绘制的矩形?

How to save the drawn rectangle in OpenCV?

我是 OpenCV 的新手,我正在制作一个视频,我必须在其中检测移动的物体并在物体周围绘制一个矩形。我必须为每个检测到的对象绘制矩形。我使用下面的代码绘制了矩形。现在我想保存矩形,有人可以告诉我如何将矩形保存为图像吗?

密码是

rectangle( frameSequence, Point( x-20, y+20), Point( x+20, y-20), Scalar( 0, 55, 255 ), +1, 4 );

frameSequence 已经是 cv::mat 的一种类型(大概),这意味着就存储在您的程序中而言它已经是 "saved"。

如果你想 "save" 将其保存到外部文件(.jpg 等),则需要使用 imwrite:

imwrite( "../../images/rectangle.jpg",frameSequence);

为了仅将矩形保存到垫子中,您只需创建一个空白矩阵,然后将其传递给 rectangle()。这意味着这个新矩阵中的唯一数据将是矩形的坐标。

cv::Mat newRectMat;
rectangle( newRectMat, Point( x-20, y+20), Point( x+20, y-20), Scalar( 0, 55, 255 ), +1, 4 );

只得到一个子矩阵或投资回报率。您可能需要深拷贝

cv::Mat frame;
//get the frame ...
cv::Mat subFrame = frame(cv::Rect(x, y, w, h).clone();
or
cv::Mat subFrame;
frame(cv::Rect(x, y, w, h)).copyTo(subFrame);
imwrite(filename, subframe);