OpenCV Python QueryFrame函数泄漏内存 [英] OpenCV Python QueryFrame function leaks memory
问题描述
我正在将Python
接口用于OpenCV 2.2.0
.下面的代码可以正确地从视频文件中抓取帧:
I'm using the Python
interface for OpenCV 2.2.0
. The following code works correctly for grabbing frames from a video file:
for f in range(1, frameCount):
# grab the left and right frames
frameL = cv.QueryFrame(videoL)
frameR = cv.QueryFrame(videoR)
# create the image for the first frame
if f==1:
imageL = cv.CreateImage(cv.GetSize(frameL), frameL.depth, frameL.channels)
imageR = cv.CreateImage(cv.GetSize(frameR), frameR.depth, frameR.channels)
# update the images
cv.Copy(frameL, imageL)
cv.Copy(frameR, imageR)
但是,随着我处理更多的视频帧,内存消耗不断增加.根据OpenCV文档,我们不需要为cv.QueryFrame
获取的图像数据释放内存.这还正确吗?我尝试了"del frameL"和"del frameR",但是并没有解决问题.这个特定功能的OpenCV的Python包装器中是否存在错误?
However, as I process more video frames, the memory consumption keeps increasing. According to the OpenCV documentation, we don't need to release the memory for the image data obtained by cv.QueryFrame
. Is this still correct? I tried "del frameL" and "del frameR", but it didn't solve the problem. Is there a bug in the Python wrapper for OpenCV in this particular function?
谢谢.
推荐答案
您应该为两个图像分配一次内存: imageL = cv.CreateImageHeader(cv.GetSize(frameL),frameL.depth,frameL.channels) imageR = cv.CreateImageHeader(cv.GetSize(frameR),frameR.depth,frameR.channels)
You should allocate memory for both images once: imageL = cv.CreateImageHeader(cv.GetSize(frameL), frameL.depth, frameL.channels) imageR = cv.CreateImageHeader(cv.GetSize(frameR), frameR.depth, frameR.channels)
然后开始循环并设置数据:
then begin your loop and set the data:
cv.SetData(frameL, imageL)
cv.SetData(frameR, imageR)
类似
for f in range(1, frameCount):
# grab the left and right frames
frameL = cv.QueryFrame(videoL)
frameR = cv.QueryFrame(videoR)
# create the image for the first frame
if f==1:
imageL = cv.CreateImageHeader(cv.GetSize(frameL), frameL.depth, frameL.channels)
imageR = cv.CreateImageHeader(cv.GetSize(frameR), frameR.depth, frameR.channels)
# update the images
cv.SetData(frameL, imageL)
cv.SetData(frameR, imageR)
这篇关于OpenCV Python QueryFrame函数泄漏内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!