gyandeep8693 / javacv

Automatically exported from code.google.com/p/javacv
GNU General Public License v2.0
0 stars 0 forks source link

Memory leaks #323

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The Problem
I use the grabber to capture image from the webcam, and I use any methods to 
transform the image, and detect the blobs whith blob detection. But the memory 
of the java only increase, and after some minutes the computer break...
I try to use CvMemStorage, but I couldn't got a good result

Please anyone help me!
Thanks!

The Code:

import com.googlecode.javacv.Blobs;
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.OpenCVFrameGrabber;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class BlobDetection {
    private static FrameGrabber grabber = new OpenCVFrameGrabber(1);
    private static IplImage RawImage = null;
    private static CanvasFrame canvas;
    private static int MinX;
    private static int MaxX;
    private static int MinY;
    private static int MaxY;
    private static int dimensaoX;
    private static int dimensaoY;
    private ExecutorService threadExecutor = Executors.newCachedThreadPool();

    public void iniciarCaptura() {
        System.out.println("STARTING...\n");
        canvas = new CanvasFrame("Webcam");
        canvas.setSize(512, 512);
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

        try {
            grabber.start();
            CvMemStorage tempStorage = CvMemStorage.create();
            while (true) {
                if (grabber != null) {
                   RawImage=grabber.grab();
                     blobs();
                    tempStorage.release();                    
                  //  grabber.flush();
                } else {
                    grabber.stop();
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public void blobs() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                int MinArea = 250;
                int ErodeCount = 0;
                int DilateCount = 1;
                IplImage GrayImage = cvCreateImage(cvGetSize(RawImage),
                        IPL_DEPTH_8U, 1);

                cvCvtColor(RawImage, GrayImage, CV_BGR2GRAY);
                cvThreshold(GrayImage, GrayImage, 100, 255, CV_THRESH_BINARY);
                cvErode(GrayImage, GrayImage, null, ErodeCount);
                cvDilate(GrayImage, GrayImage, null, DilateCount);
                cvCanny(GrayImage, GrayImage, 50, 120, 3);
                Blobs Regions = new Blobs();
                 Regions.BlobAnalysis(GrayImage, // image
                 -1, -1, // ROI start col, row
                 -1, -1, // ROI cols, rows
                 1, // border (0 = black; 1 = white)
                 250); // minarea
                 for (int i = 1; i <= Blobs.MaxLabel; i++) {
                 double[] Region = Blobs.RegionData[i];
                 int Parent = (int) Region[Blobs.BLOBPARENT];
                 int Color = (int) Region[Blobs.BLOBCOLOR];
                 MinX = (int) Region[Blobs.BLOBMINX];
                 MaxX = (int) Region[Blobs.BLOBMAXX];
                 MinY = (int) Region[Blobs.BLOBMINY];
                 MaxY = (int) Region[Blobs.BLOBMAXY];
                 dimensaoX = MaxX - MinX;
                 dimensaoY = MaxY - MinY;

                 }
                 Highlight(RawImage, MinX, MinY, MaxX, MaxY, 2);

                canvas.showImage(RawImage);
            }
        };
        threadExecutor.execute(runnable);
    }

    public static void Highlight(IplImage image, int xMin, int yMin, int xMax,
            int yMax, int Thick) {
        CvPoint pt1 = cvPoint(xMin, yMin);
        CvPoint pt2 = cvPoint(xMax, yMax);
        CvScalar color = cvScalar(255, 0, 0, 0); // blue [green] [red]
        cvRectangle(image, pt1, pt2, color, Thick, 4, 0);
    }
}

Original issue reported on code.google.com by duffmack...@gmail.com on 10 Jun 2013 at 7:46

GoogleCodeExporter commented 9 years ago
Please try to use IplImage.create() instead of cvCreateImage() as explained in 
the README.txt file: That should take care of the memory leak.

And please ask your questions on the mailing list next time if possible, thank 
you!

Original comment by samuel.a...@gmail.com on 16 Jun 2013 at 2:05

GoogleCodeExporter commented 9 years ago
Ok Thanks Samuel!!

Original comment by duffmack...@gmail.com on 16 Jun 2013 at 11:52