cvthresh.cpp.svn-base

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

SVN-BASE
451
字号
    return CV_NO_ERR;
}


/*F///////////////////////////////////////////////////////////////////////////////////////
//    Name:    icvThreshold32fC1R
//    Purpose: Thresholding the source array
//    Context:
//    Parameters:
//      Src     - source array
//      roi     - size of picture in elements
//      srcStep - length of string
//      Thresh  - threshold parameter
//      Type    - thresholding type, must be one of
//                  CV_THRESH_BINARY       - val = (val > Thresh ? MAX    : 0)
//                  CV_THRESH_BINARY_INV   - val = (val > Thresh ? 0      : MAX)
//                  CV_THRESH_TRUNC        - val = (val > Thresh ? Thresh : val)
//                  CV_THRESH_TOZERO       - val = (val > Thresh ? val    : 0)
//                  CV_THRESH_TOZERO_INV   - val = (val > Thresh ? 0      : val)
//    Returns:
//    Notes:
//      The MAX constant for uchar is 255, for char is 127
//F*/
IPCVAPI_IMPL( CvStatus, icvThresh_32f_C1R, (const float *src,
                                            int src_step,
                                            float *dst,
                                            int dst_step,
                                            CvSize roi,
                                            float thresh, float maxval, CvThreshType type) )
{
    /* Some variables */
    int i, j;
    int iThresh = CV_TOGGLE_FLT( (int &) thresh );
    int iMax = (int &) maxval;
    int *isrc = (int *) src;
    int *idst = (int *) dst;

    assert( sizeof( int ) == sizeof( float ));

    /* Check for bad arguments */
    if( !src || !dst )
        return CV_NULLPTR_ERR;
    if( roi.width < 0 || roi.height < 0 )
        return CV_BADSIZE_ERR;
    if( roi.width * sizeof_float > src_step )
        return CV_BADSIZE_ERR;
    if( roi.width * sizeof_float > dst_step )
        return CV_BADSIZE_ERR;
    if( (src_step & (sizeof_float - 1)) != 0 || (dst_step & (sizeof_float - 1)) != 0 )
        return CV_BADSIZE_ERR;

    src_step /= sizeof_float;
    dst_step /= sizeof_float;

    if( roi.width == src_step && roi.width == dst_step )
    {
        roi.width *= roi.height;
        roi.height = 1;
    }

    /* Calculating */
    switch (type)
    {
    case CV_THRESH_BINARY:
        for( i = 0; i < roi.height; i++, isrc += src_step, idst += dst_step )
        {
            for( j = 0; j < roi.width; j++ )
            {
                int temp = isrc[j];

                idst[j] = ((CV_TOGGLE_FLT( temp ) <= iThresh) - 1) & iMax;
            }
        }
        break;

    case CV_THRESH_BINARY_INV:
        for( i = 0; i < roi.height; i++, isrc += src_step, idst += dst_step )
        {
            for( j = 0; j < roi.width; j++ )
            {
                int temp = isrc[j];

                idst[j] = CV_TOGGLE_FLT( temp ) > iThresh ? 0 : iMax;
            }
        }
        break;

    case CV_THRESH_TRUNC:
        for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
        {
            for( j = 0; j < roi.width; j++ )
            {
                float temp = src[j];

                if( temp > thresh )
                    temp = thresh;
                dst[j] = temp;
            }
        }
        break;

    case CV_THRESH_TOZERO:
        for( i = 0; i < roi.height; i++, isrc += src_step, idst += dst_step )
        {
            for( j = 0; j < roi.width; j++ )
            {
                int temp = isrc[j];

                idst[j] = ((CV_TOGGLE_FLT( temp ) <= iThresh) - 1) & temp;
            }
        }
        break;

    case CV_THRESH_TOZERO_INV:
        for( i = 0; i < roi.height; i++, isrc += src_step, idst += dst_step )
        {
            for( j = 0; j < roi.width; j++ )
            {
                int temp = isrc[j];

                idst[j] = CV_TOGGLE_FLT( temp ) > iThresh ? 0 : temp;
            }
        }
        break;

    default:
        return CV_BADFLAG_ERR;
    }

    return CV_OK;
}


CV_IMPL void
cvThreshold( const void* srcarr, void* dstarr, double thresh, double maxval, CvThreshType type )
{
    CV_FUNCNAME( "cvThreshold" );

    __BEGIN__;

    CvSize roi;
    int src_step, dst_step;
    CvMat src_stub, *src = (CvMat*)srcarr;
    CvMat dst_stub, *dst = (CvMat*)dstarr;
    int coi1 = 0, coi2 = 0;
    int ival = 0;

    CV_CALL( src = cvGetMat( src, &src_stub, &coi1 ));
    CV_CALL( dst = cvGetMat( dst, &dst_stub, &coi2 ));

    if( coi1 + coi2 )
        CV_ERROR( CV_BadCOI, "COI is not supported by the function" );

    if( !CV_ARE_CNS_EQ( src, dst ) )
        CV_ERROR( CV_StsUnmatchedFormats, "Both arrays must have equal number of channels" );

    if( !CV_ARE_DEPTHS_EQ( src, dst ) )
    {
        if( CV_MAT_TYPE(dst->type) != CV_8UC1 )
            CV_ERROR( CV_StsUnsupportedFormat, "In case of different types destination should be 8uC1" );

        if( type != CV_THRESH_BINARY && type != CV_THRESH_BINARY_INV )
            CV_ERROR( CV_StsBadArg,
            "In case of different types only CV_THRESH_BINARY "
            "and CV_THRESH_BINARY_INV thresholding types are supported" );

        if( maxval < 0 )
        {
            CV_CALL( cvSetZero( dst ));
        }
        else
        {
            CV_CALL( cvCmpS( src, thresh, dst, type == CV_THRESH_BINARY ? CV_CMP_GT : CV_CMP_LE ));
            if( maxval < 255 )
                CV_CALL( cvAndS( dst, cvScalarAll( maxval ), dst ));
        }
        EXIT;
    }

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

    roi = icvGetMatSize( src );
    roi.width *= CV_MAT_CN(src->type);
    if( CV_IS_MAT_CONT( src->type & dst->type ))
    {
        roi.width *= roi.height;
        roi.height = 1;
        src_step = dst_step = CV_STUB_STEP;
    }
    else
    {
        src_step = src->step;
        dst_step = dst->step;
    }

    switch( CV_MAT_DEPTH(src->type) )
    {
    case CV_8U:
        ival = cvRound(maxval);
        IPPI_CALL( icvThresh_8u_C1R( (uchar*)src->data.ptr, src_step,
                                     (uchar*)dst->data.ptr, dst_step, roi,
                                     cvRound(thresh), CV_CAST_8U(ival), type ));
        break;
    case CV_8S:
        ival = cvRound(maxval);
        IPPI_CALL( icvThresh_8s_C1R( (char*)src->data.ptr, src_step,
                                     (char*)dst->data.ptr, dst_step, roi,
                                     cvRound(thresh), CV_CAST_8S(ival), type ));
        break;
    case CV_32F:
        IPPI_CALL( icvThresh_32f_C1R( src->data.fl, src_step,
                                      dst->data.fl, dst_step, roi,
                                      (float)thresh, (float)maxval, type ));
        break;
    default:
        CV_ERROR( CV_BadDepth, icvUnsupportedFormat );
    }

    __END__;
}


/* End of file. */

⌨️ 快捷键说明

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