Massive memory leaks in smartdashboard image processing library.

Hi all!

Our team is using the libraries in SmartDashboard (WPIJavaCV) to do our image processing. We are planning to do image processing on a driverstation computer and then send motor values on the robot. We are up to the point where we can detect rectangles (ie the backboard) but we have been noticing huge memory leaks- SmartDashboard will take more than a gigabyte of memory after only a couple minutes. I have managed to narrow the problem down to the highligted line below. It leaks only when that line is in the code regardless of whether this line assigns its result to a variable. I have been monitoring the code with JVisualVM, which shows a memory usage of under 10MB in java, while the task manager reads usages in the hundreds of MB. Is this a bug in OpenCV? It could make sense due to the fact that JavaCV is just a wrapper of the c version…

    public WPIImage processImage(WPIColorImage rawImage) {
        int cameraCenterX = rawImage.getWidth() - (rawImage.getWidth() / 2);
        int cameraCenterY = rawImage.getHeight() - (rawImage.getHeight() / 2);
        threshold = thresholdDialog.getThreshold();
        WPIBinaryImage redChannel;
        WPIBinaryImage greenChannel;
        WPIBinaryImage blueChannel;
        WPIBinaryImage finalImage;
        if (thresholdDialog.isInverted()) {
            redChannel = rawImage.getRedChannel().getThreshold(threshold);
            greenChannel = rawImage.getGreenChannel().getThreshold(threshold);
            blueChannel = rawImage.getBlueChannel().getThreshold(threshold);
        } else {
            redChannel = rawImage.getGreenChannel().getThresholdInverted(threshold);
            greenChannel = rawImage.getGreenChannel().getThresholdInverted(threshold);
            blueChannel = rawImage.getGreenChannel().getThresholdInverted(threshold);
        }
        finalImage = redChannel.getAnd(greenChannel.getAnd(blueChannel));

        finalImage.erode(thresholdDialog.getErodeLevel());
        finalImage.dilate(thresholdDialog.getDilateLevel());
        WPIContour] contours = finalImage.findContours(); // This line causes the memory leak even without assigning to a variable.
        minSize = thresholdDialog.getMinSize();
        maxSize = thresholdDialog.getMaxSize();
        int rectCenterX = 0;
        int rectCenterY = 0;
        int aspectRatio = 0;
        int polyAccuracy = thresholdDialog.getPolyAccuracy();
        
        for (int i = 0; i < contours.length; i++) {
            aspectRatio = contours*.getWidth() / contours*.getHeight();
            if (contours*.getHeight() > minSize && contours*.getWidth() > minSize && 
                   contours*.getHeight() < maxSize && contours*.getWidth() < maxSize) {
                if (aspectRatio < 1.5 && aspectRatio > .75) {
                    WPIPolygon currPolygon = contours*.approxPolygon(polyAccuracy);
                    if (currPolygon.isConvex() && currPolygon.getNumVertices() == 4) {
                        rectCenterX = currPolygon.getX() + currPolygon.getWidth() / 2;
                        rectCenterY = currPolygon.getY() + currPolygon.getHeight()/ 2;

                        rawImage.drawPolygon(currPolygon, WPIColor.RED, 2);
                        rawImage.drawPoint(new WPIPoint(rectCenterX, rectCenterY), WPIColor.GREEN, 5);
                        rawImage.drawPoint(new WPIPoint(cameraCenterX, cameraCenterY), WPIColor.BLUE, 5);
                    }
                }
            }
        }
        return rawImage; 
    }
}

If anybody has used the same methods successfully, please do post! The knowledge would be greatly appreciated!

Thanks!

violinuxer*******

You might look at the latest svn for smartdashboard. The last commit was labeled “Fixed memory leak with contours.”

Oh my… guess it’s a good thing I have the source!

Thanks soo much!

violinuxer

Oh my… I guess its a good thing I have the source!

violinuxer

AND… sorry about the double post…

violinuxer

Hi - look at this post
http://www.chiefdelphi.com/forums/showthread.php?t=101816&referrerid=46078

I have not experienced any memory leak but I have not been running too long.

Regarding the analysis using WPI - you will see that Paul has posted a wrapper to access the IPLImage underneath. Does using the IPLImage avoids the findcontour and prevents the leak ?

Also I have not found that using the RGB space (= using WPI) allows to identify a specific color - how does it work for you ? Does not the litterature says that color identification is better done using HSV space (therefore the IPLImage) ? This is confusing to me…

The following post includes a JAR file and instructions on installing to avoid the memory leak issues when processing images in SmartDashboard:

NOTE: This is not an official release from the SmartDashboard project. I did build it by checking out the source after I noticed the commit messages related to fixing the memory leak.