【发布时间】:2011-03-30 22:50:36
【问题描述】:
我目前正在编写用于图像处理的小型应用程序。但是,我的程序的内存使用存在很大问题。我是 C++ 的新手,以前我主要是用 C# 编程。
几乎完成所有工作的函数看起来像这样
while(!prototypeVector[i]->GetIsConvergaed())
{
if(previousPrototype!=NULL) delete previousPrototype;
previousPrototype = prototypeVector[i]->CopyPrototype();
if(&matchingPointVector!=NULL) matchingPointVector.clear();
matchingPointVector = prototypeVector[i]->CalculateMatchingPointsAll(imageDataVector);
distanseMatrix = CalculateDistanceAll(i);
membershipMatrix = UpdateMembershipAll(i);
if(translation)
{
tmatrix = UpdateTranslationMatrix(i);
if(directUpdate) prototypeVector[i]->SetTranslationMatrix( tmatrix);
//prototypeVector[i]->GetTranslationMatrix().DisplayMatrix();
tmatrix.DisplayMatrix();
}
if(scaling)
{
smatrix = UpdateScalingMatrix(i);
if(directUpdate) prototypeVector[i]->SetScalingMatrix(smatrix);
smatrix.DisplayMatrix();
}
if(rotation)
{
angle = UpdateAngleCoefficient(i);
cout<<endl;
Convert::RadiansToDegrees(angle)<<endl;
if(directUpdate)prototypeVector[i]->UpdateRotationMatrix(angle);
}
prototypeVector[i]->TransformTemplateOne(prototypeVector[i]->GetRotationMatrix(), prototypeVector[i]->GetScalingMatrix() , prototypeVector[i]->GetTranslationMatrix());
}
我注意到如果上面写的函数被称为另一个函数
CalculateMatchingPointsAll 或 CalculateDistanceAll 或 UpdateScalingMatrix 内存使用量急剧上升(执行上述每个函数后为 300kB)。所以我想问题出在这些功能上。他们看起来像那样
vector<Point*> TemplateClusterPoint::CalculateMatchingPointsAll( vector<Point*> imageDataVector)
{
vector<Point*> matchinPointVector = vector<Point*>(imageDataVector.size(),new Point(0,0));
double minValue = DOUBLE_MAX_VALUE;
double currentDistance = 0;
for (int i=0;i<imageDataVector.size();i++ )
{
//matchinPointVector[i] = this->CalculateMatchingPoint(/*prototypePointVector,*/imageDataVector[i]);
for (int j=0;j<prototypePointVector.size();j++)
{
if( (currentDistance = CalculateDistance(imageDataVector[i],prototypePointVector[j]) ) <= minValue )
{
minValue = currentDistance;
matchinPointVector[i] = prototypePointVector[j];
}
}
minValue = currentDistance = DOUBLE_MAX_VALUE;
}
return matchinPointVector;
}
vector<vector<double>> AlgorithmPointBased::CalculateDistanceAll( int clusterIndex)
{
//vector<Point*> pointVector = prototypeVector[clusterIndex]->GetPrototypePointVector();
Point* firstPoint = NULL;
Point* secondPoint = NULL;
for(int i=0;i<imageDataVector.size();i++ )
{
firstPoint = imageDataVector[i];
secondPoint = matchingPointVector[i];
distanseMatrix[clusterIndex][i] = pow( (firstPoint->GetX() - secondPoint->GetX() ), 2 ) + pow( (firstPoint->GetY() - secondPoint->GetY() ), 2); //(difference*difference)[0][0]; //funkcja dystansu = d^2 = (Xi - Pji)^2
// gdzie Xi punkt z obrazu, Pij matching point w danym klastrze
}
return distanseMatrix;
}
Matrix<double> AlgorithmPointBased::UpdateScalingMatrix( int clusterIndex )
{
double currentPower = 0;
vector<Point*> prototypePointVecotr = prototypeVector[clusterIndex]->GetPrototypePointVector();
vector<Point*> templatePointVector = templateCluster->GetTemplatePointVector();
Point outcomePoint;
Matrix<double> numerator = Matrix<double>(1,1,0);
double denominator=0;
for (int i=0;i< imageDataVector.size();i++)
{
Point templatePoint = *matchingPointVector[i];
currentPower = pow(membershipMatrix[clusterIndex][i],m);
numerator += / ((*imageDataVector[i] - prototypeVector[clusterIndex]->GetTranslationMatrix()).Transpose()* (prototypeVector[clusterIndex]->GetRotationMatrix() * templatePoint )* currentPower);
denominator += (currentPower* (pow(templatePoint.GetX(),2) + pow(templatePoint.GetY(),2)));
}
numerator /= denominator;
return numerator;
}
正如您所见,这些函数所做的几乎所有工作都是计算新点或最近点或转换图像。在执行这些功能后,有什么方法可以以某种方式释放至少一些内存。 我想最消耗内存的操作是矩阵的乘法或点上的操作。我重载了 operator + * 和 / 当然返回新对象。
已编辑 重载的操作符是这样的
Point Point::operator*( double varValue )
{
return *(new Point(this->x * varValue,this->y * varValue));
}
Point Point::operator-( Point& secondPoint )
{
return *(new Point(this->x - secondPoint.GetX(),this->y - secondPoint.GetY()));
}
Point Point::operator*( double varValue )
{
return *(new Point(this->x * varValue,this->y * varValue));
}
template<typename T>
Matrix<T> Matrix<T>::operator + (double value)
{
Matrix<T>* addedMatrix = new Matrix<T>(this->rows,this->columns);
for (int i=0;i<this->rows;i++)
{
for (int j=0;j<this->columns;j++)
{
(*addedMatrix)[i][j] = (*this)[i][j]+ value;
}
}
return *addedMatrix;
}
Point Point::operator/( double varValue )
{
return *(new Point(this->x / varValue,this->y / varValue));
}
【问题讨论】:
-
"我重载了运算符 + * 和 /,它们当然会返回新对象。"这些功能是什么样的?
-
如果您正在学习 C++,请确保您拥有 a good introductory book。 C# 和 C++ 是非常不同的语言。
-
@James McNellis 看看我的编辑
标签: c++ optimization memory memory-management