Ask Your Question

Revision history [back]

How to avoid the OpenCV out of memory exception?

I am using OpenCvSharp and this code to get the position of a user's eyes.

CvCapture cap = CvCapture.FromCamera(CaptureDevice.Any);
using (CvWindow w = new CvWindow("Eye Tracker"))
{

    while (CvWindow.WaitKey(10) < 0 && beginn) //beginn)
    {
        using (IplImage img = cap.QueryFrame())
        using (IplImage smallImg = new IplImage(new CvSize(Cv.Round(img.Width / Scale), Cv.Round(img.Height / Scale)), BitDepth.U8, 1))
        {

            using (IplImage gray = new IplImage(img.Size, BitDepth.U8, 1))
            {
                Cv.CvtColor(img, gray, ColorConversion.BgrToGray);
                Cv.Resize(gray, smallImg, Interpolation.Linear);
                Cv.EqualizeHist(smallImg, smallImg);
            }

            using (CvHaarClassifierCascade cascade = CvHaarClassifierCascade.FromFile(@"D:\haarcascade_eye.xml"))
            using (CvMemStorage storage = new CvMemStorage())
            {
                storage.Clear();
                CvSeq<CvAvgComp> eyes = Cv.HaarDetectObjects(smallImg, cascade, storage, ScaleFactor, MinNeighbors, 0, new CvSize(30, 30));
                if (eyes.Total == 2)
                {

                    // do something
                }
                w.Image = img;
            }
        }
    }
}

It works, but the program gets bigger and bigger in memory till it finally says that it doesn't have enough and points at

CvSeq<CvAvgComp> eyes = Cv.HaarDetectObjects(smallImg, cascade, storage, ScaleFactor, MinNeighbors, 0, new CvSize(30, 30));

How to solve this out of memory exception?

click to hide/show revision 2
retagged

updated 2015-03-28 14:15:35 -0600

berak gravatar image

How to avoid the OpenCV out of memory exception?

I am using OpenCvSharp and this code to get the position of a user's eyes.

CvCapture cap = CvCapture.FromCamera(CaptureDevice.Any);
using (CvWindow w = new CvWindow("Eye Tracker"))
{

    while (CvWindow.WaitKey(10) < 0 && beginn) //beginn)
    {
        using (IplImage img = cap.QueryFrame())
        using (IplImage smallImg = new IplImage(new CvSize(Cv.Round(img.Width / Scale), Cv.Round(img.Height / Scale)), BitDepth.U8, 1))
        {

            using (IplImage gray = new IplImage(img.Size, BitDepth.U8, 1))
            {
                Cv.CvtColor(img, gray, ColorConversion.BgrToGray);
                Cv.Resize(gray, smallImg, Interpolation.Linear);
                Cv.EqualizeHist(smallImg, smallImg);
            }

            using (CvHaarClassifierCascade cascade = CvHaarClassifierCascade.FromFile(@"D:\haarcascade_eye.xml"))
            using (CvMemStorage storage = new CvMemStorage())
            {
                storage.Clear();
                CvSeq<CvAvgComp> eyes = Cv.HaarDetectObjects(smallImg, cascade, storage, ScaleFactor, MinNeighbors, 0, new CvSize(30, 30));
                if (eyes.Total == 2)
                {

                    // do something
                }
                w.Image = img;
            }
        }
    }
}

It works, but the program gets bigger and bigger in memory till it finally says that it doesn't have enough and points at

CvSeq<CvAvgComp> eyes = Cv.HaarDetectObjects(smallImg, cascade, storage, ScaleFactor, MinNeighbors, 0, new CvSize(30, 30));

How to solve this out of memory exception?