dimanche 25 juin 2017

memory got lost during chain of responsibility example c++

I am running sequence of image processing operations using chain of responsibility in opencv C++. The return result seems got lost as soon as the recursive function processRequest(OpRequest request) returns. Could someone point of the error I made here. Thanks in advance. The following are the code:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp> 
using namespace cv;


#define ABS_DIFF_OP 1<<0
#define THRESHOLD_ADAPTIVE_OP 1<<1

class OpRequest
{
int
    opType;
vector<Mat> imgs;

public:

OpRequest(int opType, const vector<Mat> imgs)
{
    this->opType = opType;
    this->imgs = imgs;
    return;

}//End method

int getOpType()
{
    return opType;
}//End method

void setOpType(int opType)
{
    this->opType = opType;
    return;
}//End method

void setImages(const vector<Mat> imgs)
{
    this->imgs = imgs;
    return;
}//End method

vector<Mat> getImages()
{
    return imgs;
}//End method

};//End class

// Base class
class Op  
{
private:
Op* successor;
int op;
Mat result;
public:
Op()
{
    successor = NULL;
    op = -1;
}

Op(Op* successor, int op)
{
    this->successor = successor;
    this->op = op;
    return;
}//End method


virtual Mat apply(const vector<Mat> imgs) = 0;
void setSuccessor(Op* successor)
{
    this->successor = successor;
    return;
}//End method

void setOpType(int opType)
{
    op = opType;
    return;
}//End method

int getOpType()
{
    return op;
}//End method

Mat getResult()
{
    return result;
}//End method

void processRequest(OpRequest request)
{

    if( op == request.getOpType() )
    {
        vector<Mat>
            imgs = request.getImages();
        Mat m = apply(imgs);

        if( this->successor == NULL )
        {
            m.copyTo(result);

        }
        else
        {
            vector<Mat>
                nextImgs;
            nextImgs.push_back(m);
            OpRequest nextRequest(successor->getOpType(), nextImgs);
            successor->processRequest(nextRequest);
            cout<<"move to next"<<endl;
        }
    }//End if( op == request.getOpType() )

    //imshow("result", result);
    //waitKey(0);

    return;
}//End method

};  //End class


class DIFF_Op : public Op
{
 public:

    DIFF_Op()
    : Op()    
    {

    }//End constructor

     DIFF_Op(Op* successor, int op)
    : Op(successor,op)    
    {

    }//End constructor

    Mat apply(const vector<Mat> imgs)
    {
        Mat
            f1 = imgs[0],
            f2 = imgs[1];
        Mat
            diff;
        cv::absdiff(f1,f2,diff);
        return diff;

    }//End method

};//End class


class THRESHOLD_ADAPTIVE_Op : public Op
{
 public:

    THRESHOLD_ADAPTIVE_Op()
    : Op()    
    {

    }//End constructor

     THRESHOLD_ADAPTIVE_Op(Op* successor, int op)
    : Op(successor,op)    
    {

    }//End constructor

    Mat apply(const vector<Mat> imgs)
    {
        Mat
            diff = imgs[0];

         Mat
            th;
        adaptiveThreshold(diff,th,255,ADAPTIVE_THRESH_MEAN_C, 
THRESH_BINARY,7,4);
        //imshow("th",th);
        return th;

    }//End method

};//End class
//The testing function is here:

void testChainOfRes()
{
int opType = ABS_DIFF_OP;
vector<Mat> imgs;

imgs.push_back(imread("test/0c.bmp", CV_LOAD_IMAGE_GRAYSCALE ));
imgs.push_back(imread("test/1c.bmp", CV_LOAD_IMAGE_GRAYSCALE ));

OpRequest
    request(opType, imgs); 


THRESHOLD_ADAPTIVE_Op*
    thAdOp = new THRESHOLD_ADAPTIVE_Op(NULL,THRESHOLD_ADAPTIVE_OP);
DIFF_Op*
    diffOp = new DIFF_Op(thAdOp,ABS_DIFF_OP);


diffOp->processRequest(request);

Mat 
    result = diffOp->getResult(); 

loadImage(result);




delete diffOp;

return ;
}//End method

The result Mat becomes empty as soon as the last processRequest function call returns.

Aucun commentaire:

Enregistrer un commentaire