Ask Your Question

Revision history [back]

Java OpenCV matrix .get() cause memory leak

I have a problem with this part of code :

private Mat getFilteredHSV(Mat hsv, Mat contourFilter) {
    Mat filteredHSV = Mat.zeros(hsv.size(), hsv.type());
    for (int i = 0; i < contourFilter.rows(); i++) {
        for (int j = 0; j < contourFilter.cols(); j++) {
            byte[] contourVal = new byte[1];
            contourFilter.get(i, j, contourVal);
            if (contourVal[0] != 0) {
                byte[] hsvVal = new byte[3];
                hsv.get(i, j,hsvVal);
                filteredHSV.put(i, j, hsvVal);
            }
        }
    }
    return filteredHSV;
}

It seems that the get method cause memory leak in Java. According to this analyze: image description

I also tried to use another get method which gave me the double array

private Mat getFilteredHSV(Mat hsv, Mat contourFilter) {
    Mat filteredHSV = Mat.zeros(hsv.size(), hsv.type());
    for (int i = 0; i < contourFilter.rows(); i++) {
        for (int j = 0; j < contourFilter.cols(); j++) {
            double[] contourVal = contourFilter.get(i, j);
            if (contourVal[0] != 0) {
                double[] hsvVal = hsv.get(i, j);
                filteredHSV.put(i, j, hsvVal);
            }
        }
    }
    return filteredHSV;
}

But results is same image description

Im using OpenCV 3-RC1 (I cannot downgrade if the problem is with the version) and Java 8.

Does anybody have same problem or have solution ? Im working with a lot of video files and Im soon out of memory. Im releasing matrices after the operation so I dont know what to do more. If Im not performing this operation than memory is ok, so problem must be with the get method. I also tried manually run garbage collector and It wont help. So I think that the problem is with the native objects or Im doing it wrong.

Or is there any other solution for iterating over matrix ?

Thank you so much for help.