Ask Your Question

Revision history [back]

Memory Leak Problem Quadtree OpenCv

Hello,

I'm currently programming a program that create quadtrees from images. The code works, but for 10 images (~4MB) my program take 300MB in the RAM oO.

Is there a duplication of the cv::Mat object implicitly ?

My quadtree class is like this:

   Quadtree::Quadtree(const Mat& image,const float& _x,const float& _y, const int& _level, const int& _maxLevel) :
    //bloc(image),
    x       (_x),
    y       (_y),
    width   (image.rows/pow(2,_level)),
    height  (image.cols/pow(2,_level)),
    level   (_level),
    maxLevel(_maxLevel),
    isLeaf(false)
{

    //cout << level << " -" <<width<<endl;
    //cout << level << "-"<<maxLevel <<endl;
    if (level == maxLevel){
        isLeaf=true;
        return;
    }

    NW = new Quadtree(image,x, y, level+1, maxLevel);
    NE = new Quadtree(image,x + width / 2.0f, y,  level+1, maxLevel);
    SW = new Quadtree(image,x, y + height / 2.0f,  level+1, maxLevel);
    SE = new Quadtree(image,x + width / 2.0f, y + height / 2.0f, level+1, maxLevel);
}
Quadtree::Quadtree(const Mat& image):

    x       (0),
    y       (0),
    width   (image.rows),
    height  (image.cols),
    level   (0),
    isLeaf(false)
{
    maxLevel=maxDepthAuthorised();

    NW = new Quadtree(image,x, y, level+1, maxLevel);
    NE = new Quadtree(image,x + width / 2.0f, y,  level+1, maxLevel);
    SW = new Quadtree(image,x, y + height / 2.0f,  level+1, maxLevel);
    SE = new Quadtree(image,x + width / 2.0f, y + height / 2.0f, level+1, maxLevel);

}

bool Quadtree::isNode(){
    if (level == maxLevel || isLeaf){return false;}
    return true;
}




void 
Quadtree::createQuadtree(Mat& image,AbstractCriteria& critere){
    if (isNode()){
        //cout <<1 <<endl;
        Rect rect =  Rect(x,y, width,  height);
        Mat test(image,rect);
        //cout <<3 <<endl;
        if (critere.isInteresting(test)){
            isLeaf=false;
            //cout<< NW->x <<endl;
            NW->createQuadtree(image,critere);
            NE->createQuadtree(image,critere);
            SW->createQuadtree(image,critere);
            SE->createQuadtree(image,critere);
        }
        else{
            isLeaf=true;
        }
    }
}

And the hpp file:

class Quadtree {
public:
        Quadtree()=default;
        Quadtree(const Mat& image,const float& _x,const float& _y, const int& _level, const int& _maxLevel);
        Quadtree(const Mat& image);
        bool isNode();
        int maxDepthAuthorised();

        const int& getMaxLevel()const;



        void createQuadtree(Mat& image,AbstractCriteria& critere);

        void dessinerQuadtree(Mat& imageQuad,const bool& lin,const bool& filled);
        Scalar calculeCouleur();
        Mat afficherQuadtree(Mat& image,const bool& line_,const bool& filled);

        void changeDepth(int val,AbstractCriteria& critere,Mat& image);

        void freqPerLevel(map<int,int>& freqHash);
        void areaPerLevel(map<int,int>& areaHash);

        void reinitialiser(int& max);

        //~Quadtree();


protected:
    float   x;
    float   y;
    float   width;
    float   height;
    int     level;
    int     maxLevel;
    bool    isLeaf;

    Quadtree *  parent;
    Quadtree *  NW;
    Quadtree *  NE;
    Quadtree *  SW;
    Quadtree *  SE;

};

Thank you in advance for your help.