简体   繁体   English

使用C ++ STL而不复制数据

[英]Using C++ STL without copying data

I am using Opencv Mat container to read frames from video steam. 我正在使用Opencv Mat容器从视频流中读取帧。 I need both current and previous frames. 我需要当前和以前的帧。

For now I am copying the current frame to the previous frame at the end of each iteration. 现在我在每次迭代结束时将当前帧复制到前一帧。 Is it possible to avoid this deep copy using one of the STL containers? 是否可以使用其中一个STL容器来避免此深层复制?

std::string VidPath;
VideoCapture VidStream;
cv::Mat Prev,Curr;

if (!VidStream.open(VidPath))
{
    cout << "Cant open video" << endl;
    return 1;
}

VidStream.read(Prev);

while(VidStream.read(Curr))
{
   //do some operations between Curr and Prev 
   Curr.copyTo(Prev)// is it possible to avoid this copy?
}

Yes, this is called swapping. 是的,这称为交换。

One option would be to hold two pointers in your class. 一种选择是在你的班级中保留两个指针。 One is for the current frame and one for the previous frame. 一个用于当前帧,一个用于前一帧。
When you are done processing your current frame, just swap the pointers and start working on the next frame again (which then can override the current frame). 处理完当前帧后,只需交换指针并再次开始处理下一帧(然后可以覆盖当前帧)。

An other option would be to use two std::vector which provide the swap method, which means no copying. 另一种选择是使用两个提供swap方法的std::vector ,这意味着不进行复制。
Other Container like std::list or std::dequeu also provide the swap method. 其他容器如std::liststd::dequeu也提供swap方法。

You may find additional information when searching for double buffering, which is a technique for flicker free content displaying. 搜索双缓冲时,您可能会发现其他信息,这是一种无闪烁内容显示技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM