cvdxt.cpp.svn-base

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

SVN-BASE
1,339
字号
                if( cols % 2 == 0 )
                {
                    data[cols - 1] *= re_scale;
                    if( rows % 2 == 0 )
                        data[(rows-1)*step + cols-1] *= re_scale;

                    for( j = 1; j <= rows - 2; j += 2 )
                    {
                        data[j*step + cols-1] *= re_scale;
                        data[(j+1)*step + cols-1] *= im_scale;
                    }
                }

                data += 1;
                cols = (cols - 1) & -2;
            }

            for( ; rows--; data += step )
            {
                for( j = 0; j < cols; j += 2 )
                {
                    float t0 = data[j]*re_scale;
                    float t1 = data[j+1]*im_scale;
                    data[j] = t0;
                    data[j+1] = t1;
                }
            }
        }
    }
    else
    {
        double re_scale = _re_scale;
        double im_scale = _im_scale;
        double* data = (double*)mat->data.ptr;

        if( is_1d )
        {
            int len = cols + rows - 1;

            if( CV_MAT_CN(mat->type) == 1 )
            {
                data[0] *= re_scale;
                if( len % 2 == 0 )
                    data[len-1] *= re_scale;
                data += 1;
                len = (len - 1) & -2;
            }

            for( j = 0; j < len; j += 2 )
            {
                double t0 = data[j]*re_scale;
                double t1 = data[j+1]*im_scale;

                data[j] = t0;
                data[j+1] = t1;
            }
        }
        else
        {
            int step = mat->step/sizeof(double);

            if( CV_MAT_CN(mat->type) == 1 )
            {
                data[0] *= re_scale;
                if( rows % 2 == 0 )
                    data[(rows-1)*step] *= re_scale;

                for( j = 1; j <= rows - 2; j += 2 )
                {
                    data[j*step] *= re_scale;
                    data[(j+1)*step] *= im_scale;
                }

                if( cols % 2 == 0 )
                {
                    data[cols - 1] *= re_scale;
                    if( rows % 2 == 0 )
                        data[(rows-1)*step + cols-1] *= re_scale;

                    for( j = 1; j <= rows - 2; j += 2 )
                    {
                        data[j*step + cols-1] *= re_scale;
                        data[(j+1)*step + cols-1] *= im_scale;
                    }
                }

                data += 1;
                cols = (cols - 1) & -2;
            }

            for( ; rows--; data += step )
            {
                for( j = 0; j < cols; j += 2 )
                {
                    double t0 = data[j]*re_scale;
                    double t1 = data[j+1]*im_scale;
                    data[j] = t0;
                    data[j+1] = t1;
                }
            }
        }
    }

    __END__;
}

typedef CvStatus (*CvDXTFastFunc)( void* array, int log_len, int* bitrev_tab );
typedef CvStatus (*CvDXTSlowFunc)( const void* src, void* dst, int len, int inv );

static void icvCopyColumn( const CvMat* src, CvMat* dst, int pix_size )
{
    int i, rows = src->rows;
    int src_step = src->step, dst_step = dst->step;
    uchar *src_data = src->data.ptr, *dst_data = dst->data.ptr;

    for( i = 0; i < rows; i++ )
        memcpy( dst_data + i*dst_step, src_data + i*src_step, pix_size );
}


CV_IMPL void
cvDFT( const CvArr* srcarr, CvArr* dstarr, int flags )
{
    static CvDXTFastFunc fast_tbl[6];
    static CvDXTSlowFunc slow_tbl[6];
    static int inittab = 0;
    
    uchar* buffer = 0;
    int local_alloc = 0;

    int* itab = 0;
    int local_itab_alloc = 0;
    
    CV_FUNCNAME( "cvDFT" );

    __BEGIN__;

    double re_scale = 1, im_scale = 1;
    CvMat srcstub, *src = (CvMat*)srcarr;
    CvMat dststub, *dst = (CvMat*)dstarr;
    CvMat buf, *dst_tmp;
    CvSize size;
    int is_1d;
    int type, pix_size;
    int width_flag, height_flag;
    int tbl_offset;
    int inv = (flags & CV_DXT_INVERSE) != 0;

    if( !inittab )
    {
        fast_tbl[0] = (CvDXTFastFunc)icvFFT_fwd_32fc;
        fast_tbl[1] = (CvDXTFastFunc)icvFFT_fwd_32f;
        fast_tbl[2] = (CvDXTFastFunc)icvFFT_inv_32f;

        fast_tbl[3] = (CvDXTFastFunc)icvFFT_fwd_64fc;
        fast_tbl[4] = (CvDXTFastFunc)icvFFT_fwd_64f;
        fast_tbl[5] = (CvDXTFastFunc)icvFFT_inv_64f;

        slow_tbl[0] = (CvDXTSlowFunc)icvDFT_32fc;
        slow_tbl[1] = (CvDXTSlowFunc)icvDFT_fwd_32f;
        slow_tbl[2] = (CvDXTSlowFunc)icvDFT_inv_32f;

        slow_tbl[3] = (CvDXTSlowFunc)icvDFT_64fc;
        slow_tbl[4] = (CvDXTSlowFunc)icvDFT_fwd_64f;
        slow_tbl[5] = (CvDXTSlowFunc)icvDFT_inv_64f;
        
        inittab = 1;
    }

    if( !CV_IS_MAT( src ))
    {
        int coi = 0;
        CV_CALL( src = cvGetMat( src, &srcstub, &coi ));

        if( coi != 0 )
            CV_ERROR( CV_BadCOI, "" );
    }

    if( !CV_IS_MAT( dst ))
    {
        int coi = 0;
        CV_CALL( dst = cvGetMat( dst, &dststub, &coi ));

        if( coi != 0 )
            CV_ERROR( CV_BadCOI, "" );
    }

    if( !CV_ARE_TYPES_EQ( src, dst ))
        CV_ERROR( CV_StsUnmatchedFormats, "" );

    if( !CV_ARE_SIZES_EQ( src, dst ))
        CV_ERROR( CV_StsUnmatchedSizes, "" );

    dst_tmp = dst;
    type = CV_MAT_TYPE( dst->type );

    if( CV_MAT_CN(type) > 2 || CV_MAT_DEPTH(type) < CV_32F )
        CV_ERROR( CV_StsUnsupportedFormat,
        "Only 32fC1, 32fC2, 64fC1 and 64fC2 formats are supported" );

    pix_size = icvPixSize[type];
    tbl_offset = (CV_MAT_DEPTH(type)==CV_64F ? 3 : 0);

    size = icvGetMatSize( dst );
    is_1d = size.width == 1 || size.height == 1;

    if( flags & CV_DXT_SCALE )
        re_scale = im_scale = 1./(size.width*size.height);

    width_flag = (size.width&(size.width-1)) == 0;
    height_flag = (size.height&(size.height-1)) == 0;

    if( !CV_IS_MAT_CONT(dst->type) || !is_1d || !width_flag || !height_flag )
    {
        int buffer_size = pix_size*MAX(size.width,size.height)*4;
        if( buffer_size <= CV_MAX_LOCAL_SIZE )
        {
            buffer = (uchar*)alloca( buffer_size );
            local_alloc = 1;
        }
        else
            CV_CALL( buffer = (uchar*)cvAlloc( buffer_size ));
        /* buf is initialized for 1D case, for 2D case it needs to be reinitialized */
        buf = cvMat( size.height, size.width, type, buffer );
        dst_tmp = &buf;
    }

    if( is_1d )
    {
        int n = size.width + size.height - 1;
        if( (n & (n-1)) == 0 )
        {
            int m = icvlog2(n);

            if( dst_tmp->data.ptr != src->data.ptr )
                cvCopy( src, dst_tmp, 0 );

            if( CV_MAT_CN(type) == 2 )
            {
                if( inv )
                {
                    icvConjugate( dst_tmp );
                    im_scale = -re_scale;
                }

                fast_tbl[0 + tbl_offset]( (CvPoint2D32f*)(dst_tmp->data.ptr), m, 0 );
            }
            else
                fast_tbl[inv + 1 + tbl_offset]((float*)(dst_tmp->data.ptr), m, 0);
        }
        else
        {
            CvMat buf2;
            CvMat* src_tmp = src;

            if( CV_IS_MAT_CONT(dst->type))
                dst_tmp = dst;
            if( !CV_IS_MAT_CONT(src->type) || src->data.ptr == dst_tmp->data.ptr )
            {
                buf2 = cvMat( size.height, size.width, type, buffer + n*pix_size );
                src_tmp = &buf2;
                cvCopy( src, src_tmp, 0 );
            }

            if( CV_MAT_CN(type) == 2 )
                slow_tbl[0 + tbl_offset]( (CvPoint2D32f*)(src_tmp->data.ptr),
                

⌨️ 快捷键说明

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