0

I have been working on an image recognition program for a while now, but suddenly I noticed that when I test my program for a longer amount of time the function cvLoadImage stops returning an image sometimes. I see no clear pattern or time this occurs, but after it fails once it will keep failing for the rest of the program untill I restart again. The piece of code only runs once in a while to update my user interface.

My code:

IplImage* UIframe = cvLoadImage("../images/background.jpg", CV_LOAD_IMAGE_COLOR);
if(UIframe) {
 //do stuff
} else {
 cout<<"Image not loaded"<<endl;
}
cvReleaseImage(&UIframe);

There are a couple of reasons I already found for cvLoadImage failing that I checked:

  • The path is static and is working earlier on in the program so the file is there
  • I release the image and there are no other memory leaks to cause this
  • The .jpg is not being updated while trying to load it
  • I have all the right libraries installed as cvLoadImage is working earlier on

One solution is to only load the image once and keep updating it from memory, but I'd like to find out why this is happening and how to fix this.

4

3 回答 3

1

您使用的是最新的 OpenCV (2.3.1) 吗?

要注意的另一件事是,其中的任何内容都//do stuff可能会产生内存泄漏,并且在一段时间后会cvLoadImage()失败,因为您的系统根本没有足够的内存来加载新图像。

事实是,有一个简单的测试可以发现问题是在 OpenCV 中还是在您的代码中:编写一个最小的应用程序来加载相同的图像 N 次。cvLoadImage()除了调用后跟.之外,不要在循环内执行任何操作cvReleaseImage()

如果这个简单的应用程序失败,则可能是 OpenCV 方面的问题。如果没有,你就知道你的代码做错了。

于 2012-04-25T13:25:10.750 回答
0

您还应该在释放 UIframe 后将其设置为 NULL。释放它很好,但甚至可能存在一个在 NULL 赋值之前确实会导致问题的错误,谁知道呢?:)

这将确保您的文件没有任何保留,以防万一。阅读关于 SO 的这个问题以获得更多解释。

于 2012-04-25T09:44:43.230 回答
0

当你决定使用相对路径时要小心,你最好仔细检查你的工作目录。那是你的 .exe 被执行的地方。或者说您的程序在运行时是否更改了工作目录?

于 2012-04-25T09:20:29.363 回答