⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cvderiv.cpp

📁 opencv库在TI DM6437上的移植,目前包括两个库cv.lib和cxcore.lib的工程
💻 CPP
📖 第 1 页 / 共 3 页
字号:
            CvSize size = cvGetMatSize( src );
            uchar* src_ptr = src->data.ptr;
            uchar* dst_ptr = dst->data.ptr;
            int src_step = src->step ? src->step : CV_STUB_STEP;
            int dst_step = dst->step ? dst->step : CV_STUB_STEP;
            const int bordertype = 1; // replication border
            CvStatus status;

            status = ipp_sobel_getbufsize_func ?
                ipp_sobel_getbufsize_func( size, masksize, &bufsize ) :
                ipp_scharr_getbufsize_func( size, &bufsize );

            if( status >= 0 )
            {
                if( bufsize <= CV_MAX_LOCAL_SIZE )
                {
                    buffer = cvStackAlloc( bufsize );
                    local_alloc = 1;
                }
                else
                    CV_CALL( buffer = cvAlloc( bufsize ));
            
                status =
                    ipp_sobel_func_8u ? ipp_sobel_func_8u( src_ptr, src_step, dst_ptr, dst_step,
                                                           size, masksize, bordertype, 0, buffer ) :
                    ipp_sobel_func_32f ? ipp_sobel_func_32f( src_ptr, src_step, dst_ptr, dst_step,
                                                             size, masksize, bordertype, 0, buffer ) :
                    ipp_scharr_func_8u ? ipp_scharr_func_8u( src_ptr, src_step, dst_ptr, dst_step,
                                                             size, bordertype, 0, buffer ) :
                    ipp_scharr_func_32f ? ipp_scharr_func_32f( src_ptr, src_step, dst_ptr, dst_step,
                                                               size, bordertype, 0, buffer ) : 
                        CV_NOTDEFINED_ERR;
            }

            if( status >= 0 &&
                (dx == 0 && dy == 1 && origin || dx == 1 && dy == 1 && !origin)) // negate the output
                cvSubRS( dst, cvScalarAll(0), dst );

            if( status >= 0 )
                EXIT;
        }
    }

    CV_CALL( filter.init_deriv( src->cols, src_type, dst_type, dx, dy,
                aperture_size, origin ? CvSepFilter::FLIP_KERNEL : 0));
    CV_CALL( filter.process( src, dst ));

    __END__;

    if( buffer && !local_alloc )
        cvFree( &buffer );
}


/****************************************************************************************\
                                     Laplacian Filter
\****************************************************************************************/

static void icvLaplaceRow_8u32s( const uchar* src, int* dst, void* params );
static void icvLaplaceRow_8u32f( const uchar* src, float* dst, void* params );
static void icvLaplaceRow_32f( const float* src, float* dst, void* params );
static void icvLaplaceCol_32s16s( const int** src, short* dst, int dst_step,
                                  int count, void* params );
static void icvLaplaceCol_32f( const float** src, float* dst, int dst_step,
                               int count, void* params );

CvLaplaceFilter::CvLaplaceFilter()
{
    normalized = basic_laplacian = false;
}


CvLaplaceFilter::CvLaplaceFilter( int _max_width, int _src_type, int _dst_type, bool _normalized,
                                  int _ksize, int _border_mode, CvScalar _border_value )
{
    normalized = basic_laplacian = false;
    init( _max_width, _src_type, _dst_type, _normalized, _ksize, _border_mode, _border_value );
}


CvLaplaceFilter::~CvLaplaceFilter()
{
    clear();
}


void CvLaplaceFilter::get_work_params()
{
    int min_rows = max_ky*2 + 3, rows = MAX(min_rows,10), row_sz;
    int width = max_width, trow_sz = 0;
    int dst_depth = CV_MAT_DEPTH(dst_type);
    int work_depth = dst_depth < CV_32F ? CV_32S : CV_32F;
    work_type = CV_MAKETYPE( work_depth, CV_MAT_CN(dst_type)*2 );
    trow_sz = cvAlign( (max_width + ksize.width - 1)*CV_ELEM_SIZE(src_type), ALIGN );
    row_sz = cvAlign( width*CV_ELEM_SIZE(work_type), ALIGN );
    buf_size = rows*row_sz;
    buf_size = MIN( buf_size, 1 << 16 );
    buf_size = MAX( buf_size, min_rows*row_sz );
    max_rows = (buf_size/row_sz)*3 + max_ky*2 + 8;
    buf_size += trow_sz;
}


void CvLaplaceFilter::init( int _max_width, int _src_type, int _dst_type, bool _normalized,
                            int _ksize0, int _border_mode, CvScalar _border_value )
{
    CvMat *kx = 0, *ky = 0;

    CV_FUNCNAME( "CvLaplaceFilter::init" );

    __BEGIN__;

    int src_depth = CV_MAT_DEPTH(_src_type), dst_depth = CV_MAT_DEPTH(_dst_type);
    int _ksize = MAX( _ksize0, 3 );

    normalized = _normalized;
    basic_laplacian = _ksize0 == 1;

    if( (src_depth != CV_8U || dst_depth != CV_16S && dst_depth != CV_32F) &&
        (src_depth != CV_32F || dst_depth != CV_32F) ||
        CV_MAT_CN(_src_type) != CV_MAT_CN(_dst_type) )
        CV_ERROR( CV_StsUnmatchedFormats,
        "Laplacian can either transform 8u->16s, or 8u->32f, or 32f->32f.\n"
        "The channel number must be the same." );

    if( _ksize < 1 || _ksize > CV_MAX_SOBEL_KSIZE || _ksize % 2 == 0 )
        CV_ERROR( CV_StsOutOfRange, "kernel size must be within 1..7 and odd" );

    CV_CALL( kx = cvCreateMat( 1, _ksize, CV_32F ));
    CV_CALL( ky = cvCreateMat( 1, _ksize, CV_32F ));

    CvSepFilter::init_sobel_kernel( kx, ky, 2, 0, 0 );
    CvSepFilter::init( _max_width, _src_type, _dst_type, kx, ky,
                       cvPoint(-1,-1), _border_mode, _border_value );

    x_func = 0;
    y_func = 0;

    if( src_depth == CV_8U )
    {
        if( dst_depth == CV_16S )
        {
            x_func = (CvRowFilterFunc)icvLaplaceRow_8u32s;
            y_func = (CvColumnFilterFunc)icvLaplaceCol_32s16s;
        }
        else if( dst_depth == CV_32F )
        {
            x_func = (CvRowFilterFunc)icvLaplaceRow_8u32f;
            y_func = (CvColumnFilterFunc)icvLaplaceCol_32f;
        }
    }
    else if( src_depth == CV_32F )
    {
        if( dst_depth == CV_32F )
        {
            x_func = (CvRowFilterFunc)icvLaplaceRow_32f;
            y_func = (CvColumnFilterFunc)icvLaplaceCol_32f;
        }
    }

    if( !x_func || !y_func )
        CV_ERROR( CV_StsUnsupportedFormat, "" );

    __END__;

    cvReleaseMat( &kx );
    cvReleaseMat( &ky );
}


void CvLaplaceFilter::init( int _max_width, int _src_type, int _dst_type,
                            bool _is_separable, CvSize _ksize,
                            CvPoint _anchor, int _border_mode,
                            CvScalar _border_value )
{
    CvSepFilter::init( _max_width, _src_type, _dst_type, _is_separable,
                       _ksize, _anchor, _border_mode, _border_value );
}


void CvLaplaceFilter::init( int _max_width, int _src_type, int _dst_type,
                            const CvMat* _kx, const CvMat* _ky,
                            CvPoint _anchor, int _border_mode,
                            CvScalar _border_value )
{
    CvSepFilter::init( _max_width, _src_type, _dst_type, _kx, _ky,
                       _anchor, _border_mode, _border_value );
}


#define ICV_LAPLACE_ROW( flavor, srctype, dsttype, load_macro )         \
static void                                                             \
icvLaplaceRow_##flavor( const srctype* src, dsttype* dst, void* params )\
{                                                                       \
    const CvLaplaceFilter* state = (const CvLaplaceFilter*)params;      \
    const CvMat* _kx = state->get_x_kernel();                           \
    const CvMat* _ky = state->get_y_kernel();                           \
    const dsttype* kx = (dsttype*)_kx->data.ptr;                        \
    const dsttype* ky = (dsttype*)_ky->data.ptr;                        \
    int ksize = _kx->cols + _kx->rows - 1;                              \
    int i = 0, j, k, width = state->get_width();                        \
    int cn = CV_MAT_CN(state->get_src_type());                          \
    int ksize2 = ksize/2, ksize2n = ksize2*cn;                          \
    const srctype* s = src + ksize2n;                                   \
    bool basic_laplacian = state->is_basic_laplacian();                 \
                                                                        \
    kx += ksize2;                                                       \
    ky += ksize2;                                                       \
    width *= cn;                                                        \
                                                                        \
    if( basic_laplacian )                                               \
        for( i = 0; i < width; i++ )                                    \
        {                                                               \
            dsttype s0 = load_macro(s[i]);                              \
            dsttype s1 = (dsttype)(s[i-cn] - s0*2 + s[i+cn]);           \
            dst[i] = s0; dst[i+width] = s1;                             \
        }                                                               \
    else if( ksize == 3 )                                               \
        for( i = 0; i < width; i++ )                                    \
        {                                                               \
            dsttype s0 = (dsttype)(s[i-cn] + s[i]*2 + s[i+cn]);         \
            dsttype s1 = (dsttype)(s[i-cn] - s[i]*2 + s[i+cn]);         \
            dst[i] = s0; dst[i+width] = s1;                             \
        }                                                               \
    else if( ksize == 5 )                                               \
        for( i = 0; i < width; i++ )                                    \
        {                                                               \
            dsttype s0 = (dsttype)(s[i-2*cn]+(s[i-cn]+s[i+cn])*4+s[i]*6+s[i+2*cn]);\
            dsttype s1 = (dsttype)(s[i-2*cn]-s[i]*2+s[i+2*cn]);         \
            dst[i] = s0; dst[i+width] = s1;                             \
        }                                                               \
    else                                                                \
        for( i = 0; i < width; i++, s++ )                               \
        {                                                               \
            dsttype s0 = ky[0]*load_macro(s[0]), s1 = kx[0]*load_macro(s[0]);\
            for( k = 1, j = cn; k <= ksize2; k++, j += cn )             \
            {                                                           \
                dsttype t = load_macro(s[j] + s[-j]);                   \
                s0 += ky[k]*t; s1 += kx[k]*t;                           \
            }                                                           \
            dst[i] = s0; dst[i+width] = s1;                             \
        }                                                               \
}

ICV_LAPLACE_ROW( 8u32s, uchar, int, CV_NOP )
ICV_LAPLACE_ROW( 8u32f, uchar, float, CV_8TO32F )
ICV_LAPLACE_ROW( 32f, float, float, CV_NOP )

static void
icvLaplaceCol_32s16s( const int** src, short* dst,
                      int dst_step, int count, void* params )
{
    const CvLaplaceFilter* state = (const CvLaplaceFilter*)params;
    const CvMat* _kx = state->get_x_kernel();
    const CvMat* _ky = state->get_y_kernel();
    const int* kx = (const int*)_kx->data.ptr;
    const int* ky = (const int*)_ky->data.ptr;
    int ksize = _kx->cols + _kx->rows - 1, ksize2 = ksize/2;
    int i = 0, k, width = state->get_width();
    int cn = CV_MAT_CN(state->get_src_type());
    bool basic_laplacian = state->is_basic_laplacian();
    bool normalized = state->is_normalized();
    int shift = ksize - 1, delta = (1 << shift) >> 1;
    
    width *= cn;
    src += ksize2;
    kx += ksize2;
    ky += ksize2;
    dst_step /= sizeof(dst[0]);

    if( basic_laplacian || !normalized )
    {
        normalized = false;
        shift = delta = 0;
    }

    for( ; count--; dst += dst_step, src++ )
    {
        if( ksize == 3 )
        {
            const int *src0 = src[-1], *src1 = src[0], *src2 = src[1];
            if( basic_laplacian )
            {
                for( i = 0; i <= width - 2; i += 2 )
                {
                    int s0 = src0[i] - src1[i]*2 + src2[i] + src1[i+width];
                    int s1 = src0[i+1] - src1[i+1]*2 + src2[i+1] + src1[i+width+1];
                    dst[i] = (short)s0; dst[i+1] = (short)s1;
                }

                for( ; i < width; i++ )
                    dst[i] = (short)(src0[i] - src1[i]*2 + src2[i] + src1[i+width]);
            }
            else if( !normalized )

⌨️ 快捷键说明

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