cvmotempl.cpp.svn-base

来自「非结构化路识别」· SVN-BASE 代码 · 共 722 行 · 第 1/2 页

SVN-BASE
722
字号
            {
                int bin = cvRound( orient[x] * hist_scale );

                bin = CV_IMAX( bin, 0 );
                bin = CV_IMIN( bin, _CV_MT2_HIST_SIZE - 1 );
                orient_hist[bin]++;
            }
        orient += orientStep;
        mask += maskStep;
    }

    /* 2. find historgam maximum */
    {
        int max_bin_val = orient_hist[0];

        for( y = 1; y < _CV_MT2_HIST_SIZE; y++ )
        {
            int bin_val = orient_hist[y];
            int max_mask = (max_bin_val >= bin_val) - 1;

            /*
               if( !(max_bin_val > bin_val) )
               max_bin_val = bin_val, base_orient = y;
             */
            max_bin_val ^= (max_bin_val ^ bin_val) & max_mask;
            base_orient ^= (base_orient ^ y) & max_mask;
        }

        base_orient *= 30;
    }


    /* 3. find the shift as weighted sum of relative angles */
    {
        double shift_orient = 0, shift_weight = 0;
        double a = (254. / 255.) / mhi_duration;
        double b = 1. - curr_mhi_timestamp * a;
        double fbase_orient = base_orient;

        /*
           a = 254/(255*dt)
           b = 1 - t*a = 1 - 254*t/(255*dur) =
           (255*dt - 254*t)/(255*dt) =
           (dt - (t - dt)*254)/(255*dt);
           --------------------------------------------------------
           ax + b = 254*x/(255*dt) + (dt - (t - dt)*254)/(255*dt) =
           (254*x + dt - (t - dt)*254)/(255*dt) =
           ((x - (t - dt))*254 + dt)/(255*dt) =
           (((x - (t - dt))/dt)*254 + 1)/255 = (((x - low_time)/dt)*254 + 1)/255
         */

        orient -= size.height * orientStep;
        mask -= size.height * maskStep;

        for( y = 0; y < size.height; y++ )
        {
            int x;

            for( x = 0; x < size.width; x++ )
                if( mask[x] != 0 && mhi[x] > delbound )
                {
                    /*
                       orient in 0..360, base_orient in 0..360
                       -> (rel_angle = orient - base_orient) in -360..360.
                       rel_angle is translated to -180..180
                     */
                    double weight = mhi[x] * a + b;
                    int rel_angle = cvRound( orient[x] - fbase_orient );

                    rel_angle += (rel_angle < -180 ? 360 : 0);
                    rel_angle += (rel_angle > 180 ? -360 : 0);
                    shift_orient += weight * rel_angle;
                    shift_weight += weight;
                }

            orient += orientStep;
            mask += maskStep;
            mhi += mhiStep;
        }

        /* 4. add base and shift orientations and normalize result */

        /* set lowest mantissa's bit to avoid division by 0 */
        *((int *) &shift_weight) |= 1;

        base_orient = base_orient + cvRound( shift_orient / shift_weight );
    }

    base_orient -= (base_orient < 360 ? 0 : 360);
    base_orient += (base_orient >= 0 ? 0 : 360);

    *angle = (float) base_orient;
    return CV_OK;

#undef _CV_MT2_HIST_SIZE
}


/* motion templates */
CV_IMPL void
cvUpdateMotionHistory( const void* silhouette, void* mhimg,
                       double timestamp, double mhi_duration )
{
    CvSize size;
    CvMat  silhstub, *silh = (CvMat*)silhouette;
    CvMat  mhistub, *mhi = (CvMat*)mhimg;
    int mhi_step, silh_step;

    CV_FUNCNAME( "cvUpdateMHIByTime" );

    __BEGIN__;

    CV_CALL( silh = cvGetMat( silh, &silhstub ));
    CV_CALL( mhi = cvGetMat( mhi, &mhistub ));

    if( !CV_IS_MASK_ARR( silh ))
        CV_ERROR( CV_StsBadMask, "" );

    if( CV_MAT_CN( mhi->type ) > 1 )
        CV_ERROR( CV_BadNumChannels, "" );

    if( CV_MAT_DEPTH( mhi->type ) != CV_32F )
        CV_ERROR( CV_BadDepth, "" );

    if( !CV_ARE_SIZES_EQ( mhi, silh ))
        CV_ERROR( CV_StsUnmatchedSizes, "" );

    size = icvGetMatSize( mhi );

    mhi_step = mhi->step;
    silh_step = silh->step;

    if( CV_IS_MAT_CONT( mhi->type & silh->type ))
    {
        size.width *= size.height;
        mhi_step = silh_step = CV_STUB_STEP;
        size.height = 1;
    }

    IPPI_CALL( icvUpdateMotionHistory_8u32f_C1IR( (const uchar*)(silh->data.ptr), silh_step,
                                                  mhi->data.fl, mhi_step, size,
                                                  (float)timestamp, (float)mhi_duration ));
    __END__;
}


CV_IMPL void
cvCalcMotionGradient( const void* mhiimg, void* maskimg,
                      void* orientation,
                      double maxTDelta, double minTDelta,
                      int apertureSize )
{
    CvSize  size;
    CvMat  mhistub, *mhi = (CvMat*)mhiimg;
    CvMat  maskstub, *mask = (CvMat*)maskimg;
    CvMat  orientstub, *orient = (CvMat*)orientation;

    CV_FUNCNAME( "cvCalcMotionGradient" );

    __BEGIN__;

    CV_CALL( mhi = cvGetMat( mhi, &mhistub ));
    CV_CALL( mask = cvGetMat( mask, &maskstub ));
    CV_CALL( orient = cvGetMat( orient, &orientstub ));

    if( !CV_IS_MASK_ARR( mask ))
        CV_ERROR( CV_StsBadMask, "" );

    if( apertureSize < 3 || apertureSize > 7 || (apertureSize & 1) == 0 )
        CV_ERROR( CV_StsOutOfRange, "apertureSize must be 3, 5 or 7" );

    if( minTDelta <= 0 || maxTDelta <= 0 )
        CV_ERROR( CV_StsOutOfRange, "both delta's must be positive" );

    if( CV_MAT_CN( mhi->type ) != 1 || CV_MAT_CN( orient->type ) != 1 )
        CV_ERROR( CV_BadNumChannels, "" );

    if( CV_MAT_DEPTH( mhi->type ) != CV_32F ||
        CV_MAT_DEPTH( orient->type ) != CV_32F )
        CV_ERROR( CV_BadDepth, "" );

    if( !CV_ARE_SIZES_EQ( mhi, mask ) || !CV_ARE_SIZES_EQ( orient, mhi ))
        CV_ERROR( CV_StsUnmatchedSizes, "" );

    if( minTDelta > maxTDelta )
    {
        double t;
        CV_SWAP( minTDelta, maxTDelta, t );
    }

    size = icvGetMatSize( mhi );

    IPPI_CALL( icvCalcMotionGradient32fC1R( mhi->data.fl, mhi->step,
                                            (uchar*)(mask->data.ptr), mask->step,
                                            orient->data.fl, orient->step,
                                            size, apertureSize,
                                            (float) maxTDelta, (float) minTDelta,
                                            CV_IS_IMAGE_HDR(orientation) ?
                                            ((IplImage*)orientation)->origin : 0 ));
    __END__;
}


CV_IMPL double
cvCalcGlobalOrientation( const void* orientation, const void* maskimg, const void* mhiimg,
                         double curr_mhi_timestamp, double mhi_duration )
{
    CvSize  size;
    float  angle = 0;
    CvMat  mhistub, *mhi = (CvMat*)mhiimg;
    CvMat  maskstub, *mask = (CvMat*)maskimg;
    CvMat  orientstub, *orient = (CvMat*)orientation;
    int  mhi_step, orient_step, mask_step;

    CV_FUNCNAME( "cvCalcGlobalOrientation" );

    __BEGIN__;

    CV_CALL( mhi = cvGetMat( mhi, &mhistub ));
    CV_CALL( mask = cvGetMat( mask, &maskstub ));
    CV_CALL( orient = cvGetMat( orient, &orientstub ));

    if( !CV_IS_MASK_ARR( mask ))
        CV_ERROR( CV_StsBadMask, "" );

    if( CV_MAT_CN( mhi->type ) != 1 || CV_MAT_CN( orient->type ) != 1 )
        CV_ERROR( CV_BadNumChannels, "" );

    if( CV_MAT_DEPTH( mhi->type ) != CV_32F ||
        CV_MAT_DEPTH( orient->type ) != CV_32F )
        CV_ERROR( CV_BadDepth, "" );

    if( !CV_ARE_SIZES_EQ( mhi, mask ) || !CV_ARE_SIZES_EQ( orient, mhi ))
        CV_ERROR( CV_StsUnmatchedSizes, "" );

    size = icvGetMatSize( mhi );

    mhi_step = mhi->step;
    mask_step = mask->step;
    orient_step = orient->step;

    if( CV_IS_MAT_CONT( mhi->type & mask->type & orient->type ))
    {
        size.width *= size.height;
        mhi_step = mask_step = orient_step = CV_STUB_STEP;
        size.height = 1;
    }

    IPPI_CALL( icvCalcGlobalOrientation32fC1R( orient->data.fl, orient_step,
                                               (uchar*)(mask->data.ptr), mask_step,
                                               mhi->data.fl, mhi_step, size,
                                               (float)curr_mhi_timestamp,
                                               (float)mhi_duration, &angle ));
    __END__;

    return angle;
}


CV_IMPL CvSeq*
cvSegmentMotion( const CvArr* mhiimg, CvArr* segmask, CvMemStorage* storage,
                 double timestamp, double seg_thresh )
{
    CvSeq* components = 0;
    CvMat* mask8u = 0;

    CV_FUNCNAME( "cvSegmentMotion" );

    __BEGIN__;

    CvMat  mhistub, *mhi = (CvMat*)mhiimg;
    CvMat  maskstub, *mask = (CvMat*)segmask;
    float  ts = (float)timestamp;
    float  comp_idx = 1;
    float  stub_val = FLT_MAX*0.1f;
    int x, y;

    if( !storage )
        CV_ERROR( CV_StsNullPtr, "NULL memory storage" );

    CV_CALL( mhi = cvGetMat( mhi, &mhistub ));
    CV_CALL( mask = cvGetMat( mask, &maskstub ));

    if( CV_MAT_TYPE( mhi->type ) != CV_32FC1 || CV_MAT_TYPE( mask->type ) != CV_32FC1 )
        CV_ERROR( CV_BadDepth, "Both MHI and the destination mask" );

    if( !CV_ARE_SIZES_EQ( mhi, mask ))
        CV_ERROR( CV_StsUnmatchedSizes, "" );

    CV_CALL( mask8u = cvCreateMat( mhi->rows + 2, mhi->cols + 2, CV_8UC1 ));
    cvZero( mask8u );
    cvZero( mask );
    CV_CALL( components = cvCreateSeq( CV_SEQ_KIND_GENERIC, sizeof(CvSeq),
                                       sizeof(CvConnectedComp), storage ));

    for( y = 0; y < mhi->rows; y++ )
    {
        int* mhi_row = (int*)(mhi->data.ptr + y*mhi->step);
        for( x = 0; x < mhi->cols; x++ )
        {
            if( mhi_row[x] == 0 )
                mhi_row[x] = (int&)stub_val;
        }
    }

    for( y = 0; y < mhi->rows; y++ )
    {
        int* mhi_row = (int*)(mhi->data.ptr + y*mhi->step);
        uchar* mask8u_row = mask8u->data.ptr + (y+1)*mask8u->step + 1;

        for( x = 0; x < mhi->cols; x++ )
        {
            if( mhi_row[x] == (int&)ts && mask8u_row[x] == 0 )
            {
                CvConnectedComp comp;
                int x1, y1;

                CV_CALL( cvFloodFill( mhi, cvPoint( x, y ), 0, seg_thresh, seg_thresh,
                                      &comp, CV_FLOODFILL_MASK_ONLY + 2*256 + 4, mask8u ));

                for( y1 = 0; y1 < comp.rect.height; y1++ )
                {
                    int* mask_row1 = (int*)(mask->data.ptr +
                                    (comp.rect.y + y1)*mask->step) + comp.rect.x;
                    uchar* mask8u_row1 = mask8u->data.ptr +
                                    (comp.rect.y + y1+1)*mask8u->step + comp.rect.x+1;

                    for( x1 = 0; x1 < comp.rect.width; x1++ )
                    {
                        if( mask8u_row1[x1] > 1 )
                        {
                            mask8u_row1[x1] = 1;
                            mask_row1[x1] = (int&)comp_idx;
                        }
                    }
                }
                comp_idx++;
                cvSeqPush( components, &comp );
            }
        }
    }

    for( y = 0; y < mhi->rows; y++ )
    {
        int* mhi_row = (int*)(mhi->data.ptr + y*mhi->step);
        for( x = 0; x < mhi->cols; x++ )
        {
            if( mhi_row[x] == (int&)stub_val )
                mhi_row[x] = 0;
        }
    }

    __END__;

    cvReleaseMat( &mask8u );

    return components;
}

/* End of file. */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?