cvderiv.cpp.svn-base

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

SVN-BASE
1,921
字号

                        for( i = 1; i <= ker_y; i++ )
                        {
                            float *trow1, *trow2;
                            float m = fmaskY[i];
                            trow1 = rows[ker_y - i];
                            trow2 = rows[ker_y + i];
                            val0 += (trow2[x] - trow1[x])*m;
                            val1 += (trow2[x+1] - trow1[x+1])*m;
                            val2 += (trow2[x+2] - trow1[x+2])*m;
                            val3 += (trow2[x+3] - trow1[x+3])*m;
                        }

                        tdst2[x + 0] = val0;
                        tdst2[x + 1] = val1;
                        tdst2[x + 2] = val2;
                        tdst2[x + 3] = val3;
                    }
                }
            }
            else
            {
                if( y_type == ICV_1_2_1_KERNEL )
                {
                    float *trow1 = rows[0], *trow2 = rows[2];

                    for( x = 0; x < width; x += CV_MORPH_ALIGN )
                    {
                        float val0, val1;
                        val0 = trow[x]*2 + trow1[x] + trow2[x];
                        val1 = trow[x + 1]*2 + trow1[x+1] + trow2[x+1];
                
                        tdst2[x + 0] = (float)val0;
                        tdst2[x + 1] = (float)val1;
                
                        val0 = trow[x + 2]*2 + trow1[x+2] + trow2[x+2];
                        val1 = trow[x + 3]*2 + trow1[x+3] + trow2[x+3];
                
                        tdst2[x + 2] = (float)val0;
                        tdst2[x + 3] = (float)val1;
                    }
                }
                else if( y_type == ICV_3_10_3_KERNEL )
                {
                    float *trow1 = rows[0], *trow2 = rows[2];

                    for( x = 0; x < width; x += CV_MORPH_ALIGN )
                    {
                        float val0, val1;
                        val0 = trow[x]*10 + (trow1[x] + trow2[x])*3;
                        val1 = trow[x + 1]*10 + (trow1[x+1] + trow2[x+1])*3;
                
                        tdst2[x + 0] = (float)val0;
                        tdst2[x + 1] = (float)val1;
                
                        val0 = trow[x + 2]*10 + (trow1[x+2] + trow2[x+2])*3;
                        val1 = trow[x + 3]*10 + (trow1[x+3] + trow2[x+3])*3;
                
                        tdst2[x + 2] = (float)val0;
                        tdst2[x + 3] = (float)val1;
                    }
                }
                else
                {
                    for( x = 0; x < width; x += CV_MORPH_ALIGN )
                    {
                        float val0, val1, val2, val3;

                        val0 = trow[x]*fmY0;
                        val1 = trow[x + 1]*fmY0;
                        val2 = trow[x + 2]*fmY0;
                        val3 = trow[x + 3]*fmY0;

                        for( i = 1; i <= ker_y; i++ )
                        {
                            float *trow1, *trow2;
                            float m = fmaskY[i];
                            trow1 = rows[ker_y - i];
                            trow2 = rows[ker_y + i];
                            val0 += (trow2[x] + trow1[x])*m;
                            val1 += (trow2[x+1] + trow1[x+1])*m;
                            val2 += (trow2[x+2] + trow1[x+2])*m;
                            val3 += (trow2[x+3] + trow1[x+3])*m;
                        }

                        tdst2[x + 0] = val0;
                        tdst2[x + 1] = val1;
                        tdst2[x + 2] = val2;
                        tdst2[x + 3] = val3;
                    }
                }
            }

            if( width_rest )
            {
                if( need_copy )
                    CV_COPY( dst, tbufw, width, x );
                else
                    CV_COPY( dst + width, tbufw + width, CV_MORPH_ALIGN, x );
            }
        }
        else
        {
            for( x = 0; x < width; x++ )
                dst[x] = (float)trow[x];
        }

        rows[ker_y] = saved_row;

        /* rotate buffer */
        {
            float *t = rows[0];

            CV_COPY( rows, rows + 1, ker_height - 1, i );
            rows[i] = t;
            crows--;
            dst += dstStep;
        }
    }
    while( ++y < dst_height );

    roiSize->height = y;
    state->crows = crows;

    return CV_OK;
}


/****************************************************************************************\
*                                      S C H A R R                                       *
\****************************************************************************************/

IPCVAPI_IMPL( CvStatus, icvScharrInitAlloc,(
            int roiwidth, int datatype, int origin,
            int dx, int dy, CvFilterState** state ))
{
    return icvSobelInitAlloc( roiwidth, datatype, CV_SCHARR, origin, dx, dy, state );
}

IPCVAPI_IMPL( CvStatus, icvScharr_8u16s_C1R,(
             const uchar* pSrc, int srcStep,
             short* dst, int dstStep, CvSize* roiSize,
             CvFilterState* state, int stage ))
{
    assert( state->kerType == ICV_MAKE_SEPARABLE_KERNEL( ICV_m1_0_1_KERNEL, ICV_3_10_3_KERNEL ) ||
            state->kerType == ICV_MAKE_SEPARABLE_KERNEL( ICV_3_10_3_KERNEL, ICV_m1_0_1_KERNEL ));
    return icvSobel_8u16s_C1R( pSrc, srcStep, dst, dstStep, roiSize, state, stage );
}


IPCVAPI_IMPL( CvStatus, icvScharr_8s16s_C1R,(
             const char* pSrc, int srcStep,
             short* dst, int dstStep, CvSize* roiSize,
             CvFilterState* state, int stage ))
{
    assert( state->kerType == ICV_MAKE_SEPARABLE_KERNEL( ICV_m1_0_1_KERNEL, ICV_3_10_3_KERNEL ) ||
            state->kerType == ICV_MAKE_SEPARABLE_KERNEL( ICV_3_10_3_KERNEL, ICV_m1_0_1_KERNEL ));
    return icvSobel_8s16s_C1R( pSrc, srcStep, dst, dstStep, roiSize, state, stage );
}


IPCVAPI_IMPL( CvStatus, icvScharr_32f_C1R,(
             const float* pSrc, int srcStep,
             float* dst, int dstStep, CvSize* roiSize,
             CvFilterState* state, int stage ))
{
    assert( state->kerType == ICV_MAKE_SEPARABLE_KERNEL( ICV_m1_0_1_KERNEL, ICV_3_10_3_KERNEL ) ||
            state->kerType == ICV_MAKE_SEPARABLE_KERNEL( ICV_3_10_3_KERNEL, ICV_m1_0_1_KERNEL ));
    return icvSobel_32f_C1R( pSrc, srcStep, dst, dstStep, roiSize, state, stage );
}

/****************************************************************************************\
*                                      L A P L A C E                                     *
\****************************************************************************************/

IPCVAPI_IMPL( CvStatus, icvLaplaceInitAlloc,(
                int roiwidth, int datatype,
                int size, CvFilterState** state ))
{
    #define MAX_KERNEL_SIZE  7
    int ker[MAX_KERNEL_SIZE*2+1];
    CvDataType worktype = datatype != cv32f ? cv32s : cv32f;
    CvStatus status;
    int x_filter_type, y_filter_type;
    int x_size = size;
    
    if( !state )
        return CV_NULLPTR_ERR;

    if( (size&1) == 0 || size < 1 || size > MAX_KERNEL_SIZE )
        return CV_BADRANGE_ERR;

    if( size == 1 )
        x_size = 3;

    x_filter_type = icvCalcKer( (char*)ker, 2, x_size, worktype, 0 );
    y_filter_type = icvCalcKer( (char*)(ker + x_size), 0, size, worktype, 0 );

    status = icvFilterInitAlloc( roiwidth, worktype, 2, cvSize( x_size, x_size ),
                                 cvPoint( x_size/2, x_size/2 ), ker,
                                 ICV_MAKE_SEPARABLE_KERNEL(x_filter_type, y_filter_type),
                                 state );
    if( status < 0 )
        return status;

    (*state)->origin = 0;

    return CV_OK;
}


IPCVAPI_IMPL( CvStatus, icvLaplace_8u16s_C1R,(
             const uchar* pSrc, int srcStep,
             short* dst, int dstStep, CvSize* roiSize,
             CvFilterState* state, int stage ))
{
    uchar* src = (uchar*)pSrc;
    int width = roiSize->width;
    int src_height = roiSize->height;
    int dst_height = src_height;
    int x, y = 0, i;

    int ker_width = state->ker_width;
    int ker_height = state->ker_height;
    int ker_x = ker_width/2;
    int ker_y = ker_height/2;
    int ker_right = ker_width - ker_x;

    int crows = state->crows;
    int **rows = (int**)(state->rows);
    short *tbufw = (short*)(state->tbuf);
    int *trow = 0;

    int* fmaskX = (int*)(state->ker0) + ker_x;
    int* fmaskY = (int*)(state->ker1) + ker_y;
    int fmX0 = fmaskX[0], fmY0 = fmaskY[0];

    int is_small_width = width < MAX( ker_x, ker_right );
    int starting_flag = 0;
    int width_rest = width & (CV_MORPH_ALIGN - 1);
    int y_type = ICV_Y_KERNEL_TYPE(state->kerType);

    /* initialize cyclic buffer when starting */
    if( stage == CV_WHOLE || stage == CV_START )
    {
        for( i = 0; i < ker_height; i++ )
            rows[i] = (int*)(state->buffer + state->buffer_step * i);

        crows = ker_y;
        if( stage != CV_WHOLE )
            dst_height -= ker_height - ker_y - 1;
        starting_flag = 1;
    }

    if( stage == CV_END )
        dst_height += ker_height - ker_y - 1;

    dstStep /= sizeof(dst[0]);

    do
    {
        int need_copy = is_small_width | (y == 0);
        uchar *tsrc;
        int   *tdst;
        short *tdst2;
        int   *saved_row = rows[ker_y];

        /* fill cyclic buffer - horizontal filtering */
        for( ; crows < ker_height; crows++ )
        {
            tsrc = src - ker_x;
            tdst = rows[crows];

            if( src_height-- <= 0 )
            {
                if( stage != CV_WHOLE && stage != CV_END )
                    break;
                /* duplicate last row */
                trow = rows[crows - 1];
                CV_COPY( tdst, trow, width*2, x );
                continue;
            }

            need_copy |= src_height == 1;

            {
                uchar* tbufc = (uchar*)tbufw;

                if( need_copy )
                {
                    tsrc = tbufc - ker_x;
                    CV_COPY( tbufc, src, width, x );
                }
                else
                {
                    CV_COPY( tbufc - ker_x, src - ker_x, ker_x, x );
                    CV_COPY( tbufc, src + width, ker_right, x );
                }

                /* make replication borders */
                {
                uchar pix = tsrc[ker_x];
                CV_SET( tsrc, pix, ker_x, x );

                pix = tsrc[width + ker_x - 1];
                CV_SET( tsrc + width + ker_x, pix, ker_right, x );
                }

                if( ker_width == 3 )
                {
                    if( y_type == ICV_1_2_1_KERNEL )
                    {
                        for( i = 0; i < width; i++ )
                        {
                            int t0 = tsrc[i] + tsrc[i+2] - tsrc[i+1]*2;
                            int t1 = tsrc[i] + tsrc[i+2] + tsrc[i+1]*2;
                            tdst[i] = t0;
                            tdst[i+width] = t1;
                        }
                    }
                    else
                    {
                        for( i = 0; i < width; i++ )
                        {
                            int t0 = tsrc[i] + tsrc[i+2] - tsrc[i+1]*2;
                            int t1 = tsrc[i+1];
                            tdst[i] = t0;
                            tdst[i+width] = t1;
                        }
                    }
                }
                else if( ker_width == 5 )
                {
                    for( i = 0; i < width; i++ )
                    {
                        int t0 = tsrc[i] + tsrc[i+4] - tsrc[i+2]*2;
                        int t1 = tsrc[i] + tsrc[i+4] + tsrc[i+2]*6 +
                                 (tsrc[i+1] + tsrc[i+3])*4;
                        tdst[i] = t0;
                        tdst[i+width] = t1;
                    }
                }
                else
                {
                    for( i = 0; i < width; i++ )
                    {
                        int j;
                        int t0 = tsrc[i + ker_x]*fmX0;
                        int t1 = tsrc[i + ker_x]*fmY0;

                        for( j = 1; j <= ker_x; j++ )
                        {
                            t0 += (tsrc[i+ker_x+j] + tsrc[i+ker_x-j])*fmaskX[j];
                            t1 += (tsrc[i+ker_x+j] + tsrc[i+ker_x-j])*fmaskY[j];
                        }

                        tdst[i] = t0;
                        tdst[i+width] = t1;
                    }
                }

                if( !need_copy )
                {
                    /* restore borders */
                    CV_COPY( src - ker_x, tbufc - ker_x, ker_x, x );
                    CV_COPY( src + width, tbufc, ker_right, x );
                }
            }

            if( crows < ker_height )
                src += srcStep;
        }

        if( starting_flag )
        {
            starting_flag = 0;
            trow = rows[ker_y];

            for( i = 0; i < ker_y; i++ )
            {
                tdst = rows[i];
                CV_COPY( tdst, trow, width*2, x );
            }
        }

⌨️ 快捷键说明

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