1

When i run this code it give "insufficient memory error"

#include "stdafx.h"
#include "PixelProcess.h"
#include "ContourProcess.h"

using namespace std;
using namespace cv;

int _tmain(int argc, _TCHAR* argv[])
{

    int width = 480, height = 640, N = 3000, framesToLearn = 500, i, w, h, carCounter, PixelCounter, R, G, B, MyResult, c;
    float circularity;
    String fileName; 
    IplImage* image;
    Mat Output  = Mat::zeros(width, height,CV_8UC1);
    Mat Draw = Mat::zeros(width, height,CV_8UC3);
    vector<vector<Point> > contours;
    vector<Point> hull;
    vector<Point>  keys;
    vector<Vec4i> hierarchy;
    vector<Point> polygonRegion;
    Point centroid;
    GaussianMex *F = new GaussianMex[480*640];

    int Y[5] = {35,35,360,360,35};
    int X[5] = {223,340,590,1,223};

    clock_t begin = 0,end = 0;

    for(i = 1; i <= N ; i++){
        if (i>framesToLearn){begin = clock();}
        carCounter = 0;
        PixelCounter = 0;
        fileName = intToStr(i,"D:/Datasets/Elda2ry1/(");
        image = cvLoadImage(fileName.c_str(),CV_LOAD_IMAGE_COLOR);
        // Update the pixel gaussian model
        for(w = 0; w < width ; w++)
            for(h = 0; h < height ; h++)
            {
                R = uchar((image->imageData+w*image->widthStep)[h*image->nChannels+0]);
                G = uchar((image->imageData+w*image->widthStep)[h*image->nChannels+1]);
                B = uchar((image->imageData+w*image->widthStep)[h*image->nChannels+2]);
                MyResult = F[PixelCounter].Process(R,G,B);
                Output.row(w).col(h) = MyResult*255;
                PixelCounter ++;
            }
            if(i>framesToLearn){ //Extract Cars
                //Output dilation
                dilate(Output,Output,Mat());
                //Matrix to draw on
                cvtColor(Output,Draw,CV_GRAY2RGB);
                polygonRegion = DrawRegionOfInterest(Draw,X,Y,5);
                findContours(Output,contours,hierarchy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE,Point (0,0));

                for(c = 0; c< contours.size(); c++ )//for each contour
                {
                    //get convex hull and it's centroid
                    convexHull(contours[c],hull,false);
                    centroid = GetCentroidOfConvexHull(hull);
                    //check if the centroid is within the region of interest
                    if (pointPolygonTest(polygonRegion,centroid,false) >=0 )
                        // Check area and Solidity of the convex hull
                        if( contourArea(hull) > 800 && contourArea(contours[c])/contourArea(hull) > 0.7)
                        {
                            circularity = GetCircularity(hull,contourArea(hull),centroid);
                            // Check circularity wrt the hull position
                            if((centroid.y < width/3 && circularity <= 0.42)||(centroid.y > width/3 && (circularity >= 0.33) && (circularity <= 0.4)))
                            {

                                // Crop the car and save it
                                fileName = "D:\\Dataset\\F" + itos(i) + "C" + itos(carCounter) + ".jpg";
                                Rect myRect = cropMyImage(hull,image,fileName);
                            }
                        }
                }
            }
            clrscr();
    }
}

I found here : OpenCV Error: Insufficient memory

that findcontours function can cause this error but in my case i declared the vector> contours at the beginning of the code.

2 Answers 2

4

The error is "Insufficient memory" because you don't have enough memory... You have memory leak. You are reading images from the disk but you never releasing the memory they use.

And you shouldn't use IplImage in the first place. This is old format that was replaced by Mat (which you used as well). This situation wouldn't happen with Mat because it is a smart pointer and will release its own memory.

Sign up to request clarification or add additional context in comments.

4 Comments

But i only declared one IplImage at the beginning, What is the problem, it's only one!!.
cvLoadImage(fileName.c_str(),CV_LOAD_IMAGE_COLOR); allocates a new image and returns a pointer to it. At this point, you "own" the allocation, opencv has no way to know whether you are still tracking it or not. When you are done with it, you have to call cvReleaseImage to free the memory. This is why it's better to use the Mat concept - unlike pointers, objects have managed lifetimes.
@Misaki, it doesn't matter when the pointer was declared. The only thing that matters is when the memory it points to was allocated. And in your case it is allocated 3000 times and never released.
@kfsone i used cvReleaseImage and it works fine. Thank you.
3

here's the culprit:

image = cvLoadImage(fileName.c_str(),CV_LOAD_IMAGE_COLOR);

you're loading 3000 IplImages and never release them...

instead, you should use the c++ api:

Mat image = imread(fileName,CV_LOAD_IMAGE_COLOR);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.