19,468
社区成员
发帖
与我相关
我的任务
分享
//Live-Wire 2-D DP graph search
void CSegmentDlg::SearchOptimalPath()
{
float tempTotalCost;
int m,n,i,j;
while (!ifActivePixelsEmpty())//While still points to expand
{
//Remove minimum cost pixel q from active list.
double minCost=0;
// int m,n;
for(i=0;i<nw;i++)
for( j=0;j<nh;j++)
if(ActivePixels[i][j])
{
if (minCost=0)
{
minCost=TotalCost[i][j];
m=i;
n=j;
}
else if(TotalCost[i][j]<=minCost)
{
minCost=TotalCost[i][j];
m=i;
n=j;
}
}
ActivePixels[m][n]=0;
//Mark q as expanded (i.e., processed).
ifExpanded[m][n]=1;
for( j=n-1;j<=n+1;j++)
for(i=m-1;i<=m+1;i++)
if(i!=m&&j!=n&&i>=0&&i<nw&&j>=0&&j<nh)
if(ifExpanded[i][j]==0)
{
//Compute total cost to neighbor.
getthefD(TheResult,m,n,i,j);
getthefG(TheResult,i,j);
getthefZ(TheResult,i,j);
tempTotalCost=TotalCost[m][n]+getCost();
//Remove higher cost neighbor’s from list
if((ActivePixels[i][j]==1)&&(tempTotalCost<TotalCost[i][j]))
{
ActivePixels[i][j]=0;
TotalCost[i][j]=tempTotalCost;
Pointers[i][j]=CPoint(m+tx,n+ty);
CClientDC dc(this);
dc.MoveTo(i+tx,j+ty);
dc.LineTo(m+tx,n+ty);
}
//If neighbor not on list,
if(ActivePixels[i][j]==0)
{
//assign neighbor’s total cost,
TotalCost[i][j]=tempTotalCost;
// set (or reset) back pointer, what pointer??
Pointers[i][j]=CPoint(m+tx,n+ty);
CClientDC dc(this);
dc.MoveTo(i+tx,j+ty);
dc.LineTo(m+tx,n+ty);
ActivePixels[i][j]=1;// and place on (or return to) active list.
}
}
}
}
//判断List of active pixels是否为空
bool CSegmentDlg::ifActivePixelsEmpty()
{
for(int i=0;i<256;i++)
{
for(int j=0;j<256;j++)
if(ActivePixels[i][j]==1)
//if(ifExpanded[i][j]==0)
{
return false;
}
}
return true;
}
if(mx>=0&&mx<=nw &&my<=nh&&my>=0)
{
if(ifPainting==false&&(SeedPoint.x==0||SeedPoint.y==0))
{
ifPainting=true;
SeedPoint=point;
FreePoint=point;
SeedPixel=cvGet2D(TheResult,mx,my);
ActivePixels[mx][my]=1;
SearchOptimalPath();
}
}