📄 aoptflowhs.cpp
字号:
trsWrite( ATS_LST|ATS_CON,
"usePrevious = %d, criteria = EPS, epsilon = %f\n",
usePrevious, criteria.epsilon);
break;
}
case 2:
{
criteria.type = CV_TERMCRIT_EPS | CV_TERMCRIT_ITER;
criteria.epsilon = 0.0001f;
criteria.maxIter = 3;
trsWrite( ATS_LST|ATS_CON,
"usePrevious = %d,"
"criteria = EPS|ITER,"
"epsilon = %f, maxIter = %d\n",
usePrevious, criteria.epsilon, criteria.maxIter);
break;
}
case 3:
{
criteria.type = CV_TERMCRIT_EPS | CV_TERMCRIT_ITER;
criteria.epsilon = 0.00001f;
criteria.maxIter = 100;
trsWrite( ATS_LST|ATS_CON,
"usePrevious = %d,"
"criteria = EPS|ITER,"
"epsilon = %f, maxIter = %d\n",
usePrevious, criteria.epsilon, criteria.maxIter);
break;
}
}
Stop = 0;
/* Run CVL function */
cvCalcOpticalFlowHS( imgA , imgB, usePrevious,
testVelocityX, testVelocityY,
lambda, criteria );
/* Calc by other way */
if (!usePrevious)
{
/* Filling initial velocity with zero */
for (i = 0; i < lImageWidth * lImageHeight; i++ )
{
VelocityX[i] = 0 ;
VelocityY[i] = 0 ;
}
}
iteration = 0;
while ( !Stop )
{
float* oldX;
float* oldY;
float* newX;
float* newY;
iteration++;
if ( iteration & 1 )
{
oldX = VelocityX;
oldY = VelocityY;
newX = auxVelocityX;
newY = auxVelocityY;
}
else
{
oldX = auxVelocityX;
oldY = auxVelocityY;
newX = VelocityX;
newY = VelocityY;
}
for( i = 0; i < lImageHeight; i++)
{
for(j = 0; j< lImageWidth; j++)
{
float aveX = 0;
float aveY = 0;
float dx,dy,dt;
aveX +=(j==0) ? oldX[ i*lImageWidth + j ] : oldX[ i*lImageWidth + j-1 ];
aveX +=(j==lImageWidth-1) ? oldX[ i*lImageWidth + j ] :
oldX[ i*lImageWidth + j+1 ];
aveX +=(i==0) ? oldX[ i*lImageWidth + j ] : oldX[ (i-1)*lImageWidth + j ];
aveX +=(i==lImageHeight-1) ? oldX[ i*lImageWidth + j ] :
oldX[ (i+1)*lImageWidth + j ];
aveX /=4;
aveY +=(j==0) ? oldY[ i*lImageWidth + j ] : oldY[ i*lImageWidth + j-1 ];
aveY +=(j==lImageWidth-1) ? oldY[ i*lImageWidth + j ] :
oldY[ i*lImageWidth + j+1 ];
aveY +=(i==0) ? oldY[ i*lImageWidth + j ] : oldY[ (i-1)*lImageWidth + j ];
aveY +=(i==lImageHeight-1) ? oldY[ i*lImageWidth + j ] :
oldY[ (i+1)*lImageWidth + j ];
aveY /=4;
dx = DerX[ i*lImageWidth + j ];
dy = DerY[ i*lImageWidth + j ];
dt = DerT[ i*lImageWidth + j ];
/* Horn & Schunck pure formulas */
newX[ i*lImageWidth + j ] = aveX - ( dx * aveX +
dy * aveY + dt ) * lambda * dx /
(1 + lambda * ( dx*dx + dy*dy ));
newY[ i*lImageWidth + j ] = aveY - ( dx * aveX +
dy * aveY + dt ) * lambda * dy /
(1 + lambda * ( dx*dx + dy*dy ));
}
}
/* evaluate epsilon */
epsilon = 0;
for ( i = 0; i < lImageHeight; i++)
{
for ( j = 0; j < lImageWidth; j++)
{
epsilon = MAX((float)fabs(newX[i*lImageWidth + j]
- oldX[i*lImageWidth + j]), epsilon );
epsilon = MAX((float)fabs(newY[i*lImageWidth + j]
- oldY[i*lImageWidth + j]), epsilon );
}
}
switch (criteria.type)
{
case CV_TERMCRIT_ITER:
Stop = (criteria.maxIter == iteration );break;
case CV_TERMCRIT_EPS:
Stop = (criteria.epsilon > epsilon );break;
case CV_TERMCRIT_ITER|CV_TERMCRIT_EPS:
Stop = ( ( criteria.epsilon > epsilon ) ||
( criteria.maxIter == iteration ));
break;
}
if (Stop)
{
if ( (newX != VelocityX) && (newY != VelocityY) )
{
memcpy( VelocityX, newX, lImageWidth * lImageHeight * sizeof(float) );
memcpy( VelocityY, newY, lImageWidth * lImageHeight * sizeof(float) );
}
}
}
trsWrite( ATS_LST|ATS_CON,
"%d iterations are made\n", iteration );
for( i = 0; i < lImageHeight; i++)
{
for(j = 0; j< lImageWidth; j++)
{
float tvx = ((float*)(testVelocityX->imageData + i*testVelocityX->widthStep))[j];
float tvy = ((float*)(testVelocityY->imageData + i*testVelocityY->widthStep))[j];
if (( fabs( tvx - VelocityX[i*lImageWidth + j])>EPSILON )||
( fabs( tvy - VelocityY[i*lImageWidth + j])>EPSILON ) )
{
//trsWrite( ATS_LST | ATS_CON, " ValueX %f \n",
// testVelocityX[i*lROIWidth + j] );
//trsWrite( ATS_LST | ATS_CON, " mustX %f \n",
// VelocityX[i*lROIWidth + j] );
//trsWrite( ATS_LST | ATS_CON, " ValueY %f \n",
// testVelocityY[i*lROIWidth + j] );
//trsWrite( ATS_LST | ATS_CON, " mustY %f \n",
// VelocityY[i*lROIWidth + j] );
//trsWrite( ATS_LST | ATS_CON, " Coordinates %d %d\n", i, j );
lErrors++;
}
}
}
}/* for */
/* Filling initial velocity with zero */
iplSetFP( testVelocityX, 0 );
iplSetFP( testVelocityY, 0 );
for (i = 0; i < lImageWidth * lImageHeight; i++ )
{
VelocityX[i] = 0 ;
VelocityY[i] = 0 ;
}
}
/* Free memory */
icvFree( (void**)&VelocityX );
icvFree( (void**)&VelocityY );
icvFree( (void**)&auxVelocityX );
icvFree( (void**)&auxVelocityY );
icvFree( (void**)&DerX );
icvFree( (void**)&DerY );
icvFree( (void**)&DerT );
cvReleaseImage( &imgA );
cvReleaseImage( &imgB );
cvReleaseImage( &testVelocityX );
cvReleaseImage( &testVelocityY );
if( lErrors == 0 ) return trsResult( TRS_OK, "No errors fixed for this text" );
else return trsResult( TRS_FAIL, "Total fixed %d errors", lErrors );
} /*fmaCalcOpticalFlowHS*/
void InitACalcOpticalFlowHS( void )
{
/* Registering test function */
trsReg( FuncName, TestName, TestClass, fmaCalcOpticalFlowHS );
} /* InitACalcOpticalFlowHS */
/* End of file. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -