cvmat.cpp.svn-base

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

SVN-BASE
1,075
字号
                refcount = src->refcount;
            }
            assert( refcount );
            refcount[0]++;
        }
        else
            create( m, n, a->type );
    }

    if( mmul.alpha == 1 )
    {
        if( mmul.t_ab == 0 )
        {
            cvMatMulAdd( a, b, 0, this );
            return *this;
        }
        
        if( a->data.ptr == b->data.ptr && mmul.t_ab < 3 &&
            a->rows == b->rows && a->cols == b->cols &&
            a->data.ptr != data.ptr )
        {
            cvMulTransposed( a, this, mmul.t_ab & 1 );
            return *this;
        }
    }

    cvGEMM( a, b, mmul.alpha, 0, 0, this, mmul.t_ab );
    return *this;
}


CvMAT& CvMAT::operator = ( const _CvMAT_MUL_ADD_& mmuladd )
{
    CvMAT* a = mmuladd.a;
    CvMAT* b = mmuladd.b;
    CvMAT* c = mmuladd.c;
    int t_a = mmuladd.t_abc & 1;
    int t_b = (mmuladd.t_abc & 2) != 0;
    int m = (&(a->rows))[t_a];
    int l = (&(a->rows))[t_a ^ 1];
    int n = (&(b->rows))[t_b ^ 1];
    CvMAT* src = 0;
    /* this(m x n) = (a^o1(t))(m x l) * (b^o2(t))(l x n) */

    if( !data.ptr )
    {
        if( c->istemp() )
            src = c;
        else if( a->istemp() && l == n )
            src = a;
        else if( b->istemp() && l == m )
            src = b;

        if( src )
        {
            if( src->rows == m )
            {
                memcpy( this, src, sizeof(*this));
                type &= ~CV_MAT_TEMP_FLAG;
            }
            else
            {
                cvInitMatHeader( this, m, n, src->type, src->data.ptr );
                refcount = src->refcount;
            }
            assert( refcount );
            refcount[0]++;
        }
        else
            create( m, n, a->type );
    }

    if( mmuladd.t_abc == 0 && mmuladd.alpha == 1 && mmuladd.beta == 1 )
        cvMatMulAdd( a, b, c, this );
    else
        cvGEMM( a, b, mmuladd.alpha, c, mmuladd.beta, this, mmuladd.t_abc );
    return *this;
}


CvMAT& CvMAT::operator = ( const _CvMAT_INV_& inv_mat )
{
    CvMAT* src = (CvMAT*)&inv_mat.a;
    
    if( !data.ptr )
    {
        if( src->istemp() )
        {
            memcpy( this, src, sizeof(*this));
            type &= ~CV_MAT_TEMP_FLAG;
            assert( refcount );
            refcount[0]++;
        }
        else
            create( src->height, src->width, src->type );
    }

    if( inv_mat.method == 0 )
        cvInvert( src, this );
    else
        cvPseudoInv( src, this );
    return *this;
}


CvMAT& CvMAT::operator = ( const _CvMAT_NOT_& not_mat )
{
    CvMAT* src = not_mat.a;
    
    if( !data.ptr )
    {
        if( src->istemp() )
        {
            memcpy( this, src, sizeof(*this));
            type &= ~CV_MAT_TEMP_FLAG;
            assert( refcount );
            refcount[0]++;
        }
        else
            create( src->height, src->width, src->type );
    }

    cvNot( src, this );
    return *this;
}


CvMAT& CvMAT::operator = ( const _CvMAT_LOGIC_& mat_logic )
{
    CvMAT* a = mat_logic.a;
    CvMAT* b = mat_logic.b;
    int flags = mat_logic.flags;
    _CvMAT_LOGIC_::Op op = mat_logic.op;

    if( !data.ptr )
    {
        if( a->istemp() )
        {
            memcpy( this, a, sizeof(*this));
            refcount[0]++;
        }
        else if( b->istemp() )
        {
            memcpy( this, b, sizeof(*this));
            refcount[0]++;
        }
        else
            create( a->height, a->width, a->type );
        type &= ~CV_MAT_TEMP_FLAG;
    }

    switch( op )
    {
    case _CvMAT_LOGIC_::AND:

        if( flags == 0 )
            cvAnd( a, b, this );
        else if( flags == 3 )
        {
            cvOr( a, b, this );
            cvNot( this, this );
        }
        else if( flags == 1 )
        {
            if( data.ptr == b->data.ptr )
            {
                cvNot( b, this );
                cvOr( this, a, this );
                cvNot( this, this );
            }
            else
            {
                cvNot( a, this );
                cvAnd( this, b, this );
            }
        }
        else
        {
            if( data.ptr == a->data.ptr )
            {
                cvNot( a, this );
                cvOr( this, b, this );
                cvNot( this, this );
            }
            else
            {
                cvNot( b, this );
                cvAnd( this, a, this );
            }
        }
        break;

    case _CvMAT_LOGIC_::OR:

        if( flags == 0 )
            cvOr( a, b, this );
        else if( flags == 3 )
        {
            cvAnd( a, b, this );
            cvNot( this, this );
        }
        else if( flags == 1 )
        {
            if( data.ptr == b->data.ptr )
            {
                cvNot( b, this );
                cvAnd( this, a, this );
                cvNot( this, this );
            }
            else
            {
                cvNot( a, this );
                cvOr( this, b, this );
            }
        }
        else
        {
            if( data.ptr == a->data.ptr )
            {
                cvNot( a, this );
                cvAnd( this, b, this );
                cvNot( this, this );
            }
            else
            {
                cvNot( b, this );
                cvOr( this, a, this );
            }
        }
        break;

    case _CvMAT_LOGIC_::XOR:

        cvXor( a, b, this );
        if( flags == 1 || flags == 2 )
            cvNot( this, this );
        break;
    }

    return *this;
}


CvMAT& CvMAT::operator = ( const _CvMAT_UN_LOGIC_& mat_logic )
{
    CvMAT* a = mat_logic.a;
    CvScalar scalar = cvScalarAll( mat_logic.alpha );
    int flags = mat_logic.flags;
    _CvMAT_LOGIC_::Op op = mat_logic.op;

    if( !data.ptr )
    {
        if( a->istemp() )
        {
            memcpy( this, a, sizeof(*this));
            refcount[0]++;
        }
        else
            create( a->height, a->width, a->type );
        type &= ~CV_MAT_TEMP_FLAG;
    }

    switch( op )
    {
    case _CvMAT_LOGIC_::AND:

        if( flags == 0 )
            cvAndS( a, scalar, this );
        else
        {
            cvNot( a, this );
            cvAndS( this, scalar, this );
        }
        break;

    case _CvMAT_LOGIC_::OR:

        if( flags == 0 )
            cvOrS( a, scalar, this );
        else
        {
            cvNot( a, this );
            cvOrS( this, scalar, this );
        }
        break;

    case _CvMAT_LOGIC_::XOR:

        if( flags == 0 )
            cvXorS( a, scalar, this );
        else
            cvXorS( a, ~scalar, this );
        break;
    }

    return *this;
}


CvMAT& CvMAT::operator = ( const _CvMAT_COPY_& mat_copy )
{
    CvMAT* src = (CvMAT*)mat_copy.a;

    if( !data.ptr )
    {
        if( src->istemp() )
        {
            memcpy( this, src, sizeof(*this));
            type &= ~CV_MAT_TEMP_FLAG;
            assert( refcount );
            refcount[0]++;
            return *this;
        }
        else
            create( src->height, src->width, src->type );
    }

    if( src != this )
        cvCopy( src, this );

    return *this;
}


CvMAT& CvMAT::operator = ( const _CvMAT_CVT_& mat_cvt )
{
    CvMAT* src = (CvMAT*)&mat_cvt.a;
    
    if( !data.ptr )
    {
        int depth = mat_cvt.newdepth;
        
        if( src->istemp() && depth < 0 )
        {
            memcpy( this, src, sizeof(*this));
            type &= ~CV_MAT_TEMP_FLAG;
            assert( refcount );
            refcount[0]++;
        }
        else
            create( src->height, src->width, depth < 0 ? src->type :
                    CV_MAT_CN(src->type)|CV_MAT_DEPTH(depth));
    }

    cvCvtScale( src, this, mat_cvt.scale, mat_cvt.shift );
    return *this;
}


CvMAT& CvMAT::operator = ( const _CvMAT_DOT_OP_& dot_op )
{
    CvMAT* a = (CvMAT*)&(dot_op.a);
    CvMAT* b = dot_op.b;
    
    if( !data.ptr )
    {
        CvMAT* src = 0;

        if( a->istemp() )
            src = a;
        else if( b->istemp() )
            src = b;

        if( src )
        {
            memcpy( this, src, sizeof(*this));
            assert( refcount );
            refcount[0]++;
        }
        else
            create( a->height, a->width, a->type );
    }

    type &= ~CV_MAT_TEMP_FLAG;
    switch( dot_op.op )
    {
    case '*':
        cvMul( a, b, this, dot_op.alpha );
        break;
    case '/':
        if( b != 0 )
            cvDiv( a, b, this, dot_op.alpha );
        else
            cvDiv( 0, a, this, dot_op.alpha );
        break;
    case 'm':
        if( b != 0 )
            cvMin( a, b, this );
        else
            cvMinS( a, dot_op.alpha, this );
        break;
    case 'M':
        if( b != 0 )
            cvMax( a, b, this );
        else
            cvMaxS( a, dot_op.alpha, this );
        break;
    case 'a':
        if( b != 0 )
            cvAbsDiff( a, b, this );
        else
            cvAbsDiffS( a, this, cvScalar(dot_op.alpha) );
        break;
    default:
        assert(0);
    }

    return *this;
}


CvMAT& CvMAT::operator = ( const _CvMAT_SOLVE_& solve_mat )
{
    CvMAT* a = (CvMAT*)(solve_mat.a);
    CvMAT* b = (CvMAT*)(solve_mat.b);

    if( !data.ptr )
    {
        if( b->istemp() && a->cols == a->rows )
        {
            memcpy( this, b, sizeof(*this));
            assert( refcount );
            refcount[0]++;
        }
        else
            create( a->height, b->width, a->type );
        type &= ~CV_MAT_TEMP_FLAG;
    }

    if( solve_mat.method == 0 )
        cvSolve( a, b, this );
    else
    {
        CvMAT temp;
        
        if( a->istemp() && a->cols == a->rows )
        {
            memcpy( &temp, a, sizeof(temp));
            assert( temp.refcount );
            temp.refcount[0]++;
        }
        else
        {
            cvInitMatHeader( &temp, a->cols, a->rows, a->type );
            cvCreateData( &temp );
        }

        cvPseudoInv( a, &temp );
        cvMatMul( &temp, b, this );
    }

    return *this;
}


CvMAT& CvMAT::operator = ( const _CvMAT_CMP_& mat_cmp )
{
    CvMAT* a = mat_cmp.a;
    CvMAT* b = mat_cmp.b;

    if( !data.ptr )
    {
        if( a->istemp() && CV_IS_MASK_ARR(a))
            memcpy( this, a, sizeof(*this));
        else if( b && b->istemp() && CV_IS_MASK_ARR(b))
            memcpy( this, b, sizeof(*this));
        else
            create( a->height, a->width, CV_8UC1 );
        type &= ~CV_MAT_TEMP_FLAG;
    }

    if( b )
        cvCmp( a, b, this, mat_cmp.cmp_op );
    else
        cvCmpS( a, mat_cmp.alpha, this, mat_cmp.cmp_op );
    return *this;
}


/****************************************************************************************\
*                                  CvMAT I/O operations                                  *
\****************************************************************************************/

void  CvMAT::write( const char* name, FILE* f, const char* fmt )
{
    int i, j, w = width * CV_MAT_CN(type);
    FILE* out = f ? f : stdout;

    if( name )
        fprintf( stdout, "%s(%d x %d) =\n\t", name, rows, cols );
    
    for( i = 0; i < rows; i++ )
    {
        switch( CV_MAT_DEPTH(type))
        {
        case CV_8U: if( !fmt )
                        fmt = "%4d";
                    for( j = 0; j < w; j++ )
                        fprintf( out, fmt, ((uchar*)(data.ptr + i*step))[j] );
                    break;
        case CV_8S: if( !fmt )
                        fmt = "%5d";
                    for( j = 0; j < w; j++ )
                        fprintf( out, fmt, ((char*)(data.ptr + i*step))[j] );
                    break;
        case CV_16S: if( !fmt )
                        fmt = "%7d";
                    for( j = 0; j < w; j++ )
                        fprintf( out, fmt, ((short*)(data.ptr + i*step))[j] );
                    break;
        case CV_32S: if( !fmt )
                        fmt = " %08x";
                    for( j = 0; j < w; j++ )
                        fprintf( out, fmt, ((int*)(data.ptr + i*step))[j] );
                    break;
        case CV_32F: if( !fmt )
                        fmt = "%15g";
                    for( j = 0; j < w; j++ )
                        fprintf( out, fmt, ((float*)(data.ptr + i*step))[j] );
                    break;
        case CV_64F: if( !fmt )
                        fmt = "%15g";
                    for( j = 0; j < w; j++ )
                        fprintf( out, fmt, ((double*)(data.ptr + i*step))[j] );
                    break;
        }
        fprintf( out, "\n%s", i < rows - 1 ? "\t" : "" );
    }
    fprintf( out, "\n" );
}

#endif /* _MSC_VER || __BORLANDC__ */

/* End of file. */


⌨️ 快捷键说明

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