aoptflowlk.cpp


读源程序系列

LK光流算法

#include "cvtest.h"

#if 0

/* Testing parameters */
static char FuncName[] = "cvCalcOpticalFlowLK";
static char TestName[] = "Optical flow (Lucas & Kanade)";
static char TestClass[] = "Algorithm";

static long lImageWidth;
static long lImageHeight;
static long lWinWidth;
static long lWinHeight;

#define EPSILON 0.00001f

static int fmaCalcOpticalFlowLK( void )
{
    /* Some Variables */
    int* WH = NULL;
    int* WV = NULL;

    int W3[3] =  { 1,  2, 1 };
    int W5[5] =  { 1,  4,  6,  4, 1 };
    int W7[7] =  { 1,  6,  15, 20, 15, 6, 1 };
    int W9[9] =  { 1,  8,  28, 56, 70, 56, 28, 8, 1 };
    int W11[11] = {1, 10,  45, 120, 210, 252, 210, 120, 45, 10, 1 };

    int            i,j,m,k;
    uchar*         roiA;
    uchar*         roiB;

    float*         VelocityX;
    float*         VelocityY;

    float*         DerivativeX;
    float*         DerivativeY;
    float*         DerivativeT;

    long           lErrors = 0;
    CvSize        winSize;

    int HRad;
    int VRad;

    float A1, A2, B1, B2, C1, C2;

    static int  read_param = 0;

    /* Initialization global parameters */
    if( !read_param )
    {
        read_param = 1;
        /* Reading test-parameters */
        trslRead( &lImageHeight, "563", "Image height" );
        trslRead( &lImageWidth, "345", "Image width" );
        trslRead( &lWinHeight, "7", "win height 3/5/7/9/11 " );
        trslRead( &lWinWidth, "9", "win width 3/5/7/9/11 " );
    }

    /* Checking all sizes of source histogram in ranges */
    IplImage* imgA = cvCreateImage( cvSize(lImageWidth,lImageHeight), IPL_DEPTH_8U, 1 );
    IplImage* imgB = cvCreateImage( cvSize(lImageWidth,lImageHeight), IPL_DEPTH_8U, 1 );

    IplImage* testVelocityX = cvCreateImage( cvSize(lImageWidth,lImageHeight), IPL_DEPTH_32F, 1 );
    IplImage* testVelocityY = cvCreateImage( cvSize(lImageWidth,lImageHeight), IPL_DEPTH_32F, 1 );

    VelocityX = (float*)cvAlloc(  lImageWidth * lImageHeight * sizeof(float) );
    VelocityY = (float*)cvAlloc(  lImageWidth * lImageHeight * sizeof(float) );

    DerivativeX = (float*)cvAlloc( lImageWidth * lImageHeight * sizeof(float) );
    DerivativeY = (float*)cvAlloc( lImageWidth * lImageHeight * sizeof(float) );
    DerivativeT = (float*)cvAlloc( lImageWidth * lImageHeight * sizeof(float) );

    winSize.height = lWinHeight;
    winSize.width =  lWinWidth;

    switch (lWinHeight)
    {
    case 3:
        WV = W3; break;
    case 5:
        WV = W5; break;
    case 7:
        WV = W7; break;
    case 9:
        WV = W9; break;
    case 11:
        WV = W11; break;
    }
    switch (lWinWidth)
    {
    case 3:
        WH = W3; break;
    case 5:
        WH = W5; break;
    case 7:
        WH = W7; break;
    case 9:
        WH = W9; break;
    case 11:
        WH = W11; break;

    }

    HRad = (winSize.width - 1)/2;
    VRad = (winSize.height - 1)/2;

    /* Filling images */
    ats1bInitRandom( 0, 255, (uchar*)imgA->imageData, lImageHeight * imgA->widthStep );
    ats1bInitRandom( 0, 255, (uchar*)imgB->imageData, imgA->widthStep * lImageHeight );

    /* Run CVL function */
    cvCalcOpticalFlowLK( imgA , imgB, winSize,
                         testVelocityX, testVelocityY );


    /* Calc by other way */
    roiA = (uchar*)imgA->imageData;
    roiB = (uchar*)imgB->imageData;  

    /* Calculate derivatives */
    for (i=0; i<imgA->height; i++)
    {
        for(j=0; j<imgA->width; j++)
        {
            int jr,jl,it,ib;

            if ( j==imgA->width-1 )
                jr = imgA->width-1;
            else jr = j + 1;

            if ( j==0 )
                jl = 0;
            else jl = j - 1;

            if ( i==(imgA->height - 1) )
                ib = imgA->height - 1;
            else ib = i + 1;

            if ( i==0 )
                it = 0;
            else it = i - 1;

            DerivativeX[ i*lImageWidth + j ] = (float)
                (roiA[ (it)*imgA->widthStep + jr ]
                - roiA[ (it)*imgA->widthStep + jl ]
                + 2*roiA[ (i)*imgA->widthStep + jr ]
                - 2*roiA[ (i)*imgA->widthStep + jl ]
                + roiA[ (ib)*imgA->widthStep + jr ]
                - roiA[ (ib)*imgA->widthStep + jl ]) ;

            DerivativeY[ i*lImageWidth + j ] = (float)
                ( roiA[ (ib)*imgA->widthStep + jl ]
                + 2*roiA[ (ib)*imgA->widthStep + j  ]
                + roiA[ (ib)*imgA->widthStep + jr ]
                - roiA[ (it)*imgA->widthStep + jl ]
                - 2*roiA[ (it)*imgA->widthStep + j  ]
                - roiA[ (it)*imgA->widthStep + jr ])  ;

            DerivativeT[ i*lImageWidth + j ] = (float)
                (roiB[i*imgB->widthStep + j] - roiA[i*imgA->widthStep + j])*8;
        }
    }

    for( i = 0; i < lImageHeight; i++)
    {
        for(j = 0; j< lImageWidth; j++)
        {
            A1 =0;
            A2 =0;
            B1 =0;
            B2 =0;
            C1= 0;
            C2= 0;

            for( k = -VRad ; k <= VRad ; k++ )
            {
                for( m = - HRad; m <= HRad ; m++ )
                {
                    int coord = (i+k)*lImageWidth + (j+m);
                    if ( (j+m<0)              || 
                         (j+m >lImageWidth-1) || 
                         ( (k+i)<0 )          || 
                         ( (k+i)>lImageHeight-1) )
                    {continue;}

                    A1+=WV[k+VRad]*WH[m+HRad]* DerivativeX[coord]*DerivativeY[coord];
                    A2+=WV[k+VRad]*WH[m+HRad]* DerivativeX[coord]*DerivativeX[coord];
                    B1+=WV[k+VRad]*WH[m+HRad]* DerivativeY[coord]*DerivativeY[coord];
                    B2+=WV[k+VRad]*WH[m+HRad]* DerivativeX[coord]*DerivativeY[coord];
                    C1+=WV[k+VRad]*WH[m+HRad]* DerivativeY[coord]*DerivativeT[coord];
                    C2+=WV[k+VRad]*WH[m+HRad]* DerivativeX[coord]*DerivativeT[coord];
                }
            }
            if (A1*B2 - A2*B1)
            {
                VelocityX[i*lImageWidth + j] = - (C1*B2 - C2*B1)/(A1*B2 - A2*B1);
                VelocityY[i*lImageWidth + j] = - (A1*C2 - A2*C1)/(A1*B2 - A2*B1);
            }
            else if ( (A1+A2)*(A1+A2) + (B1+B2)*(B1+B2) )
            {   /* Calculate Normal flow */
                VelocityX[i*lImageWidth + j] = -(A1+A2)*(C1+C2)/((A1+A2)*(A1+A2)+(B1+B2)*(B1+B2));
                VelocityY[i*lImageWidth + j] = -(B1+B2)*(C1+C2)/((A1+A2)*(A1+A2)+(B1+B2)*(B1+B2));
            }
            else
            {
                VelocityX[i*lImageWidth + j] = 0;
                VelocityY[i*lImageWidth + j] = 0;
            }
        }
    }

    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", tvx );
                //trsWrite( ATS_LST | ATS_CON, " mustX  %f \n", VelocityX[i*lImageWidth + j] );

                //trsWrite( ATS_LST | ATS_CON, " ValueY %f \n", tvy );
                //trsWrite( ATS_LST | ATS_CON, " mustY  %f \n", VelocityY[i*lImageWidth + j] );

                //trsWrite( ATS_LST | ATS_CON, " Coordinates %d %d\n", i, j );

                lErrors++;
            }
        }
    }
    cvFree( &VelocityX );
    cvFree( &VelocityY );
    
    cvFree( &DerivativeX );
    cvFree( &DerivativeY );
    cvFree( &DerivativeT );

    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 );
} /*fmaCalcOpticalFlowLK*/

void InitACalcOpticalFlowLK( void )
{
    /* Registering test function */
    trsReg( FuncName, TestName, TestClass, fmaCalcOpticalFlowLK );
} /* InitAACalcOpticalFlowLK */

#endif

/* End of file. */


Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值