Javacv memory leak

I am trying to create a program that takes a picture from a webcam and then resizes it, converts it to HSV and creates a specific threshold for it to find a specific color. After that, I use the threshold image to search for paths and print the x, y coordinates of different paths. This is repeated over and over to do real-time webcam processing.

Everything works very well, except that I use about 100 MB of RAM every 2 seconds when it starts.

So far, I have found that if I use a static image instead of live images from a webcam, I can significantly reduce memory leak, although there is still memory consumption.

Below is my code:

public class Application {
private CaptureImage ci;
private ImageUtils iu;
private CanvasFrame canvasContours;

IplImage grabbedFrame;
IplImage resizedFrame;
IplImage thresholdedFrame;
IplImage clonedImage;

public Application(){
    ci = new CaptureImage();
    iu = new ImageUtils();
    canvasContours = new CanvasFrame("contours");

}

public void frameProcessing(){

    grabbedFrame = ci.grabImage();
    //below call used for testing purposes
    //grabbedFrame = (IplImage) opencv_highgui.cvLoadImage("testingImage.jpg");
    //cloning image due to highgui guidelines.
    clonedImage = opencv_core.cvCloneImage(grabbedFrame);
    resizedFrame = iu.resizeImage(clonedImage);

    opencv_core.cvReleaseImage(clonedImage);

    thresholdedFrame = iu.thresholdImage(resizedFrame);


    IplImage contoursFrame = iu.findContours(thresholdedFrame, resizedFrame);

    canvasContours.showImage(contoursFrame);


}
}

GrabImage frameGrabber javacv, :

public class CaptureImage {
private final OpenCVFrameGrabber grabber;
private IplImage img = null;


public CaptureImage(){
    // 0-default camera, 1 - next...so on
            grabber = new OpenCVFrameGrabber(0);
            try {
                grabber.start();
            } catch (Exception e) {
                System.err.print("Failed to initialize camera");
                e.printStackTrace();
            }

}

public IplImage grabImage(){

    try {

    //A grabbed image from Logitech webcam is in following resolution: 1200x800px

        img = grabber.grab();



    } catch (Exception e) {

        e.printStackTrace();
    }
    return img;
}

, , , , !

/Jesper

+5
1

, - , . , , cvReleaseImage , .

+1

All Articles