Ask Your Question

Memory Leaks opencv 2.3

asked 2013-05-20 20:57:06 -0500

baharsan gravatar image

I'am trying to use drawLargestContour mehthod to my program, but unfortunately my program suddenly turns off after about 1 minutes. I don't know why ? please some body help me :

this is my code (i use this for online capture) :

Mat HSV = new Mat();
    Imgproc.cvtColor(image, HSV, Imgproc.COLOR_RGB2HSV, 3);

    Mat mHSVThreshed = new Mat();
    //skin color detection
    Core.inRange(HSV,  new Scalar(0, 58, 89), new Scalar(25, 173, 229), mHSVThreshed);

    Mat hierarchy = new Mat(mHSVThreshed.rows(), mHSVThreshed.cols(), CvType.CV_8UC1, new Scalar(0));
    List<Mat> contours =new ArrayList<Mat>(100);

    Imgproc.findContours(mHSVThreshed, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);

    Mat Mask = new Mat();
    Mask = Mat.zeros(mHSVThreshed.rows(), mHSVThreshed.cols(), CvType.CV_8UC1);

    Imgproc.drawContours(Mask, contours, -1, new Scalar(255), Core.FILLED);

    Mat crop = new Mat(image.rows(), image.cols(), CvType.CV_8UC3);

    crop.setTo(new Scalar(0,255,0));

    image.copyTo(crop, Mask);

    Core.normalize(Mask.clone(), Mask, 0.0, 255.0, Core.NORM_MINMAX, CvType.CV_8UC1);

    Mat Biner = new Mat();
    Mask.convertTo(Biner, CvType.CV_8U);

    Imgproc.cvtColor(Biner, image, Imgproc.COLOR_GRAY2RGB, 0);
    Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2RGBA, 0);

    return image;
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-05-31 10:31:10 -0500

Prasanna gravatar image


I suggest you to have a look at your RAM. If it is increasing while the program is running then it must be a problem with the memory allocation.

Since you are running this in a loop, I would suggest that you pre - allocate your memory before beginning the loop and only modify it within the loop. Maybe re - declaring it every time is the problem.(the "new" statement every time might be the problem. It is being reallocated in a different memory than overwriting it and the previous memory is not being released by the program)

Hope this helps.

Regards, Prasanna S

edit flag offensive delete link more


Like he said. If you use the new operator, then you have to manually call the delete operation on the same object. However C++ can do this all for you. Remove the new operator and just call the Mat() without the new, memory will be released at the end of the scope of the variable then.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-05-31 12:48:53 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2013-05-20 20:57:06 -0500

Seen: 1,012 times

Last updated: May 31 '13