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.

diip_thomas
  • 1,531
  • 13
  • 25

3 Answers3

1

Are you using the latest OpenCV (2.3.1)?

Another thing to look out for is that whatever is in //do stuff it might be producing a memory leak and after some time cvLoadImage() fails because your system simply don't have enough memory to load new images.

The truth is that there is a simple test to discover if the problem is in OpenCV or in your code: write a minimal application that loads the same image N times. Don't do nothing inside the loop except calling cvLoadImage() followed by cvReleaseImage().

If this simple application fails, it might be a problem in the OpenCV side of things. If not, you know you are doing something wrong in your code.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • I am not using the latest version of openCV, because at the time I started this project 2.2 was the latest version. I tried your simple test and came to the expected conclusion that there is nothing wrong with openCV or loading images from the hard disk. I also tested my //do stuff part for any memory leaks and there doesn;t seem to be a memory issue. – diip_thomas Apr 25 '12 at 13:50
  • agree about possible memory leak – tbridge Apr 29 '12 at 09:49
0

be careful when you decide to use the relative path, you'd better double check your working directory. that is where your .exe get executed. or say is your program changing working directory during run time ?

zinking
  • 5,561
  • 5
  • 49
  • 81
  • My program is not changing working directory during runtime and as I mentioned the program can load images fine for a few minutes and load the image... but after a while it stops doing this – diip_thomas Apr 25 '12 at 09:32
0

You should also set your UIframe to NULL after releasing it. Releasing it is fine but there might be even a bug that just does cause a problem prior to a NULL assignment, who knows?? :)

This will ensure nothing holds on to your file just in case. Read this question on SO for more explanation.

Community
  • 1
  • 1