Kevin phytagoras
Kevin phytagoras

Reputation: 123

access local variable from outsie it scope c++

i have a function (trackObject) that has local variables(posX and posY).

and inside that function there are two function call that depends on those 2 variables.(checkIntersection and checkHit).

If i compile the program, the program run well but laggy.

I want to call those 2 function outside trackObject, but those two local variables can't be accessed. I want to access it, but i don't know how.

I try to make those variables global, but making those variables global make another variables(moments, moment10, moment01,and area) have to set to global too.

But when i do that, i get Heap Corruption exception.

Anyone know how to do it ? Or there are another solution for this ?

Here's my code

void trackObject(IplImage* imgThresh){
    // Calculate the moments of 'imgThresh'
    CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
    cvMoments(imgThresh, moments, 1);
    double moment10 = cvGetSpatialMoment(moments, 1, 0);
    double moment01 = cvGetSpatialMoment(moments, 0, 1);
    double area = cvGetCentralMoment(moments, 0, 0);

     // if the area<1000, I consider that the there are no object in the image and it's because of the noise, the area is not zero 
    if(area>1000){
        // calculate the position of the ball
        int posX = moment10/area;
        int posY = moment01/area;        

       if(lastX>=0 && lastY>=0 && posX>=0 && posY>=0)
        {
            // Draw a yellow line from the previous point to the current point
            cvLine(imgTracking, cvPoint(posX, posY), cvPoint(lastX, lastY), cvScalar(0,0,255), 4);

        }
        checkIntersection(300, lastY, posY);
        checkHit(posX,posY,lastX,lastY,startLineX, startLineY, endLineX, endLineY);
        lastX = posX;
        lastY = posY;
    }
    cvLine(imgTracking,cv::Point(100,300) , cv::Point(600,300),cv::Scalar(0,200,0),2,8);
    cvLine(imgTracking,cv::Point(100,400) , cv::Point(168,400),cv::Scalar(255,0,122),8,8);
    cvLine(imgTracking,cv::Point(171,400) , cv::Point(239,400),cv::Scalar(255,0,122),8,8);
    cvLine(imgTracking,cv::Point(241,400) , cv::Point(309,400),cv::Scalar(255,0,122),8,8);
    cvLine(imgTracking,cv::Point(312,400) , cv::Point(380,400),cv::Scalar(255,0,122),8,8);
    cvLine(imgTracking,cv::Point(383,400) , cv::Point(451,400),cv::Scalar(255,0,122),8,8);
    cvLine(imgTracking,cv::Point(454,400) , cv::Point(522,400),cv::Scalar(255,0,122),8,8);
    cvLine(imgTracking,cv::Point(525,400) , cv::Point(600,400),cv::Scalar(255,0,122),8,8);
    free(moments); 
}

Upvotes: 1

Views: 157

Answers (3)

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

To make less "laggy" by removing calls to malloc() and free():

void trackObject(IplImage* imgThresh){
    CvMoments moments;
    cvMoments(imgThresh, &moments, 1);
    double moment10 = cvGetSpatialMoment(&moments, 1, 0);
    double moment01 = cvGetSpatialMoment(&moments, 0, 1);
    double area = cvGetCentralMoment(&moments, 0, 0);
    //...

Note the declaration of the CvMoments local variable, and then all that is needed is to pass the address of this local variable to the functions that require a pointer to CvMoments.

Upvotes: 1

Commodore63
Commodore63

Reputation: 359

If you are having problems with performance, it is probably due to the line:

CvMoments moments = (CvMoments)malloc(sizeof(CvMoments));

If trackObject() is called frequently, malloc is expensive to call repeatedly.

Upvotes: 0

Fiddling Bits
Fiddling Bits

Reputation: 8861

Copy them to variables of the calling function by using pointers:

void trackObject(IplImage* imgThresh, int *pX, int *pY)
{
    ...

    *pX = moment10 / area;
    *pY = moment01 / area;

    ...
}

The calling function will need some ints to store these values:

IplImage imgThresh;
int copyX, copyY;

trackObject(&imgThresh, &copyX, &copyY);

Upvotes: 2

Related Questions