Memory Leak Problem Quadtree OpenCv

asked 2015-04-15 07:42:29 -0600

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.

edit retag flag offensive close merge delete

Comments

You are not cloning the cv::Mat object, so no implicit copies are made. cv::Mat objects are anyway reference counted and the actual data is not moved around because the object stores a pointer to the data. Are you deleting the parent, NW, NE, SW and SE pointers somewhere else? Otherwise your Quadtree class leaks the data it created (with new) in the constructor.

boaz001 gravatar imageboaz001 ( 2015-04-16 05:31:36 -0600 )edit