python - OpenCV Python QueryFrame 函数泄漏内存

标签 python video opencv

我正在为 OpenCV 2.2.0 使用 Python 接口(interface)。以下代码可以正确地从视频文件中抓取帧:

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 包装器中是否存在错误?

谢谢。

最佳答案

你应该为两个图像分配一次内存: imageL = cv.CreateImageHeader(cv.GetSize(frameL), frameL.depth, frameL.channels) imageR = cv.CreateImageHeader(cv.GetSize(frameR), frameR.depth, frameR.channels)

然后开始循环并设置数据:

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)

关于python - OpenCV Python QueryFrame 函数泄漏内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5318311/

相关文章:

C#视频转码库

c++ - OpenCV 3.0 - 错误 : (-215) scn == 3 || scn == 4 in function ipp_cvtColor

python - OpenCV “imshow”函数可打开许多窗口,而不是替换一个窗口上的框架-Python

c++ - 在C++中使用imfilter(matlab)

python - 在单行 for 循环中分配给列表的一项

python graph_tool : get _all_ shortest paths

python - Debug = True 时 Django 错误报告电子邮件

algorithm - MPEG4 压缩是如何工作的?

c++ - 将 RGB 编码为 H.264

python - flask 属性错误 : module 'app' has no attribute 'run'