从 Outsie it 范围 C++ 访问局部变量
access local variable from outsie it scope c++
我有一个函数(trackObject),它有局部变量(posX和posY)。
在该函数中,有两个依赖于这两个变量的函数调用。(检查交叉点和检查命中)。
如果我编译程序,程序运行良好但滞后。
我想在 trackObject 之外调用这两个函数,但这两个局部变量无法访问。我想访问它,但我不知道如何访问。
我尝试将这些变量设为全局变量,但使这些变量全局化会使另一个变量(moments、moment10、moment01 和 area)也必须设置为全局变量。
但是当我这样做时,我会收到堆损坏异常。
有人知道怎么做吗?或者还有另一种解决方案?
这是我的代码
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);
}
使用指针将它们复制到调用函数的变量中:
void trackObject(IplImage* imgThresh, int *pX, int *pY)
{
...
*pX = moment10 / area;
*pY = moment01 / area;
...
}
调用函数将需要一些int
来存储这些值:
IplImage imgThresh;
int copyX, copyY;
trackObject(&imgThresh, ©X, ©Y);
通过删除对 malloc() 和 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);
//...
请注意 CvMoments 局部变量的声明,然后只需要将此局部变量的地址传递给需要指向 CvMoments 的指针的函数。
如果您遇到性能问题,可能是由于以下行:
CvMoment = (CvMoments)malloc(sizeof(CvMoments));
如果频繁调用trackObject()
,则重复调用malloc
成本很高。
相关文章:
- 使用的未初始化局部变量'Quick'
- 修复未初始化的局部变量错误
- 局部变量保留函数中的值
- 如何使用 C++ 中的继承函数访问派生类中的局部变量
- 将引用分配给局部变量,如果局部变量超出范围,它会超出范围吗?
- 如何在函数外部访问函数中局部变量的值?
- 从 void 函数访问局部变量
- 访问调用函数的局部变量
- 从 Outsie it 范围 C++ 访问局部变量
- 在C++中如何在类方法之间访问静态局部变量的值
- 这段代码如何访问C++外部的局部变量
- Lambdas和引用捕获局部变量:在范围之后访问
- 函数的局部变量是如何从堆栈中访问的
- 在c++中是否有办法访问外部作用域中的局部变量?
- 使用局部变量或多次访问结构体值(c++)
- visual是在c++中访问不从函数外部访问的volatile局部变量的可观察行为
- 通过非线程局部对象访问的线程局部变量
- 在不同优化级别访问gcc/g++中的局部变量与全局变量的速度
- 是否可以在其范围之外访问局部变量的内存?
- 访问与局部变量(或参数)同名的成员字段