gpt4 book ai didi

c - OpenCV 泄漏内存(cvResize、cvCreateImage)

转载 作者:太空宇宙 更新时间:2023-11-03 22:45:51 25 4
gpt4 key购买 nike

我在 RaspberryPi 上运行 OpenCV 并使用 OpenCVs C 接口(interface)。

我需要调整网络摄像头图像的尺寸,因此我使用了 cvResize() 函数。

它工作正常但几秒钟后我内存不足,因为我在 while(1)-Loop 中执行代码并阅读其他人遇到这个问题,我怀疑内存泄漏。

这是我的代码:

IplImage *frame;
IplImage *frameRaw;

main() {
CvCapture *capture = cvCreateCameraCapture(-1);

while (1) {
frameRaw = cvQueryFrame(capture);
frame = cvCreateImage(cvSize(WIDTH, HEIGHT), frameRaw->depth, frameRaw->nChannels);

cvResize(frameRaw, frame, 0); // 0 = CV_INTER_NEAREST

// Do something with "frame"
}

}

我已经尝试使用 cvReleaseImage(&frameRaw)(或 &frame)在每次迭代结束时释放保留的内存,但这总是会导致段错误。使用 cvReleaseImageHeader() 不会导致段错误,但也不会释放任何内存。

我还尝试通过 cvSetCaptureProperty() 更改帧的捕获分辨率,但没有任何作用。

有人可以帮助我了解这里出了什么问题吗?

提前致谢

最佳答案

感谢@DanMašek,我找到了解决方案:

在每次 while 循环迭代之后,必须释放为帧分配的内存。这可以使用 cvReleaseImage(&frame) 来完成。

完整的代码现在是这样的:

IplImage *frame;
IplImage *frameRaw;

main() {
CvCapture *capture = cvCreateCameraCapture(-1);

while (1) {
frameRaw = cvQueryFrame(capture);
frame = cvCreateImage(cvSize(WIDTH, HEIGHT), frameRaw->depth, frameRaw->nChannels);

cvResize(frameRaw, frame, 0); // 0 = CV_INTER_NEAREST

// Do something with "frame"

cvReleaseImage(&frame);
}

}

关于c - OpenCV 泄漏内存(cvResize、cvCreateImage),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47683755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com